Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,50 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import whisper
|
| 3 |
from transformers import pipeline
|
| 4 |
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 5 |
-
from collections import defaultdict
|
| 6 |
-
import asyncio
|
| 7 |
|
| 8 |
# Load Whisper model for transcription
|
| 9 |
-
whisper_model = whisper.load_model("
|
| 10 |
|
| 11 |
# Load summarization and question-answering pipelines
|
| 12 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 13 |
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
| 14 |
|
| 15 |
-
# Function to transcribe
|
| 16 |
-
|
| 17 |
-
result = whisper_model.transcribe(
|
| 18 |
-
|
| 19 |
-
return result["segments"]
|
| 20 |
|
| 21 |
-
# Function to
|
| 22 |
-
|
| 23 |
-
speakers = defaultdict(list)
|
| 24 |
-
current_speaker = "Speaker 1"
|
| 25 |
-
for segment in segments:
|
| 26 |
-
speakers[current_speaker].append(segment["text"])
|
| 27 |
-
if segment["end"] - segment["start"] > 2.0: # Pause longer than 2 seconds
|
| 28 |
-
current_speaker = f"Speaker {len(speakers) + 1}"
|
| 29 |
-
await progress(0.66) # Update progress to 66% after speaker detection
|
| 30 |
-
return speakers
|
| 31 |
-
|
| 32 |
-
# Function to summarize text with keywords
|
| 33 |
-
async def summarize_text_with_keywords(text, keywords=None, progress=None):
|
| 34 |
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
await progress(1.0) # Update progress to 100% after summarization
|
| 42 |
-
return summary[0]["summary_text"], keyword_summary
|
| 43 |
-
if progress:
|
| 44 |
-
await progress(1.0) # Update progress to 100% after summarization
|
| 45 |
-
return summary[0]["summary_text"], {}
|
| 46 |
|
| 47 |
# Main function for Gradio interface
|
| 48 |
-
|
| 49 |
-
# Step 1: Transcribe the
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# Step 3: Summarize the transcript
|
| 56 |
-
full_transcript = " ".join([segment["text"] for segment in segments])
|
| 57 |
-
summary, keyword_summary = await summarize_text_with_keywords(full_transcript, keywords, progress)
|
| 58 |
-
|
| 59 |
-
# Format speakers' output
|
| 60 |
-
speakers_output = "\n\n".join([f"{speaker}:\n{' '.join(text)}" for speaker, text in speakers.items()])
|
| 61 |
-
|
| 62 |
-
# Format keyword summary
|
| 63 |
-
keyword_summary_output = "\n".join([f"{k}: {v:.2f}" for k, v in keyword_summary.items()])
|
| 64 |
-
|
| 65 |
-
return speakers_output, summary, keyword_summary_output
|
| 66 |
|
| 67 |
def ask_question(summary, question):
|
| 68 |
-
# Step
|
| 69 |
answer = answer_question(summary, question)
|
| 70 |
return answer
|
| 71 |
|
| 72 |
# Gradio interface
|
| 73 |
with gr.Blocks() as demo:
|
| 74 |
-
gr.Markdown("#
|
| 75 |
with gr.Tab("Transcribe and Summarize"):
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
speakers_output = gr.Textbox(label="Speakers", lines=10)
|
| 79 |
summary_output = gr.Textbox(label="Summary", lines=5)
|
| 80 |
-
|
| 81 |
-
process_button = gr.Button("Process Media")
|
| 82 |
with gr.Tab("Ask a Question"):
|
| 83 |
summary_input = gr.Textbox(label="Paste Summary Here", lines=5)
|
| 84 |
question_input = gr.Textbox(label="Your Question")
|
|
@@ -86,10 +52,10 @@ with gr.Blocks() as demo:
|
|
| 86 |
ask_button = gr.Button("Ask Question")
|
| 87 |
|
| 88 |
# Set up button actions
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
inputs=
|
| 92 |
-
outputs=[
|
| 93 |
)
|
| 94 |
ask_button.click(
|
| 95 |
ask_question,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import whisper
|
| 3 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Load Whisper model for transcription
|
| 6 |
+
whisper_model = whisper.load_model("base")
|
| 7 |
|
| 8 |
# Load summarization and question-answering pipelines
|
| 9 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 10 |
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
| 11 |
|
| 12 |
+
# Function to transcribe video
|
| 13 |
+
def transcribe_video(video_path):
|
| 14 |
+
result = whisper_model.transcribe(video_path)
|
| 15 |
+
return result["text"]
|
|
|
|
| 16 |
|
| 17 |
+
# Function to summarize text
|
| 18 |
+
def summarize_text(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
|
| 20 |
+
return summary[0]["summary_text"]
|
| 21 |
+
|
| 22 |
+
# Function to answer questions
|
| 23 |
+
def answer_question(summary, question):
|
| 24 |
+
answer = qa_pipeline(question=question, context=summary)
|
| 25 |
+
return answer["answer"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Main function for Gradio interface
|
| 28 |
+
def process_video(video_path):
|
| 29 |
+
# Step 1: Transcribe the video
|
| 30 |
+
transcript = transcribe_video(video_path)
|
| 31 |
+
# Step 2: Summarize the transcript
|
| 32 |
+
summary = summarize_text(transcript)
|
| 33 |
+
return transcript, summary
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def ask_question(summary, question):
|
| 36 |
+
# Step 3: Answer the question
|
| 37 |
answer = answer_question(summary, question)
|
| 38 |
return answer
|
| 39 |
|
| 40 |
# Gradio interface
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("# Video Transcription, Summarization, and Q&A")
|
| 43 |
with gr.Tab("Transcribe and Summarize"):
|
| 44 |
+
video_input = gr.Video(label="Upload Video")
|
| 45 |
+
transcript_output = gr.Textbox(label="Transcript", lines=10)
|
|
|
|
| 46 |
summary_output = gr.Textbox(label="Summary", lines=5)
|
| 47 |
+
transcribe_button = gr.Button("Transcribe and Summarize")
|
|
|
|
| 48 |
with gr.Tab("Ask a Question"):
|
| 49 |
summary_input = gr.Textbox(label="Paste Summary Here", lines=5)
|
| 50 |
question_input = gr.Textbox(label="Your Question")
|
|
|
|
| 52 |
ask_button = gr.Button("Ask Question")
|
| 53 |
|
| 54 |
# Set up button actions
|
| 55 |
+
transcribe_button.click(
|
| 56 |
+
process_video,
|
| 57 |
+
inputs=video_input,
|
| 58 |
+
outputs=[transcript_output, summary_output]
|
| 59 |
)
|
| 60 |
ask_button.click(
|
| 61 |
ask_question,
|