Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,40 @@
|
|
1 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
2 |
import datetime
|
3 |
-
import requests
|
4 |
import pytz
|
5 |
-
import
|
6 |
-
from tools.final_answer import FinalAnswerTool
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
11 |
-
@tool
|
12 |
-
def my_custom_tool(arg1: str, arg2: int) -> str: # it's important to specify the return type
|
13 |
-
"""A tool that does nothing yet
|
14 |
-
Args:
|
15 |
-
arg1: the first argument
|
16 |
-
arg2: the second argument
|
17 |
-
"""
|
18 |
-
return "What magic will you build ?"
|
19 |
-
|
20 |
-
@tool
|
21 |
-
def get_current_time_in_timezone(timezone: str) -> str:
|
22 |
-
"""A tool that fetches the current local time in a specified timezone.
|
23 |
-
Args:
|
24 |
-
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
25 |
-
"""
|
26 |
try:
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
31 |
-
return f"The current local time in {timezone} is: {local_time}"
|
32 |
except Exception as e:
|
33 |
-
return f"Error
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
max_steps=6,
|
63 |
-
verbosity_level=1,
|
64 |
-
grammar=None,
|
65 |
-
planning_interval=None,
|
66 |
-
name=None,
|
67 |
-
description=None,
|
68 |
-
prompt_templates=prompt_templates
|
69 |
-
)
|
70 |
-
|
71 |
-
GradioUI(agent).launch()
|
|
|
|
|
1 |
import datetime
|
|
|
2 |
import pytz
|
3 |
+
import gradio as gr
|
|
|
4 |
|
5 |
+
# Tool implementation: get current time in a timezone
|
6 |
+
def get_current_time_in_timezone(timezone_str: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
try:
|
8 |
+
tz = pytz.timezone(timezone_str)
|
9 |
+
now = datetime.datetime.now(tz)
|
10 |
+
return now.strftime("%Y-%m-%d %H:%M:%S %Z%z")
|
|
|
|
|
11 |
except Exception as e:
|
12 |
+
return f"Error: invalid timezone '{timezone_str}'"
|
13 |
+
|
14 |
+
# Wrapper function your agent will call
|
15 |
+
def agent_code_executor(user_input: str) -> str:
|
16 |
+
# Example simple parser for timezone queries
|
17 |
+
if "time in" in user_input.lower():
|
18 |
+
# extract timezone, naive extraction
|
19 |
+
import re
|
20 |
+
match = re.search(r"time in ([\w/_]+)", user_input, re.I)
|
21 |
+
if match:
|
22 |
+
tz = match.group(1)
|
23 |
+
return get_current_time_in_timezone(tz)
|
24 |
+
else:
|
25 |
+
return "Please specify a timezone like 'America/New_York'."
|
26 |
+
return "I don't know how to answer that yet."
|
27 |
+
|
28 |
+
# Gradio UI for testing
|
29 |
+
def chat_interface(user_message):
|
30 |
+
# Run the agent logic (in real code, you run your agent with tools)
|
31 |
+
output = agent_code_executor(user_message)
|
32 |
+
return output
|
33 |
+
|
34 |
+
with gr.Blocks() as demo:
|
35 |
+
chatbot = gr.Chatbot()
|
36 |
+
msg = gr.Textbox(placeholder="Ask me something...")
|
37 |
+
msg.submit(chat_interface, msg, chatbot)
|
38 |
+
|
39 |
+
if __name__ == "__main__":
|
40 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|