Spaces:
Running
on
Zero
Running
on
Zero
import spaces | |
import torch | |
import gradio as gr | |
from transformers import AutoTokenizer, LlamaForCausalLM | |
import bitsandbytes, flash_attn | |
import os | |
MODEL_NAME = "openai/whisper-large-v3-turbo" | |
BATCH_SIZE = 8 | |
FILE_LIMIT_MB = 1000 | |
device = 0 if torch.cuda.is_available() else "cpu" | |
# Initialize the transcription pipeline | |
pipe = pipeline( | |
task="automatic-speech-recognition", | |
model=MODEL_NAME, | |
chunk_length_s=30, | |
device=device, | |
) | |
# Load tokenizer and model for SOAP note generation | |
tokenizer = AutoTokenizer.from_pretrained("NousResearch/Hermes-3-Llama-3.1-8B", trust_remote_code=True) | |
model = LlamaForCausalLM.from_pretrained( | |
"NousResearch/Hermes-3-Llama-3.1-8B", | |
torch_dtype=torch.float16, | |
device_map="auto", | |
load_in_8bit=False, | |
load_in_4bit=True, | |
use_flash_attention_2=True | |
) | |
# Prompt for SOAP note generation | |
sys_prompt = "You are a world class clinical assistant." | |
task_prompt = """ | |
Convert the following transcribed conversation into a clinical SOAP note. | |
The text includes dialogue between a physician and a patient. Please clearly distinguish between the physician's and the patient's statements. | |
Extract and organize the information into the relevant sections of a SOAP note: | |
- Subjective (symptoms and patient statements), | |
- Objective (clinical findings and observations, these might be missing if the physician has not conducted a physical exam or has not verbally stated findings), | |
- Assessment (diagnosis or potential diagnoses, objectively provide a top 5 most likely diagnosis based on just the subjective findings, and use the objective findings if available), | |
- Plan (treatment and follow-up). | |
Ensure the note is concise, clear, and accurately reflects the conversation. | |
""" | |
# Function to transcribe audio inputs | |
def transcribe(inputs, task): | |
if inputs is None: | |
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.") | |
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"] | |
return text | |
# Function to generate SOAP notes using LLM | |
def generate_soap(transcribed_text): | |
prompt = f"<|im_start|>system\n{sys_prompt}<|im_end|>\n<|im_start|>user\n{task_prompt}\n{transcribed_text}<|im_end|>\n<|im_start|>assistant" | |
input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to("cuda") | |
generated_ids = model.generate(input_ids, max_new_tokens=2048, temperature=0.8, repetition_penalty=1.1, do_sample=True, eos_token_id=tokenizer.eos_token_id) | |
response = tokenizer.decode(generated_ids[0][input_ids.shape[-1]:], skip_special_tokens=True, clean_up_tokenization_space=True) | |
return response | |
# Gradio Interfaces for different inputs | |
demo = gr.Blocks(theme=gr.themes.Ocean()) | |
# Interface for microphone or file transcription | |
mf_transcribe = gr.Interface( | |
fn=transcribe, | |
inputs=[gr.Audio(sources="microphone", type="filepath"), gr.Radio(["transcribe", "translate"], label="Task", value="transcribe")], | |
outputs="text", | |
title="Audio Transcribe", | |
description="Transcribe long-form microphone or audio inputs." | |
) | |
file_transcribe = gr.Interface( | |
fn=transcribe, | |
inputs=[gr.Audio(sources="upload", type="filepath", label="Audio file"), gr.Radio(["transcribe", "translate"], label="Task", value="transcribe")], | |
outputs="text", | |
title="Audio Transcribe" | |
) | |
# SOAP Note generation interface | |
soap_note = gr.Interface( | |
fn=generate_soap, | |
inputs="text", | |
outputs="text", | |
title="Generate Clinical SOAP Note", | |
description="Convert transcribed conversation to a clinical SOAP note with structured sections (Subjective, Objective, Assessment, Plan)." | |
) | |
# Tabbed interface integrating SOAP note below transcription | |
with demo: | |
with gr.TabbedInterface([mf_transcribe, file_transcribe], ["Microphone", "Audio file"]) as transcribe_tab: | |
transcribe_tab.outputs[0] # Output from transcription feeds directly to SOAP note | |
soap_note # SOAP note interface placed directly below transcription output | |
demo.queue().launch(ssr_mode=False) | |