Chikeka commited on
Commit
32d4d45
·
verified ·
1 Parent(s): 18bc41a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -20
app.py CHANGED
@@ -1,26 +1,80 @@
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 get_weather_forecast(weather:int, feels_like: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
  """
@@ -33,29 +87,26 @@ def get_current_time_in_timezone(timezone: str) -> str:
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 +116,4 @@ agent = CodeAgent(
65
  prompt_templates=prompt_templates
66
  )
67
 
68
-
69
  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
+ # Fully functional weather forecast tool
10
  @tool
11
+ def get_weather_forecast(city: str, country_code: str = "") -> str:
12
+ """A tool that fetches the current weather and forecast for a specified location.
13
+
14
  Args:
15
+ city: The name of the city to get weather for (e.g., 'London', 'New York')
16
+ country_code: Optional two-letter country code (e.g., 'US', 'UK', 'FR')
17
  """
18
+ try:
19
+ # OpenWeatherMap API endpoint and parameters
20
+ # Note: In a real application, you should store API keys securely
21
+ API_KEY = "YOUR_OPENWEATHERMAP_API_KEY" # Replace with your actual API key
22
+ base_url = "https://api.openweathermap.org/data/2.5/weather"
23
+
24
+ # Build location string based on inputs
25
+ location = city
26
+ if country_code:
27
+ location = f"{city},{country_code}"
28
+
29
+ # Create request parameters
30
+ params = {
31
+ 'q': location,
32
+ 'appid': API_KEY,
33
+ 'units': 'metric' # Use metric units (Celsius)
34
+ }
35
+
36
+ # Make API request
37
+ response = requests.get(base_url, params=params)
38
+ response.raise_for_status() # Raise exception for HTTP errors
39
+
40
+ # Parse response
41
+ weather_data = response.json()
42
+
43
+ # Extract relevant information
44
+ current_temp = weather_data['main']['temp']
45
+ feels_like = weather_data['main']['feels_like']
46
+ humidity = weather_data['main']['humidity']
47
+ conditions = weather_data['weather'][0]['description']
48
+ wind_speed = weather_data['wind']['speed']
49
+
50
+ # Format and return results
51
+ result = (
52
+ f"Current Weather in {weather_data['name']}, {weather_data['sys']['country']}:\n"
53
+ f"• Temperature: {current_temp}°C (feels like {feels_like}°C)\n"
54
+ f"• Conditions: {conditions.capitalize()}\n"
55
+ f"• Humidity: {humidity}%\n"
56
+ f"• Wind Speed: {wind_speed} m/s"
57
+ )
58
+
59
+ return result
60
+
61
+ except requests.exceptions.HTTPError as http_err:
62
+ return f"HTTP Error occurred: {http_err}"
63
+ except requests.exceptions.ConnectionError:
64
+ return f"Connection Error: Unable to connect to weather service"
65
+ except requests.exceptions.Timeout:
66
+ return f"Timeout Error: Request timed out"
67
+ except requests.exceptions.RequestException as err:
68
+ return f"Error fetching weather data: {err}"
69
+ except KeyError as key_err:
70
+ return f"Error parsing weather data: {key_err}"
71
+ except Exception as e:
72
+ return f"Unexpected error: {str(e)}"
73
 
74
  @tool
75
  def get_current_time_in_timezone(timezone: str) -> str:
76
  """A tool that fetches the current local time in a specified timezone.
77
+
78
  Args:
79
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
80
  """
 
87
  except Exception as e:
88
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
89
 
 
90
  final_answer = FinalAnswerTool()
91
 
92
  # 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:
93
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
 
94
  model = HfApiModel(
95
+ max_tokens=2096,
96
+ temperature=0.5,
97
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
98
+ custom_role_conversions=None,
99
  )
100
 
 
101
  # Import tool from Hub
102
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
103
 
104
  with open("prompts.yaml", 'r') as stream:
105
  prompt_templates = yaml.safe_load(stream)
106
+
107
  agent = CodeAgent(
108
  model=model,
109
+ tools=[final_answer, get_weather_forecast, get_current_time_in_timezone, image_generation_tool], # Added all tools here
110
  max_steps=6,
111
  verbosity_level=1,
112
  grammar=None,
 
116
  prompt_templates=prompt_templates
117
  )
118
 
 
119
  GradioUI(agent).launch()