Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,42 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 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()
|