Spaces:
Sleeping
Sleeping
apps
Browse files
app.py
CHANGED
@@ -1,52 +1,75 @@
|
|
1 |
import streamlit as st
|
2 |
from pytube import YouTube
|
3 |
-
from moviepy.editor import
|
4 |
import speech_recognition as sr
|
5 |
from transformers import pipeline
|
6 |
-
from pydub import AudioSegment
|
7 |
import os
|
8 |
|
9 |
def download_and_extract_audio(youtube_link):
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
def transcribe_audio(audio_path):
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
def summarize_text(text):
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
|
35 |
def main():
|
36 |
st.title("YouTube Video Summary")
|
37 |
youtube_link = st.text_input("Enter YouTube Video Link:")
|
38 |
if st.button("Summarize"):
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
if __name__ == '__main__':
|
52 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
from pytube import YouTube
|
3 |
+
from moviepy.editor import AudioFileClip
|
4 |
import speech_recognition as sr
|
5 |
from transformers import pipeline
|
|
|
6 |
import os
|
7 |
|
8 |
def download_and_extract_audio(youtube_link):
|
9 |
+
try:
|
10 |
+
yt = YouTube(youtube_link)
|
11 |
+
video = yt.streams.filter(only_audio=True).first()
|
12 |
+
out_file = video.download(output_path=".")
|
13 |
+
base, ext = os.path.splitext(out_file)
|
14 |
+
new_file = base + '.mp3'
|
15 |
+
os.rename(out_file, new_file)
|
16 |
+
|
17 |
+
# Convert MP3 to WAV using moviepy
|
18 |
+
audio_clip = AudioFileClip(new_file)
|
19 |
+
wav_file = base + '.wav'
|
20 |
+
audio_clip.write_audiofile(wav_file, codec='pcm_s16le') # codec for WAV format
|
21 |
+
audio_clip.close()
|
22 |
+
return wav_file
|
23 |
+
except Exception as e:
|
24 |
+
st.error(f"Error downloading and extracting audio: {e}")
|
25 |
+
return None
|
26 |
|
27 |
def transcribe_audio(audio_path):
|
28 |
+
try:
|
29 |
+
recognizer = sr.Recognizer()
|
30 |
+
with sr.AudioFile(audio_path) as source:
|
31 |
+
audio_data = recognizer.record(source)
|
32 |
+
text = recognizer.recognize_google(audio_data)
|
33 |
+
return text
|
34 |
+
except Exception as e:
|
35 |
+
st.error(f"Error transcribing audio: {e}")
|
36 |
+
return None
|
37 |
|
38 |
def summarize_text(text):
|
39 |
+
try:
|
40 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
41 |
+
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
|
42 |
+
return summary[0]['summary_text']
|
43 |
+
except Exception as e:
|
44 |
+
st.error(f"Error summarizing text: {e}")
|
45 |
+
return None
|
46 |
|
47 |
def main():
|
48 |
st.title("YouTube Video Summary")
|
49 |
youtube_link = st.text_input("Enter YouTube Video Link:")
|
50 |
if st.button("Summarize"):
|
51 |
+
if youtube_link:
|
52 |
+
with st.spinner('Downloading and extracting audio...'):
|
53 |
+
audio_path = download_and_extract_audio(youtube_link)
|
54 |
+
if audio_path is None:
|
55 |
+
return
|
56 |
+
|
57 |
+
with st.spinner('Transcribing audio to text...'):
|
58 |
+
transcription = transcribe_audio(audio_path)
|
59 |
+
if transcription is None:
|
60 |
+
return
|
61 |
+
|
62 |
+
with st.spinner('Summarizing transcription...'):
|
63 |
+
summary = summarize_text(transcription)
|
64 |
+
if summary is None:
|
65 |
+
return
|
66 |
+
|
67 |
+
st.subheader("Transcription:")
|
68 |
+
st.write(transcription)
|
69 |
+
st.subheader("Summary:")
|
70 |
+
st.write(summary)
|
71 |
+
else:
|
72 |
+
st.error("Please enter a valid YouTube video link.")
|
73 |
|
74 |
if __name__ == '__main__':
|
75 |
main()
|