Spaces:
Running
Running
Commit
Β·
b6d4bd3
1
Parent(s):
cd2b0ba
updated app.py
Browse files- app.py +48 -30
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,40 +1,58 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from transformers import BertTokenizer, EncoderDecoderModel
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
# Load model
|
| 6 |
-
|
| 7 |
-
tokenizer = BertTokenizer.from_pretrained("Imsachinsingh00/bert2bert-mts-summary")
|
| 8 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
-
model = model.to(device)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 16 |
-
return summary
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
text_input = gr.Textbox(label="π Or Paste Dialogue", lines=10, placeholder="Paste or speak a conversation here...")
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
summary = summarize_text(text)
|
| 36 |
-
return transcribed_text, summary
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
import gradio as gr
|
| 3 |
+
import whisper
|
| 4 |
from transformers import BertTokenizer, EncoderDecoderModel
|
|
|
|
| 5 |
|
| 6 |
+
# Load Whisper model for transcription
|
| 7 |
+
model_whisper = whisper.load_model("base")
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Load Summarization Model & Tokenizer
|
| 10 |
+
model_name = "Imsachinsingh00/bert2bert-mts-summary"
|
| 11 |
+
tokenizer = BertTokenizer.from_pretrained(model_name)
|
| 12 |
+
model = EncoderDecoderModel.from_pretrained(model_name)
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# β
REQUIRED for encoder-decoder models
|
| 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 |
+
result = model_whisper.transcribe(audio_file)
|
| 32 |
+
transcription = result["text"]
|
| 33 |
+
summary = generate_summary(transcription)
|
| 34 |
+
return transcription, summary
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
def summarize_text_input(text_input):
|
| 37 |
+
summary = generate_summary(text_input)
|
| 38 |
+
return text_input, summary
|
| 39 |
|
| 40 |
+
# Gradio UI
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("## π₯ Medical Dialogue Summarizer")
|
| 43 |
+
|
| 44 |
+
with gr.Tab("ποΈ Record & Summarize"):
|
| 45 |
+
audio_input = gr.Audio(type="filepath", label="Record Doctor-Patient Conversation")
|
| 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 Here")
|
| 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)
|
requirements.txt
CHANGED
|
@@ -6,4 +6,5 @@ torch
|
|
| 6 |
gradio
|
| 7 |
scikit-learn
|
| 8 |
huggingface_hub
|
| 9 |
-
whisper
|
|
|
|
|
|
| 6 |
gradio
|
| 7 |
scikit-learn
|
| 8 |
huggingface_hub
|
| 9 |
+
whisper
|
| 10 |
+
git+https://github.com/openai/whisper.git
|