ortexsolution/VR2
0
1import torch2import spaces3 4import gradio as gr5from transformers import pipeline6 7import os8 9model_ids = {10"Fast": "openai/whisper-small",11"Balanced": "openai/whisper-medium",12"Accurate": "openai/whisper-large-v3-turbo"13}14device = 0 if torch.cuda.is_available() else "cpu"15 16@spaces.GPU17def transcribe(model_name, inputs):18 if inputs is None:19 raise gr.Error("No audio file submitted!")20 21 pipe = pipeline(22 task="automatic-speech-recognition",23 model=model_ids[model_name],24 chunk_length_s=30,25 device=device,26 )27 text = pipe(inputs, batch_size=8, generate_kwargs={"task": "transcribe" , "language": "en"}, return_timestamps=True)["text"] 28 return text29 30def read_template_files(folder_path):31 32 template_file_contents = []33 file_list = sorted(os.listdir(folder_path))34 35 for file_name in file_list:36 with open(folder_path+file_name, 'r', encoding='utf-8') as file:37 content = file.read()38 template_file_contents.append(content)39 return template_file_contents40 41templates_content = read_template_files("report_templates/")42 43templates = {44 "" : "",45 "Mauro Cervical Spine Levels -- 3MCS" : templates_content[0],46 "Mauro - CT Chest (With Contrastor Non-Contrast) -- MCSMCTC" : templates_content[1],47 "Houman - CT Cervical Nerve -- HECTPSI" : templates_content[2],48 "Houman - Result Consult -- HERESCON" : templates_content[3],49}50 51demo = gr.Blocks(theme=gr.themes.Ocean())52 53file_transcribe = gr.Interface(54 fn=transcribe,55 inputs=[56 gr.Radio(list(model_ids.keys()), label="Step 2. Select your model⬇️", value="Fast"),57 gr.Audio(sources="upload", type="filepath",label="Step 3. Upload your audio file, and click the submit button⬇️")58 ],59 outputs="text",60 flagging_mode="never",61)62 63mf_transcribe = gr.Interface(64 fn=transcribe,65 inputs=[66 gr.Radio(list(model_ids.keys()), label="Step 2. Select your model⬇️", value="Fast"),67 gr.Audio(sources="microphone", type="filepath",label="Step 3. Record your audio, and click the submit button⬇️")68 ],69 outputs="text",70 flagging_mode="never",71)72def show_template(tn):73 return templates[tn]74with demo:75 with gr.Row():76 gr.Markdown("<div style='text-align: center;'><h2>Automated transcription of voice comments</h2></div>")77 with gr.Row():78 with gr.Column():79 dropdown = gr.Dropdown(choices=list(templates.keys()), label="Step 1. Select your report template⬇️ (in this version, templates are only for guidance)")80 output = gr.TextArea()81 dropdown.change(fn=show_template, inputs=dropdown, outputs=output)82 with gr.Column():83 gr.TabbedInterface([file_transcribe, mf_transcribe], ["Audio File","Microphone"])84 85demo.queue().launch(ssr_mode=False)86 87 