leninjr commited on
Commit
22d27ba
·
verified ·
1 Parent(s): 6d9ed4c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import whisper
3
+ from moviepy.editor import VideoFileClip
4
+ import torch
5
+ import os
6
+ import yt_dlp # Updated import
7
+ from transformers import pipeline
8
+
9
+ # Load the Whisper model once with GPU support
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ model = whisper.load_model("base", device=device) # Choose appropriate model size
12
+
13
+ # Load the summarization pipeline
14
+ summarizer = pipeline("summarization")
15
+
16
+ # Define chunk length in seconds
17
+ chunk_len_s = 10
18
+
19
+ def download_video(youtube_url, audio_file_path):
20
+ """
21
+ Downloads a YouTube video and extracts audio, saving it as an MP3 file.
22
+ """
23
+ try:
24
+ ydl_opts = {
25
+ 'format': 'bestaudio/best',
26
+ 'outtmpl': audio_file_path.replace('.mp3', '') + '.%(ext)s', # Ensure correct extension handling
27
+ 'postprocessors': [{
28
+ 'key': 'FFmpegExtractAudio',
29
+ 'preferredcodec': 'mp3',
30
+ 'preferredquality': '192',
31
+ }],
32
+ }
33
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
34
+ ydl.download([youtube_url])
35
+
36
+ final_audio_file_path = audio_file_path if audio_file_path.endswith('.mp3') else audio_file_path + '.mp3'
37
+ print(f"Audio downloaded and saved as {final_audio_file_path}")
38
+ return final_audio_file_path
39
+
40
+ except Exception as e:
41
+ print(f"Error downloading video: {e}")
42
+ return None
43
+
44
+ def transcribe_audio_in_chunks(audio_file_path, chunk_len_s):
45
+ """
46
+ Transcribes a provided audio file in chunks using the loaded Whisper model.
47
+ """
48
+ try:
49
+ if not os.path.exists(audio_file_path):
50
+ print(f"Error: MP3 file {audio_file_path} not found")
51
+ return None
52
+
53
+ # Load and preprocess the audio file
54
+ audio = whisper.load_audio(audio_file_path)
55
+ audio_length = len(audio) / whisper.audio.SAMPLE_RATE
56
+
57
+ # Transcribe the audio in chunks
58
+ transcription = ""
59
+ for start in range(0, int(audio_length), chunk_len_s):
60
+ end = min(start + chunk_len_s, int(audio_length))
61
+ chunk = audio[int(start * whisper.audio.SAMPLE_RATE):int(end * whisper.audio.SAMPLE_RATE)]
62
+ chunk = whisper.pad_or_trim(chunk)
63
+ result = model.transcribe(chunk)
64
+ transcription += result['text'] + " "
65
+
66
+ return transcription.strip()
67
+ except Exception as e:
68
+ print(f"Error transcribing audio: {e}")
69
+ return None
70
+
71
+ def summarize_text(text):
72
+ """
73
+ Summarizes the provided text by splitting it into smaller chunks if necessary.
74
+ """
75
+ try:
76
+ # Split the text into chunks of 1024 tokens
77
+ max_chunk_size = 1024
78
+ text_chunks = [text[i:i + max_chunk_size] for i in range(0, len(text), max_chunk_size)]
79
+
80
+ # Summarize each chunk and combine the summaries
81
+ summaries = []
82
+ for chunk in text_chunks:
83
+ summary = summarizer(chunk, max_length=150, min_length=40, do_sample=False)[0]['summary_text']
84
+ summaries.append(summary)
85
+
86
+ # Combine all summaries into one
87
+ combined_summary = " ".join(summaries)
88
+ return combined_summary
89
+ except Exception as e:
90
+ print(f"Error summarizing text: {e}")
91
+ return None
92
+
93
+ def main(youtube_url):
94
+ """
95
+ Main workflow: Downloads audio from YouTube video, transcribes it in chunks, and summarizes the transcription.
96
+ """
97
+ audio_file_path = "audio.mp3"
98
+
99
+ # Download video and extract audio
100
+ downloaded_audio_path = download_video(youtube_url, audio_file_path)
101
+ if downloaded_audio_path:
102
+ # Transcribe the MP3 file in chunks
103
+ transcription = transcribe_audio_in_chunks(downloaded_audio_path, chunk_len_s)
104
+ if transcription:
105
+ print("Transcription:", transcription)
106
+ # Summarize the transcription
107
+ summary = summarize_text(transcription)
108
+ if summary:
109
+ return transcription, summary
110
+ return None, None
111
+
112
+ # Streamlit interface
113
+ st.title("YouTube Video Transcription and Summarization")
114
+
115
+ youtube_url = st.text_input("Enter YouTube Video URL", "https://www.youtube.com/watch?v=your_video_id")
116
+ if st.button("Submit"):
117
+ transcription, summary = main(youtube_url)
118
+ if transcription:
119
+ st.subheader("Transcription")
120
+ st.text_area("Transcription", transcription, height=300)
121
+ else:
122
+ st.error("Transcription failed.")
123
+
124
+ if summary:
125
+ st.subheader("Summary")
126
+ st.text_area("Summary", summary, height=150)
127
+ else:
128
+ st.error("Summary failed.")