Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,28 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool,
|
| 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 |
-
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
-
arg1:
|
| 17 |
-
arg2:
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -25,45 +31,48 @@ 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 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
# Import tool from Hub
|
| 51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
|
|
|
| 52 |
|
| 53 |
-
|
|
|
|
| 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,
|
| 62 |
planning_interval=None,
|
| 63 |
name=None,
|
| 64 |
description=None,
|
| 65 |
-
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, 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 |
+
# --- Tools ---
|
| 10 |
@tool
|
| 11 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 12 |
+
"""A tool that changes your height from metric to imperial
|
|
|
|
| 13 |
Args:
|
| 14 |
+
arg1: your name
|
| 15 |
+
arg2: your height in metric system (centimeters)
|
| 16 |
"""
|
| 17 |
+
afeet = 30.48 # cm per foot
|
| 18 |
+
ainch = 2.54 # cm per inch
|
| 19 |
+
|
| 20 |
+
# keep your logic, just truncate feet (no decimals)
|
| 21 |
+
the_feet = int(arg2 // afeet) # whole feet
|
| 22 |
+
remainder = arg2 - the_feet * afeet # leftover cm
|
| 23 |
+
the_inch = remainder / ainch # inches from leftover
|
| 24 |
+
|
| 25 |
+
return f"Height of {arg1} in imperial units: {the_feet} ft {the_inch:.2f} in"
|
| 26 |
|
| 27 |
@tool
|
| 28 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 31 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 32 |
"""
|
| 33 |
try:
|
|
|
|
| 34 |
tz = pytz.timezone(timezone)
|
|
|
|
| 35 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 36 |
return f"The current local time in {timezone} is: {local_time}"
|
| 37 |
except Exception as e:
|
| 38 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 39 |
|
| 40 |
+
# --- Final answer tool required by the template ---
|
| 41 |
final_answer = FinalAnswerTool()
|
| 42 |
|
| 43 |
+
# --- Model (use this on HF Spaces; it reads HF_TOKEN automatically) ---
|
| 44 |
+
model = InferenceClientModel(
|
| 45 |
+
max_tokens=2096,
|
| 46 |
+
temperature=0.5,
|
| 47 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 48 |
+
custom_role_conversions=None,
|
|
|
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
+
# Import tool from Hub (image generation) and set up web search
|
|
|
|
| 52 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 53 |
+
web_search_tool = DuckDuckGoSearchTool()
|
| 54 |
|
| 55 |
+
# Load system prompts
|
| 56 |
+
with open("prompts.yaml", "r") as stream:
|
| 57 |
prompt_templates = yaml.safe_load(stream)
|
| 58 |
+
|
| 59 |
+
# --- Agent: register ALL the tools here ---
|
| 60 |
agent = CodeAgent(
|
| 61 |
model=model,
|
| 62 |
+
tools=[
|
| 63 |
+
final_answer, # do not remove
|
| 64 |
+
my_custom_tool, # your converter
|
| 65 |
+
get_current_time_in_timezone,
|
| 66 |
+
image_generation_tool, # HF Hub tool
|
| 67 |
+
web_search_tool, # web search
|
| 68 |
+
],
|
| 69 |
max_steps=6,
|
| 70 |
verbosity_level=1,
|
| 71 |
grammar=None,
|
| 72 |
planning_interval=None,
|
| 73 |
name=None,
|
| 74 |
description=None,
|
| 75 |
+
prompt_templates=prompt_templates,
|
| 76 |
)
|
| 77 |
|
| 78 |
+
GradioUI(agent).launch()
|
|
|