Team Ai
Apppublic

textToSQL/doctor_visit

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
app.py131 linesDownload Raw Back to root
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 of a patient's recording in a non-English language prior to doctor's visit. \44    Extract the following information from the transcription, replace curly brackets with relevant extracted information, and present in English as follows, one category per line: \ 45        46    Demographic information: {name, age, gender, address, phone number}47 48    Medical history: {chronic health conditions, any past surgery, any hospitalization, current medications}49 50    Symptoms: {current symptoms, when did they start, how did they progress} 51 52    Allergies: {any known allergies, any allergic reaction to medications} 53 54    Family history: {any family members with chronic health condition, anyone in the family with a hereditary condition?} 55 56    Lifestyle factors: {typical diet, how often you exercise, smoking, drinking alcohol} 57 58    Psychosocial factors: {stress, anxiety, any mental health condition}59 60    Review of systems: {any issues with vision or hearing, digestive issues, any problems with skin or nails, any problems with joints or muscles}61 62    All information in the report needs to be in English only. 63    64    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.65         """66        },67        {"role": "user", "content": prompt}68        ]69    response = openai.ChatCompletion.create(70        model = model, 71        messages = messages, 72        temperature = 0, 73        74    ) 75    return response.choices[0].message['content']76 77with gr.Blocks() as demo:78    79    gr.Markdown("""80    # Meet your doctor  <br>81    82    This is to make life of non-English speaking patients easier. 83    Describe your complaints and symptoms in your native language , have it emailed to your doctor prior to your visit. 84    Information that is useful to include: your name, age, gender, address, phone number, medical history, symptoms, allergies, family medical history, lifestyle factors. 85    Have it all recorded, transcribed, and presented in a standard form. 86    """)87 88    89    title = "Chat with NP"90    audio = gr.Audio(source="microphone", type="filepath")91    92    b1 = gr.Button("Transcribe audio")93    b2 = gr.Button("Prepare a report in English")94    b3 = gr.Button("Email report to your doctor")95 96 97    text1 = gr.Textbox(lines=5)98    text2 = gr.Textbox(lines=5)99 100    prompt = text1101    102  103    104    b1.click(transcribe, inputs=audio, outputs=text1)105    b2.click(get_completion, inputs=text1, outputs=text2)106 107 108    # b1.click(transcribe, inputs=audio, outputs=text1)109    # b2.click(get_completion, inputs=prompt, outputs=text2)110 111 112 113demo.launch()114 115#demo.launch(share=True, auth=("username", "password"))116 117# 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.118 119 120# gr.Interface(121#     title = 'OpenAI Whisper ASR Gradio Web UI', 122#     fn=transcribe, 123#     inputs=[124#         gr.inputs.Audio(source="microphone", type="filepath")125#     ],126#     outputs=[127#         "textbox"128#     ],129    130#     live=True).launch()131