rakesh-dvg commited on
Commit
e32f334
·
verified ·
1 Parent(s): d5aa5dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -24
app.py CHANGED
@@ -1,41 +1,42 @@
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()
 
 
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()