Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,16 @@
|
|
| 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 |
-
#
|
|
|
|
|
|
|
| 11 |
@tool
|
| 12 |
-
def my_custom_tool(arg1:str, arg2:int)-> str:
|
| 13 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 14 |
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
arg1: the first argument
|
|
@@ -25,37 +25,98 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 25 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
# Create timezone object
|
| 29 |
tz = pytz.timezone(timezone)
|
| 30 |
-
# Get current time in that timezone
|
| 31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
return f"The current local time in {timezone} is: {local_time}"
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
#
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
|
|
|
|
|
|
|
|
|
|
| 53 |
with open("prompts.yaml", 'r') as stream:
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
@@ -65,5 +126,4 @@ agent = CodeAgent(
|
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
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 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
+
# -------------------------
|
| 10 |
+
# Local custom tools
|
| 11 |
+
# -------------------------
|
| 12 |
@tool
|
| 13 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
|
|
| 14 |
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
arg1: the first argument
|
|
|
|
| 25 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 26 |
"""
|
| 27 |
try:
|
|
|
|
| 28 |
tz = pytz.timezone(timezone)
|
|
|
|
| 29 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 30 |
return f"The current local time in {timezone} is: {local_time}"
|
| 31 |
except Exception as e:
|
| 32 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 33 |
|
| 34 |
+
@tool
|
| 35 |
+
def calculator(a: int, b: int) -> int:
|
| 36 |
+
"""Multiply two integers.
|
| 37 |
+
Args:
|
| 38 |
+
a: first integer
|
| 39 |
+
b: second integer
|
| 40 |
+
"""
|
| 41 |
+
return int(a) * int(b)
|
| 42 |
|
| 43 |
+
# (Optional) simple weather tool using Open-Meteo (no key needed)
|
| 44 |
+
CITY_COORDS = {
|
| 45 |
+
"new york": (40.7128, -74.0060),
|
| 46 |
+
"paris": (48.8566, 2.3522),
|
| 47 |
+
"london": (51.5074, -0.1278),
|
| 48 |
+
"tokyo": (35.6762, 139.6503),
|
| 49 |
+
}
|
| 50 |
+
@tool
|
| 51 |
+
def get_weather(city: str) -> str:
|
| 52 |
+
"""Get current temperature and humidity for a known city (New York, Paris, London, Tokyo).
|
| 53 |
+
Args:
|
| 54 |
+
city: City name (case-insensitive)
|
| 55 |
+
"""
|
| 56 |
+
coords = CITY_COORDS.get(city.lower())
|
| 57 |
+
if not coords:
|
| 58 |
+
return f"Unknown city '{city}'. Try one of: {', '.join(CITY_COORDS.keys())}."
|
| 59 |
+
lat, lon = coords
|
| 60 |
+
url = (
|
| 61 |
+
"https://api.open-meteo.com/v1/forecast"
|
| 62 |
+
f"?latitude={lat}&longitude={lon}"
|
| 63 |
+
"¤t=temperature_2m,relative_humidity_2m,weather_code"
|
| 64 |
+
)
|
| 65 |
+
try:
|
| 66 |
+
r = requests.get(url, timeout=10)
|
| 67 |
+
r.raise_for_status()
|
| 68 |
+
cur = r.json().get("current", {})
|
| 69 |
+
t = cur.get("temperature_2m")
|
| 70 |
+
h = cur.get("relative_humidity_2m")
|
| 71 |
+
if t is None or h is None:
|
| 72 |
+
return "Weather service did not return expected fields."
|
| 73 |
+
return f"Current weather in {city.title()}: {t}°C, {h}% humidity."
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return f"Weather fetch failed: {e}"
|
| 76 |
|
| 77 |
+
# -------------------------
|
| 78 |
+
# Required final answer tool
|
| 79 |
+
# -------------------------
|
| 80 |
+
final_answer = FinalAnswerTool()
|
| 81 |
|
| 82 |
+
# -------------------------
|
| 83 |
+
# Model (you can swap to a different one if overloaded)
|
| 84 |
+
# -------------------------
|
| 85 |
model = HfApiModel(
|
| 86 |
+
max_tokens=2096,
|
| 87 |
+
temperature=0.5,
|
| 88 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # change if needed
|
| 89 |
+
custom_role_conversions=None,
|
| 90 |
)
|
| 91 |
|
| 92 |
+
# -------------------------
|
| 93 |
+
# Pre-made tools from Hub / smolagents
|
| 94 |
+
# -------------------------
|
| 95 |
+
# DuckDuckGo web search (no key)
|
| 96 |
+
duck_search = DuckDuckGoSearchTool()
|
| 97 |
+
# Text-to-image tool from the course (remote tool, trust code)
|
| 98 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 99 |
|
| 100 |
+
# -------------------------
|
| 101 |
+
# Prompts
|
| 102 |
+
# -------------------------
|
| 103 |
with open("prompts.yaml", 'r') as stream:
|
| 104 |
prompt_templates = yaml.safe_load(stream)
|
| 105 |
+
|
| 106 |
+
# -------------------------
|
| 107 |
+
# Agent with tools registered
|
| 108 |
+
# -------------------------
|
| 109 |
agent = CodeAgent(
|
| 110 |
model=model,
|
| 111 |
+
tools=[
|
| 112 |
+
final_answer, # keep this
|
| 113 |
+
duck_search, # web search
|
| 114 |
+
image_generation_tool, # text -> image
|
| 115 |
+
calculator, # local calc tool
|
| 116 |
+
get_current_time_in_timezone,
|
| 117 |
+
get_weather,
|
| 118 |
+
my_custom_tool, # your playground tool
|
| 119 |
+
],
|
| 120 |
max_steps=6,
|
| 121 |
verbosity_level=1,
|
| 122 |
grammar=None,
|
|
|
|
| 126 |
prompt_templates=prompt_templates
|
| 127 |
)
|
| 128 |
|
| 129 |
+
GradioUI(agent).launch()
|
|
|