cfc-tech commited on
Commit
05349b5
·
verified ·
1 Parent(s): 7b1110c
Files changed (1) hide show
  1. app.py +0 -90
app.py DELETED
@@ -1,90 +0,0 @@
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
- from pydub import AudioSegment, silence
7
- import os
8
-
9
- def download_and_extract_audio(youtube_link):
10
- try:
11
- yt = YouTube(youtube_link)
12
- video = yt.streams.filter(only_audio=True).first()
13
- out_file = video.download(output_path=".")
14
- base, ext = os.path.splitext(out_file)
15
- new_file = base + '.mp3'
16
- os.rename(out_file, new_file)
17
-
18
- # Convert MP3 to WAV using moviepy
19
- audio_clip = AudioFileClip(new_file)
20
- wav_file = base + '.wav'
21
- audio_clip.write_audiofile(wav_file, codec='pcm_s16le') # codec for WAV format
22
- audio_clip.close()
23
- return wav_file
24
- except Exception as e:
25
- st.error(f"Error downloading and extracting audio: {e}")
26
- return None
27
-
28
- def transcribe_audio(audio_path):
29
- try:
30
- recognizer = sr.Recognizer()
31
- audio = AudioSegment.from_wav(audio_path)
32
-
33
- # Split the audio into non-silent chunks
34
- audio_chunks = silence.split_on_silence(audio, min_silence_len=1000, silence_thresh=-40)
35
-
36
- full_text = ""
37
- for i, chunk in enumerate(audio_chunks):
38
- chunk.export(f"/tmp/chunk{i}.wav", format="wav")
39
- with sr.AudioFile(f"/tmp/chunk{i}.wav") as source:
40
- audio_data = recognizer.record(source)
41
- try:
42
- text = recognizer.recognize_google(audio_data)
43
- full_text += " " + text
44
- except sr.UnknownValueError:
45
- # Skip segments that the recognizer can't understand
46
- continue
47
-
48
- return full_text.strip()
49
- except Exception as e:
50
- st.error(f"Error transcribing audio: {e}")
51
- return None
52
-
53
- def summarize_text(text):
54
- try:
55
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
56
- summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
57
- return summary[0]['summary_text']
58
- except Exception as e:
59
- st.error(f"Error summarizing text: {e}")
60
- return None
61
-
62
- def main():
63
- st.title("YouTube Video Summary")
64
- youtube_link = st.text_input("Enter YouTube Video Link:")
65
- if st.button("Summarize"):
66
- if youtube_link:
67
- with st.spinner('Downloading and extracting audio...'):
68
- audio_path = download_and_extract_audio(youtube_link)
69
- if audio_path is None:
70
- return
71
-
72
- with st.spinner('Transcribing audio to text...'):
73
- transcription = transcribe_audio(audio_path)
74
- if transcription is None:
75
- return
76
-
77
- with st.spinner('Summarizing transcription...'):
78
- summary = summarize_text(transcription)
79
- if summary is None:
80
- return
81
-
82
- st.subheader("Transcription:")
83
- st.write(transcription)
84
- st.subheader("Summary:")
85
- st.write(summary)
86
- else:
87
- st.error("Please enter a valid YouTube video link.")
88
-
89
- if __name__ == '__main__':
90
- main()