Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,50 @@
|
|
|
|
1 |
import os
|
2 |
-
import torch
|
3 |
-
from pytubefix import YouTube
|
4 |
-
from moviepy.editor import VideoFileClip
|
5 |
from transformers import pipeline
|
6 |
-
pip install moviepy
|
7 |
-
# ---- STEP 1: Download YouTube Video ----
|
8 |
-
url = "https://www.youtube.com/watch?v=VgxnyKnB3qc&ab"
|
9 |
-
yt = YouTube(url)
|
10 |
-
title = yt.title
|
11 |
-
print(f"Downloading: {title}")
|
12 |
|
13 |
-
|
14 |
-
video_path = f"/content/{title}.mp4"
|
15 |
-
video_stream.download(filename=video_path)
|
16 |
-
|
17 |
-
print(f"Video saved as: {video_path}")
|
18 |
-
|
19 |
-
# ---- STEP 2: Extract Audio from Video ----
|
20 |
-
output_audio = f"/content/{title}.wav"
|
21 |
-
|
22 |
-
video = VideoFileClip(video_path)
|
23 |
-
video.audio.write_audiofile(output_audio)
|
24 |
-
|
25 |
-
print(f"Audio extracted: {output_audio}")
|
26 |
-
|
27 |
-
# ---- STEP 3: Transcribe Audio ----
|
28 |
asr = pipeline(task="automatic-speech-recognition", model="distil-whisper/distil-small.en")
|
29 |
|
30 |
-
|
31 |
-
print("Transcribing audio...")
|
32 |
-
transcription_result = asr(audio_file)
|
33 |
-
transcribed_text = transcription_result["text"]
|
34 |
-
return transcribed_text
|
35 |
-
|
36 |
-
transcribed_text = transcribe_audio(output_audio)
|
37 |
-
print("Transcription Complete:\n", transcribed_text[:500]) # Preview first 500 characters
|
38 |
-
|
39 |
-
# ---- STEP 4: Summarize Transcription ----
|
40 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
import os
|
|
|
|
|
|
|
3 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# Load ASR (Speech-to-Text) pipeline with timestamp handling
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
asr = pipeline(task="automatic-speech-recognition", model="distil-whisper/distil-small.en")
|
7 |
|
8 |
+
# Load Summarization model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
10 |
|
11 |
+
# Function to transcribe and summarize audio
|
12 |
+
def transcribe_and_summarize(audio_file):
|
13 |
+
if audio_file is None:
|
14 |
+
return "Error: No audio file provided.", ""
|
15 |
+
|
16 |
+
try:
|
17 |
+
# Transcribe audio (handling long-form audio)
|
18 |
+
transcription_result = asr(audio_file, return_timestamps=True)
|
19 |
+
|
20 |
+
# Extract transcribed text
|
21 |
+
transcribed_text = " ".join([segment['text'] for segment in transcription_result['chunks']])
|
22 |
+
|
23 |
+
# Ensure the transcribed text isn't too short for summarization
|
24 |
+
if len(transcribed_text.split()) < 50:
|
25 |
+
summarized_text = "Text too short to summarize."
|
26 |
+
else:
|
27 |
+
# Summarize the transcribed text
|
28 |
+
summary_result = summarizer(transcribed_text, max_length=100, min_length=30, do_sample=False)
|
29 |
+
summarized_text = summary_result[0]['summary_text']
|
30 |
+
|
31 |
+
return transcribed_text, summarized_text
|
32 |
+
|
33 |
+
except Exception as e:
|
34 |
+
return f"Error: {str(e)}", ""
|
35 |
+
|
36 |
+
# Create Gradio interface
|
37 |
+
iface = gr.Interface(
|
38 |
+
fn=transcribe_and_summarize,
|
39 |
+
inputs=gr.Audio(type="filepath"), # Accepts an audio file
|
40 |
+
outputs=[
|
41 |
+
gr.Textbox(label="Transcribed Text"),
|
42 |
+
gr.Textbox(label="Summarized Text")
|
43 |
+
]
|
44 |
+
)
|
45 |
+
|
46 |
+
# Get port safely (default to 7860 if not set)
|
47 |
+
port = int(os.environ.get('PORT1', 7860))
|
48 |
+
|
49 |
+
# Launch Gradio app
|
50 |
+
iface.launch(share=True, server_port=port)
|