Update app.py
Browse files
app.py
CHANGED
@@ -3,25 +3,28 @@ import os
|
|
3 |
from moviepy.editor import VideoFileClip
|
4 |
from transformers import pipeline
|
5 |
|
6 |
-
#
|
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 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
)
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|