lgsantini1 commited on
Commit
2123081
·
verified ·
1 Parent(s): c9bb78f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -42
app.py CHANGED
@@ -1,42 +1,42 @@
1
- 1 import gradio as gr
2
- 2 from youtube_transcript_api import YouTubeTranscriptApi
3
- 3 import re
4
- 4
5
- 5 def extract_video_id(url):
6
- 6 """
7
- 7 Extracts the YouTube video ID from a URL.
8
- 8 """
9
- 9 regex = r"(?:v=|\/)([0-9A-Za-z_-]{11}).*"
10
- 10 match = re.search(regex, url)
11
- 11 if match:
12
- 12 return match.group(1)
13
- 13 else:
14
- 14 return url # Assume the input is already a video ID
15
- 15
16
- 16 def get_transcript(video_url, language="en"):
17
- 17 """
18
- 18 Retrieves the transcript of a YouTube video in the specified language.
19
- 19 """
20
- 20 try:
21
- 21 video_id = extract_video_id(video_url)
22
- 22 transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
23
- 23 transcript = transcript_list.find_transcript([language])
24
- 24 transcript_data = transcript.fetch()
25
- 25 return "\n".join([f"{t['start']}: {t['text']}" for t in transcript_data])
26
- 26 except Exception as e:
27
- 27 return f"Error retrieving transcript: {str(e)}"
28
- 28
29
- 29 # Create Gradio interface
30
- 30 iface = gr.Interface(
31
- 31 fn=get_transcript,
32
- 32 inputs=[
33
- 33 gr.Textbox(lines=1, label="YouTube Video URL"),
34
- 34 gr.Textbox(lines=1, value="en", label="Language Code")
35
- 35 ],
36
- 36 outputs="text",
37
- 37 title="YouTube Video Transcription",
38
- 38 description="Enter a YouTube video URL and a language code (e.g., 'en' for English) to get the transcript."
39
- 39 )
40
- 40
41
- 41 if __name__ == "__main__":
42
- 42 iface.launch()
 
1
+ import gradio as gr
2
+ from youtube_transcript_api import YouTubeTranscriptApi
3
+ import re
4
+
5
+ def extract_video_id(url):
6
+ """
7
+ Extracts the YouTube video ID from a URL.
8
+ """
9
+ regex = r"(?:v=|\/)([0-9A-Za-z_-]{11}).*"
10
+ match = re.search(regex, url)
11
+ if match:
12
+ return match.group(1)
13
+ else:
14
+ return url # Assume the input is already a video ID
15
+
16
+ def get_transcript(video_url, language="en"):
17
+ """
18
+ Retrieves the transcript of a YouTube video in the specified language.
19
+ """
20
+ try:
21
+ video_id = extract_video_id(video_url)
22
+ transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
23
+ transcript = transcript_list.find_transcript([language])
24
+ transcript_data = transcript.fetch()
25
+ return "\n".join([f"{t['start']}: {t['text']}" for t in transcript_data])
26
+ except Exception as e:
27
+ return f"Error retrieving transcript: {str(e)}"
28
+
29
+ # Create Gradio interface
30
+ iface = gr.Interface(
31
+ fn=get_transcript,
32
+ inputs=[
33
+ gr.Textbox(lines=1, label="YouTube Video URL"),
34
+ gr.Textbox(lines=1, value="en", label="Language Code")
35
+ ],
36
+ outputs="text",
37
+ title="YouTube Video Transcription",
38
+ description="Enter a YouTube video URL and a language code (e.g., 'en' for English) to get the transcript."
39
+ )
40
+
41
+ if __name__ == "__main__":
42
+ iface.launch()