Spaces:
Runtime error
Runtime error
AbeerTrial
commited on
Commit
·
ce9797c
1
Parent(s):
35b22df
Update app.py
Browse files
app.py
CHANGED
@@ -13,9 +13,10 @@ import tempfile
|
|
13 |
import os
|
14 |
import boto3
|
15 |
from gradio import Interface, components as gr
|
16 |
-
from gradio import Interface
|
17 |
import io
|
18 |
from scipy.io import wavfile
|
|
|
19 |
import pyttsx3
|
20 |
from nltk.tokenize import sent_tokenize
|
21 |
import nltk
|
@@ -44,8 +45,7 @@ def construct_index(directory_path):
|
|
44 |
|
45 |
return index
|
46 |
|
47 |
-
|
48 |
-
def transcribe_audio(audio):
|
49 |
sampling_rate, audio_data = audio # unpack the tuple
|
50 |
|
51 |
if audio_data.ndim > 1:
|
@@ -62,32 +62,38 @@ def transcribe_audio(audio):
|
|
62 |
r = sr.Recognizer()
|
63 |
with sr.AudioFile(fp.name) as source:
|
64 |
audio_data = r.record(source)
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
finally:
|
85 |
os.unlink(fp.name)
|
86 |
|
87 |
return text
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
def get_gpt_response(input_text):
|
92 |
try:
|
93 |
# Check that input_text is not empty
|
@@ -95,7 +101,7 @@ def get_gpt_response(input_text):
|
|
95 |
return "No input provided.", "", "", "", ""
|
96 |
|
97 |
conversation = [
|
98 |
-
{"role": "system", "content": "You are an experienced medical consultant who provides a SOAP note based on the
|
99 |
{"role": "user", "content": input_text}
|
100 |
]
|
101 |
response = openai.ChatCompletion.create(
|
@@ -126,36 +132,32 @@ def get_gpt_response(input_text):
|
|
126 |
|
127 |
|
128 |
|
129 |
-
def chatbot(input_text, input_voice, patient_name=None):
|
130 |
-
|
131 |
-
# Check if patient_name is in index
|
132 |
index = GPTSimpleVectorIndex.load_from_disk('index.json')
|
133 |
if patient_name: # Only do the check if patient_name is not None and not an empty string
|
134 |
-
|
135 |
if patient_name and patient_name not in patient_names:
|
136 |
-
return "", "", "", "", "", "", "", "", "", "
|
137 |
if input_voice is not None:
|
138 |
-
input_text = transcribe_audio(input_voice)
|
139 |
|
140 |
# Get a response from GPT-3.5-turbo
|
141 |
gpt_subjective, gpt_objective, gpt_assessment, gpt_plan, gpt_general = get_gpt_response(input_text)
|
142 |
-
|
143 |
-
gpt_file_path = os.path.join('GPTresponses/', f"{patient_name}.txt")
|
144 |
-
with open(gpt_file_path, "a") as f:
|
145 |
-
f.write(f"Subjective: {gpt_subjective}\nObjective: {gpt_objective}\nAssessment: {gpt_assessment}\nPlan: {gpt_plan}\nGeneral: {gpt_general}\n\n")
|
146 |
index = GPTSimpleVectorIndex.load_from_disk('index.json')
|
147 |
response_index = index.query(input_text, response_mode="compact")
|
148 |
|
149 |
soap_response = response_index.response
|
150 |
|
151 |
patient_name = soap_response.split(' ')[1] if 'Subjective:' in soap_response else 'General'
|
152 |
-
patient_file_path = os.path.join('Docs
|
153 |
|
154 |
-
if all(keyword
|
155 |
-
s_index = soap_response.
|
156 |
-
o_index = soap_response.
|
157 |
-
a_index = soap_response.
|
158 |
-
p_index = soap_response.
|
159 |
|
160 |
subjective = soap_response[s_index:o_index].replace('Subjective:', '').strip()
|
161 |
objective = soap_response[o_index:a_index].replace('Objective:', '').strip()
|
@@ -165,39 +167,43 @@ def chatbot(input_text, input_voice, patient_name=None):
|
|
165 |
with open(patient_file_path, "a") as f:
|
166 |
f.write(f"Subjective: {subjective}\nObjective: {objective}\nAssessment: {assessment}\nPlan: {plan}\n\n")
|
167 |
|
168 |
-
output = [
|
169 |
-
|
170 |
else:
|
171 |
with open(patient_file_path, "a" , encoding='utf-8') as f:
|
172 |
f.write(f"General: {soap_response}\n\n")
|
173 |
|
174 |
-
output = ["", soap_response]
|
175 |
-
|
176 |
-
return *output, f"Subjective: {gpt_subjective}\nObjective: {gpt_objective}\nAssessment: {gpt_assessment}\nPlan: {gpt_plan}", gpt_general, input_text # return the transcribed text and the GPT response
|
177 |
-
|
178 |
-
|
179 |
|
180 |
-
|
|
|
181 |
|
182 |
-
from gradio import Interface
|
|
|
|
|
183 |
|
184 |
interface = Interface(
|
185 |
fn=chatbot,
|
186 |
inputs=[
|
187 |
Textbox(label="Enter your text"),
|
188 |
Audio(source="microphone", type="numpy", label="Speak Something"),
|
|
|
189 |
],
|
190 |
outputs=[
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
)
|
|
|
|
|
|
|
|
|
|
|
198 |
|
199 |
-
|
200 |
-
index = construct_index('Docs
|
201 |
interface.launch()
|
202 |
|
203 |
|
|
|
13 |
import os
|
14 |
import boto3
|
15 |
from gradio import Interface, components as gr
|
16 |
+
from gradio import Interface
|
17 |
import io
|
18 |
from scipy.io import wavfile
|
19 |
+
from google.cloud import speech
|
20 |
import pyttsx3
|
21 |
from nltk.tokenize import sent_tokenize
|
22 |
import nltk
|
|
|
45 |
|
46 |
return index
|
47 |
|
48 |
+
def transcribe_audio(audio, service="Google"):
|
|
|
49 |
sampling_rate, audio_data = audio # unpack the tuple
|
50 |
|
51 |
if audio_data.ndim > 1:
|
|
|
62 |
r = sr.Recognizer()
|
63 |
with sr.AudioFile(fp.name) as source:
|
64 |
audio_data = r.record(source)
|
65 |
+
if service == "Google":
|
66 |
+
try:
|
67 |
+
text = r.recognize_google(audio_data)
|
68 |
+
except sr.RequestError as e:
|
69 |
+
print(f"Could not request results from Google Speech Recognition service; {e}")
|
70 |
+
except sr.UnknownValueError:
|
71 |
+
print("Google Speech Recognition could not understand audio")
|
72 |
+
text = sent_tokenize(text)
|
73 |
+
elif service == "Whisper":
|
74 |
+
try:
|
75 |
+
with open(fp.name, "rb") as audio_file:
|
76 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
77 |
+
|
78 |
+
print(transcript)
|
79 |
+
|
80 |
+
conversation = [{"role": "user", "content": transcript["text"]}]
|
81 |
+
|
82 |
+
response = openai.ChatCompletion.create(
|
83 |
+
model="gpt-3.5-turbo",
|
84 |
+
messages=conversation
|
85 |
+
)
|
86 |
+
|
87 |
+
print(response)
|
88 |
+
text = transcript["text"]
|
89 |
+
|
90 |
+
except Exception as e:
|
91 |
+
print("Error with Whisper Service:", str(e))
|
92 |
+
text = sent_tokenize(text)
|
93 |
finally:
|
94 |
os.unlink(fp.name)
|
95 |
|
96 |
return text
|
|
|
|
|
|
|
97 |
def get_gpt_response(input_text):
|
98 |
try:
|
99 |
# Check that input_text is not empty
|
|
|
101 |
return "No input provided.", "", "", "", ""
|
102 |
|
103 |
conversation = [
|
104 |
+
{"role": "system", "content": "You are an experienced medical consultant who provides a SOAP note based on the conversation provided."},
|
105 |
{"role": "user", "content": input_text}
|
106 |
]
|
107 |
response = openai.ChatCompletion.create(
|
|
|
132 |
|
133 |
|
134 |
|
135 |
+
def chatbot(input_text, input_voice, transcription_service, patient_name=None):
|
136 |
+
# Check if patient_name is in index
|
|
|
137 |
index = GPTSimpleVectorIndex.load_from_disk('index.json')
|
138 |
if patient_name: # Only do the check if patient_name is not None and not an empty string
|
139 |
+
patient_names = [doc['name'] for doc in index.documents] # Assuming each document is a dictionary with a 'name' field
|
140 |
if patient_name and patient_name not in patient_names:
|
141 |
+
return "Patient not found in index.", "", "", "", "", "", "", "", "", "", "", input_text # Fill the rest of the outputs with empty strings
|
142 |
if input_voice is not None:
|
143 |
+
input_text = transcribe_audio(input_voice, transcription_service)
|
144 |
|
145 |
# Get a response from GPT-3.5-turbo
|
146 |
gpt_subjective, gpt_objective, gpt_assessment, gpt_plan, gpt_general = get_gpt_response(input_text)
|
147 |
+
|
|
|
|
|
|
|
148 |
index = GPTSimpleVectorIndex.load_from_disk('index.json')
|
149 |
response_index = index.query(input_text, response_mode="compact")
|
150 |
|
151 |
soap_response = response_index.response
|
152 |
|
153 |
patient_name = soap_response.split(' ')[1] if 'Subjective:' in soap_response else 'General'
|
154 |
+
patient_file_path = os.path.join('/home/user/app/Docs', f"{patient_name}.txt")
|
155 |
|
156 |
+
if all(keyword in soap_response for keyword in ["Subjective:", "Objective:", "Assessment:", "Plan:"]):
|
157 |
+
s_index = soap_response.find('Subjective:')
|
158 |
+
o_index = soap_response.find('Objective:')
|
159 |
+
a_index = soap_response.find('Assessment:')
|
160 |
+
p_index = soap_response.find('Plan:')
|
161 |
|
162 |
subjective = soap_response[s_index:o_index].replace('Subjective:', '').strip()
|
163 |
objective = soap_response[o_index:a_index].replace('Objective:', '').strip()
|
|
|
167 |
with open(patient_file_path, "a") as f:
|
168 |
f.write(f"Subjective: {subjective}\nObjective: {objective}\nAssessment: {assessment}\nPlan: {plan}\n\n")
|
169 |
|
170 |
+
output = [subjective, objective, assessment, plan, ""]
|
|
|
171 |
else:
|
172 |
with open(patient_file_path, "a" , encoding='utf-8') as f:
|
173 |
f.write(f"General: {soap_response}\n\n")
|
174 |
|
175 |
+
output = ["", "", "", "", soap_response]
|
|
|
|
|
|
|
|
|
176 |
|
177 |
+
return *output, gpt_subjective, gpt_objective, gpt_assessment, gpt_plan, gpt_general, input_text # return the transcribed text and the GPT response
|
178 |
+
#return *output, gpt_subjective, gpt_objective, gpt_assessment, gpt_plan, output[4] + gpt_general, input_text(this to merge the general (none SOAP) from Index and GPT)
|
179 |
|
180 |
+
from gradio import Interface
|
181 |
+
from gradio.inputs import Textbox, Audio, Radio
|
182 |
+
from gradio.outputs import Textbox
|
183 |
|
184 |
interface = Interface(
|
185 |
fn=chatbot,
|
186 |
inputs=[
|
187 |
Textbox(label="Enter your text"),
|
188 |
Audio(source="microphone", type="numpy", label="Speak Something"),
|
189 |
+
Radio(["Google", "Whisper"], label="Choose a transcription service")
|
190 |
],
|
191 |
outputs=[
|
192 |
+
Textbox(label="Subjective"),
|
193 |
+
Textbox(label="Objective"),
|
194 |
+
Textbox(label="Assessment"),
|
195 |
+
Textbox(label="Plan"),
|
196 |
+
Textbox(label="General"),
|
197 |
+
Textbox(label="GPT Subjective"),
|
198 |
+
Textbox(label="GPT Objective"),
|
199 |
+
Textbox(label="GPT Assessment"),
|
200 |
+
Textbox(label="GPT Plan"),
|
201 |
+
Textbox(label="GPT General"),
|
202 |
+
Textbox(label="Transcribed Text"), # window for the transcribed text
|
203 |
+
],
|
204 |
|
205 |
+
)
|
206 |
+
index = construct_index('/home/user/app/Docs')
|
207 |
interface.launch()
|
208 |
|
209 |
|