-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-working.py
230 lines (194 loc) · 8.49 KB
/
app-working.py
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from flask import Flask, request, Response, jsonify
import openai
from openai import OpenAI
from flask_cors import CORS
import json
import logging
app=Flask(__name__)
CORS(app)
# Set your OpenAI API key here
token = ''
openai.api_key = token
client = OpenAI(api_key = token)
@app.route('/')
def index():
return render_template('index.html')
# @app.route('/fhir', methods=['POST'])
# def convert_text_to_fhir():
# text = request.json['text']
# try:
# # Using OpenAI's API to process the text and generate FHIR resource
# response = client.chat.completions.create(
# model="gpt-4-turbo",
# messages=[
# {
# "role": "user",
# "content": f"convert radiologist dictation to fhir resources, don't respond with verbose just return FHIR resources.extract the relevant clinical information and structure it into appropriate FHIR resources such as DiagnosticReport, Observation, and ImagingStudy: {text}"
# }
# ],
# temperature=0.5,
# max_tokens=900,
# top_p=1,
# stream=True
# )
# # Construct a FHIR resource from the response
# print(response.choices[0].message.content)
# print(response)
# # fhir_resource = {
# # "resourceType": "Observation",
# # "text": text,
# # "interpretation": response.choices[0].message.content.strip()
# # }
# fhir_resource = response.choices[0].message.content.strip()
# except Exception as e:
# # print(e)
# return jsonify({"error": str(e)}), 500
# return jsonify(fhir_resource)
# # return fhir_resource
# @app.route('/fhir', methods=['GET'])
# def convert_text_to_fhir():
# text = request.args.get('text')
# logging.debug(f"Received text: {text}")
# if not text:
# return jsonify({"error": "Missing text"}), 400
# try:
# responses = client.chat.completions.create(
# model="gpt-4",
# messages=[{
# "role": "user",
# "content": f"Convert radiologist dictation to FHIR resources: {text}"
# }],
# temperature=0,
# max_tokens=900,
# top_p=1,
# stream=True
# )
# print(responses)
# # for chunk in responses:
# # # print(chunk)
# # # print(chunk.choices[0].delta.content)
# # print("****************")
# full_response = ""
# for response in responses:
# part = response.choices[0].delta.content
# print(part)
# full_response += part
# logging.debug(f"Appending response part: {part}")
# # if 'choices' in response and len(response['choices']) > 0:
# # # part = response['choices'][0]['delta']['content']
# # part = response.choices[0].delta.content
# # print(part)
# # full_response += part
# # logging.debug(f"Appending response part: {part}")
# if not full_response:
# logging.error("Received an empty response from the API.")
# return jsonify({"error": "Empty response from API"}), 500
# # Check the full concatenated response before parsing
# logging.debug(f"Full concatenated response: {full_response}")
# fhir_resource = json.loads(full_response)
# return jsonify(fhir_resource)
# except json.JSONDecodeError as e:
# logging.error(f"JSON parsing error: {e}")
# return jsonify({"error": "Failed to parse JSON response"}), 500
# except Exception as e:
# logging.error(f"An error occurred: {e}")
# return jsonify({"error": str(e)}), 500
@app.route('/fhir', methods=['GET'])
def convert_text_to_fhir():
text = request.args.get('text')
logging.debug(f"Received text: {text}")
if not text:
return jsonify({"error": "Missing text"}), 400
def generate_fhir_data():
try:
responses = client.chat.completions.create(
model="gpt-4o",
messages=[{
"role": "user",
"content": f"Convert radiologist dictation to FHIR resources, please do not return any content other than the FHIR resources. I am using streaming so please return the content appropriately: {text}"
}],
temperature=0,
max_tokens=900,
top_p=1,
stream=True
)
for response in responses:
# part = response['choices'][0]['delta']['content']
part = response.choices[0].delta.content
logging.debug(f"Streaming response part: {part}")
if part: # Make sure part is not None
logging.debug(f"Streaming response part: {part}")
# print(f"Streaming response part: {part}")
yield f"data: {part}\n\n".encode('utf-8')
except Exception as e:
logging.error(f"An error occurred while streaming: {e}")
print(f"An error occurred while streaming: {e}")
yield json.dumps({"error": str(e)}) # Send error message as JSON
return Response(generate_fhir_data(), mimetype='text/event-stream')
@app.route('/layman', methods=['GET'])
def convert_text_to_layman():
text = request.args.get('text')
logging.debug(f"Received text: {text}")
if not text:
return jsonify({"error": "Missing text"}), 400
def generate_fhir_data():
try:
responses = client.chat.completions.create(
model="gpt-4o",
messages=[{
"role": "user",
"content": f"convert radiologist dictation to text which a layman can understand: {text}"
}],
temperature=0,
max_tokens=900,
top_p=1,
stream=True
)
for response in responses:
# part = response['choices'][0]['delta']['content']
part = response.choices[0].delta.content
logging.debug(f"Streaming response part: {part}")
if part: # Make sure part is not None
logging.debug(f"Streaming response part: {part}")
# print(f"Streaming response part: {part}")
yield f"data: {part}\n\n".encode('utf-8')
except Exception as e:
logging.error(f"An error occurred while streaming: {e}")
print(f"An error occurred while streaming: {e}")
yield json.dumps({"error": str(e)}) # Send error message as JSON
return Response(generate_fhir_data(), mimetype='text/event-stream')
# return fhir_resource
@app.route('/conversation', methods=['GET'])
def convert_conversation_to_layman():
text = request.args.get('text')
logging.debug(f"Received text: {text}")
if not text:
return jsonify({"error": "Missing text"}), 400
def generate_fhir_data():
try:
responses = client.chat.completions.create(
model="gpt-4o",
messages=[{
"role": "user",
"content": f"convert conversation between a patient and a doctor consultation into Diagnostic report along with medical codes: {text}"
}],
temperature=0,
max_tokens=900,
top_p=1,
stream=True
)
for response in responses:
# part = response['choices'][0]['delta']['content']
part = response.choices[0].delta.content
logging.debug(f"Streaming response part: {part}")
if part: # Make sure part is not None
logging.debug(f"Streaming response part: {part}")
# print(f"Streaming response part: {part}")
yield f"data: {part}\n\n".encode('utf-8')
except Exception as e:
logging.error(f"An error occurred while streaming: {e}")
print(f"An error occurred while streaming: {e}")
yield json.dumps({"error": str(e)}) # Send error message as JSON
return Response(generate_fhir_data(), mimetype='text/event-stream')
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000,debug=True,threaded=True)