Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,42 @@
|
|
1 |
-
import
|
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 |
-
|
11 |
-
return
|
12 |
-
|
13 |
-
|
14 |
|
15 |
-
# Simple agent logic that parses user input and calls get_time if applicable
|
16 |
def agent_response(user_input: str) -> str:
|
17 |
-
#
|
18 |
-
match = re.search(r"time
|
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,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
clear = gr.Button("Clear")
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
chat_history = chat_history + [(message, response)]
|
36 |
-
return "", chat_history
|
37 |
|
38 |
-
|
39 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
40 |
|
41 |
-
|
|
|
|
1 |
+
import datetime
|
2 |
import pytz
|
|
|
3 |
import re
|
4 |
+
import gradio as gr
|
5 |
|
|
|
6 |
def get_time(timezone_str: str) -> str:
|
7 |
try:
|
8 |
tz = pytz.timezone(timezone_str)
|
9 |
+
except pytz.UnknownTimeZoneError:
|
10 |
+
return f"Sorry, '{timezone_str}' is not a recognized timezone."
|
11 |
+
now = datetime.datetime.now(tz)
|
12 |
+
return now.strftime("%Y-%m-%d %H:%M:%S")
|
13 |
|
|
|
14 |
def agent_response(user_input: str) -> str:
|
15 |
+
# Updated regex to capture timezone after 'time' and 'in'
|
16 |
+
match = re.search(r"time\s+(?:is\s+it\s+)?in\s+([\w/_]+)", user_input, re.IGNORECASE)
|
17 |
if match:
|
18 |
timezone_str = match.group(1)
|
|
|
19 |
time_str = get_time(timezone_str)
|
20 |
return f"The current time in {timezone_str} is {time_str}"
|
21 |
else:
|
22 |
+
return ("Sorry, I can only answer questions about time in a specific timezone, "
|
23 |
+
"e.g. 'What time is it in America/New_York?'")
|
24 |
+
|
25 |
+
def main():
|
26 |
+
with gr.Blocks() as demo:
|
27 |
+
gr.Markdown("## Ask me about the current time in a timezone!")
|
28 |
+
chatbot = gr.Chatbot()
|
29 |
+
txt = gr.Textbox(placeholder="Enter your question here...")
|
30 |
|
31 |
+
def respond(user_message, chat_history):
|
32 |
+
response = agent_response(user_message)
|
33 |
+
chat_history = chat_history + [(user_message, response)]
|
34 |
+
return chat_history, ""
|
|
|
35 |
|
36 |
+
txt.submit(respond, inputs=[txt, chatbot], outputs=[chatbot, txt])
|
37 |
+
txt.submit(lambda: "", None, txt) # Clear input box after submit
|
|
|
|
|
38 |
|
39 |
+
demo.launch()
|
|
|
40 |
|
41 |
+
if __name__ == "__main__":
|
42 |
+
main()
|