brufino commited on
Commit
d5bdfb5
·
verified ·
1 Parent(s): 80113de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -8,6 +8,7 @@ from tools.final_answer import FinalAnswerTool
8
  from Gradio_UI import GradioUI
9
 
10
  import requests
 
11
 
12
  @tool
13
  def get_location() -> str:
@@ -25,37 +26,35 @@ def get_location() -> str:
25
  return f"Error retrieving location: {str(e)}"
26
 
27
  @tool
28
- def get_weather(location: str) -> str:
29
- """Gets the current weather for a given location.
30
 
31
  Args:
32
- location: The latitude and longitude or city name.
33
 
34
  Returns:
35
  A string containing the current weather conditions for the specified location.
36
  """
37
  try:
38
- api_key = "your_openweathermap_api_key" # Replace with your OpenWeatherMap API key
39
- base_url = "https://api.openweathermap.org/data/2.5/weather"
 
40
 
41
- params = {
42
- "q": location, # You can also use lat, lon instead of city name
43
- "appid": api_key,
44
- "units": "metric" # Use "imperial" for Fahrenheit
45
  }
46
 
47
- response = requests.get(base_url, params=params)
48
- weather_data = response.json()
49
-
50
- if weather_data.get("cod") != 200:
51
- return f"Weather data not found for {location}."
52
-
53
- temp = weather_data["main"]["temp"]
54
- description = weather_data["weather"][0]["description"]
55
- return f"The current weather in {location} is {temp}°C with {description}."
56
 
57
  except Exception as e:
58
- return f"Error retrieving weather data: {str(e)}"
59
 
60
  @tool
61
  def get_current_time_in_timezone(timezone: str) -> str:
 
8
  from Gradio_UI import GradioUI
9
 
10
  import requests
11
+ from bs4 import BeautifulSoup
12
 
13
  @tool
14
  def get_location() -> str:
 
26
  return f"Error retrieving location: {str(e)}"
27
 
28
  @tool
29
+ def get_weather_no_api(location: str) -> str:
30
+ """Gets the current weather for a given location without using an API key.
31
 
32
  Args:
33
+ location: The city name or location.
34
 
35
  Returns:
36
  A string containing the current weather conditions for the specified location.
37
  """
38
  try:
39
+ # Format the location for the search query
40
+ search_query = location.replace(" ", "+")
41
+ url = f"https://www.google.com/search?q=weather+{search_query}"
42
 
43
+ headers = {
44
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
 
 
45
  }
46
 
47
+ response = requests.get(url, headers=headers)
48
+ soup = BeautifulSoup(response.text, "html.parser")
49
+
50
+ # Extract weather information
51
+ temp = soup.find("span", class_="wob_t").text
52
+ condition = soup.find("div", class_="wob_dcp").text
53
+
54
+ return f"The current weather in {location} is {temp}°C with {condition}."
 
55
 
56
  except Exception as e:
57
+ return f"Could not retrieve weather for {location}. Error: {str(e)}"
58
 
59
  @tool
60
  def get_current_time_in_timezone(timezone: str) -> str: