Spaces:
Runtime error
Runtime error
duckduckgo
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 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 os
|
|
|
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
|
|
@@ -16,7 +16,10 @@ def get_weather(city: str) -> str:
|
|
| 16 |
city: The name of the city (e.g., 'New York').
|
| 17 |
"""
|
| 18 |
try:
|
| 19 |
-
api_key = os.getenv("OPENWEATHERMAP_API_KEY")
|
|
|
|
|
|
|
|
|
|
| 20 |
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
|
| 21 |
response = requests.get(url).json()
|
| 22 |
|
|
@@ -31,17 +34,18 @@ def get_weather(city: str) -> str:
|
|
| 31 |
return f"Error fetching weather for '{city}': {str(e)}"
|
| 32 |
|
| 33 |
|
| 34 |
-
#
|
| 35 |
@tool
|
| 36 |
-
def
|
| 37 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 38 |
"""A tool that does nothing yet
|
| 39 |
Args:
|
| 40 |
arg1: the first argument
|
| 41 |
arg2: the second argument
|
| 42 |
"""
|
| 43 |
-
return "What magic will you build
|
|
|
|
| 44 |
|
|
|
|
| 45 |
@tool
|
| 46 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 47 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -49,37 +53,49 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 49 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 50 |
"""
|
| 51 |
try:
|
| 52 |
-
# Create timezone object
|
| 53 |
tz = pytz.timezone(timezone)
|
| 54 |
-
# Get current time in that timezone
|
| 55 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 56 |
return f"The current local time in {timezone} is: {local_time}"
|
| 57 |
except Exception as e:
|
| 58 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 59 |
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
final_answer = FinalAnswerTool()
|
|
|
|
|
|
|
| 62 |
model = HfApiModel(
|
| 63 |
-
max_tokens=2096,
|
| 64 |
-
temperature=0.5,
|
| 65 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 66 |
-
custom_role_conversions=None,
|
| 67 |
)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
# Import tool from Hub
|
| 71 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 72 |
|
|
|
|
| 73 |
with open("prompts.yaml", 'r') as stream:
|
| 74 |
prompt_templates = yaml.safe_load(stream)
|
| 75 |
-
|
|
|
|
| 76 |
agent = CodeAgent(
|
| 77 |
model=model,
|
| 78 |
tools=[
|
| 79 |
-
final_answer,
|
| 80 |
-
|
| 81 |
-
image_generation_tool,
|
| 82 |
-
get_weather,
|
| 83 |
get_current_time_in_timezone, # Enables timezone-based time retrieval
|
| 84 |
],
|
| 85 |
max_steps=6,
|
|
@@ -91,5 +107,5 @@ agent = CodeAgent(
|
|
| 91 |
prompt_templates=prompt_templates
|
| 92 |
)
|
| 93 |
|
| 94 |
-
|
| 95 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
| 6 |
import os
|
| 7 |
+
from tools.final_answer import FinalAnswerTool
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
|
|
|
|
| 16 |
city: The name of the city (e.g., 'New York').
|
| 17 |
"""
|
| 18 |
try:
|
| 19 |
+
api_key = os.getenv("OPENWEATHERMAP_API_KEY")
|
| 20 |
+
if not api_key:
|
| 21 |
+
return "Error: Missing API key. Set OPENWEATHERMAP_API_KEY as an environment variable."
|
| 22 |
+
|
| 23 |
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
|
| 24 |
response = requests.get(url).json()
|
| 25 |
|
|
|
|
| 34 |
return f"Error fetching weather for '{city}': {str(e)}"
|
| 35 |
|
| 36 |
|
| 37 |
+
# Define a custom tool
|
| 38 |
@tool
|
| 39 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
|
|
| 40 |
"""A tool that does nothing yet
|
| 41 |
Args:
|
| 42 |
arg1: the first argument
|
| 43 |
arg2: the second argument
|
| 44 |
"""
|
| 45 |
+
return "What magic will you build?"
|
| 46 |
+
|
| 47 |
|
| 48 |
+
# Define a timezone-aware clock tool
|
| 49 |
@tool
|
| 50 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 51 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 53 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 54 |
"""
|
| 55 |
try:
|
|
|
|
| 56 |
tz = pytz.timezone(timezone)
|
|
|
|
| 57 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 58 |
return f"The current local time in {timezone} is: {local_time}"
|
| 59 |
except Exception as e:
|
| 60 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 61 |
|
| 62 |
|
| 63 |
+
# Define web search tool
|
| 64 |
+
@tool
|
| 65 |
+
def web_search(query: str) -> str:
|
| 66 |
+
"""A tool that searches the web using DuckDuckGo.
|
| 67 |
+
Args:
|
| 68 |
+
query: The search query.
|
| 69 |
+
"""
|
| 70 |
+
return DuckDuckGoSearchTool().search(query)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# Load final answer tool
|
| 74 |
final_answer = FinalAnswerTool()
|
| 75 |
+
|
| 76 |
+
# Hugging Face model setup
|
| 77 |
model = HfApiModel(
|
| 78 |
+
max_tokens=2096,
|
| 79 |
+
temperature=0.5,
|
| 80 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 81 |
+
custom_role_conversions=None,
|
| 82 |
)
|
| 83 |
|
| 84 |
+
# Import tool from Hugging Face Hub
|
|
|
|
| 85 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 86 |
|
| 87 |
+
# Load prompt templates
|
| 88 |
with open("prompts.yaml", 'r') as stream:
|
| 89 |
prompt_templates = yaml.safe_load(stream)
|
| 90 |
+
|
| 91 |
+
# Initialize the agent with multiple tools
|
| 92 |
agent = CodeAgent(
|
| 93 |
model=model,
|
| 94 |
tools=[
|
| 95 |
+
final_answer,
|
| 96 |
+
web_search, # Enables web search via DuckDuckGo
|
| 97 |
+
image_generation_tool, # Enables text-to-image generation
|
| 98 |
+
get_weather, # Enables weather fetching
|
| 99 |
get_current_time_in_timezone, # Enables timezone-based time retrieval
|
| 100 |
],
|
| 101 |
max_steps=6,
|
|
|
|
| 107 |
prompt_templates=prompt_templates
|
| 108 |
)
|
| 109 |
|
| 110 |
+
# Launch Gradio UI
|
| 111 |
+
GradioUI(agent).launch()
|