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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -11
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
- # Agent 1: Summarizer
 
 
 
 
 
 
 
6
  def summarize_youtube(video_url):
7
  try:
8
- video_id = video_url.split("v=")[-1]
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 function
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=[gr.Textbox(label="English Summary"), gr.Textbox(label="Spanish Translation")],
33
- title="Lead with AI Agents",
34
- description="πŸ”Ή Agent 1: Summarizes YouTube video πŸ”Ή Agent 2: Translates to Spanish"
 
 
 
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()