CoolFace
Apppublic

ortexsolution/VR4

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
app.py142 linesDownload Raw Back to root
1import torch2import spaces3 4import gradio as gr5from transformers import pipeline6 7import time8import os9import shutil10import requests11from openai import OpenAI12 13# from CGPT_tools import fill_template14 15model_ids = {16"Fast": "openai/whisper-small",17"Balanced": "openai/whisper-medium",18"Accurate": "openai/whisper-large-v3-turbo"19}20 21device = 0 if torch.cuda.is_available() else "cpu"22 23def upload_to_server(audio_file, text_file):24    url = "http://82.115.24.188:8000/upload" 25    files = {'audio_file': (os.path.basename(audio_file.name), audio_file, 'application/octet-stream'),26             'text_file': (os.path.basename(text_file.name), text_file, 'text/plain')}27    28    response = requests.post(url, files=files)29    return response.text30 31@spaces.GPU32def transcribe(model_name, inputs):33    if inputs is None:34        raise gr.Error("No audio file submitted!")35    36    pipe = pipeline(37        task="automatic-speech-recognition",38        model=model_ids[model_name],39        chunk_length_s=30,40        device=device,41    )42    id = str(int(time.time()))43    audio_files_path = "audio_files/"44    shutil.copy(inputs, audio_files_path+id+".wav")45 46    text = pipe(inputs, batch_size=8, generate_kwargs={"task": "transcribe" , "language": "en","num_beams": 3}, return_timestamps=True)["text"] 47    with open(audio_files_path+id+".txt", "w", encoding='utf-8') as f:48        f.write(text)49 50    with open(audio_files_path+id+".wav", 'rb') as audio_file, open(audio_files_path+id+".txt", 'rb') as text_file:51        upload_to_server(audio_file, text_file)52 53    return text54 55def read_template_files(folder_path):56 57    template_file_contents = []58    file_list = sorted(os.listdir(folder_path))59 60    for file_name in file_list:61        with open(folder_path+file_name, 'r', encoding='utf-8') as file:62            content = file.read()63            template_file_contents.append(content)64    return template_file_contents65 66templates_content = read_template_files("report_templates/")67 68templates = {69    "" : "",70    "Mauro Cervical Spine Levels -- 3MCS" : templates_content[0],71    "Mauro - CT Chest (With Contrastor Non-Contrast) -- MCSMCTC" : templates_content[1],72    "Houman - CT Cervical Nerve -- HECTPSI" : templates_content[2],73    "Houman - Result Consult -- HERESCON" : templates_content[3],74}75 76def show_template(tn):77    return templates[tn]78 79def fill_template(template_content, massage):80    client = OpenAI(81        api_key=os.environ.get("OPENAI_API_KEY"),  # This is the default and can be omitted82    )83    reponse = client.chat.completions.create(84    messages=[85        {86            "role": "system", "content":"You are a helpful and knowledgeable assistant in a radiology center. Answer briefly and concisely.",87            "role": "user", "content": f"Complete the following template with the given text. Do not add any extra information, just insert the details from the text provided in the appropriate places\nHere is a report template:\n{template_content}\n Complete the  template with this text: \n'{massage}' "88        }89    ],90    model="gpt-4",91    temperature= 0.0,92    )93    return reponse.choices[0].message.content94 95 96 97 98demo = gr.Blocks(theme=gr.themes.Ocean())99file_transcribe_output = gr.TextArea()100mf_transcribe_output = gr.TextArea()101file_transcribe = gr.Interface(102    fn=transcribe,103    inputs=[104        gr.Radio(list(model_ids.keys()), label="Step 2. Select your model⬇️", value="Fast"),105        gr.Audio(sources="upload", type="filepath",label="Step 3. Upload your audio file, and click the submit button⬇️")106    ],107    outputs=file_transcribe_output,108    flagging_mode="never",109)110 111mf_transcribe = gr.Interface(112    fn=transcribe,113    inputs=[114        gr.Radio(list(model_ids.keys()), label="Step 2. Select your model⬇️", value="Fast"),115        gr.Audio(sources="microphone", type="filepath",label="Step 3. Record your audio, and click the submit button⬇️")116    ],117    outputs=mf_transcribe_output,118    flagging_mode="never",119)120 121with demo:122    with gr.Row():123        gr.Markdown("<div style='text-align: center;'><h2>Automated transcription of voice comments</h2></div>")124    with gr.Row():125        with gr.Column():126            dropdown = gr.Dropdown(choices=list(templates.keys()), label="Step 1. Select your report template⬇️")127            report_template = gr.TextArea()128            dropdown.change(fn=show_template, inputs=dropdown, outputs=report_template)129        with gr.Column():130            gr.TabbedInterface([file_transcribe, mf_transcribe], ["Audio File","Microphone"])131    with gr.Row():132        textbox_fill_template = gr.Textbox(label="Result", interactive=False) 133        button = gr.Button("Filling the Template")134         135        if file_transcribe_output != "":136            button.click(fill_template, inputs=[report_template, mf_transcribe_output], outputs=textbox_fill_template)137        elif mf_transcribe_output != "":138            button.click(fill_template, inputs=[report_template, file_transcribe_output], outputs=textbox_fill_template)139 140demo.queue().launch(ssr_mode=False)141 142