Update app.py
Browse filesAdded error handling to .wav file input and .mp3 conversion
Changed ifelse disaster to a dictionary
app.py
CHANGED
|
@@ -19,32 +19,19 @@ note_transcript = ""
|
|
| 19 |
def transcribe(audio, history_type):
|
| 20 |
global note_transcript
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
with open("Format_Library/Weldon_Handover_Note_Format.txt", "r") as f:
|
| 36 |
-
role = f.read()
|
| 37 |
-
elif history_type == "Meds Only":
|
| 38 |
-
with open("Format_Library/Medications.txt", "r") as f:
|
| 39 |
-
role = f.read()
|
| 40 |
-
elif history_type == "EMS":
|
| 41 |
-
with open("Format_Library/EMS_Handover_Note_Format.txt", "r") as f:
|
| 42 |
-
role = f.read()
|
| 43 |
-
elif history_type == "Triage":
|
| 44 |
-
with open("Format_Library/Triage_Note_Format.txt", "r") as f:
|
| 45 |
-
role = f.read()
|
| 46 |
-
else:
|
| 47 |
-
with open("Format_Library/Weldon_Full_Note_Format.txt", "r") as f:
|
| 48 |
role = f.read()
|
| 49 |
|
| 50 |
messages = [{"role": "system", "content": role}]
|
|
@@ -58,9 +45,14 @@ def transcribe(audio, history_type):
|
|
| 58 |
#audio_data = (audio_data * 32767).astype("int16")
|
| 59 |
#audio_data = audio_data.mean(axis=1)
|
| 60 |
sf.write("Audio_Files/test.wav", audio_data, samplerate, subtype='PCM_16')
|
|
|
|
|
|
|
|
|
|
| 61 |
sound = AudioSegment.from_wav("Audio_Files/test.wav")
|
| 62 |
sound.export("Audio_Files/test.mp3", format="mp3")
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
| 65 |
#Send file to Whisper for Transcription
|
| 66 |
audio_file = open("Audio_Files/test.mp3", "rb")
|
|
|
|
| 19 |
def transcribe(audio, history_type):
|
| 20 |
global note_transcript
|
| 21 |
|
| 22 |
+
history_type_map = {
|
| 23 |
+
"History": "Weldon_Note_Format.txt",
|
| 24 |
+
"Physical": "Weldon_PE_Note_Format.txt",
|
| 25 |
+
"H+P": "Weldon_Full_Note_Format.txt",
|
| 26 |
+
"Impression/Plan": "Weldon_Impression_Note_Format.txt",
|
| 27 |
+
"Handover": "Weldon_Handover_Note_Format.txt",
|
| 28 |
+
"Meds Only": "Medications.txt",
|
| 29 |
+
"EMS": "EMS_Handover_Note_Format.txt",
|
| 30 |
+
"Triage": "Triage_Note_Format.txt"
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
file_name = history_type_map.get(history_type, "Weldon_Full_Note_Format.txt")
|
| 34 |
+
with open(f"Format_Library/{file_name}", "r") as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
role = f.read()
|
| 36 |
|
| 37 |
messages = [{"role": "system", "content": role}]
|
|
|
|
| 45 |
#audio_data = (audio_data * 32767).astype("int16")
|
| 46 |
#audio_data = audio_data.mean(axis=1)
|
| 47 |
sf.write("Audio_Files/test.wav", audio_data, samplerate, subtype='PCM_16')
|
| 48 |
+
if not os.path.exists("Audio_Files/test.wav"):
|
| 49 |
+
print("Error: Failed to create test.wav file")
|
| 50 |
+
return
|
| 51 |
sound = AudioSegment.from_wav("Audio_Files/test.wav")
|
| 52 |
sound.export("Audio_Files/test.mp3", format="mp3")
|
| 53 |
+
if not os.path.exists("Audio_Files/test.mp3"):
|
| 54 |
+
print("Error: Failed to create test.mp3 file")
|
| 55 |
+
return
|
| 56 |
|
| 57 |
#Send file to Whisper for Transcription
|
| 58 |
audio_file = open("Audio_Files/test.mp3", "rb")
|