Spaces:
Sleeping
Sleeping
| from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool | |
| import datetime | |
| import requests | |
| import pytz | |
| import yaml | |
| from tools.final_answer import FinalAnswerTool | |
| from datetime import timedelta | |
| import math | |
| from typing import Dict | |
| from Gradio_UI import GradioUI | |
| # Below is an example of a tool that does nothing. Amaze us with your creativity ! | |
| def estimate_sun_times(date: datetime, lat: float, lon: float) -> Dict[str, str]: | |
| """ | |
| Estimate approximate sunrise and sunset times in UTC for a given date and location (no external API). | |
| This tool uses a simplified version of the NOAA solar position algorithm to compute sun times | |
| with acceptable accuracy (~1–5 minutes). It's fully offline and suitable for agent environments. | |
| Args: | |
| date: The date for which to compute sunrise and sunset. Time part is ignored. | |
| lat: Latitude in decimal degrees. North is positive, south is negative. | |
| lon: Longitude in decimal degrees. East is positive, west is negative. | |
| Returns: | |
| Dict[str, str]: A dictionary containing, "sunrise_utc": Sunrise time in UTC (ISO format: "HH:MM"), "sunset_utc": Sunset time in UTC (ISO format: "HH:MM"), "sunrise_iso": Full ISO 8601 datetime string (UTC), "sunset_iso": Full ISO 8601 datetime string (UTC) | |
| """ | |
| def calculate_julian_day(dt: datetime) -> float: | |
| year, month, day = dt.year, dt.month, dt.day | |
| if month <= 2: | |
| year -= 1 | |
| month += 12 | |
| A = math.floor(year / 100) | |
| B = 2 - A + math.floor(A / 4) | |
| jd = math.floor(365.25 * (year + 4716)) + \ | |
| math.floor(30.6001 * (month + 1)) + \ | |
| day + B - 1524.5 | |
| return jd | |
| def sun_mean_anomaly(t: float) -> float: | |
| return (357.52911 + t * (35999.05029 - 0.0001537 * t)) % 360 | |
| def sun_equation_of_center(m: float) -> float: | |
| m_rad = math.radians(m) | |
| return (1.914602 - 0.004817 - 0.000014) * math.sin(m_rad) + \ | |
| (0.019993 - 0.000101) * math.sin(2 * m_rad) + \ | |
| 0.000289 * math.sin(3 * m_rad) | |
| def ecliptic_longitude(m: float, c: float) -> float: | |
| return (m + c + 180 + 102.9372) % 360 | |
| def declination_of_sun(l: float) -> float: | |
| return math.degrees(math.asin(math.sin(math.radians(l)) * math.sin(math.radians(23.44)))) | |
| def solar_transit(jd: float, m: float, l: float) -> float: | |
| return jd + (0.0053 * math.sin(math.radians(m))) - (0.0069 * math.sin(math.radians(2 * l))) | |
| def hour_angle(lat: float, decl: float) -> float: | |
| lat_rad = math.radians(lat) | |
| decl_rad = math.radians(decl) | |
| ha = math.acos((math.cos(math.radians(90.833)) / | |
| (math.cos(lat_rad) * math.cos(decl_rad))) - | |
| math.tan(lat_rad) * math.tan(decl_rad)) | |
| return math.degrees(ha) | |
| def jd_to_datetime(jd: float) -> datetime: | |
| days = jd - 2440587.5 | |
| seconds = days * 86400.0 | |
| return datetime.utcfromtimestamp(seconds) | |
| # Step 1: Julian day | |
| jd = calculate_julian_day(date) | |
| lng_hour = lon / 15 | |
| # Step 2: Approximate solar noon Julian century | |
| t = (jd - 2451545.0 + lng_hour / 24) / 36525 | |
| m = sun_mean_anomaly(t) | |
| c = sun_equation_of_center(m) | |
| l = ecliptic_longitude(m, c) | |
| dec = declination_of_sun(l) | |
| ha = hour_angle(lat, dec) | |
| delta = ha / 360 | |
| # Step 3: Solar transit and sunrise/sunset times | |
| solar_transit_jd = solar_transit(jd, m, l) | |
| sunrise_jd = solar_transit_jd - delta | |
| sunset_jd = solar_transit_jd + delta | |
| sunrise_dt = jd_to_datetime(sunrise_jd) | |
| sunset_dt = jd_to_datetime(sunset_jd) | |
| return { | |
| "sunrise_utc": sunrise_dt.strftime("%H:%M"), | |
| "sunset_utc": sunset_dt.strftime("%H:%M"), | |
| "sunrise_iso": sunrise_dt.strftime("%Y-%m-%dT%H:%M:%SZ"), | |
| "sunset_iso": sunset_dt.strftime("%Y-%m-%dT%H:%M:%SZ") | |
| } | |
| def get_current_time_in_timezone(timezone: str) -> str: | |
| """A tool that fetches the current local time in a specified timezone. | |
| Args: | |
| timezone: A string representing a valid timezone (e.g., 'America/New_York'). | |
| """ | |
| try: | |
| # Create timezone object | |
| tz = pytz.timezone(timezone) | |
| # Get current time in that timezone | |
| local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S") | |
| return f"The current local time in {timezone} is: {local_time}" | |
| except Exception as e: | |
| return f"Error fetching time for timezone '{timezone}': {str(e)}" | |
| final_answer = FinalAnswerTool() | |
| # 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: | |
| # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' | |
| model = HfApiModel( | |
| max_tokens=2096, | |
| temperature=0.5, | |
| model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded | |
| custom_role_conversions=None, | |
| ) | |
| # Import tool from Hub | |
| # image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) | |
| with open("prompts.yaml", 'r') as stream: | |
| prompt_templates = yaml.safe_load(stream) | |
| agent = CodeAgent( | |
| model=model, | |
| tools=[final_answer, estimate_sun_times, get_current_time_in_timezone], ## add your tools here (don't remove final answer) | |
| max_steps=6, | |
| verbosity_level=1, | |
| grammar=None, | |
| planning_interval=None, | |
| name=None, | |
| description=None, | |
| prompt_templates=prompt_templates, | |
| add_base_tools=True | |
| ) | |
| GradioUI(agent).launch() |