Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,50 @@
|
|
1 |
import gradio as gr
|
2 |
from youtube_transcript_api import YouTubeTranscriptApi
|
3 |
from deep_translator import GoogleTranslator
|
|
|
4 |
|
5 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
def summarize_youtube(video_url):
|
7 |
try:
|
8 |
-
video_id = video_url
|
9 |
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
10 |
text = " ".join([entry["text"] for entry in transcript[:15]])
|
11 |
return text[:500]
|
12 |
except Exception as e:
|
13 |
-
return f"Error: {e}"
|
14 |
|
15 |
-
# Agent 2: Translator
|
16 |
def translate_to_spanish(english_text):
|
17 |
try:
|
18 |
return GoogleTranslator(source='auto', target='es').translate(english_text)
|
19 |
except Exception as e:
|
20 |
-
return f"Error: {e}"
|
21 |
|
22 |
-
# Workflow
|
23 |
def agent_workflow(video_url):
|
24 |
summary = summarize_youtube(video_url)
|
|
|
|
|
25 |
translated = translate_to_spanish(summary)
|
26 |
return summary, translated
|
27 |
|
28 |
-
# Gradio UI
|
29 |
demo = gr.Interface(
|
30 |
fn=agent_workflow,
|
31 |
-
inputs=gr.Textbox(label="YouTube Video Link"),
|
32 |
-
outputs=[
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
35 |
)
|
36 |
|
37 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from youtube_transcript_api import YouTubeTranscriptApi
|
3 |
from deep_translator import GoogleTranslator
|
4 |
+
import re
|
5 |
|
6 |
+
# β
Extract video ID from YouTube URL
|
7 |
+
def extract_video_id(url):
|
8 |
+
# Supports standard, short, and embed URLs
|
9 |
+
regex = r"(?:v=|\/)([0-9A-Za-z_-]{11})"
|
10 |
+
match = re.search(regex, url)
|
11 |
+
return match.group(1) if match else url.strip()
|
12 |
+
|
13 |
+
# β
Agent 1: Summarizer
|
14 |
def summarize_youtube(video_url):
|
15 |
try:
|
16 |
+
video_id = extract_video_id(video_url)
|
17 |
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
18 |
text = " ".join([entry["text"] for entry in transcript[:15]])
|
19 |
return text[:500]
|
20 |
except Exception as e:
|
21 |
+
return f"β Error in Summarizer: {str(e)}"
|
22 |
|
23 |
+
# β
Agent 2: Translator
|
24 |
def translate_to_spanish(english_text):
|
25 |
try:
|
26 |
return GoogleTranslator(source='auto', target='es').translate(english_text)
|
27 |
except Exception as e:
|
28 |
+
return f"β Error in Translator: {str(e)}"
|
29 |
|
30 |
+
# β
Workflow Function (Master Agent)
|
31 |
def agent_workflow(video_url):
|
32 |
summary = summarize_youtube(video_url)
|
33 |
+
if summary.startswith("β"):
|
34 |
+
return summary, ""
|
35 |
translated = translate_to_spanish(summary)
|
36 |
return summary, translated
|
37 |
|
38 |
+
# β
Gradio UI
|
39 |
demo = gr.Interface(
|
40 |
fn=agent_workflow,
|
41 |
+
inputs=gr.Textbox(label="π₯ Enter YouTube Video Link"),
|
42 |
+
outputs=[
|
43 |
+
gr.Textbox(label="π§ English Summary"),
|
44 |
+
gr.Textbox(label="π Spanish Translation")
|
45 |
+
],
|
46 |
+
title="Lead with AI Agents Hackathon Project",
|
47 |
+
description="π§ Agent 1: Summarizes YouTube Video Β· π Agent 2: Translates Summary to Spanish"
|
48 |
)
|
49 |
|
50 |
demo.launch()
|