rakesh-dvg commited on
Commit
18c495a
·
verified ·
1 Parent(s): eb1d493

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -66
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 yaml
6
- from tools.final_answer import FinalAnswerTool
7
 
8
- from Gradio_UI import GradioUI
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
- # Create timezone object
28
- tz = pytz.timezone(timezone)
29
- # Get current time in that timezone
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 fetching time for timezone '{timezone}': {str(e)}"
34
-
35
-
36
- final_answer = FinalAnswerTool()
37
-
38
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
39
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
40
-
41
- model = HfApiModel(
42
- max_tokens=2096,
43
- temperature=0.5,
44
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
45
- custom_role_conversions=None,
46
- )
47
-
48
- # Import tool from Hub
49
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
50
-
51
- with open("prompts.yaml", 'r') as stream:
52
- prompt_templates = yaml.safe_load(stream)
53
-
54
- agent = CodeAgent(
55
- model=model,
56
- tools=[
57
- final_answer,
58
- my_custom_tool,
59
- get_current_time_in_timezone,
60
- image_generation_tool,
61
- ], # added all useful tools here
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()