Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
from numpy import True_
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import openai, subprocess
|
| 7 |
+
import os
|
| 8 |
+
import soundfile as sf
|
| 9 |
+
from pydub import AudioSegment
|
| 10 |
+
|
| 11 |
+
%cd /content/drive/MyDrive/Colab_Notebooks/
|
| 12 |
+
note_transcript = ""
|
| 13 |
+
|
| 14 |
+
def transcribe(audio, history_type):
|
| 15 |
+
global note_transcript
|
| 16 |
+
|
| 17 |
+
if history_type == "History":
|
| 18 |
+
with open("Format_Library/Weldon_Note_Format.txt", "r") as f:
|
| 19 |
+
role = f.read()
|
| 20 |
+
elif history_type == "Physical":
|
| 21 |
+
with open("Format_Library/Weldon_PE_Note_Format.txt", "r") as f:
|
| 22 |
+
role = f.read()
|
| 23 |
+
elif history_type == "H+P":
|
| 24 |
+
with open("Format_Library/Weldon_Full_Note_Format.txt", "r") as f:
|
| 25 |
+
role = f.read()
|
| 26 |
+
elif history_type == "Impression/Plan":
|
| 27 |
+
with open("Format_Library/Weldon_Impression_Note_Format.txt", "r") as f:
|
| 28 |
+
role = f.read()
|
| 29 |
+
elif history_type == "Handover":
|
| 30 |
+
with open("Format_Library/Weldon_Handover_Note_Format.txt", "r") as f:
|
| 31 |
+
role = f.read()
|
| 32 |
+
elif history_type == "Meds Only":
|
| 33 |
+
with open("Format_Library/Medications.txt", "r") as f:
|
| 34 |
+
role = f.read()
|
| 35 |
+
elif history_type == "EMS":
|
| 36 |
+
with open("Format_Library/EMS_Handover_Note_Format.txt", "r") as f:
|
| 37 |
+
role = f.read()
|
| 38 |
+
elif history_type == "Triage":
|
| 39 |
+
with open("Format_Library/Triage_Note_Format.txt", "r") as f:
|
| 40 |
+
role = f.read()
|
| 41 |
+
else:
|
| 42 |
+
with open("Format_Library/Weldon_Full_Note_Format.txt", "r") as f:
|
| 43 |
+
role = f.read()
|
| 44 |
+
|
| 45 |
+
messages = [{"role": "system", "content": role}]
|
| 46 |
+
|
| 47 |
+
###### Create Dialogue Transcript from Audio Recording and Append(via Whisper)
|
| 48 |
+
# Load the audio file (from filepath)
|
| 49 |
+
audio_data, samplerate = sf.read(audio)
|
| 50 |
+
|
| 51 |
+
#### Massage .wav and save as .mp3
|
| 52 |
+
#audio_data = audio_data.astype("float32")
|
| 53 |
+
#audio_data = (audio_data * 32767).astype("int16")
|
| 54 |
+
#audio_data = audio_data.mean(axis=1)
|
| 55 |
+
sf.write("Audio_Files/test.wav", audio_data, samplerate, subtype='PCM_16')
|
| 56 |
+
sound = AudioSegment.from_wav("Audio_Files/test.wav")
|
| 57 |
+
sound.export("Audio_Files/test.mp3", format="mp3")
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
#Send file to Whisper for Transcription
|
| 61 |
+
audio_file = open("Audio_Files/test.mp3", "rb")
|
| 62 |
+
audio_transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
| 63 |
+
print(audio_transcript)
|
| 64 |
+
messages.append({"role": "user", "content": audio_transcript["text"]})
|
| 65 |
+
|
| 66 |
+
#Create Sample Dialogue Transcript from File (for debugging)
|
| 67 |
+
#with open('Audio_Files/Test_Elbow.txt', 'r') as file:
|
| 68 |
+
# audio_transcript = file.read()
|
| 69 |
+
#messages.append({"role": "user", "content": audio_transcript})
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
### Word and MB Count
|
| 73 |
+
file_size = os.path.getsize("Audio_Files/test.mp3")
|
| 74 |
+
mp3_megabytes = file_size / (1024 * 1024)
|
| 75 |
+
mp3_megabytes = round(mp3_megabytes, 2)
|
| 76 |
+
|
| 77 |
+
audio_transcript_words = audio_transcript["text"].split() # Use when using mic input
|
| 78 |
+
#audio_transcript_words = audio_transcript.split() #Use when using file
|
| 79 |
+
|
| 80 |
+
num_words = len(audio_transcript_words)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
#Ask OpenAI to create note transcript
|
| 84 |
+
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", temperature=0, messages=messages)
|
| 85 |
+
note_transcript = (response["choices"][0]["message"]["content"])
|
| 86 |
+
|
| 87 |
+
return [note_transcript, num_words,mp3_megabytes]
|
| 88 |
+
|
| 89 |
+
#Define Gradio Interface
|
| 90 |
+
my_inputs = [
|
| 91 |
+
gr.Audio(source="microphone", type="filepath"),
|
| 92 |
+
gr.Radio(["History","Physical", "H+P","Impression/Plan","Handover","EMS","Triage","Meds Only"], show_label=False),
|
| 93 |
+
]
|
| 94 |
+
|
| 95 |
+
ui = gr.Interface(fn=transcribe,
|
| 96 |
+
inputs=my_inputs,
|
| 97 |
+
outputs=[gr.Textbox(label="Your Note").style(show_copy_button=True),
|
| 98 |
+
gr.Number(label="Audio Word Count"),
|
| 99 |
+
gr.Number(label=".mp3 MB")])
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
ui.launch(share=False, debug=True)
|