Spaces:
Sleeping
Sleeping
import gradio as gr | |
from youtube_transcript_api import YouTubeTranscriptApi | |
from deep_translator import GoogleTranslator | |
import re | |
# β Extract video ID from YouTube URL | |
def extract_video_id(url): | |
# Supports standard, short, and embed URLs | |
regex = r"(?:v=|\/)([0-9A-Za-z_-]{11})" | |
match = re.search(regex, url) | |
return match.group(1) if match else url.strip() | |
# β Agent 1: Summarizer | |
def summarize_youtube(video_url): | |
try: | |
video_id = extract_video_id(video_url) | |
transcript = YouTubeTranscriptApi.get_transcript(video_id) | |
text = " ".join([entry["text"] for entry in transcript[:15]]) | |
return text[:500] | |
except Exception as e: | |
return f"β Error in Summarizer: {str(e)}" | |
# β Agent 2: Translator | |
def translate_to_spanish(english_text): | |
try: | |
return GoogleTranslator(source='auto', target='es').translate(english_text) | |
except Exception as e: | |
return f"β Error in Translator: {str(e)}" | |
# β Workflow Function (Master Agent) | |
def agent_workflow(video_url): | |
summary = summarize_youtube(video_url) | |
if summary.startswith("β"): | |
return summary, "" | |
translated = translate_to_spanish(summary) | |
return summary, translated | |
# β Gradio UI | |
demo = gr.Interface( | |
fn=agent_workflow, | |
inputs=gr.Textbox(label="π₯ Enter YouTube Video Link"), | |
outputs=[ | |
gr.Textbox(label="π§ English Summary"), | |
gr.Textbox(label="π Spanish Translation") | |
], | |
title="Lead with AI Agents Hackathon Project", | |
description="π§ Agent 1: Summarizes YouTube Video Β· π Agent 2: Translates Summary to Spanish" | |
) | |
demo.launch() | |