Sonu313131 commited on
Commit
8753522
·
verified ·
1 Parent(s): 254e51c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -20,14 +20,18 @@ import re
20
  from youtube_transcript_api import YouTubeTranscriptApi
21
  from smolagents import Tool
22
 
23
- class YouTubeCaptionTool(Tool):
24
- name = "youtube_caption_reader"
25
- description = "Extracts captions from a YouTube video given its URL and returns the transcript or a segment."
 
 
 
 
26
 
27
  inputs = {
28
  "url": {
29
  "type": "string",
30
- "description": "Full YouTube video URL (e.g., https://www.youtube.com/watch?v=abc123)"
31
  }
32
  }
33
 
@@ -35,19 +39,21 @@ class YouTubeCaptionTool(Tool):
35
 
36
  def forward(self, url: str) -> str:
37
  try:
38
- # Extract the video ID from the URL
39
- match = re.search(r"(?:v=|youtu.be/)([\w-]+)", url)
40
- if not match:
41
- return "Could not extract video ID from URL."
42
 
43
- video_id = match.group(1)
44
- transcript = YouTubeTranscriptApi.get_transcript(video_id)
45
- full_text = " ".join([entry['text'] for entry in transcript])
46
 
47
- return full_text[:3000] # return first 3000 characters
 
 
48
 
 
49
  except Exception as e:
50
- return f"Failed to retrieve transcript: {str(e)}"
 
51
 
52
 
53
  ##Tool 2
@@ -121,7 +127,7 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
121
  model=OpenAIServerModel(model_id="gpt-4o-mini",
122
  api_key=os.environ["OPENAI_API_KEY"],
123
  temperature=0.0),
124
- max_steps=12,
125
  verbosity_level=2
126
  )
127
  except Exception as e:
 
20
  from youtube_transcript_api import YouTubeTranscriptApi
21
  from smolagents import Tool
22
 
23
+ from smolagents import Tool
24
+ from youtube_transcript_api import YouTubeTranscriptApi
25
+ from urllib.parse import urlparse, parse_qs
26
+
27
+ class YouTubeTranscriptTool(Tool):
28
+ name = "youtube_transcript"
29
+ description = "Fetches the full transcript of a YouTube video from its URL."
30
 
31
  inputs = {
32
  "url": {
33
  "type": "string",
34
+ "description": "The full YouTube video URL"
35
  }
36
  }
37
 
 
39
 
40
  def forward(self, url: str) -> str:
41
  try:
42
+ # Extract video ID from URL
43
+ parsed = urlparse(url)
44
+ video_id = parse_qs(parsed.query).get("v", [None])[0]
 
45
 
46
+ if not video_id:
47
+ return "Error: Invalid YouTube URL or missing video ID."
 
48
 
49
+ # Fetch the transcript
50
+ transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
51
+ transcript_text = " ".join(entry["text"] for entry in transcript_list)
52
 
53
+ return transcript_text[:5000] # Optional: truncate to 5000 chars
54
  except Exception as e:
55
+ return f"Error retrieving transcript: {str(e)}"
56
+
57
 
58
 
59
  ##Tool 2
 
127
  model=OpenAIServerModel(model_id="gpt-4o-mini",
128
  api_key=os.environ["OPENAI_API_KEY"],
129
  temperature=0.0),
130
+ max_steps=18,
131
  verbosity_level=2
132
  )
133
  except Exception as e: