Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,13 @@
|
|
| 1 |
-
from smolagents import CodeAgent, HfApiModel, tool
|
| 2 |
import datetime
|
| 3 |
import pytz
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
-
# Define
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
class FinalAnswerTool:
|
| 8 |
def __call__(self, answer: str) -> str:
|
| 9 |
return f"Final Answer: {answer}"
|
|
@@ -38,48 +42,52 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 38 |
except Exception as e:
|
| 39 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # Replace with alternative if overloaded
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
-
)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
# Initialize
|
| 55 |
-
|
| 56 |
-
|
| 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
|
| 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
|
| 81 |
-
description="
|
| 82 |
)
|
| 83 |
|
| 84 |
-
|
| 85 |
-
interface.launch(server_port=7860, share=False)
|
|
|
|
|
|
|
| 1 |
import datetime
|
| 2 |
import pytz
|
| 3 |
import gradio as gr
|
| 4 |
+
from typing import Callable, List
|
| 5 |
|
| 6 |
+
# Define a simple tool decorator (mimicking smolagents' @tool)
|
| 7 |
+
def tool(func: Callable) -> Callable:
|
| 8 |
+
return func
|
| 9 |
+
|
| 10 |
+
# Define the FinalAnswerTool
|
| 11 |
class FinalAnswerTool:
|
| 12 |
def __call__(self, answer: str) -> str:
|
| 13 |
return f"Final Answer: {answer}"
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 44 |
|
| 45 |
+
# Simple CodeAgent class (mimicking smolagents' CodeAgent)
|
| 46 |
+
class CodeAgent:
|
| 47 |
+
def __init__(self, tools: List[Callable]):
|
| 48 |
+
self.tools = {tool.__name__: tool for tool in tools}
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
def run(self, query: str) -> str:
|
| 51 |
+
# Simple logic to route queries to tools
|
| 52 |
+
if "time in" in query.lower():
|
| 53 |
+
# Extract timezone from query (e.g., "time in America/New_York")
|
| 54 |
+
try:
|
| 55 |
+
timezone = query.lower().split("time in")[-1].strip()
|
| 56 |
+
return self.tools["get_current_time_in_timezone"](timezone)
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return f"Error processing timezone: {str(e)}"
|
| 59 |
+
elif "combine" in query.lower():
|
| 60 |
+
# Extract arguments for my_custom_tool (e.g., "combine hello and 42")
|
| 61 |
+
try:
|
| 62 |
+
parts = query.lower().split("combine")[-1].strip().split("and")
|
| 63 |
+
arg1 = parts[0].strip()
|
| 64 |
+
arg2 = int(parts[1].strip())
|
| 65 |
+
return self.tools["my_custom_tool"](arg1, arg2)
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"Error processing combine query: {str(e)}"
|
| 68 |
+
else:
|
| 69 |
+
return "Sorry, I don't understand the query. Try 'time in <timezone>' or 'combine <string> and <number>'."
|
| 70 |
|
| 71 |
+
# Initialize tools and agent
|
| 72 |
+
final_answer = FinalAnswerTool()
|
| 73 |
+
agent = CodeAgent(tools=[final_answer, my_custom_tool, get_current_time_in_timezone])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
# Define Gradio interface function
|
| 76 |
+
def run_agent(query: str) -> str:
|
| 77 |
try:
|
| 78 |
response = agent.run(query)
|
| 79 |
+
return final_answer(response)
|
| 80 |
except Exception as e:
|
| 81 |
return f"Error: {str(e)}"
|
| 82 |
|
| 83 |
+
# Create and launch Gradio interface
|
| 84 |
interface = gr.Interface(
|
| 85 |
fn=run_agent,
|
| 86 |
inputs=gr.Textbox(label="Enter your query"),
|
| 87 |
outputs=gr.Textbox(label="Agent Response"),
|
| 88 |
+
title="Simple Agent Example",
|
| 89 |
+
description="Try queries like 'Get the time in America/New_York' or 'Combine hello and 42'"
|
| 90 |
)
|
| 91 |
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
interface.launch(server_port=7860, share=False, quiet=True)
|