Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,58 +1,48 @@
|
|
1 |
import torch
|
2 |
import gradio as gr
|
3 |
import whisper
|
4 |
-
from transformers import
|
5 |
|
6 |
-
# Load Whisper model for transcription
|
7 |
model_whisper = whisper.load_model("base")
|
8 |
|
9 |
-
# Load
|
10 |
-
|
11 |
-
tokenizer = BertTokenizer.from_pretrained(model_name)
|
12 |
-
model = EncoderDecoderModel.from_pretrained(model_name)
|
13 |
|
14 |
-
#
|
15 |
-
model.config.decoder_start_token_id = tokenizer.cls_token_id
|
16 |
-
model.config.pad_token_id = tokenizer.pad_token_id
|
17 |
-
|
18 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
19 |
-
model = model.to(device)
|
20 |
-
|
21 |
-
# Summarization Function
|
22 |
-
def generate_summary(text):
|
23 |
-
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding="max_length", max_length=512)
|
24 |
-
inputs = {k: v.to(device) for k, v in inputs.items()}
|
25 |
-
summary_ids = model.generate(**inputs, max_length=64, num_beams=4, early_stopping=True)
|
26 |
-
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
27 |
-
return summary
|
28 |
-
|
29 |
-
# Gradio Functions
|
30 |
def transcribe_and_summarize(audio_file):
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
def summarize_text_input(text_input):
|
37 |
-
|
38 |
-
|
|
|
|
|
39 |
|
40 |
-
# Gradio UI
|
41 |
with gr.Blocks() as demo:
|
42 |
-
gr.Markdown("## π₯ Medical
|
43 |
|
44 |
with gr.Tab("ποΈ Record & Summarize"):
|
45 |
-
audio_input = gr.Audio(type="filepath", label="Record
|
46 |
mic_transcript = gr.Textbox(label="Transcript")
|
47 |
mic_summary = gr.Textbox(label="Summary", interactive=False)
|
48 |
mic_button = gr.Button("Transcribe & Summarize")
|
49 |
mic_button.click(transcribe_and_summarize, inputs=[audio_input], outputs=[mic_transcript, mic_summary])
|
50 |
|
51 |
with gr.Tab("π Paste & Summarize"):
|
52 |
-
text_input = gr.Textbox(lines=8, label="Paste Dialogue
|
53 |
text_output = gr.Textbox(label="Summary", interactive=False)
|
54 |
text_button = gr.Button("Summarize")
|
55 |
text_button.click(summarize_text_input, inputs=[text_input], outputs=[text_input, text_output])
|
56 |
|
57 |
-
# Launch with sharing for local + link
|
58 |
demo.launch(share=True)
|
|
|
1 |
import torch
|
2 |
import gradio as gr
|
3 |
import whisper
|
4 |
+
from transformers import pipeline
|
5 |
|
6 |
+
# πΉ Load Whisper model (base) for transcription
|
7 |
model_whisper = whisper.load_model("base")
|
8 |
|
9 |
+
# πΉ Load summarization pipeline using BART
|
10 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
|
|
|
|
11 |
|
12 |
+
# πΉ Transcription + Summarization
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def transcribe_and_summarize(audio_file):
|
14 |
+
try:
|
15 |
+
result = model_whisper.transcribe(audio_file)
|
16 |
+
transcription = result.get("text", "")
|
17 |
+
if not transcription.strip():
|
18 |
+
return "No speech detected", "Summary not generated"
|
19 |
+
summary = summarizer(transcription, max_length=100, min_length=30, do_sample=False)
|
20 |
+
return transcription, summary[0]['summary_text']
|
21 |
+
except Exception as e:
|
22 |
+
return "Transcription failed", f"Error: {str(e)}"
|
23 |
+
|
24 |
+
# πΉ Text-only summarization
|
25 |
def summarize_text_input(text_input):
|
26 |
+
if not text_input.strip():
|
27 |
+
return "", "Input is empty"
|
28 |
+
summary = summarizer(text_input, max_length=100, min_length=30, do_sample=False)
|
29 |
+
return text_input, summary[0]['summary_text']
|
30 |
|
31 |
+
# πΉ Gradio UI
|
32 |
with gr.Blocks() as demo:
|
33 |
+
gr.Markdown("## π₯ Medical Summarization App (Whisper + BART)")
|
34 |
|
35 |
with gr.Tab("ποΈ Record & Summarize"):
|
36 |
+
audio_input = gr.Audio(type="filepath", label="Upload or Record Audio")
|
37 |
mic_transcript = gr.Textbox(label="Transcript")
|
38 |
mic_summary = gr.Textbox(label="Summary", interactive=False)
|
39 |
mic_button = gr.Button("Transcribe & Summarize")
|
40 |
mic_button.click(transcribe_and_summarize, inputs=[audio_input], outputs=[mic_transcript, mic_summary])
|
41 |
|
42 |
with gr.Tab("π Paste & Summarize"):
|
43 |
+
text_input = gr.Textbox(lines=8, label="Paste Medical Report or Dialogue")
|
44 |
text_output = gr.Textbox(label="Summary", interactive=False)
|
45 |
text_button = gr.Button("Summarize")
|
46 |
text_button.click(summarize_text_input, inputs=[text_input], outputs=[text_input, text_output])
|
47 |
|
|
|
48 |
demo.launch(share=True)
|