Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,50 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
import os
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
from transformers import pipeline
|
8 |
|
9 |
-
# Load ASR (Speech-to-Text) pipeline
|
10 |
asr = pipeline(task="automatic-speech-recognition", model="distil-whisper/distil-small.en")
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
# Load Summarization model
|
16 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
return "Error: No audio file provided.", ""
|
22 |
-
|
23 |
|
24 |
try:
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
29 |
transcribed_text = " ".join([segment['text'] for segment in transcription_result['chunks']])
|
30 |
|
31 |
-
#
|
32 |
if len(transcribed_text.split()) < 50:
|
33 |
summarized_text = "Text too short to summarize."
|
34 |
else:
|
35 |
-
# Summarize the transcribed text
|
36 |
summary_result = summarizer(transcribed_text, max_length=100, min_length=30, do_sample=False)
|
37 |
summarized_text = summary_result[0]['summary_text']
|
38 |
|
39 |
return transcribed_text, summarized_text
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
except Exception as e:
|
45 |
return f"Error: {str(e)}", ""
|
46 |
|
47 |
# Create Gradio interface
|
48 |
-
|
49 |
iface = gr.Interface(
|
50 |
fn=transcribe_and_summarize,
|
51 |
-
inputs=gr.
|
52 |
outputs=[
|
53 |
-
gr.Textbox(label="Transcribed Text"),
|
54 |
gr.Textbox(label="Summarized Text")
|
55 |
]
|
56 |
)
|
57 |
|
58 |
-
#
|
59 |
port = int(os.environ.get('PORT1', 7860))
|
60 |
-
|
61 |
-
# Launch Gradio app
|
62 |
-
iface.launch(share=True, server_port=port)
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
+
from moviepy.editor import VideoFileClip
|
|
|
|
|
4 |
from transformers import pipeline
|
5 |
|
6 |
+
# Load ASR (Speech-to-Text) pipeline
|
7 |
asr = pipeline(task="automatic-speech-recognition", model="distil-whisper/distil-small.en")
|
8 |
|
|
|
|
|
|
|
9 |
# Load 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 if long enough
|
27 |
if len(transcribed_text.split()) < 50:
|
28 |
summarized_text = "Text too short to summarize."
|
29 |
else:
|
|
|
30 |
summary_result = summarizer(transcribed_text, max_length=100, min_length=30, do_sample=False)
|
31 |
summarized_text = summary_result[0]['summary_text']
|
32 |
|
33 |
return transcribed_text, summarized_text
|
34 |
|
|
|
|
|
|
|
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(type="filepath", 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)
|
|
|
|