Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,85 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
import datetime
|
| 3 |
-
import requests
|
| 4 |
import pytz
|
| 5 |
-
import
|
| 6 |
-
from tools.final_answer import FinalAnswerTool
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, tool
|
| 2 |
import datetime
|
|
|
|
| 3 |
import pytz
|
| 4 |
+
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
+
# Define the FinalAnswerTool (minimal implementation for this example)
|
| 7 |
+
class FinalAnswerTool:
|
| 8 |
+
def __call__(self, answer: str) -> str:
|
| 9 |
+
return f"Final Answer: {answer}"
|
| 10 |
|
| 11 |
+
@tool
|
| 12 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 13 |
+
"""A tool that combines two arguments into a message.
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
arg1: the first argument (string)
|
| 17 |
+
arg2: the second argument (integer)
|
| 18 |
+
|
| 19 |
+
Returns:
|
| 20 |
+
A message combining both arguments.
|
| 21 |
+
"""
|
| 22 |
+
return f"You provided '{arg1}' and the number {arg2}."
|
| 23 |
|
| 24 |
+
@tool
|
| 25 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
| 26 |
+
"""A tool that fetches the current local time in a specified timezone.
|
| 27 |
+
|
| 28 |
+
Args:
|
| 29 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 30 |
+
|
| 31 |
+
Returns:
|
| 32 |
+
The current local time in the specified timezone.
|
| 33 |
+
"""
|
| 34 |
+
try:
|
| 35 |
+
tz = pytz.timezone(timezone)
|
| 36 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 37 |
+
return f"The current local time in {timezone} is: {local_time}"
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 40 |
|
| 41 |
+
# Initialize the model
|
| 42 |
+
model = HfApiModel(
|
| 43 |
+
max_tokens=2096,
|
| 44 |
+
temperature=0.5,
|
| 45 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # Replace with alternative if overloaded
|
| 46 |
+
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# Define a simple prompt template (instead of loading from prompts.yaml)
|
| 50 |
+
prompt_templates = {
|
| 51 |
+
"default": "You are a helpful assistant. Use the provided tools to answer the user's query: {query}"
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
# Initialize the agent
|
| 55 |
+
agent = CodeAgent(
|
| 56 |
+
model=model,
|
| 57 |
+
tools=[FinalAnswerTool(), my_custom_tool, get_current_time_in_timezone],
|
| 58 |
+
max_steps=6,
|
| 59 |
+
verbosity_level=1,
|
| 60 |
+
grammar=None,
|
| 61 |
+
planning_interval=None,
|
| 62 |
+
name="SimpleAgent",
|
| 63 |
+
description="A simple agent with custom tools",
|
| 64 |
+
prompt_templates=prompt_templates
|
| 65 |
)
|
| 66 |
+
|
| 67 |
+
# Define a basic Gradio UI
|
| 68 |
+
def run_agent(query):
|
| 69 |
+
try:
|
| 70 |
+
response = agent.run(query)
|
| 71 |
+
return response
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return f"Error: {str(e)}"
|
| 74 |
+
|
| 75 |
+
# Create Gradio interface
|
| 76 |
+
interface = gr.Interface(
|
| 77 |
+
fn=run_agent,
|
| 78 |
+
inputs=gr.Textbox(label="Enter your query"),
|
| 79 |
+
outputs=gr.Textbox(label="Agent Response"),
|
| 80 |
+
title="Simple CodeAgent Example",
|
| 81 |
+
description="Ask the agent to use tools, e.g., 'Get the current time in America/New_York' or 'Combine hello and 42'"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Launch the Gradio interface
|
| 85 |
+
interface.launch(server_port=7860, share=False)
|