Sumayyea commited on
Commit
233caf2
Β·
verified Β·
1 Parent(s): 32e2508

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -62
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("small") # Use a smaller model for faster processing
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 media
16
- async def transcribe_media(media_path, progress):
17
- result = whisper_model.transcribe(media_path)
18
- await progress(0.33) # Update progress to 33% after transcription
19
- return result["segments"]
20
 
21
- # Function to detect speakers (simple heuristic based on pauses)
22
- async def detect_speakers(segments, progress):
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
- if keywords:
36
- vectorizer = TfidfVectorizer(vocabulary=keywords)
37
- tfidf = vectorizer.fit_transform([summary[0]["summary_text"]])
38
- keyword_scores = dict(zip(keywords, tfidf.toarray()[0]))
39
- keyword_summary = {k: v for k, v in keyword_scores.items() if v > 0}
40
- if progress:
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
- async def process_media(media_file, keywords=None, progress=gr.Progress()):
49
- # Step 1: Transcribe the media
50
- segments = await transcribe_media(media_file, progress)
51
-
52
- # Step 2: Detect speakers
53
- speakers = await detect_speakers(segments, progress)
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 4: Answer the question
69
  answer = answer_question(summary, question)
70
  return answer
71
 
72
  # Gradio interface
73
  with gr.Blocks() as demo:
74
- gr.Markdown("# AI News & Podcast Summarizer")
75
  with gr.Tab("Transcribe and Summarize"):
76
- media_input = gr.Audio(label="Upload Audio/Video", type="filepath")
77
- keywords_input = gr.Textbox(label="Keywords (comma-separated, optional)", placeholder="e.g., finance, technology")
78
- speakers_output = gr.Textbox(label="Speakers", lines=10)
79
  summary_output = gr.Textbox(label="Summary", lines=5)
80
- keyword_summary_output = gr.Textbox(label="Keyword Summary", lines=5)
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
- process_button.click(
90
- process_media,
91
- inputs=[media_input, keywords_input],
92
- outputs=[speakers_output, summary_output, keyword_summary_output]
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,