Oyelami Oyewole A. commited on
Commit
c699f08
·
verified ·
1 Parent(s): ebf8488

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -45
app.py CHANGED
@@ -4,7 +4,6 @@ import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
- from geopy.geocoders import Nominatim
8
 
9
  from Gradio_UI import GradioUI
10
 
@@ -20,54 +19,32 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
20
  return "What magic will you build ?"
21
 
22
  @tool
23
- def get_weather_forecast(city_name: str) -> str:
24
- """Fetches the current weather forecast for a specified city using Open-Meteo API.
25
-
26
- Args:
27
- city_name: The name of the city to fetch the weather forecast for.
28
-
29
  Returns:
30
- A string describing the current weather forecast.
31
  """
32
  try:
33
- # Geocode the city name to get latitude and longitude
34
- geolocator = Nominatim(user_agent="weather_app")
35
- location = geolocator.geocode(city_name)
36
- if not location:
37
- return f"Could not find location for '{city_name}'."
38
-
39
- latitude = location.latitude
40
- longitude = location.longitude
41
-
42
- # Construct the API URL
43
- api_url = (
44
- f"https://api.open-meteo.com/v1/forecast?"
45
- f"latitude={latitude}&longitude={longitude}&current_weather=true"
46
- )
47
-
48
- # Make the API request
49
- response = requests.get(api_url)
50
- response.raise_for_status()
51
- data = response.json()
52
-
53
- # Extract weather information
54
- weather = data.get("current_weather")
55
- if not weather:
56
- return f"No weather data available for '{city_name}'."
57
-
58
- temperature = weather.get("temperature")
59
- windspeed = weather.get("windspeed")
60
- weather_code = weather.get("weathercode")
61
-
62
- return (
63
- f"Current weather in {city_name}:\n"
64
- f"Temperature: {temperature}°C\n"
65
- f"Wind Speed: {windspeed} km/h\n"
66
- f"Weather Code: {weather_code}"
67
- )
68
 
69
  except Exception as e:
70
- return f"An error occurred: {e}"
71
 
72
  @tool
73
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -106,7 +83,7 @@ with open("prompts.yaml", 'r') as stream:
106
 
107
  agent = CodeAgent(
108
  model=model,
109
- tools=[final_answer,get_current_time_in_timezone(),get_weather_forecast() ], ## add your tools here (don't remove final answer)
110
  max_steps=6,
111
  verbosity_level=1,
112
  grammar=None,
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
7
 
8
  from Gradio_UI import GradioUI
9
 
 
19
  return "What magic will you build ?"
20
 
21
  @tool
22
+ def get_random_quote() -> str:
23
+ """Fetches a random quote from the web using only the requests module.
24
+
 
 
 
25
  Returns:
26
+ A string containing the quote and author.
27
  """
28
  try:
29
+ url = "https://quotes.toscrape.com/random"
30
+ response = requests.get(url)
31
+ html = response.text
32
+
33
+ # Very basic string parsing
34
+ quote_start = html.find('<span class="text"') # Locate quote start
35
+ quote_start = html.find('>', quote_start) + 1
36
+ quote_end = html.find('</span>', quote_start)
37
+ quote = html[quote_start:quote_end]
38
+
39
+ author_start = html.find('<small class="author"') # Locate author start
40
+ author_start = html.find('>', author_start) + 1
41
+ author_end = html.find('</small>', author_start)
42
+ author = html[author_start:author_end]
43
+
44
+ return f"{quote} {author}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  except Exception as e:
47
+ return f"Error: {e}"
48
 
49
  @tool
50
  def get_current_time_in_timezone(timezone: str) -> str:
 
83
 
84
  agent = CodeAgent(
85
  model=model,
86
+ tools=[final_answer,get_current_time_in_timezone(),get_random_quote() ], ## add your tools here (don't remove final answer)
87
  max_steps=6,
88
  verbosity_level=1,
89
  grammar=None,