Alaaeldin commited on
Commit
5ee2dd5
·
verified ·
1 Parent(s): 6b14cc6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -4
app.py CHANGED
@@ -2,10 +2,12 @@ from smolagents import CodeAgent, tool
2
  import datetime
3
  import pytz
4
  import yaml
 
 
5
  from tools.final_answer import FinalAnswerTool
6
  from Gradio_UI import GradioUI
7
 
8
- # Define a simple tool that doesn't rely on any external libraries
9
  @tool
10
  def text_analyzer(text: str) -> str:
11
  """Analyzes text and returns statistics about it.
@@ -46,6 +48,7 @@ def text_analyzer(text: str) -> str:
46
  except Exception as e:
47
  return f"Error analyzing text: {str(e)}"
48
 
 
49
  @tool
50
  def get_current_time_in_timezone(timezone: str) -> str:
51
  """A tool that fetches the current local time in a specified timezone.
@@ -62,7 +65,69 @@ def get_current_time_in_timezone(timezone: str) -> str:
62
  except Exception as e:
63
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
64
 
65
- # Set up the agent with minimal tools
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  final_answer = FinalAnswerTool()
67
 
68
  with open("prompts.yaml", 'r') as stream:
@@ -77,10 +142,10 @@ model = HfApiModel(
77
  custom_role_conversions=None,
78
  )
79
 
80
- # Create agent with minimal tools
81
  agent = CodeAgent(
82
  model=model,
83
- tools=[text_analyzer, get_current_time_in_timezone, final_answer],
84
  max_steps=6,
85
  verbosity_level=1,
86
  grammar=None,
 
2
  import datetime
3
  import pytz
4
  import yaml
5
+ import requests
6
+ import json
7
  from tools.final_answer import FinalAnswerTool
8
  from Gradio_UI import GradioUI
9
 
10
+ # Text Analyzer Tool
11
  @tool
12
  def text_analyzer(text: str) -> str:
13
  """Analyzes text and returns statistics about it.
 
48
  except Exception as e:
49
  return f"Error analyzing text: {str(e)}"
50
 
51
+ # Timezone Tool
52
  @tool
53
  def get_current_time_in_timezone(timezone: str) -> str:
54
  """A tool that fetches the current local time in a specified timezone.
 
65
  except Exception as e:
66
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
67
 
68
+ # Weather Forecast Tool
69
+ @tool
70
+ def weather_forecast(location: str) -> str:
71
+ """Fetches weather forecast for a specified location.
72
+
73
+ Args:
74
+ location: The location to get weather forecast for (city name or coordinates).
75
+ """
76
+ try:
77
+ # Connect to a public weather API
78
+ api_url = f"https://wttr.in/{location}?format=j1"
79
+
80
+ # Make the API request
81
+ response = requests.get(api_url, timeout=10)
82
+ response.raise_for_status() # Raise an exception for HTTP errors
83
+
84
+ # Parse the JSON response
85
+ weather_data = response.json()
86
+
87
+ # Extract relevant information
88
+ current_condition = weather_data.get("current_condition", [{}])[0]
89
+ weather_desc = current_condition.get("weatherDesc", [{}])[0].get("value", "Unknown")
90
+ temp_c = current_condition.get("temp_C", "Unknown")
91
+ temp_f = current_condition.get("temp_F", "Unknown")
92
+ feels_like_c = current_condition.get("FeelsLikeC", "Unknown")
93
+ humidity = current_condition.get("humidity", "Unknown")
94
+ wind_speed = current_condition.get("windspeedKmph", "Unknown")
95
+ wind_dir = current_condition.get("winddir16Point", "Unknown")
96
+
97
+ # Get forecast for upcoming days
98
+ forecast = weather_data.get("weather", [])
99
+ forecast_info = ""
100
+
101
+ if forecast:
102
+ forecast_info = "\n\nForecast for the next few days:\n"
103
+ for day in forecast[:3]: # Limit to 3 days
104
+ date = day.get("date", "Unknown")
105
+ max_temp_c = day.get("maxtempC", "Unknown")
106
+ min_temp_c = day.get("mintempC", "Unknown")
107
+ desc = day.get("hourly", [{}])[0].get("weatherDesc", [{}])[0].get("value", "Unknown")
108
+
109
+ forecast_info += f"- {date}: {desc}, Max: {max_temp_c}°C, Min: {min_temp_c}°C\n"
110
+
111
+ # Format the response
112
+ weather_report = f"""
113
+ Weather for {location}:
114
+ Current Conditions: {weather_desc}
115
+ Temperature: {temp_c}°C / {temp_f}°F (Feels like: {feels_like_c}°C)
116
+ Humidity: {humidity}%
117
+ Wind: {wind_speed} km/h, Direction: {wind_dir}
118
+ {forecast_info}
119
+ """
120
+
121
+ return weather_report.strip()
122
+
123
+ except requests.exceptions.RequestException as e:
124
+ return f"Error fetching weather for {location}: Connection error - {str(e)}"
125
+ except json.JSONDecodeError:
126
+ return f"Error fetching weather for {location}: Invalid response from weather service"
127
+ except Exception as e:
128
+ return f"Error fetching weather for {location}: {str(e)}"
129
+
130
+ # Set up the agent with our tools
131
  final_answer = FinalAnswerTool()
132
 
133
  with open("prompts.yaml", 'r') as stream:
 
142
  custom_role_conversions=None,
143
  )
144
 
145
+ # Create agent with our tools (now 4 including final_answer)
146
  agent = CodeAgent(
147
  model=model,
148
+ tools=[text_analyzer, get_current_time_in_timezone, weather_forecast, final_answer],
149
  max_steps=6,
150
  verbosity_level=1,
151
  grammar=None,