Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +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 |
-
#
|
| 11 |
@tool
|
| 12 |
-
def my_custom_tool(arg1:str, arg2:int)-> str:
|
| 13 |
-
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
arg1: the first argument
|
| 17 |
arg2: the second argument
|
| 18 |
"""
|
| 19 |
-
return "
|
| 20 |
|
|
|
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
-
"""
|
| 24 |
Args:
|
| 25 |
-
timezone: A string
|
| 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
|
| 35 |
-
|
| 36 |
|
|
|
|
| 37 |
final_answer = FinalAnswerTool()
|
|
|
|
|
|
|
| 38 |
model = HfApiModel(
|
| 39 |
-
model_id="meta-llama/Llama-2-7b-chat-hf",
|
| 40 |
max_tokens=512,
|
| 41 |
-
temperature=0.5
|
| 42 |
)
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
agent = CodeAgent(
|
| 45 |
model=model,
|
| 46 |
tools=[final_answer, my_custom_tool, get_current_time_in_timezone],
|
| 47 |
max_steps=6,
|
| 48 |
verbosity_level=1,
|
| 49 |
-
name="
|
| 50 |
-
|
| 51 |
)
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
| 1 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
|
|
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 6 |
from Gradio_UI import GradioUI
|
| 7 |
|
| 8 |
+
# Example custom tool
|
| 9 |
@tool
|
| 10 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 11 |
+
"""A tool that does nothing yet.
|
|
|
|
| 12 |
Args:
|
| 13 |
arg1: the first argument
|
| 14 |
arg2: the second argument
|
| 15 |
"""
|
| 16 |
+
return f"My tool received arg1={arg1} and arg2={arg2}"
|
| 17 |
|
| 18 |
+
# Time tool
|
| 19 |
@tool
|
| 20 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 21 |
+
"""Get current local time in a specified timezone.
|
| 22 |
Args:
|
| 23 |
+
timezone: A string like 'Asia/Taipei'
|
| 24 |
"""
|
| 25 |
try:
|
|
|
|
| 26 |
tz = pytz.timezone(timezone)
|
|
|
|
| 27 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 28 |
return f"The current local time in {timezone} is: {local_time}"
|
| 29 |
except Exception as e:
|
| 30 |
+
return f"Error: {str(e)}"
|
|
|
|
| 31 |
|
| 32 |
+
# Final Answer Tool
|
| 33 |
final_answer = FinalAnswerTool()
|
| 34 |
+
|
| 35 |
+
# Free model instead of Qwen32B (to avoid 402 errors)
|
| 36 |
model = HfApiModel(
|
| 37 |
+
model_id="meta-llama/Llama-2-7b-chat-hf", # free model
|
| 38 |
max_tokens=512,
|
| 39 |
+
temperature=0.5,
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# Load prompt templates
|
| 43 |
+
with open("prompts.yaml", "r") as stream:
|
| 44 |
+
prompt_templates = yaml.safe_load(stream)
|
| 45 |
+
|
| 46 |
+
# Agent (no system_prompt/instructions!)
|
| 47 |
agent = CodeAgent(
|
| 48 |
model=model,
|
| 49 |
tools=[final_answer, my_custom_tool, get_current_time_in_timezone],
|
| 50 |
max_steps=6,
|
| 51 |
verbosity_level=1,
|
| 52 |
+
name="my_agent",
|
| 53 |
+
prompt_templates=prompt_templates,
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# Launch Gradio UI
|
| 57 |
+
GradioUI(agent).launch()
|