Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,41 @@
|
|
1 |
-
import datetime
|
2 |
-
import pytz
|
3 |
import gradio as gr
|
|
|
|
|
|
|
4 |
|
5 |
-
# Tool
|
6 |
-
def
|
7 |
try:
|
8 |
tz = pytz.timezone(timezone_str)
|
9 |
-
now = datetime.
|
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 |
-
#
|
15 |
-
def
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
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
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
demo.launch()
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import pytz
|
3 |
+
from datetime import datetime
|
4 |
+
import re
|
5 |
|
6 |
+
# Tool function to get current time in a timezone
|
7 |
+
def get_time(timezone_str: str) -> str:
|
8 |
try:
|
9 |
tz = pytz.timezone(timezone_str)
|
10 |
+
now = datetime.now(tz)
|
11 |
return now.strftime("%Y-%m-%d %H:%M:%S %Z%z")
|
12 |
except Exception as e:
|
13 |
return f"Error: invalid timezone '{timezone_str}'"
|
14 |
|
15 |
+
# Simple agent logic that parses user input and calls get_time if applicable
|
16 |
+
def agent_response(user_input: str) -> str:
|
17 |
+
# Try to extract timezone string like 'America/New_York' from user input
|
18 |
+
match = re.search(r"time in ([\w/_]+)", user_input, re.IGNORECASE)
|
19 |
+
if match:
|
20 |
+
timezone_str = match.group(1)
|
21 |
+
# Call the get_time tool
|
22 |
+
time_str = get_time(timezone_str)
|
23 |
+
return f"The current time in {timezone_str} is {time_str}"
|
24 |
+
else:
|
25 |
+
return "Sorry, I can only answer questions about time in a specific timezone, e.g. 'What time is it in America/New_York?'"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
# Gradio UI
|
28 |
with gr.Blocks() as demo:
|
29 |
chatbot = gr.Chatbot()
|
30 |
+
msg = gr.Textbox(placeholder="Ask me the time in a timezone, e.g. 'What time is it in America/New_York?'")
|
31 |
+
clear = gr.Button("Clear")
|
32 |
+
|
33 |
+
def user_submit(message, chat_history):
|
34 |
+
response = agent_response(message)
|
35 |
+
chat_history = chat_history + [(message, response)]
|
36 |
+
return "", chat_history
|
37 |
+
|
38 |
+
msg.submit(user_submit, [msg, chatbot], [msg, chatbot])
|
39 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
40 |
|
41 |
+
demo.launch()
|
|