ortexsolution/VR3
0
1import torch2import spaces3 4 5import gradio as gr6from transformers import pipeline7 8import time9import os10import shutil11import requests12 13 14model_ids = {15"Fast": "openai/whisper-small",16"Balanced": "openai/whisper-medium",17"Accurate": "openai/whisper-large-v3-turbo"18}19 20device = 0 if torch.cuda.is_available() else "cpu"21 22def upload_to_server(audio_file, text_file):23 url = "http://82.115.24.188:8000/upload" 24 files = {'audio_file': (os.path.basename(audio_file.name), audio_file, 'application/octet-stream'),25 'text_file': (os.path.basename(text_file.name), text_file, 'text/plain')}26 27 response = requests.post(url, files=files)28 return response.text29 30@spaces.GPU31def transcribe(model_name, inputs):32 if inputs is None:33 raise gr.Error("No audio file submitted!")34 35 pipe = pipeline(36 task="automatic-speech-recognition",37 model=model_ids[model_name],38 chunk_length_s=30,39 device=device,40 )41 id = str(int(time.time()))42 audio_files_path = "audio_files/"43 shutil.copy(inputs, audio_files_path+id+".wav")44 45 text = pipe(inputs, batch_size=8, generate_kwargs={"task": "transcribe" , "language": "en","num_beams": 3}, return_timestamps=True)["text"] 46 with open(audio_files_path+id+".txt", "w", encoding='utf-8') as f:47 f.write(text)48 49 with open(audio_files_path+id+".wav", 'rb') as audio_file, open(audio_files_path+id+".txt", 'rb') as text_file:50 upload_to_server(audio_file, text_file)51 52 return text53 54def read_template_files(folder_path):55 56 template_file_contents = []57 file_list = sorted(os.listdir(folder_path))58 59 for file_name in file_list:60 with open(folder_path+file_name, 'r', encoding='utf-8') as file:61 content = file.read()62 template_file_contents.append(content)63 return template_file_contents64 65templates_content = read_template_files("report_templates/")66 67templates = {68 "" : "",69 "Mauro Cervical Spine Levels -- 3MCS" : templates_content[0],70 "Mauro - CT Chest (With Contrastor Non-Contrast) -- MCSMCTC" : templates_content[1],71 "Houman - CT Cervical Nerve -- HECTPSI" : templates_content[2],72 "Houman - Result Consult -- HERESCON" : templates_content[3],73}74 75demo = gr.Blocks(theme=gr.themes.Ocean())76 77file_transcribe = gr.Interface(78 fn=transcribe,79 inputs=[80 gr.Radio(list(model_ids.keys()), label="Step 2. Select your model⬇️", value="Fast"),81 gr.Audio(sources="upload", type="filepath",label="Step 3. Upload your audio file, and click the submit button⬇️")82 ],83 outputs="text",84 flagging_mode="never",85)86 87mf_transcribe = gr.Interface(88 fn=transcribe,89 inputs=[90 gr.Radio(list(model_ids.keys()), label="Step 2. Select your model⬇️", value="Fast"),91 gr.Audio(sources="microphone", type="filepath",label="Step 3. Record your audio, and click the submit button⬇️")92 ],93 outputs="text",94 flagging_mode="never",95)96def show_template(tn):97 return templates[tn]98with demo:99 with gr.Row():100 gr.Markdown("<div style='text-align: center;'><h2>Automated transcription of voice comments</h2></div>")101 with gr.Row():102 with gr.Column():103 dropdown = gr.Dropdown(choices=list(templates.keys()), label="Step 1. Select your report template⬇️")104 output = gr.TextArea()105 dropdown.change(fn=show_template, inputs=dropdown, outputs=output)106 with gr.Column():107 gr.TabbedInterface([file_transcribe, mf_transcribe], ["Audio File","Microphone"])108 109demo.queue().launch(ssr_mode=False)110 111 