| import os |
| import openai |
| import time |
| import numpy as np |
| from numpy import True_ |
| import gradio as gr |
| from gradio_rich_textbox import RichTextbox |
| import soundfile as sf |
| from pydub import AudioSegment |
|
|
| from openai import OpenAI |
|
|
| |
| |
| |
| |
|
|
| |
| OPENAI_SECRET_KEY = os.environ.get("OPENAI_SECRET_KEY") |
| client = OpenAI(api_key = OPENAI_SECRET_KEY) |
|
|
|
|
|
|
| note_transcript = "" |
|
|
| def transcribe(audio, history_type): |
| global note_transcript |
| print(f"Received audio file path: {audio}") |
| |
| history_type_map = { |
| "History Simple": "McIntyre_Short_History.txt", |
| "History Complex": "McIntyre_Long_History.txt", |
| "Full Visit": "McIntyre_Full_Visit.txt", |
| "Impression/Plan": "McIntyre_Impression.txt", |
| "EMS": "McIntyre_EMS_Handover.txt", |
| "Dx/DDx": "McIntyre_Dx_DDx_Format.txt", |
| "Feedback": "McIntyre_Feedback.txt", |
| "Hallway Consult": "McIntyre_Hallway_Consult_Format.txt" |
| } |
| |
| file_name = history_type_map.get(history_type, "McIntyre_Full_Visit.txt") |
| with open(f"Format_Library/{file_name}", "r") as f: |
| role = f.read() |
| messages = [{"role": "system", "content": role}] |
|
|
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| max_attempts = 1 |
| attempt = 0 |
| audio_data = None |
| samplerate = None |
| while attempt < max_attempts: |
| try: |
| if audio is None: |
| raise TypeError("Invalid file: None") |
| audio_data, samplerate = sf.read(audio) |
| break |
| except (OSError, TypeError) as e: |
| print(f"Attempt {attempt + 1} of {max_attempts} failed with error: {e}") |
| attempt += 1 |
| time.sleep(3) |
| else: |
| print(f"###############Failed to open audio file after {max_attempts} attempts.##############") |
| return |
|
|
| |
| sf.write("Audio_Files/test.wav", audio_data, samplerate, subtype='PCM_16') |
| sound = AudioSegment.from_wav("Audio_Files/test.wav") |
| sound.export("Audio_Files/test.mp3", format="mp3") |
|
|
| sf.write("Audio_Files/test.mp3", audio_data, samplerate) |
| |
| |
| |
| audio_file = open("Audio_Files/test.mp3", "rb") |
| |
| max_attempts = 3 |
| attempt = 0 |
| while attempt < max_attempts: |
| try: |
| audio_transcript = client.audio.transcriptions.create(model="whisper-1", file=audio_file) |
| break |
| except openai.error.APIConnectionError as e: |
| print(f"Attempt {attempt + 1} failed with error: {e}") |
| attempt += 1 |
| time.sleep(3) |
| else: |
| print("Failed to transcribe audio after multiple attempts") |
| |
| print(audio_transcript.text) |
| messages.append({"role": "user", "content": audio_transcript.text}) |
| |
| |
| |
| |
| |
| |
|
|
| |
| file_size = os.path.getsize("Audio_Files/test.mp3") |
| mp3_megabytes = file_size / (1024 * 1024) |
| mp3_megabytes = round(mp3_megabytes, 2) |
|
|
| audio_transcript_words = audio_transcript.text.split() |
| |
|
|
| num_words = len(audio_transcript_words) |
|
|
|
|
| |
| response = client.chat.completions.create(model="gpt-4o", temperature=0, messages=messages) |
| |
| |
| |
| note_transcript = response.choices[0].message.content |
| print(note_transcript) |
| return [note_transcript, num_words, mp3_megabytes] |
|
|
| |
| my_inputs = [ |
| |
| |
| |
| gr.Microphone(type="filepath",format="wav"), |
| gr.Radio(["History Simple","History Complex","Full Visit","Impression/Plan","EMS","Dx/DDx","Feedback","Hallway Consult"], show_label=False), |
| ] |
|
|
| ui = gr.Interface(fn=transcribe, |
| inputs=my_inputs, |
| outputs=[ |
| gr.Textbox(label="Your Note (gpt-4o)", show_copy_button=True), |
| gr.Number(label=".mp3 MB")], |
| title="Jenkins", |
| ) |
| ui.config['template'] = '<!DOCTYPE html><html><title>Jenkins</title><body>{}</body></html>' |
|
|
| ui.launch(share=False, debug=True) |