-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackend.py
More file actions
196 lines (158 loc) · 7.82 KB
/
backend.py
File metadata and controls
196 lines (158 loc) · 7.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from flask import Flask, request, jsonify, Response, send_file, make_response
import requests
from faster_whisper import WhisperModel
import torch
from TTS.api import TTS
from dotenv import load_dotenv
import google.generativeai as genai
import os
import base64
import json
from ocr import get_text_from_image
from flask_cors import CORS
from pymongo import MongoClient
from mail import generate_text, generate_letter_pdf
load_dotenv()
uri=os.getenv("mongo")
cluster = MongoClient(uri)
db = cluster['data']
collection = db["users"]
device = "cuda" if torch.cuda.is_available() else "cpu"
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False).to(device)
model_size = "medium"
audio_model = WhisperModel(model_size, device="cuda", compute_type="int8_float16")
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
genai.configure(api_key=GEMINI_API_KEY)
gemini_model = genai.GenerativeModel("gemini-pro")
app = Flask(__name__)
CORS(app, origins="*")
@app.route('/query-prices', methods=['POST'])
def query_prices():
procedure_json = request.get_json()
insurance_provider = procedure_json['insurance_provider']
dicter = {}
for procedure in procedure_json['procedures']:
answer = requests.get("https://www.dolthub.com/api/v1alpha1/dolthub/hospital-price-transparency/master?q=SELECT+Avg%28price%29%0AFROM+%60prices%60+WHERE+CODE%3D\"{}\"+AND+payer+LIKE+\"%25{}%25\"+LIMIT+1000%3B".format(procedure, insurance_provider))
answer = answer.json()
inner_dict = {}
inner_dict['cost_difference'] = float(answer['rows'][0]['Avg(price)']) - float(procedure_json['procedures'][procedure])
inner_dict['avg_cost'] = float(answer['rows'][0]['Avg(price)'])
inner_dict['your_cost'] = procedure_json['procedures'][procedure]
inner_dict['percent_difference'] = (inner_dict['cost_difference'] / float(procedure_json['procedures'][procedure])) * 100
dicter[procedure] = inner_dict
uzer=collection.find_one({'email': emailid })
if uzer.get("procedures") !={"dummy":"dummy"}:
dict = uzer["procedures"]
dict.update(dicter)
else:
# Handle the case where `uzer["procedures"]` is None
dict = dicter
update_operation = {
'$set': {
'procedures': dict
}
}
collection.update_one({'email': emailid}, update_operation)
return jsonify(dicter)
@app.route('/process-audio', methods=['POST'])
def process_audio():
request.files['audio'].save("audio.mp3")
# Use FasterWhisper to convert audio to text
segments, info = audio_model.transcribe("audio.mp3", beam_size=5)
final_text = ""
for segment in segments:
final_text += segment.text
if "delete" in final_text and ("bill" in final_text or "bills" in final_text):
return "delete_bill"
if "remove" in final_text and "address" in final_text:
return "remove_address"
response = gemini_model.generate_content([final_text, "You are a general support assistant for users. You can help educate them and support privacy and security practices on the internet. You also teach users about how they can resolve billing disuputes with hospitals. You also answer any general purpose insurance related query of the user. Don't include any extra formatting for text. Keep responses limited to 5 lines."])
#ping gemini for the answer
tts_output_path = r"C:\Users\sange\OneDrive\Desktop\hackp\sample_output.wav"
tts.tts_to_file(response.text, file_path=tts_output_path)
return send_file(tts_output_path )
@app.route("/get-audio", methods = ['GET'])
def get_audio():
return send_file("sample_output.wav")
@app.route('/overcharge-chat', methods=['POST'])
def overcharge_chat():
request.files['audio'].save("audio.mp3")
#history = request.form['history']
# with open("tp.json") as f:
# data_json = json.load(f)
# f.close()
# data = json.dumps(data_json)
history=""
data_json = get_history()
data_json=data_json['procedures']
# Use FasterWhisper to convert audio to text
segments, info = audio_model.transcribe("audio.mp3", beam_size=5)
#final_text = "Explain where I have been overcharged and what I can do about it."
final_text = ""
for segment in segments:
final_text += segment.text
with open("insuaid/target/directions.txt", "r") as file:
directions_contents = file.read()
# Add contents to a string or perform any other operations
if "generate" in final_text.lower() and "letter" in final_text.lower():
return_text = generate_text(str(data_json))
generate_letter_pdf(return_text, r"C:\Users\sange\OneDrive\Desktop\hackp\letter.pdf")
response = make_response(send_file(r"C:\Users\sange\OneDrive\Desktop\hackp\letter.pdf"))
response.headers['content-type'] = 'application/pdf'
return response
print(final_text)
response = gemini_model.generate_content(["You have been provided with data about the patient's procedure and its costs. The accompanying json data contains structured data about each procedure done to the patient, indicated by the CPT code, and its associated information, your_cost represents how much you spent and avg_cost represents the national average. Respond to user queries in plain and simple text, do not exceed more than four sentences.",
str(data_json), "This is the user's query: " + final_text, "This was your previous answer (ignore if empty): " + history, "Suggest writing a letter to the hospital's billing department to request a refund for the overcharge.", "This is a set of general directions that the user can do to help with the overcharge: " + directions_contents])
#ping gemini for the answer
tts_output_path = r"C:\Nishkal\HackPrinceton Spring 2024\sample_output.wav"
print(tts_output_path)
tts.tts_to_file(response.text, file_path=tts_output_path)
return send_file(tts_output_path )
@app.route('/update_email', methods=['POST'])
def update_email():
data=request.json
print(data)
global emailid
emailid=data['email']
global address
address=data['address']
global insurance
insurance=data['insuranceName']
print(emailid,address,insurance)
return "updated"
@app.route('/image', methods=['POST'])
def process_image():
image = request.files['image']
insurance = request.form['insurance']
imgbase64 = base64.b64encode(image.read()).decode("utf-8")
description = get_text_from_image(imgbase64)
description_json = json.loads(description)
data = {'insurance_provider': insurance, 'procedures': description_json}
response = requests.post("http://127.0.0.1:5000/query-prices", json=data)
if response.status_code == 200:
# Return a JSON response with the data obtained from the query-prices endpoint
return Response(response=response.text, status=response.status_code, content_type='application/json')
else:
# If the request was not successful, return an appropriate error response
return jsonify({'error': 'Failed to process the image'}), 500
@app.route('/add_user', methods=['POST'])
def add_user():
# Get user data from the request
user_data = request.json
if not user_data:
return jsonify({"error": "No user data provided"}), 400
# Check if the user already exists in the database
existing_user = collection.find_one({"email": user_data.get("email")})
if existing_user:
return "user exists"
# Insert the user into the database
result = collection.insert_one(user_data)
if result.inserted_id:
return jsonify({"message": "User added successfully"}), 201
else:
return jsonify({"error": "Failed to add user"}), 500
def get_history():
print(collection.find_one({'email': emailid}))
return collection.find_one({'email': emailid})
if __name__ == '__main__':
app.run(debug = False)