Thorfast commited on
Commit
8712cc0
·
verified ·
1 Parent(s): 9ae337d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -44
app.py CHANGED
@@ -1,30 +1,19 @@
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 currency_converter(amount: float, from_currency: str, to_currency: str) -> str:
23
  """Convert between currencies using real-time exchange rates
24
  Args:
25
- amount: The amount to convert
26
- from_currency: Currency code to convert from (e.g. USD)
27
- to_currency: Currency code to convert to (e.g. EUR)
28
  """
29
  try:
30
  url = f"https://api.exchangerate-api.com/v4/latest/{from_currency}"
@@ -34,7 +23,7 @@ def currency_converter(amount: float, from_currency: str, to_currency: str) -> s
34
  return f"Convertion of {amount} {from_currency} = {converted:.2f} {to_currency}"
35
  except Exception as e:
36
  return f"Error converting {amount} {from_currency} to {to_currency}"
37
-
38
  @tool
39
  def website_availability(url: str) -> str:
40
  """Check if a website is online and responsive
@@ -58,70 +47,71 @@ def text_summarizer(long_text: str) -> str:
58
  return '. '.join(sentences[:3]) + '...'
59
  except Exception as e:
60
  return f"Error summarizing text"
61
-
62
 
63
  @tool
64
  def get_current_time_in_timezone(timezone: str) -> str:
65
- """A tool that fetches the current local time in a specified timezone.
66
  Args:
67
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
68
  """
69
  try:
70
- # Create timezone object
71
  tz = pytz.timezone(timezone)
72
- # Get current time in that timezone
73
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
74
  return f"The current local time in {timezone} is: {local_time}"
75
  except Exception as e:
76
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  final_answer = FinalAnswerTool()
80
 
81
- # 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:
82
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
83
-
84
  model = HfApiModel(
85
- max_tokens=2096,
86
- temperature=0.5,
87
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
88
- custom_role_conversions=None,
89
  )
90
 
91
-
92
- # Import tool from Hub
93
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
94
- weather_tool = load_tool("agents-course/weather-api", trust_remote_code=True)
95
- wiki_tool = load_tool("agents-course/wiki-search", trust_remote_code=True)
96
- calculator_tool = load_tool("agents-course/calculator", trust_remote_code=True)
97
 
 
 
 
 
98
  all_tools = [
99
  final_answer,
100
  DuckDuckGoSearchTool(), # Built-in search
101
  image_generation_tool,
102
- weather_tool,
103
- wiki_tool,
104
- calculator_tool, # Hub-loaded calculator
105
  get_current_time_in_timezone,
106
  currency_converter,
107
  website_availability,
108
  text_summarizer
109
  ]
110
 
111
- with open("prompts.yaml", 'r') as stream:
112
- prompt_templates = yaml.safe_load(stream)
113
-
114
  agent = CodeAgent(
115
  model=model,
116
  tools=all_tools,
117
- max_steps=12, # Increased step limit
118
  verbosity_level=1,
119
  grammar=None,
120
  planning_interval=None,
121
  name=None,
122
  description=None,
123
- prompt_templates=prompt_templates # No comma here!
124
  )
125
 
126
-
127
  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 with complete docstrings
 
 
 
 
 
 
 
 
 
 
10
  @tool
11
  def currency_converter(amount: float, from_currency: str, to_currency: str) -> str:
12
  """Convert between currencies using real-time exchange rates
13
  Args:
14
+ amount: The amount to convert (positive number)
15
+ from_currency: Currency code to convert from (3-letter ISO code like USD)
16
+ to_currency: Currency code to convert to (3-letter ISO code like EUR)
17
  """
18
  try:
19
  url = f"https://api.exchangerate-api.com/v4/latest/{from_currency}"
 
23
  return f"Convertion of {amount} {from_currency} = {converted:.2f} {to_currency}"
24
  except Exception as e:
25
  return f"Error converting {amount} {from_currency} to {to_currency}"
26
+
27
  @tool
28
  def website_availability(url: str) -> str:
29
  """Check if a website is online and responsive
 
47
  return '. '.join(sentences[:3]) + '...'
48
  except Exception as e:
49
  return f"Error summarizing text"
 
50
 
51
  @tool
52
  def get_current_time_in_timezone(timezone: str) -> str:
53
+ """Get current time in a specific timezone
54
  Args:
55
+ timezone: Valid timezone name (e.g. America/New_York or Asia/Tokyo)
56
  """
57
  try:
 
58
  tz = pytz.timezone(timezone)
 
59
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
60
  return f"The current local time in {timezone} is: {local_time}"
61
  except Exception as e:
62
+ return f"Error fetching time: {str(e)}"
63
 
64
+ @tool
65
+ def get_current_weather(city: str) -> str:
66
+ """Get current weather for a city
67
+ Args:
68
+ city: City name (e.g. Paris, London, Tokyo)
69
+ """
70
+ try:
71
+ # Using a free weather API (no key required)
72
+ url = f"https://wttr.in/{city}?format=%C+%t+%w"
73
+ response = requests.get(url)
74
+ return f"Weather in {city}: {response.text.strip()}"
75
+ except Exception as e:
76
+ return f"Weather lookup failed: {str(e)}"
77
 
78
  final_answer = FinalAnswerTool()
79
 
 
 
 
80
  model = HfApiModel(
81
+ max_tokens=2096,
82
+ temperature=0.5,
83
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
84
+ custom_role_conversions=None,
85
  )
86
 
87
+ # Load only working tools from Hub
 
88
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
 
 
89
 
90
+ with open("prompts.yaml", 'r') as stream:
91
+ prompt_templates = yaml.safe_load(stream)
92
+
93
+ # Build tools list
94
  all_tools = [
95
  final_answer,
96
  DuckDuckGoSearchTool(), # Built-in search
97
  image_generation_tool,
98
+ get_current_weather, # Our custom weather tool
 
 
99
  get_current_time_in_timezone,
100
  currency_converter,
101
  website_availability,
102
  text_summarizer
103
  ]
104
 
 
 
 
105
  agent = CodeAgent(
106
  model=model,
107
  tools=all_tools,
108
+ max_steps=12,
109
  verbosity_level=1,
110
  grammar=None,
111
  planning_interval=None,
112
  name=None,
113
  description=None,
114
+ prompt_templates=prompt_templates
115
  )
116
 
 
117
  GradioUI(agent).launch()