dtkne commited on
Commit
4041d63
·
verified ·
1 Parent(s): 042492f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -16
app.py CHANGED
@@ -3,25 +3,28 @@ import os
3
  from moviepy.editor import VideoFileClip
4
  from transformers import pipeline
5
 
6
- # ASR (Speech-to-Text) pipeline
7
  asr = pipeline(task="automatic-speech-recognition", model="distil-whisper/distil-small.en")
8
-
9
- # Summarization model
10
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
 
 
 
 
11
 
12
  def transcribe_and_summarize(video_file):
 
 
13
  if video_file is None:
14
  return "Error: No file provided.", ""
15
 
16
  try:
17
- # Extract audio from the video file
18
  video = VideoFileClip(video_file)
19
  audio_path = "temp_audio.wav"
20
  video.audio.write_audiofile(audio_path, codec='pcm_s16le')
21
 
22
- # Transcribe the extracted audio
23
  transcription_result = asr(audio_path, return_timestamps=True)
24
  transcribed_text = " ".join([segment['text'] for segment in transcription_result['chunks']])
 
25
 
26
  # Summarize
27
  if len(transcribed_text.split()) < 50:
@@ -35,16 +38,30 @@ def transcribe_and_summarize(video_file):
35
  except Exception as e:
36
  return f"Error: {str(e)}", ""
37
 
38
- # Create Gradio interface
39
- iface = gr.Interface(
40
- fn=transcribe_and_summarize,
41
- inputs=gr.Video(label="Upload Video (.mp4)"),
42
- outputs=[
43
- gr.Textbox(label="Transcribed Text"),
44
- gr.Textbox(label="Summarized Text")
45
- ]
46
- )
47
-
48
- # Launch app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  port = int(os.environ.get('PORT1', 7860))
50
  iface.launch(share=True, server_port=port)
 
3
  from moviepy.editor import VideoFileClip
4
  from transformers import pipeline
5
 
6
+ # Load models
7
  asr = pipeline(task="automatic-speech-recognition", model="distil-whisper/distil-small.en")
 
 
8
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
9
+ qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
10
+
11
+ # Global variable to store transcript for Q&A
12
+ stored_transcript = ""
13
 
14
  def transcribe_and_summarize(video_file):
15
+ global stored_transcript
16
+
17
  if video_file is None:
18
  return "Error: No file provided.", ""
19
 
20
  try:
 
21
  video = VideoFileClip(video_file)
22
  audio_path = "temp_audio.wav"
23
  video.audio.write_audiofile(audio_path, codec='pcm_s16le')
24
 
 
25
  transcription_result = asr(audio_path, return_timestamps=True)
26
  transcribed_text = " ".join([segment['text'] for segment in transcription_result['chunks']])
27
+ stored_transcript = transcribed_text # Save for Q&A
28
 
29
  # Summarize
30
  if len(transcribed_text.split()) < 50:
 
38
  except Exception as e:
39
  return f"Error: {str(e)}", ""
40
 
41
+ def answer_question(question):
42
+ global stored_transcript
43
+ if not stored_transcript:
44
+ return "Please transcribe a video first."
45
+ result = qa_pipeline(question=question, context=stored_transcript)
46
+ return result['answer']
47
+
48
+ # Gradio interface with three parts
49
+ with gr.Blocks() as iface:
50
+ with gr.Row():
51
+ video_input = gr.Video(label="Upload Video (.mp4)")
52
+ transcribed_text = gr.Textbox(label="Transcribed Text", lines=6)
53
+ summarized_text = gr.Textbox(label="Summarized Text", lines=6)
54
+
55
+ transcribe_btn = gr.Button("Transcribe and Summarize")
56
+ transcribe_btn.click(fn=transcribe_and_summarize, inputs=video_input, outputs=[transcribed_text, summarized_text])
57
+
58
+ with gr.Row():
59
+ question_input = gr.Textbox(label="Ask a question about the transcript")
60
+ answer_output = gr.Textbox(label="Answer")
61
+
62
+ ask_btn = gr.Button("Get Answer")
63
+ ask_btn.click(fn=answer_question, inputs=question_input, outputs=answer_output)
64
+
65
+ # Launch
66
  port = int(os.environ.get('PORT1', 7860))
67
  iface.launch(share=True, server_port=port)