Sborole commited on
Commit
9598d95
·
verified ·
1 Parent(s): 0e80199

Update tools/YouTubeTool.py

Browse files
Files changed (1) hide show
  1. tools/YouTubeTool.py +68 -24
tools/YouTubeTool.py CHANGED
@@ -1,30 +1,74 @@
 
1
  from smolagents import Tool
2
- from youtube_transcript_api import YouTubeTranscriptApi, NoTranscriptFound, TranscriptsDisabled
3
- import re
4
 
5
- class YouTubeTool(Tool):
6
- name = "youtube_reader"
7
- description= "Fetches the transcript for the youtube video url"
8
- inputs= {"video_url": {"type": "string", "description": "URL or ID of the Youtube video"}}
 
 
 
 
 
 
 
 
9
  output_type = "string"
10
 
11
- def forward(self, video_url: str) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  try:
13
- # Extract video ID
14
- match = re.search(r"v=([A-Za-z0-9_-]{11})", video_url)
15
- if not match:
16
- return "Invalid YouTube URL."
17
- video_id = match.group(1)
18
-
19
- # Fetch transcript
20
- transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
21
- transcript_text = " ".join([t["text"] for t in transcript_list])
22
-
23
- return transcript_text
24
-
25
- except NoTranscriptFound:
26
- return "Transcript not available for this video."
27
- except TranscriptsDisabled:
28
- return "Transcripts are disabled for this video."
 
 
 
 
 
 
29
  except Exception as e:
30
- return f"Error fetching transcript: {str(e)}"
 
1
+ import os
2
  from smolagents import Tool
3
+ from googleapiclient.discovery import build
 
4
 
5
+ class YouTubeSearchTool(Tool):
6
+ """
7
+ A tool to search for YouTube videos using the Google Custom Search Engine (CSE) API,
8
+ by specifically restricting results to the youtube.com domain.
9
+ """
10
+ name = "youtube_search"
11
+ description = "Searches for and returns links and snippets for YouTube videos related to a specific query."
12
+
13
+ inputs = {
14
+ # Corrected the typo/broken line to read correctly
15
+ "query": {"type": "string", "description": "The search term to find relevant YouTube videos for."}
16
+ }
17
  output_type = "string"
18
 
19
+ def __init__(self, **kwargs):
20
+ super().__init__(**kwargs)
21
+
22
+ # Retrieve credentials from environment variables
23
+ self.api_key = os.getenv("GOOGLE_API_KEY")
24
+ self.cse_id = os.getenv("GOOGLE_CSE_ID")
25
+
26
+ if not self.api_key or not self.cse_id:
27
+ # Note: This check relies on environment variables being set up
28
+ raise ValueError("GOOGLE_API_KEY or GOOGLE_CSE_ID secret not found.")
29
+
30
+ # Initialize the Google Custom Search service
31
+ self.service = build(
32
+ "customsearch", "v1", developerKey=self.api_key
33
+ )
34
+
35
+ def forward(self, query: str) -> str:
36
+ """
37
+ Executes a Google search query, restricting results to youtube.com.
38
+
39
+ Args:
40
+ query: The search term provided by the agent.
41
+
42
+ Returns:
43
+ A formatted string of search results (max 3), or an error message.
44
+ """
45
+ print(f"Executing YouTube search for: '{query}'")
46
+
47
+ # Modify the query to ensure results are restricted to YouTube
48
+ youtube_query = f"{query} site:youtube.com"
49
+
50
  try:
51
+ # Execute the search request for up to 3 results
52
+ res = self.service.cse().list(
53
+ q=youtube_query,
54
+ cx=self.cse_id,
55
+ num=3
56
+ ).execute()
57
+
58
+ items = res.get('items', [])
59
+
60
+ if not items:
61
+ return "XX record info: No YouTube results found."
62
+
63
+ search_results = []
64
+ for i, item in enumerate(items):
65
+ search_results.append(
66
+ f"RESULT {i+1}: '{item.get('title')}'\n"
67
+ f"CONTENT: {item.get('snippet')}\n"
68
+ f"SOURCE: {item.get('link')}"
69
+ )
70
+
71
+ return "\n\n---SEPARATOR---\n\n".join(search_results)
72
+
73
  except Exception as e:
74
+ return f"Error during YouTube Search API call: {e}"