textToSQL/talk_to_NP
2
1import whisper2import gradio as gr3import openai 4import os5 6openai.api_key = os.environ["OPENAI_API_KEY"]7 8model = whisper.load_model("small")9 10 11def transcribe(audio):12 model = whisper.load_model("base")13 result = model.transcribe(audio)14 return result["text"]15 16# def transcribe(audio):17 18# #time.sleep(3)19# # load audio and pad/trim it to fit 30 seconds20# audio = whisper.load_audio(audio)21# audio = whisper.pad_or_trim(audio)22 23# # make log-Mel spectrogram and move to the same device as the model24# mel = whisper.log_mel_spectrogram(audio).to(model.device)25 26# # detect the spoken language27# _, probs = model.detect_language(mel)28# print(f"Detected language: {max(probs, key=probs.get)}")29 30# # decode the audio31# options = whisper.DecodingOptions(fp16 = False)32# result = whisper.decode(model, mel, options)33# return result.text34 35 36def process_text(input_text):37 # Apply your function here to process the input text38 output_text = input_text.upper()39 return output_text40 41def get_completion(prompt, model='gpt-3.5-turbo'):42 messages = [43 {"role": "system", "content": """You are a world class nurse practitioner. You are provided with the transcription following a patient's visit. \44 Extract the following information from the transcription, replace curly brackets with relevant extracted information, and present as follows, one category per line: \ 45 46 Date of Visit: {}47 Claimant: {}48 Client/Employer: {}49 Claim #: {}50 DOI (Date of Injury): {}51 Provider: {}52 Diagnosis Treated: {}53 Subjective findings: {}54 Objective Findings: {}55 Treatment plan: {}56 Medications: {}57 RTW (Return to Work) Status: {}58 Restrictions: {}59 NOV (Next Office Visit): {}60 61 Only use the information from the provided transcription. Do not make up stuff. If information is not available just put "N/A" next to the relevant line.62 """63 },64 {"role": "user", "content": prompt}65 ]66 response = openai.ChatCompletion.create(67 model = model, 68 messages = messages, 69 temperature = 0, 70 71 ) 72 return response.choices[0].message['content']73 74with gr.Blocks() as demo:75 76 gr.Markdown("""77 # Chat with NP <br>78 79 This is to make life of NPs easier. 80 Record post visit summary in natural language, press "transcribe audio", and then "prepare a report".81 """)82 83 84 title = "Chat with NP"85 audio = gr.Audio(source="microphone", type="filepath")86 87 b1 = gr.Button("Transcribe audio")88 b2 = gr.Button("Prepare a report")89 90 91 text1 = gr.Textbox(lines=5)92 text2 = gr.Textbox(lines=5)93 94 prompt = text195 96 97 98 b1.click(transcribe, inputs=audio, outputs=text1)99 b2.click(get_completion, inputs=text1, outputs=text2)100 101 102 # b1.click(transcribe, inputs=audio, outputs=text1)103 # b2.click(get_completion, inputs=prompt, outputs=text2)104 105 106 107demo.launch()108 109#demo.launch(share=True, auth=("username", "password"))110 111# In this example, the process_text function just converts the input text to uppercase, but you can replace it with your desired function. The Gradio Blocks interface will have two buttons: "Transcribe audio" and "Process text". The first button transcribes the audio and fills the first textbox, and the second button processes the text from the first textbox and fills the second textbox.112 113 114# gr.Interface(115# title = 'OpenAI Whisper ASR Gradio Web UI', 116# fn=transcribe, 117# inputs=[118# gr.inputs.Audio(source="microphone", type="filepath")119# ],120# outputs=[121# "textbox"122# ],123 124# live=True).launch()125 