Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,89 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
import datetime
|
| 3 |
-
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 tool description / args description but feel free to modify the tool
|
| 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 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"
|
| 33 |
except Exception as e:
|
| 34 |
-
return f"Error
|
| 35 |
|
| 36 |
|
| 37 |
-
|
| 38 |
model = InferenceClientModel(
|
| 39 |
-
|
|
|
|
| 40 |
temperature=0.5,
|
| 41 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 42 |
-
custom_role_conversions=None,
|
| 43 |
)
|
| 44 |
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
agent = CodeAgent(
|
| 54 |
model=model,
|
| 55 |
-
tools=
|
| 56 |
-
max_steps=
|
| 57 |
verbosity_level=1,
|
| 58 |
-
|
| 59 |
-
planning_interval=None,
|
| 60 |
-
name=None,
|
| 61 |
-
description=None,
|
| 62 |
-
prompt_templates=prompt_templates # Pass system prompt to CodeAgent
|
| 63 |
)
|
| 64 |
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, InferenceClientModel, tool, load_tool
|
| 2 |
import datetime
|
|
|
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
+
|
| 6 |
+
# IMPORTANT: make sure this file exists or remove it
|
| 7 |
+
try:
|
| 8 |
+
from tools.final_answer import FinalAnswerTool
|
| 9 |
+
except ImportError:
|
| 10 |
+
class FinalAnswerTool:
|
| 11 |
+
def __call__(self, answer: str):
|
| 12 |
+
return answer
|
| 13 |
|
| 14 |
from Gradio_UI import GradioUI
|
| 15 |
|
| 16 |
+
|
| 17 |
+
# ------------------ TOOLS ------------------ #
|
| 18 |
@tool
|
| 19 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
|
|
| 20 |
"""A tool that does nothing yet
|
| 21 |
Args:
|
| 22 |
arg1: the first argument
|
| 23 |
arg2: the second argument
|
| 24 |
"""
|
| 25 |
+
return f"Received {arg1} and {arg2}"
|
| 26 |
+
|
| 27 |
|
| 28 |
@tool
|
| 29 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 30 |
+
"""Fetch current time in a timezone"""
|
|
|
|
|
|
|
|
|
|
| 31 |
try:
|
|
|
|
| 32 |
tz = pytz.timezone(timezone)
|
|
|
|
| 33 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 34 |
+
return f"{timezone}: {local_time}"
|
| 35 |
except Exception as e:
|
| 36 |
+
return f"Error: {str(e)}"
|
| 37 |
|
| 38 |
|
| 39 |
+
# ------------------ MODEL ------------------ #
|
| 40 |
model = InferenceClientModel(
|
| 41 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 42 |
+
max_tokens=1024, # safer for Spaces
|
| 43 |
temperature=0.5,
|
|
|
|
|
|
|
| 44 |
)
|
| 45 |
|
| 46 |
|
| 47 |
+
# ------------------ LOAD PROMPTS ------------------ #
|
| 48 |
+
try:
|
| 49 |
+
with open("prompts.yaml", "r") as f:
|
| 50 |
+
prompt_templates = yaml.safe_load(f)
|
| 51 |
+
except FileNotFoundError:
|
| 52 |
+
prompt_templates = None
|
| 53 |
+
|
| 54 |
|
| 55 |
+
# ------------------ TOOLS ------------------ #
|
| 56 |
+
final_answer = FinalAnswerTool()
|
| 57 |
+
|
| 58 |
+
tools = [
|
| 59 |
+
final_answer,
|
| 60 |
+
my_custom_tool,
|
| 61 |
+
get_current_time_in_timezone,
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
# Optional (only if you actually use it)
|
| 65 |
+
try:
|
| 66 |
+
image_generation_tool = load_tool(
|
| 67 |
+
"agents-course/text-to-image",
|
| 68 |
+
trust_remote_code=True
|
| 69 |
+
)
|
| 70 |
+
tools.append(image_generation_tool)
|
| 71 |
+
except Exception:
|
| 72 |
+
pass
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# ------------------ AGENT ------------------ #
|
| 76 |
agent = CodeAgent(
|
| 77 |
model=model,
|
| 78 |
+
tools=tools,
|
| 79 |
+
max_steps=4, # lower = safer in Spaces
|
| 80 |
verbosity_level=1,
|
| 81 |
+
prompt_templates=prompt_templates,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
)
|
| 83 |
|
| 84 |
|
| 85 |
+
# ------------------ UI ------------------ #
|
| 86 |
+
app = GradioUI(agent)
|
| 87 |
+
|
| 88 |
+
# IMPORTANT for Spaces
|
| 89 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|