Files changed (1) hide show
  1. app.py +71 -28
app.py CHANGED
@@ -1,61 +1,105 @@
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 import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
- Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
- """
19
- return "What magic will you build ?"
20
 
21
- @tool
 
 
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
  """A tool that fetches the current local time in a specified timezone.
24
  Args:
25
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
26
  """
27
  try:
28
- # Create timezone object
29
  tz = pytz.timezone(timezone)
30
- # Get current time in that timezone
31
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
32
  return f"The current local time in {timezone} is: {local_time}"
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
 
37
- final_answer = FinalAnswerTool()
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- # 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:
40
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
41
 
42
- model = HfApiModel(
43
- max_tokens=2096,
44
- temperature=0.5,
45
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
46
- custom_role_conversions=None,
47
- )
 
 
 
 
 
 
 
48
 
49
 
50
- # Import tool from Hub
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
52
 
 
53
  with open("prompts.yaml", 'r') as stream:
54
  prompt_templates = yaml.safe_load(stream)
55
-
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
 
 
 
 
 
 
 
 
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
@@ -65,5 +109,4 @@ agent = CodeAgent(
65
  prompt_templates=prompt_templates
66
  )
67
 
68
-
69
  GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
7
  from Gradio_UI import GradioUI
8
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ # ─────────────────────────────────────────
11
+ # TOOL 1: Get current time in any timezone
12
+ # ─────────────────────────────────────────
13
  def get_current_time_in_timezone(timezone: str) -> str:
14
  """A tool that fetches the current local time in a specified timezone.
15
  Args:
16
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
17
  """
18
  try:
 
19
  tz = pytz.timezone(timezone)
 
20
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
21
  return f"The current local time in {timezone} is: {local_time}"
22
  except Exception as e:
23
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
24
 
25
 
26
+ # ─────────────────────────────────────────
27
+ # TOOL 2: Get weather for any city (free API)
28
+ # ─────────────────────────────────────────
29
+ def get_weather(city: str) -> str:
30
+ """Gets the current weather for a given city using wttr.in.
31
+ Args:
32
+ city: The name of the city to get weather for (e.g., 'Bengaluru').
33
+ """
34
+ try:
35
+ url = f"https://wttr.in/{city}?format=3"
36
+ response = requests.get(url, timeout=5)
37
+ return response.text.strip()
38
+ except Exception as e:
39
+ return f"Could not fetch weather for {city}: {str(e)}"
40
 
 
 
41
 
42
+ # ─────────────────────────────────────────
43
+ # TOOL 3: Simple calculator
44
+ # ─────────────────────────────────────────
45
+ def calculator(expression: str) -> str:
46
+ """Evaluates a basic math expression and returns the result.
47
+ Args:
48
+ expression: A math expression as a string, e.g. '25 * 4 + 10'.
49
+ """
50
+ try:
51
+ result = eval(expression, {"__builtins__": {}})
52
+ return f"Result of '{expression}' = {result}"
53
+ except Exception as e:
54
+ return f"Could not evaluate '{expression}': {str(e)}"
55
 
56
 
57
+ # ─────────────────────────────────────────
58
+ # TOOL 4: Get a fun fact about any number
59
+ # ─────────────────────────────────────────
60
+ def number_fact(number: int) -> str:
61
+ """Returns an interesting fact about a given number.
62
+ Args:
63
+ number: An integer to get a fun fact about.
64
+ """
65
+ try:
66
+ response = requests.get(f"http://numbersapi.com/{number}", timeout=5)
67
+ return response.text
68
+ except Exception as e:
69
+ return f"Could not fetch fact for {number}: {str(e)}"
70
+
71
+
72
+ # ─────────────────────────────────────────
73
+ # AGENT SETUP (don't change this much)
74
+ # ─────────────────────────────────────────
75
+ final_answer = FinalAnswerTool()
76
+
77
+ model = InferenceClientModel(
78
+ max_tokens=2096,
79
+ temperature=0.5,
80
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
81
+ custom_role_conversions=None,
82
+ )
83
+
84
+ # Load image generation tool from HF Hub
85
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
86
 
87
+ # Load system prompt
88
  with open("prompts.yaml", 'r') as stream:
89
  prompt_templates = yaml.safe_load(stream)
90
+
91
+ # ← THIS is where you register all your tools
92
  agent = CodeAgent(
93
  model=model,
94
+ tools=[
95
+ final_answer,
96
+ DuckDuckGoSearchTool(), # web search
97
+ get_current_time_in_timezone, # time zones
98
+ get_weather, # weather
99
+ calculator, # math
100
+ number_fact, # fun facts
101
+ image_generation_tool, # generate images
102
+ ],
103
  max_steps=6,
104
  verbosity_level=1,
105
  grammar=None,
 
109
  prompt_templates=prompt_templates
110
  )
111
 
 
112
  GradioUI(agent).launch()