Spaces:
Sleeping
Sleeping
Update tools/YouTubeTool.py
Browse files- tools/YouTubeTool.py +68 -24
tools/YouTubeTool.py
CHANGED
|
@@ -1,30 +1,74 @@
|
|
|
|
|
| 1 |
from smolagents import Tool
|
| 2 |
-
from
|
| 3 |
-
import re
|
| 4 |
|
| 5 |
-
class
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
output_type = "string"
|
| 10 |
|
| 11 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
try:
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
-
return f"Error
|
|
|
|
| 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}"
|