Spaces:
Sleeping
Sleeping
first commit
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from pytube import YouTube
|
3 |
+
from moviepy.editor import *
|
4 |
+
import torch
|
5 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
|
6 |
+
from transformers import pipeline
|
7 |
+
import librosa
|
8 |
+
|
9 |
+
# Streamlit interface setup
|
10 |
+
st.title("YouTube Video Summarizer")
|
11 |
+
|
12 |
+
youtube_link = st.text_input("Enter YouTube Video Link:")
|
13 |
+
|
14 |
+
if st.button('Summarize'):
|
15 |
+
if not youtube_link:
|
16 |
+
st.warning("Please enter a valid YouTube link.")
|
17 |
+
else:
|
18 |
+
with st.spinner("Processing..."):
|
19 |
+
try:
|
20 |
+
# Download YouTube Video
|
21 |
+
yt = YouTube(youtube_link)
|
22 |
+
video = yt.streams.filter(only_audio=True).first()
|
23 |
+
download_path = video.download()
|
24 |
+
|
25 |
+
# Show progress
|
26 |
+
st.progress(25)
|
27 |
+
|
28 |
+
# Extract Audio
|
29 |
+
video_clip = AudioFileClip(download_path)
|
30 |
+
audio_path = download_path.replace('.mp4', '.wav')
|
31 |
+
video_clip.write_audiofile(audio_path)
|
32 |
+
|
33 |
+
# Show progress
|
34 |
+
st.progress(50)
|
35 |
+
|
36 |
+
# Speech to Text
|
37 |
+
tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-960h")
|
38 |
+
model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
|
39 |
+
|
40 |
+
# Load and process the audio
|
41 |
+
speech, _ = librosa.load(audio_path, sr=16000)
|
42 |
+
input_values = tokenizer(speech, return_tensors="pt").input_values
|
43 |
+
logits = model(input_values).logits
|
44 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
45 |
+
|
46 |
+
# Decode the speech
|
47 |
+
transcription = tokenizer.decode(predicted_ids[0])
|
48 |
+
|
49 |
+
# Show progress
|
50 |
+
st.progress(75)
|
51 |
+
|
52 |
+
# Summarization
|
53 |
+
summarizer = pipeline("summarization")
|
54 |
+
summary = summarizer(transcription, max_length=130, min_length=30, do_sample=False)
|
55 |
+
|
56 |
+
# Display the summary
|
57 |
+
st.success("Done!")
|
58 |
+
st.write("### Summary:")
|
59 |
+
st.write(summary[0]['summary_text'])
|
60 |
+
|
61 |
+
# Final progress
|
62 |
+
st.progress(100)
|
63 |
+
|
64 |
+
except Exception as e:
|
65 |
+
st.error(f"An error occurred: {e}")
|
66 |
+
|