File size: 5,565 Bytes
9b5b26a
 
 
 
c19d193
6aae614
d68605f
 
 
8fe992b
9b5b26a
 
5df72d6
9b5b26a
6a150ad
 
 
 
 
 
 
9b5b26a
eb9fb54
 
 
6a150ad
 
eb9fb54
9b5b26a
6a150ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b5b26a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c01ffb
 
6aae614
ae7a494
 
 
 
e121372
bf6d34c
 
29ec968
fe328e0
13d500a
8c01ffb
 
9b5b26a
6a150ad
8c01ffb
861422e
 
9b5b26a
8c01ffb
8fe992b
6a150ad
8c01ffb
 
 
 
 
 
6a150ad
 
8fe992b
 
9b5b26a
8c01ffb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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 !
@tool
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")
    }


@tool
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()