Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,48 @@
|
|
1 |
import gradio as gr
|
2 |
-
from youtube_transcript_api import YouTubeTranscriptApi
|
3 |
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
transcript = YouTubeTranscriptApi.get_transcript("7ySqrQVLIF8")
|
8 |
-
return transcript
|
9 |
|
10 |
-
|
11 |
-
|
|
|
12 |
try:
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
except Exception as e:
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
|
|
23 |
iface = gr.Interface(
|
24 |
-
fn=
|
25 |
-
inputs=gr.Textbox(lines=1, placeholder="
|
26 |
-
outputs=gr.Textbox(
|
27 |
-
title="YouTube
|
28 |
-
description="
|
29 |
)
|
30 |
|
|
|
31 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
|
3 |
|
4 |
+
def get_youtube_transcript(video_id):
|
5 |
+
"""
|
6 |
+
Retrieves the transcript of a YouTube video.
|
7 |
|
8 |
+
Args:
|
9 |
+
video_id: The ID of the YouTube video (e.g., 'dQw4w9WgXcQ').
|
|
|
|
|
10 |
|
11 |
+
Returns:
|
12 |
+
A string containing the full transcript, or None if no transcript is found or an error occurs.
|
13 |
+
"""
|
14 |
try:
|
15 |
+
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
|
16 |
+
transcript = transcript_list.find_generated_transcript(['en']) # Try English first
|
17 |
+
if not transcript:
|
18 |
+
transcript = transcript_list.find_manually_created_transcript(['en']) # Fallback to manual if no generated
|
19 |
+
if not transcript:
|
20 |
+
return None # No English transcript found
|
21 |
+
transcript_data = transcript.fetch()
|
22 |
+
full_transcript = " ".join([item['text'] for item in transcript_data])
|
23 |
+
return full_transcript
|
24 |
+
except TranscriptsDisabled:
|
25 |
+
return f"Transcripts are disabled for video ID: {video_id}"
|
26 |
+
except NoTranscriptFound:
|
27 |
+
return f"No transcript found for video ID: {video_id}"
|
28 |
except Exception as e:
|
29 |
+
return f"An error occurred: {e}"
|
30 |
+
|
31 |
+
def fetch_transcript(video_id):
|
32 |
+
transcript = get_youtube_transcript(video_id)
|
33 |
+
if transcript:
|
34 |
+
return transcript
|
35 |
+
else:
|
36 |
+
return "Could not retrieve the transcript."
|
37 |
|
38 |
+
# Create the Gradio interface
|
39 |
iface = gr.Interface(
|
40 |
+
fn=fetch_transcript,
|
41 |
+
inputs=gr.inputs.Textbox(lines=1, placeholder="Enter YouTube Video ID..."),
|
42 |
+
outputs=gr.outputs.Textbox(),
|
43 |
+
title="YouTube Transcript Fetcher",
|
44 |
+
description="Enter a YouTube video ID to fetch its transcript."
|
45 |
)
|
46 |
|
47 |
+
# Launch the Gradio app
|
48 |
iface.launch()
|