Spaces:
Runtime error
Runtime error
File size: 8,148 Bytes
35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df ce9797c 35b22df |
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 |
import os
from gpt_index import SimpleDirectoryReader, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain.chat_models import ChatOpenAI
import gradio as gr
import speech_recognition as sr
import openai
import logging
import openai
from transformers import GPTJForCausalLM, GPT2Tokenizer
import numpy as np
import soundfile as sf
import tempfile
import os
import boto3
from gradio import Interface, components as gr
from gradio import Interface
import io
from scipy.io import wavfile
from google.cloud import speech
import pyttsx3
from nltk.tokenize import sent_tokenize
import nltk
nltk.download('punkt')
import langchain.schema
print(dir(langchain.schema))
logging.basicConfig(level=logging.INFO)
os.environ["OPENAI_API_KEY"]
def construct_index(directory_path):
max_input_size = 4096
num_outputs = 512
max_chunk_overlap = 20
chunk_size_limit = 2048
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
documents = SimpleDirectoryReader(directory_path).load_data()
index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
index.save_to_disk('index.json')
return index
def transcribe_audio(audio, service="Google"):
sampling_rate, audio_data = audio # unpack the tuple
if audio_data.ndim > 1:
audio_data = np.mean(audio_data, axis=1)
print(type(audio_data), audio_data)
fp = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
fp.close()
text = ""
try:
sf.write(fp.name, audio_data, sampling_rate)
r = sr.Recognizer()
with sr.AudioFile(fp.name) as source:
audio_data = r.record(source)
if service == "Google":
try:
text = r.recognize_google(audio_data)
except sr.RequestError as e:
print(f"Could not request results from Google Speech Recognition service; {e}")
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
text = sent_tokenize(text)
elif service == "Whisper":
try:
with open(fp.name, "rb") as audio_file:
transcript = openai.Audio.transcribe("whisper-1", audio_file)
print(transcript)
conversation = [{"role": "user", "content": transcript["text"]}]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=conversation
)
print(response)
text = transcript["text"]
except Exception as e:
print("Error with Whisper Service:", str(e))
text = sent_tokenize(text)
finally:
os.unlink(fp.name)
return text
def get_gpt_response(input_text):
try:
# Check that input_text is not empty
if not input_text:
return "No input provided.", "", "", "", ""
conversation = [
{"role": "system", "content": "You are an experienced medical consultant who provides a SOAP note based on the conversation provided."},
{"role": "user", "content": input_text}
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=conversation
)
gpt_response = response['choices'][0]['message']['content']
# Parse the GPT response into SOAP components
if all(keyword in gpt_response for keyword in ["Subjective:", "Objective:", "Assessment:", "Plan:"]):
s_index = gpt_response.find('Subjective:')
o_index = gpt_response.find('Objective:')
a_index = gpt_response.find('Assessment:')
p_index = gpt_response.find('Plan:')
subjective = gpt_response[s_index:o_index].replace('Subjective:', '').strip()
objective = gpt_response[o_index:a_index].replace('Objective:', '').strip()
assessment = gpt_response[a_index:p_index].replace('Assessment:', '').strip()
plan = gpt_response[p_index:].replace('Plan:', '').strip()
return subjective, objective, assessment, plan, ""
else:
return "", "", "", "", gpt_response
except Exception as e:
print(f"Error in get_gpt_response: {e}")
return "", "", "", "", ""
def chatbot(input_text, input_voice, transcription_service, patient_name=None):
# Check if patient_name is in index
index = GPTSimpleVectorIndex.load_from_disk('index.json')
if patient_name: # Only do the check if patient_name is not None and not an empty string
patient_names = [doc['name'] for doc in index.documents] # Assuming each document is a dictionary with a 'name' field
if patient_name and patient_name not in patient_names:
return "Patient not found in index.", "", "", "", "", "", "", "", "", "", "", input_text # Fill the rest of the outputs with empty strings
if input_voice is not None:
input_text = transcribe_audio(input_voice, transcription_service)
# Get a response from GPT-3.5-turbo
gpt_subjective, gpt_objective, gpt_assessment, gpt_plan, gpt_general = get_gpt_response(input_text)
index = GPTSimpleVectorIndex.load_from_disk('index.json')
response_index = index.query(input_text, response_mode="compact")
soap_response = response_index.response
patient_name = soap_response.split(' ')[1] if 'Subjective:' in soap_response else 'General'
patient_file_path = os.path.join('/home/user/app/Docs', f"{patient_name}.txt")
if all(keyword in soap_response for keyword in ["Subjective:", "Objective:", "Assessment:", "Plan:"]):
s_index = soap_response.find('Subjective:')
o_index = soap_response.find('Objective:')
a_index = soap_response.find('Assessment:')
p_index = soap_response.find('Plan:')
subjective = soap_response[s_index:o_index].replace('Subjective:', '').strip()
objective = soap_response[o_index:a_index].replace('Objective:', '').strip()
assessment = soap_response[a_index:p_index].replace('Assessment:', '').strip()
plan = soap_response[p_index:].replace('Plan:', '').strip()
with open(patient_file_path, "a") as f:
f.write(f"Subjective: {subjective}\nObjective: {objective}\nAssessment: {assessment}\nPlan: {plan}\n\n")
output = [subjective, objective, assessment, plan, ""]
else:
with open(patient_file_path, "a" , encoding='utf-8') as f:
f.write(f"General: {soap_response}\n\n")
output = ["", "", "", "", soap_response]
return *output, gpt_subjective, gpt_objective, gpt_assessment, gpt_plan, gpt_general, input_text # return the transcribed text and the GPT response
#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)
from gradio import Interface
from gradio.inputs import Textbox, Audio, Radio
from gradio.outputs import Textbox
interface = Interface(
fn=chatbot,
inputs=[
Textbox(label="Enter your text"),
Audio(source="microphone", type="numpy", label="Speak Something"),
Radio(["Google", "Whisper"], label="Choose a transcription service")
],
outputs=[
Textbox(label="Subjective"),
Textbox(label="Objective"),
Textbox(label="Assessment"),
Textbox(label="Plan"),
Textbox(label="General"),
Textbox(label="GPT Subjective"),
Textbox(label="GPT Objective"),
Textbox(label="GPT Assessment"),
Textbox(label="GPT Plan"),
Textbox(label="GPT General"),
Textbox(label="Transcribed Text"), # window for the transcribed text
],
)
index = construct_index('/home/user/app/Docs')
interface.launch()
|