asad231 commited on
Commit
1374600
Β·
verified Β·
1 Parent(s): b83d913

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -17
app.py CHANGED
@@ -1,33 +1,40 @@
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("❌"):
@@ -35,7 +42,7 @@ def agent_workflow(video_url):
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"),
@@ -43,8 +50,5 @@ demo = gr.Interface(
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()
 
1
  import gradio as gr
 
2
  from deep_translator import GoogleTranslator
3
  import re
4
 
5
+ # Extract video ID from URL
6
  def extract_video_id(url):
 
7
  regex = r"(?:v=|\/)([0-9A-Za-z_-]{11})"
8
  match = re.search(regex, url)
9
  return match.group(1) if match else url.strip()
10
 
11
+ # Simulated transcript agent
12
  def summarize_youtube(video_url):
13
  try:
14
  video_id = extract_video_id(video_url)
15
+
16
+ # Predefined summaries for demonstration
17
+ saved_transcripts = {
18
+ "dQw4w9WgXcQ": "We're no strangers to love. You know the rules and so do I...",
19
+ "F9cTlfD7ZGM": "This is a video about AI agents and their collaboration.",
20
+ "HMcFwjWVprs": "In this talk, we cover the future of GenAI protocols and their applications."
21
+ }
22
+
23
+ if video_id not in saved_transcripts:
24
+ return "❌ Transcript not available for this video. Please use a supported demo video."
25
+
26
+ return saved_transcripts[video_id]
27
  except Exception as e:
28
+ return f"❌ Error: {str(e)}"
29
 
30
+ # Translator agent
31
  def translate_to_spanish(english_text):
32
  try:
33
  return GoogleTranslator(source='auto', target='es').translate(english_text)
34
  except Exception as e:
35
+ return f"❌ Error: {str(e)}"
36
 
37
+ # Workflow (master agent)
38
  def agent_workflow(video_url):
39
  summary = summarize_youtube(video_url)
40
  if summary.startswith("❌"):
 
42
  translated = translate_to_spanish(summary)
43
  return summary, translated
44
 
45
+ # Gradio UI
46
  demo = gr.Interface(
47
  fn=agent_workflow,
48
  inputs=gr.Textbox(label="πŸŽ₯ Enter YouTube Video Link"),
 
50
  gr.Textbox(label="🧠 English Summary"),
51
  gr.Textbox(label="🌍 Spanish Translation")
52
  ],
53
+ title="🧠 AI Agents: YouTube Summary + Spanish Translation",
54
+ description="Agent 1: Summarizer (simulated)