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

Update app.py

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