Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,65 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import whisper
|
|
|
|
| 3 |
|
| 4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def transcribe_audio(model_size, audio):
|
| 6 |
-
# Load the Whisper model based on the user's choice
|
| 7 |
model = whisper.load_model(model_size)
|
| 8 |
-
|
| 9 |
-
# Transcribe the audio file
|
| 10 |
result = model.transcribe(audio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
# Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
iface = gr.Interface(
|
| 16 |
-
fn=
|
| 17 |
inputs=[
|
| 18 |
-
gr.Dropdown(label="Choose Whisper Model", choices=["tiny", "base", "small", "medium", "large"], value="base"), #
|
| 19 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
],
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
description="Upload an audio file and select a Whisper model to get the transcription."
|
| 24 |
)
|
| 25 |
|
| 26 |
# Launch the interface
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import whisper
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Load Whisper model
|
| 6 |
+
whisper_model = whisper.load_model("base")
|
| 7 |
+
|
| 8 |
+
# Load traditional summarization models
|
| 9 |
+
def get_summarizer(model_name):
|
| 10 |
+
if model_name == "BART (facebook/bart-large-cnn)":
|
| 11 |
+
return pipeline("summarization", model="facebook/bart-large-cnn")
|
| 12 |
+
elif model_name == "T5 (t5-small)":
|
| 13 |
+
return pipeline("summarization", model="t5-small")
|
| 14 |
+
elif model_name == "Pegasus (google/pegasus-xsum)":
|
| 15 |
+
return pipeline("summarization", model="google/pegasus-xsum")
|
| 16 |
+
else:
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
# Function to transcribe audio file using Whisper
|
| 20 |
def transcribe_audio(model_size, audio):
|
|
|
|
| 21 |
model = whisper.load_model(model_size)
|
|
|
|
|
|
|
| 22 |
result = model.transcribe(audio)
|
| 23 |
+
transcription = result['text']
|
| 24 |
+
return transcription
|
| 25 |
+
|
| 26 |
+
# Function to summarize the transcribed text
|
| 27 |
+
def summarize_text(transcription, model_name):
|
| 28 |
+
if len(transcription.strip()) == 0:
|
| 29 |
+
return "No text to summarize."
|
| 30 |
+
|
| 31 |
+
summarizer = get_summarizer(model_name)
|
| 32 |
|
| 33 |
+
if summarizer:
|
| 34 |
+
summary = summarizer(transcription, max_length=150, min_length=30, do_sample=False)[0]['summary_text']
|
| 35 |
+
return summary
|
| 36 |
+
else:
|
| 37 |
+
return "Invalid summarization model selected."
|
| 38 |
|
| 39 |
+
# Create a Gradio interface that combines transcription and summarization
|
| 40 |
+
def combined_transcription_and_summarization(model_size, summarizer_model, audio):
|
| 41 |
+
# Step 1: Transcribe the audio using Whisper
|
| 42 |
+
transcription = transcribe_audio(model_size, audio)
|
| 43 |
+
|
| 44 |
+
# Step 2: Summarize the transcribed text using the chosen summarizer model
|
| 45 |
+
summary = summarize_text(transcription, summarizer_model)
|
| 46 |
+
|
| 47 |
+
return transcription, summary
|
| 48 |
+
|
| 49 |
+
# Gradio interface for transcription and summarization
|
| 50 |
iface = gr.Interface(
|
| 51 |
+
fn=combined_transcription_and_summarization, # The combined function
|
| 52 |
inputs=[
|
| 53 |
+
gr.Dropdown(label="Choose Whisper Model", choices=["tiny", "base", "small", "medium", "large"], value="base"), # Whisper model selection
|
| 54 |
+
gr.Dropdown(label="Choose Summarizer Model", choices=["BART (facebook/bart-large-cnn)", "T5 (t5-small)", "Pegasus (google/pegasus-xsum)"], value="BART (facebook/bart-large-cnn)"), # Summarizer model selection
|
| 55 |
+
gr.Audio(type="filepath") # Audio upload
|
| 56 |
+
],
|
| 57 |
+
outputs=[
|
| 58 |
+
gr.Textbox(label="Transcription"), # Output for the transcribed text
|
| 59 |
+
gr.Textbox(label="Summary") # Output for the summary
|
| 60 |
],
|
| 61 |
+
title="Whisper Audio Transcription and Summarization",
|
| 62 |
+
description="Upload an audio file, choose a Whisper model for transcription, and a summarization model to summarize the transcription."
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
# Launch the interface
|