Spaces:
Sleeping
Sleeping
Updated recommendation tool for app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,59 @@
|
|
| 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_current_weather(location: str
|
| 13 |
"""
|
| 14 |
-
Returns the weather report.
|
| 15 |
-
|
| 16 |
Args:
|
| 17 |
location: the name of the place that you want the weather for. Should be a place name, followed by possibly a city name, then a country, like "Anchor Point, Taghazout, Morocco".
|
| 18 |
-
date_time: the date and time for which you want the report, formatted as '%m/%d/%y %H:%M:%S'.
|
| 19 |
"""
|
| 20 |
try:
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
except Exception as e:
|
| 23 |
-
return f"Failed to
|
| 24 |
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
try:
|
| 31 |
-
temperature_celsius, risk_of_rain, wave_height = get_weather_report_at_coordinates((lon, lat), date_time_obj)
|
| 32 |
-
return (f"Weather report for {location} on {date_time}: "
|
| 33 |
-
f"Temperature: {temperature_celsius}°C, "
|
| 34 |
-
f"Risk of rain: {risk_of_rain * 100:.0f}%, "
|
| 35 |
-
f"Wave height: {wave_height}m.")
|
| 36 |
except Exception as e:
|
| 37 |
return f"Failed to fetch weather data for {location}. Error: {str(e)}"
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
@tool
|
| 40 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 41 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -52,6 +70,67 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 52 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 53 |
|
| 54 |
@tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def recommend_activities(temperature: float, risk_of_rain: float, time_of_day: str) -> str:
|
| 56 |
"""
|
| 57 |
Suggests a friendly activity recommendation based on temperature, risk of rain, and time of day.
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
|
|
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
from tools.final_answer import FinalAnswerTool
|
| 6 |
+
import re
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
|
|
| 10 |
@tool
|
| 11 |
+
def get_current_weather(location: str) -> str:
|
| 12 |
"""
|
| 13 |
+
Returns the weather report by querying Google Search using DuckDuckGo.
|
|
|
|
| 14 |
Args:
|
| 15 |
location: the name of the place that you want the weather for. Should be a place name, followed by possibly a city name, then a country, like "Anchor Point, Taghazout, Morocco".
|
|
|
|
| 16 |
"""
|
| 17 |
try:
|
| 18 |
+
# Get the current date and time from the machine
|
| 19 |
+
date_time_obj = datetime.datetime.now()
|
| 20 |
+
|
| 21 |
+
# Convert the current datetime object to the desired string format
|
| 22 |
+
date_time_str = date_time_obj.strftime("%m/%d/%y %H:%M:%S")
|
| 23 |
+
|
| 24 |
except Exception as e:
|
| 25 |
+
return f"Failed to get current date and time. Error: {str(e)}"
|
| 26 |
|
| 27 |
+
# Use DuckDuckGo to search for weather information for the specified location
|
| 28 |
+
search_query = f"weather in {location}"
|
| 29 |
try:
|
| 30 |
+
search_results = DuckDuckGoSearchTool().search(search_query)
|
| 31 |
+
|
| 32 |
+
# Extract weather details using regex (simplified for general use)
|
| 33 |
+
weather_info = extract_weather_from_search_results(search_results)
|
| 34 |
+
|
| 35 |
+
if weather_info:
|
| 36 |
+
return f"Weather for {location} on {date_time_str}: {weather_info}"
|
| 37 |
+
else:
|
| 38 |
+
return f"Could not extract weather information for {location} from search results."
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
return f"Failed to fetch weather data for {location}. Error: {str(e)}"
|
| 42 |
|
| 43 |
+
def extract_weather_from_search_results(search_results: str) -> str:
|
| 44 |
+
"""
|
| 45 |
+
Extracts weather information from the DuckDuckGo search results.
|
| 46 |
+
"""
|
| 47 |
+
# A simple regex to extract temperature and weather condition (this can be more complex depending on the result structure)
|
| 48 |
+
temperature_match = re.search(r'(\d+)\s?°C', search_results)
|
| 49 |
+
condition_match = re.search(r"(clear|cloudy|rainy|sunny|stormy)", search_results, re.IGNORECASE)
|
| 50 |
+
|
| 51 |
+
if temperature_match and condition_match:
|
| 52 |
+
temperature = temperature_match.group(1)
|
| 53 |
+
condition = condition_match.group(1)
|
| 54 |
+
return f"Temperature: {temperature}°C, Condition: {condition.capitalize()}"
|
| 55 |
+
return None
|
| 56 |
+
|
| 57 |
@tool
|
| 58 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 59 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 70 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 71 |
|
| 72 |
@tool
|
| 73 |
+
def recommend_activities_auto(location: str = "Your City, Your Country", timezone: str = "America/New_York") -> str:
|
| 74 |
+
"""
|
| 75 |
+
Suggests an activity based on the current weather and time without requiring the user to specify them manually.
|
| 76 |
+
|
| 77 |
+
Args:
|
| 78 |
+
location: The name of the location to fetch weather for. Defaults to "Your City, Your Country".
|
| 79 |
+
timezone: The timezone for fetching local time. Defaults to "America/New_York".
|
| 80 |
+
|
| 81 |
+
Returns:
|
| 82 |
+
A friendly activity suggestion based on the current conditions.
|
| 83 |
+
"""
|
| 84 |
+
# Get the current weather using the location
|
| 85 |
+
weather_report = get_current_weather(location)
|
| 86 |
+
if "Temperature" not in weather_report: # Check if weather data was successfully retrieved
|
| 87 |
+
return "Sorry, I couldn't retrieve the weather data right now."
|
| 88 |
+
|
| 89 |
+
# Extract temperature and weather condition from the report
|
| 90 |
+
temperature_match = re.search(r"Temperature: (\d+)°C", weather_report)
|
| 91 |
+
condition_match = re.search(r"Condition: (\w+)", weather_report)
|
| 92 |
+
|
| 93 |
+
if temperature_match and condition_match:
|
| 94 |
+
temperature = float(temperature_match.group(1))
|
| 95 |
+
condition = condition_match.group(1).lower()
|
| 96 |
+
else:
|
| 97 |
+
return "Sorry, there was an issue extracting weather details."
|
| 98 |
+
|
| 99 |
+
# Get the current time using the timezone
|
| 100 |
+
time_report = get_current_time_in_timezone(timezone)
|
| 101 |
+
if "Error" in time_report: # Check if time data was successfully retrieved
|
| 102 |
+
return "Sorry, I couldn't retrieve the current time."
|
| 103 |
+
|
| 104 |
+
# Extract the time of day from the time report
|
| 105 |
+
time_of_day_match = re.search(r"current local time in [\w/]+ is: (\d+:\d+:\d+)", time_report)
|
| 106 |
+
if time_of_day_match:
|
| 107 |
+
time_of_day = get_time_of_day() # This function can extract time of day based on the time
|
| 108 |
+
else:
|
| 109 |
+
return "Sorry, there was an issue extracting the time of day."
|
| 110 |
+
|
| 111 |
+
# Now, use the current weather and time to recommend activities
|
| 112 |
+
risk_of_rain = 1 if condition in ["rainy", "stormy"] else 0
|
| 113 |
+
activity_suggestion = recommend_activities(temperature, risk_of_rain, time_of_day)
|
| 114 |
+
|
| 115 |
+
return activity_suggestion
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def get_time_of_day() -> str:
|
| 119 |
+
"""
|
| 120 |
+
Helper function to determine the time of day based on the current time.
|
| 121 |
+
Returns one of "morning", "afternoon", "evening", or "night".
|
| 122 |
+
"""
|
| 123 |
+
current_time = datetime.datetime.now()
|
| 124 |
+
if 5 <= current_time.hour < 12:
|
| 125 |
+
return "morning"
|
| 126 |
+
elif 12 <= current_time.hour < 17:
|
| 127 |
+
return "afternoon"
|
| 128 |
+
elif 17 <= current_time.hour < 21:
|
| 129 |
+
return "evening"
|
| 130 |
+
else:
|
| 131 |
+
return "night"
|
| 132 |
+
|
| 133 |
+
|
| 134 |
def recommend_activities(temperature: float, risk_of_rain: float, time_of_day: str) -> str:
|
| 135 |
"""
|
| 136 |
Suggests a friendly activity recommendation based on temperature, risk of rain, and time of day.
|