ilmarila commited on
Commit
0a7466b
·
verified ·
1 Parent(s): b6c9b3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -18
app.py CHANGED
@@ -1,21 +1,49 @@
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
  @tool
11
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
12
- #Keep this format for the description / args / args description but feel free to modify the tool
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:
@@ -33,24 +61,32 @@ def get_current_time_in_timezone(timezone: str) -> str:
33
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
34
 
35
 
 
36
  final_answer = FinalAnswerTool()
 
 
37
  model = HfApiModel(
38
- max_tokens=2096,
39
- temperature=0.5,
40
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
41
- custom_role_conversions=None,
42
  )
43
 
44
-
45
- # Import tool from Hub
46
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
47
 
 
48
  with open("prompts.yaml", 'r') as stream:
49
  prompt_templates = yaml.safe_load(stream)
50
-
 
51
  agent = CodeAgent(
52
  model=model,
53
- tools=[image_generation_tool,get_current_time_in_timezone,final_answer,DuckDuckGoSearchTool()], ## add or remove tools here
 
 
 
54
  max_steps=6,
55
  verbosity_level=1,
56
  grammar=None,
@@ -60,5 +96,5 @@ agent = CodeAgent(
60
  prompt_templates=prompt_templates
61
  )
62
 
63
-
64
- GradioUI(agent).launch()
 
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
  from Gradio_UI import GradioUI
8
 
9
+ # Custom Tools
10
+
11
  @tool
12
+ def get_weather(city: str) -> str:
13
+ """Fetches the current weather for a given city.
 
14
  Args:
15
+ city: The name of the city.
 
16
  """
17
+ try:
18
+ response = requests.get(f"https://wttr.in/{city}?format=%C+%t")
19
+ return f"The current weather in {city} is: {response.text}"
20
+ except Exception as e:
21
+ return f"Error fetching weather for {city}: {str(e)}"
22
+
23
+
24
+ @tool
25
+ def solve_math(expression: str) -> str:
26
+ """Solves basic math expressions.
27
+ Args:
28
+ expression: A math expression in string format (e.g., "2+2*3").
29
+ """
30
+ try:
31
+ result = eval(expression, {"__builtins__": None}, {})
32
+ return f"The result is {result}"
33
+ except Exception as e:
34
+ return f"Error solving math expression '{expression}': {str(e)}"
35
+
36
+
37
+ @tool
38
+ def get_joke() -> str:
39
+ """Fetches a random joke from an alternative API (icanhazdadjoke)."""
40
+ try:
41
+ headers = {"Accept": "application/json"}
42
+ response = requests.get("https://icanhazdadjoke.com/", headers=headers).json()
43
+ return response["joke"]
44
+ except Exception as e:
45
+ return f"Error fetching joke: {str(e)}"
46
+
47
 
48
  @tool
49
  def get_current_time_in_timezone(timezone: str) -> str:
 
61
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
62
 
63
 
64
+ # Final Answer Tool (Required)
65
  final_answer = FinalAnswerTool()
66
+
67
+ # Fix: Use a valid Hugging Face model ID instead of a URL
68
  model = HfApiModel(
69
+ max_tokens=2096,
70
+ temperature=0.5,
71
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # Ensure this model is available
72
+ custom_role_conversions=None,
73
  )
74
 
75
+ # Import external tools
 
76
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
77
+ search_tool = DuckDuckGoSearchTool() # Adding missing search tool
78
 
79
+ # Load prompt templates
80
  with open("prompts.yaml", 'r') as stream:
81
  prompt_templates = yaml.safe_load(stream)
82
+
83
+ # Initialize agent with necessary tools
84
  agent = CodeAgent(
85
  model=model,
86
+ tools=[
87
+ final_answer, get_weather, solve_math, get_joke, get_current_time_in_timezone,
88
+ image_generation_tool, search_tool # Ensure required tools are included
89
+ ],
90
  max_steps=6,
91
  verbosity_level=1,
92
  grammar=None,
 
96
  prompt_templates=prompt_templates
97
  )
98
 
99
+ # Launch UI
100
+ GradioUI(agent).launch()