Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -34,38 +34,34 @@ MOON_PHASES = [
|
|
34 |
("Waning Crescent", 315, 360),
|
35 |
]
|
36 |
|
37 |
-
|
38 |
@tool
|
39 |
def get_moon_info(date_time: str) -> dict:
|
40 |
"""
|
41 |
Returns Moon's Zodiac position with degrees/minutes and phase for given date/time.
|
42 |
|
43 |
Args:
|
44 |
-
date_time (str): ISO 8601
|
45 |
|
46 |
Returns:
|
47 |
dict: {"zodiac_position": "Leo 15°30'",
|
48 |
"moon_phase": "Waxing Gibbous"}
|
49 |
"""
|
50 |
try:
|
51 |
-
# Parse input time
|
52 |
user_time = datetime.datetime.strptime(date_time, "%Y-%m-%dT%H:%M:%S")
|
53 |
user_time = pytz.utc.localize(user_time)
|
54 |
|
55 |
-
# Load astronomical data
|
56 |
planets = load('de421.bsp')
|
57 |
ts = load.timescale()
|
58 |
t = ts.from_datetime(user_time)
|
59 |
|
60 |
-
# Get celestial bodies
|
61 |
earth = planets['earth']
|
62 |
moon = planets['moon']
|
63 |
sun = planets['sun']
|
64 |
|
65 |
-
# Calculate
|
66 |
astrometric = earth.at(t).observe(moon)
|
67 |
-
_, _, ecliptic_lon = astrometric.ecliptic_latlon()
|
68 |
-
lon_deg = ecliptic_lon.degrees % 360
|
69 |
|
70 |
# Calculate phase angle
|
71 |
phase_angle = earth.at(t).observe(moon).apparent().phase_angle(sun).degrees
|
@@ -79,7 +75,7 @@ def get_moon_info(date_time: str) -> dict:
|
|
79 |
position_degrees = lon_deg - start
|
80 |
break
|
81 |
|
82 |
-
#
|
83 |
degrees = int(position_degrees)
|
84 |
minutes = int((position_degrees % 1) * 60)
|
85 |
position_str = f"{zodiac_sign} {degrees}°{minutes:02}'"
|
@@ -99,26 +95,43 @@ def get_moon_info(date_time: str) -> dict:
|
|
99 |
except Exception as e:
|
100 |
return {"error": f"Error: {str(e)}"}
|
101 |
|
102 |
-
# Existing timezone tool remains unchanged
|
103 |
@tool
|
104 |
def get_current_time_in_timezone(timezone: str) -> str:
|
105 |
"""
|
106 |
-
Returns the current local time in the specified timezone.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
Args:
|
109 |
-
timezone (str): A string representing a valid timezone (e.g., '
|
110 |
|
111 |
Returns:
|
112 |
-
str:
|
113 |
"""
|
114 |
try:
|
115 |
tz = pytz.timezone(timezone)
|
116 |
-
|
117 |
-
return
|
118 |
except Exception as e:
|
119 |
return f"Error: {str(e)}"
|
120 |
|
121 |
-
# Model configuration
|
122 |
final_answer = FinalAnswerTool()
|
123 |
model = HfApiModel(
|
124 |
max_tokens=2096,
|
@@ -127,15 +140,17 @@ model = HfApiModel(
|
|
127 |
custom_role_conversions=None,
|
128 |
)
|
129 |
|
130 |
-
#
|
131 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
132 |
|
|
|
133 |
with open("prompts.yaml", 'r') as stream:
|
134 |
prompt_templates = yaml.safe_load(stream)
|
135 |
|
|
|
136 |
agent = CodeAgent(
|
137 |
model=model,
|
138 |
-
tools=[final_answer, get_moon_info, get_current_time_in_timezone, image_generation_tool],
|
139 |
max_steps=6,
|
140 |
verbosity_level=1,
|
141 |
prompt_templates=prompt_templates
|
|
|
34 |
("Waning Crescent", 315, 360),
|
35 |
]
|
36 |
|
|
|
37 |
@tool
|
38 |
def get_moon_info(date_time: str) -> dict:
|
39 |
"""
|
40 |
Returns Moon's Zodiac position with degrees/minutes and phase for given date/time.
|
41 |
|
42 |
Args:
|
43 |
+
date_time (str): ISO 8601 formatted datetime (YYYY-MM-DDTHH:MM:SS)
|
44 |
|
45 |
Returns:
|
46 |
dict: {"zodiac_position": "Leo 15°30'",
|
47 |
"moon_phase": "Waxing Gibbous"}
|
48 |
"""
|
49 |
try:
|
|
|
50 |
user_time = datetime.datetime.strptime(date_time, "%Y-%m-%dT%H:%M:%S")
|
51 |
user_time = pytz.utc.localize(user_time)
|
52 |
|
|
|
53 |
planets = load('de421.bsp')
|
54 |
ts = load.timescale()
|
55 |
t = ts.from_datetime(user_time)
|
56 |
|
|
|
57 |
earth = planets['earth']
|
58 |
moon = planets['moon']
|
59 |
sun = planets['sun']
|
60 |
|
61 |
+
# Calculate ecliptic longitude for geocentric position
|
62 |
astrometric = earth.at(t).observe(moon)
|
63 |
+
_, _, ecliptic_lon = astrometric.ecliptic_latlon()
|
64 |
+
lon_deg = ecliptic_lon.degrees % 360
|
65 |
|
66 |
# Calculate phase angle
|
67 |
phase_angle = earth.at(t).observe(moon).apparent().phase_angle(sun).degrees
|
|
|
75 |
position_degrees = lon_deg - start
|
76 |
break
|
77 |
|
78 |
+
# Format position to degrees/minutes
|
79 |
degrees = int(position_degrees)
|
80 |
minutes = int((position_degrees % 1) * 60)
|
81 |
position_str = f"{zodiac_sign} {degrees}°{minutes:02}'"
|
|
|
95 |
except Exception as e:
|
96 |
return {"error": f"Error: {str(e)}"}
|
97 |
|
|
|
98 |
@tool
|
99 |
def get_current_time_in_timezone(timezone: str) -> str:
|
100 |
"""
|
101 |
+
Returns the current local time in the specified timezone with description.
|
102 |
+
|
103 |
+
Args:
|
104 |
+
timezone (str): A string representing a valid timezone (e.g., 'UTC')
|
105 |
+
|
106 |
+
Returns:
|
107 |
+
str: Formatted local time with timezone description
|
108 |
+
"""
|
109 |
+
try:
|
110 |
+
tz = pytz.timezone(timezone)
|
111 |
+
now = datetime.datetime.now(tz)
|
112 |
+
return f"Local time in {timezone}: {now.strftime('%Y-%m-%d %H:%M:%S')}"
|
113 |
+
except Exception as e:
|
114 |
+
return f"Error: {str(e)}"
|
115 |
+
|
116 |
+
@tool
|
117 |
+
def get_current_time_raw(timezone: str) -> str:
|
118 |
+
"""
|
119 |
+
Returns current local time in specified timezone as ISO 8601 string.
|
120 |
|
121 |
Args:
|
122 |
+
timezone (str): A string representing a valid timezone (e.g., 'UTC')
|
123 |
|
124 |
Returns:
|
125 |
+
str: Datetime in ISO 8601 format (YYYY-MM-DDTHH:MM:SS)
|
126 |
"""
|
127 |
try:
|
128 |
tz = pytz.timezone(timezone)
|
129 |
+
now = datetime.datetime.now(tz)
|
130 |
+
return now.strftime("%Y-%m-%dT%H:%M:%S")
|
131 |
except Exception as e:
|
132 |
return f"Error: {str(e)}"
|
133 |
|
134 |
+
# Model configuration
|
135 |
final_answer = FinalAnswerTool()
|
136 |
model = HfApiModel(
|
137 |
max_tokens=2096,
|
|
|
140 |
custom_role_conversions=None,
|
141 |
)
|
142 |
|
143 |
+
# Load image tool from Hub
|
144 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
145 |
|
146 |
+
# Load prompt templates
|
147 |
with open("prompts.yaml", 'r') as stream:
|
148 |
prompt_templates = yaml.safe_load(stream)
|
149 |
|
150 |
+
# Initialize agent with all tools
|
151 |
agent = CodeAgent(
|
152 |
model=model,
|
153 |
+
tools=[final_answer, get_moon_info, get_current_time_in_timezone, get_current_time_raw, image_generation_tool],
|
154 |
max_steps=6,
|
155 |
verbosity_level=1,
|
156 |
prompt_templates=prompt_templates
|