Sayiqa7 commited on
Commit
f60d1cb
·
verified ·
1 Parent(s): 196d2a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -49
app.py CHANGED
@@ -144,52 +144,35 @@ else:
144
 
145
 
146
  ##########################
147
- def get_video_info(url, max_retries=3):
148
- """Get video title and transcript with retries"""
149
- video_id = extract_video_id(url)
150
- if not video_id:
151
- return None, "Invalid YouTube URL"
152
-
153
- # Try multiple times to get video info
154
- for attempt in range(max_retries):
155
- try:
156
- # Initialize YouTube object with additional parameters
157
- yt = YouTube(
158
- url,
159
- use_oauth=False,
160
- allow_oauth_cache=True
161
- )
162
-
163
- # Add a small delay
164
- time.sleep(1)
165
-
166
- # Try to get title, with fallback
167
- try:
168
- title = yt.title
169
- except:
170
- title = f"Video {video_id}"
171
-
172
- # Try to get transcript (including auto-generated captions)
173
- transcript = None
174
- try:
175
- transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
176
- transcript = ' '.join([t['text'] for t in transcript_list])
177
- except:
178
- try:
179
- # Try getting auto-generated transcript
180
- transcript_list = YouTubeTranscriptApi.get_transcript(video_id, languages=['en-US', 'en'])
181
- transcript = ' '.join([t['text'] for t in transcript_list])
182
- except Exception as e:
183
- print(f"Transcript error: {str(e)}")
184
- # If both fail, return error message
185
- return None, "No transcript available for this video. Please try a video with captions enabled."
186
-
187
- return title, transcript
188
-
189
- except Exception as e:
190
- if attempt == max_retries - 1:
191
- return None, f"Failed to fetch video information after {max_retries} attempts. Error: {str(e)}"
192
- time.sleep(1) # Wait before retrying
193
- # Launch the interface
194
- if __name__ == "__main__":
195
- interface.launch()
 
144
 
145
 
146
  ##########################
147
+ from pytube import YouTube
148
+
149
+ # Replace with your YouTube video URL
150
+ video_url = "https://www.youtube.com/watch?v=YOUR_VIDEO_ID"
151
+
152
+ # Download audio
153
+ yt = YouTube(video_url)
154
+ stream = yt.streams.filter(only_audio=True).first()
155
+ stream.download(filename="audio.mp4")
156
+
157
+ from transformers import pipeline
158
+
159
+ # Load the speech-to-text pipeline
160
+ transcriber = pipeline(model="openai/whisper-large", task="automatic-speech-recognition")
161
+
162
+ # Transcribe the audio file
163
+ result = transcriber("audio.mp4")
164
+ print(result['text'])
165
+
166
+ import gradio as gr
167
+
168
+ # Define a function for Gradio interface
169
+ def transcribe_audio(audio):
170
+ result = transcriber(audio.name)
171
+ return result['text']
172
+
173
+ # Create a Gradio interface
174
+ interface = gr.Interface(fn=transcribe_audio, inputs=gr.Audio(), outputs="text")
175
+
176
+ # Launch the Gradio interface
177
+ interface.launch()
178
+