Spaces:
Sleeping
Sleeping
import streamlit as st | |
from pytube import YouTube | |
from moviepy.editor import * | |
import speech_recognition as sr | |
from transformers import pipeline | |
from pydub import AudioSegment | |
import os | |
def download_and_extract_audio(youtube_link): | |
yt = YouTube(youtube_link) | |
video = yt.streams.filter(only_audio=True).first() | |
out_file = video.download(output_path=".") | |
base, ext = os.path.splitext(out_file) | |
new_file = base + '.mp3' | |
os.rename(out_file, new_file) | |
# Convert MP3 to WAV for compatibility with speech_recognition | |
audio = AudioSegment.from_mp3(new_file) | |
wav_file = base + '.wav' | |
audio.export(wav_file, format="wav") | |
return wav_file | |
def transcribe_audio(audio_path): | |
recognizer = sr.Recognizer() | |
with sr.AudioFile(audio_path) as source: | |
audio_data = recognizer.record(source) | |
text = recognizer.recognize_google(audio_data) | |
return text | |
def summarize_text(text): | |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn") | |
summary = summarizer(text, max_length=130, min_length=30, do_sample=False) | |
return summary[0]['summary_text'] | |
def main(): | |
st.title("YouTube Video Summary") | |
youtube_link = st.text_input("Enter YouTube Video Link:") | |
if st.button("Summarize"): | |
with st.spinner('Downloading and extracting audio...'): | |
audio_path = download_and_extract_audio(youtube_link) | |
with st.spinner('Transcribing audio to text...'): | |
transcription = transcribe_audio(audio_path) | |
with st.spinner('Summarizing transcription...'): | |
summary = summarize_text(transcription) | |
st.subheader("Transcription:") | |
st.write(transcription) | |
st.subheader("Summary:") | |
st.write(summary) | |
if __name__ == '__main__': | |
main() | |