Spaces:
Runtime error
Runtime error
add youtube search tool
Browse files
app.py
CHANGED
|
@@ -1,22 +1,57 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -55,7 +90,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 1 |
+
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
+
import requests
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
|
|
| 10 |
@tool
|
| 11 |
+
def youtube_search(query: str, max_results: int = 5) -> str:
|
| 12 |
+
"""A tool that searches YouTube and returns video titles and URLs.
|
| 13 |
+
|
| 14 |
Args:
|
| 15 |
+
query: The search query string for YouTube
|
| 16 |
+
max_results: Maximum number of video results to return (default: 5)
|
| 17 |
+
|
| 18 |
+
Returns:
|
| 19 |
+
A formatted string containing video titles and URLs, or an error message if the search fails
|
| 20 |
"""
|
| 21 |
+
import requests
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
# Replace with your actual SerpAPI key or set it as an environment variable
|
| 25 |
+
api_key = "YOUR_SERPAPI_KEY" # Add your SerpAPI key here
|
| 26 |
+
url = "https://serpapi.com/search.json"
|
| 27 |
+
params = {
|
| 28 |
+
"engine": "youtube",
|
| 29 |
+
"search_query": query,
|
| 30 |
+
"api_key": api_key
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
response = requests.get(url, params=params)
|
| 34 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
| 35 |
+
data = response.json()
|
| 36 |
+
|
| 37 |
+
# Extract video results
|
| 38 |
+
videos = data.get("video_results", [])[:max_results]
|
| 39 |
+
if not videos:
|
| 40 |
+
return "No results found for the query."
|
| 41 |
+
|
| 42 |
+
# Format the output
|
| 43 |
+
result = "YouTube Search Results:\n"
|
| 44 |
+
for i, video in enumerate(videos, 1):
|
| 45 |
+
title = video.get("title", "No title")
|
| 46 |
+
link = video.get("link", "No URL")
|
| 47 |
+
result += f"{i}. {title}\n URL: {link}\n"
|
| 48 |
+
|
| 49 |
+
return result
|
| 50 |
+
|
| 51 |
+
except requests.RequestException as e:
|
| 52 |
+
return f"Error during YouTube search: {str(e)}"
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return f"Unexpected error: {str(e)}"
|
| 55 |
|
| 56 |
@tool
|
| 57 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 90 |
|
| 91 |
agent = CodeAgent(
|
| 92 |
model=model,
|
| 93 |
+
tools=[final_answer, DuckDuckGoSearchTool, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
|
| 94 |
max_steps=6,
|
| 95 |
verbosity_level=1,
|
| 96 |
grammar=None,
|