Spaces:
Sleeping
Sleeping
File size: 11,649 Bytes
c4235b1 f42b3da e430de3 6ac4d9e a51f99d 0a92fe3 082b485 6ac4d9e adf2534 2e54146 4d5cc8b 2e54146 adf2534 5e71162 87ba6cc adf2534 6ac4d9e e4f3060 9c1bc5d 9c092bb a8298b7 9c092bb cbbefe9 c8245a1 8e35c72 789eb52 8e35c72 789eb52 8e35c72 c34a50b 1f356e1 082b485 789eb52 082b485 c34a50b 6ac4d9e 082b485 c34a50b bc12e9e e3f907f bc12e9e 00a7c75 e3f907f e0de518 bc12e9e e0de518 bc12e9e 00a7c75 4d5cc8b 00a7c75 082b485 c34a50b 93a20bb 082b485 cbbefe9 082b485 8e35c72 c34a50b 082b485 afa5764 c8245a1 cbbefe9 082b485 cbbefe9 fb1f59e 8e35c72 0a92fe3 8e35c72 c8245a1 082b485 c8245a1 71e7fb5 c8245a1 082b485 8e35c72 c34a50b c8245a1 c34a50b 082b485 c8245a1 082b485 c34a50b 082b485 c8245a1 c34a50b 082b485 cbbefe9 6ac4d9e e0b6992 fb1f59e 5eb6d5e fb1f59e e0b6992 fb1f59e e0b6992 fb1f59e e0b6992 6ac4d9e fb1f59e 6ac4d9e 426bd32 6ac4d9e fb1f59e 6ac4d9e 6cd59f7 c4235b1 6cd59f7 c4235b1 6ac4d9e fb1f59e 6ac4d9e fb1f59e 6ac4d9e c8245a1 fb1f59e 6ac4d9e fb1f59e 6ac4d9e e430de3 126d343 e430de3 6ac4d9e c8245a1 61777fc 3bfe89d |
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool, LiteLLMModel #,
import smolagents # Added for aliasing
# from smolagents.security import E2BSandbox
import datetime
import pytz
import yaml
from skyfield.api import load, Topos, load_file
from skyfield import almanac
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
import os
import base64
# Add the alias before instrumentation
smolagents.ApiModel = smolagents.HfApiModel
# smolagents.ApiModel = smolagents.LiteLLMModel
LANGFUSE_PUBLIC_KEY="pk-lf-133099c7-8644-49e8-8f6e-ec8bd6d543fd"
LF_SECRET_KEY = os.environ["LANGFUSE_SECRET_KEY"]
LANGFUSE_AUTH=base64.b64encode(f"{LANGFUSE_PUBLIC_KEY}:{LF_SECRET_KEY}".encode()).decode()
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://cloud.langfuse.com/api/public/otel" # EU data region
# os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://us.cloud.langfuse.com/api/public/otel" # US data region
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization=Basic {LANGFUSE_AUTH}"
from opentelemetry.sdk.trace import TracerProvider
from openinference.instrumentation.smolagents import SmolagentsInstrumentor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
trace_provider = TracerProvider()
trace_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter()))
SmolagentsInstrumentor().instrument(tracer_provider=trace_provider)
# Load ephemeris and timescale
planets = load('https://naif.jpl.nasa.gov/pub/naif/generic_kernels/spk/planets/de440.bsp')
ts = load.timescale()
# Define Zodiac signs and their boundaries (0° to 360° ecliptic longitude)
ZODIAC_SIGNS = [
("Aries", 0, 30),
("Taurus", 30, 60),
("Gemini", 60, 90),
("Cancer", 90, 120),
("Leo", 120, 150),
("Virgo", 150, 180),
("Libra", 180, 210),
("Scorpio", 210, 240),
("Sagittarius", 240, 270),
("Capricorn", 270, 300),
("Aquarius", 300, 330),
("Pisces", 330, 360),
]
# Moon phase boundaries (0° to 360° phase angle) for display purposes
MOON_PHASES = [
("New Moon", 0, 45),
("Waxing Crescent", 45, 90),
("First Quarter", 90, 135),
("Waxing Gibbous", 135, 180),
("Full Moon", 180, 225),
("Waning Gibbous", 225, 270),
("Last Quarter", 270, 315),
("Waning Crescent", 315, 360),
]
# Fertility sign coefficients (applicable to all plants)
FERTILITY_SIGN_COEFFS = {
"Aries": 1,
"Taurus": 2,
"Gemini": 0,
"Cancer": 2,
"Leo": 1,
"Virgo": 0,
"Libra": 0.5,
"Scorpio": 1.5,
"Sagittarius": 1,
"Capricorn": 1,
"Aquarius": 0,
"Pisces": 2,
}
# Pruning sign coefficients (applicable to all plants)
PRUNING_SIGN_COEFFS = {
"Aries": 1,
"Taurus": 0,
"Gemini": 2,
"Cancer": 0,
"Leo": 1,
"Virgo": 2,
"Libra": 1.5,
"Scorpio": 0.5,
"Sagittarius": 1,
"Capricorn": 1,
"Aquarius": 2,
"Pisces": 0,
}
# Fertility phase coefficients for above-ground plants
FERTILITY_PHASE_COEFFS_ABOVE = {
"New Moon": 0,
"Waxing Moon": 1,
"Full Moon": 0,
"Waning Moon": 0.5,
}
# Fertility phase coefficients for root crops
FERTILITY_PHASE_COEFFS_ROOT = {
"New Moon": 0,
"Waxing Moon": 0.5,
"Full Moon": 0,
"Waning Moon": 1,
}
# Pruning phase coefficients
PRUNING_PHASE_COEFFS = {
"New Moon": 0,
"Waxing Moon": 1,
"Full Moon": 0,
"Waning Moon": 0.5,
}
@tool
def get_moon_info(date_time: str) -> dict:
"""
Returns Moon's Zodiac position, phase, and fertility and pruning indices for the given date/time.
The fertility and pruning indices are calculated as sum of sign and phase fertility values of the Moon position. Moon sign fertility
amounts up to 2.0 value and phase fertility value could be 1.0 max.
It is observed that when Moon is in different Zodiac signs, the fertility of new plants and impact of pruning differs.
When Moon is in fertile sign the plant is in the active phase, when all processes are particularly intense, and any intervention
such as pruning can be very traumatic for the plant. Here:
Most fertile signs: Taurus, Pisces, Cancer - Plants are in the active growth phase, juices and nutrients actively circulate
in the plant, and it is best time for fertilizers, harvasting cutting, vaccination, rooting.
Conditionally fertile: Scorpio
Neutral: Aries, Leo, Sagittarius, Capricorn
Conditionally sterile: Libra
Sterile: Gemini, Virgo, Aquarius
Fertility indices ranges from 0.0 to 3.0 where proportionaly
0 - minimal expected fertility
3.0 - most favorable fertility for platining,
and depends on type of plant (root crop or produce above ground).
Pruning indices ranges from 0 to 3 where proportionaly:
0 - pruning is not recommended as it causes most damage to tree and can lead to:
Increased sap production from the cut points
Increased vulnerability to infections
Delayed wound healing
Possible weakening of the plant.
Instead of pruning into fertile signs, you can do:
Crown formation
Pinching the shoots
Removing dead branches
Sanitary treatment
1.0 - pruning is not recommended,
2.0 - allowed only minimum or sanitary pruning,
3.0 - most favorable time for pruning.
Args:
date_time (str): ISO 8601 formatted datetime (YYYY-MM-DDTHH:MM:SS)
Returns:
dict: {
"zodiac_position": "Leo 15°30'",
"moon_phase": "Waxing Gibbous",
"fertility_above_ground": 2.0,
"fertility_root_crop": 1.5,
"pruning": 2.0
}
"""
try:
# Parse input datetime and localize to UTC
user_time = datetime.datetime.strptime(date_time, "%Y-%m-%dT%H:%M:%S")
user_time = pytz.utc.localize(user_time)
# Use loaded ephemeris and timescale
t = ts.from_datetime(user_time)
# Define celestial bodies
earth = planets['earth']
moon = planets['moon']
sun = planets['sun']
# Calculate Moon's ecliptic longitude
astrometric = earth.at(t).observe(moon)
ecliptic_lat, ecliptic_lon, distance = astrometric.ecliptic_latlon()
lon_deg = ecliptic_lon.degrees % 360
# Calculate the phase angle using almanac.moon_phase
phase = almanac.moon_phase(planets, t)
phase_angle = phase.degrees
# Determine Zodiac sign and position
zodiac_sign = "Unknown"
position_degrees = 0
for sign, start, end in ZODIAC_SIGNS:
if start <= lon_deg < end:
zodiac_sign = sign
position_degrees = lon_deg - start
break
# Format position to degrees and minutes
degrees = int(position_degrees)
minutes = int((position_degrees % 1) * 60)
position_str = f"{zodiac_sign} {degrees}°{minutes:02}'"
# Determine moon phase for display
moon_phase = "Unknown"
for phase, start, end in MOON_PHASES:
if start <= phase_angle < end:
moon_phase = phase
break
# Determine phase category for indices with 15° orbis for New and Full Moon
if (phase_angle >= 345 or phase_angle < 15):
phase_category = "New Moon" # 345° to 15° (30° total orbis)
elif 15 <= phase_angle < 165:
phase_category = "Waxing Moon"
elif 165 <= phase_angle < 195:
phase_category = "Full Moon" # 165° to 195° (30° total orbis)
elif 195 <= phase_angle < 345:
phase_category = "Waning Moon"
else:
phase_category = "Unknown"
# Calculate fertility and pruning indices
if zodiac_sign in FERTILITY_SIGN_COEFFS and phase_category in FERTILITY_PHASE_COEFFS_ABOVE:
fertility_above_ground = FERTILITY_SIGN_COEFFS[zodiac_sign] + FERTILITY_PHASE_COEFFS_ABOVE[phase_category]
fertility_root_crop = FERTILITY_SIGN_COEFFS[zodiac_sign] + FERTILITY_PHASE_COEFFS_ROOT[phase_category]
pruning = PRUNING_SIGN_COEFFS[zodiac_sign] + PRUNING_PHASE_COEFFS[phase_category]
else:
fertility_above_ground = None
fertility_root_crop = None
pruning = None
return {
"zodiac_position": position_str,
"moon_phase": moon_phase,
"fertility_above_ground": fertility_above_ground,
"fertility_root_crop": fertility_root_crop,
"pruning": pruning
}
except Exception as e:
raise ValueError(f"Error in get_moon_info: {str(e)}")
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""
Returns the current local time in the specified timezone with description.
Args:
timezone (str): A string representing a valid timezone (e.g., 'UTC')
Returns:
str: Formatted local time with timezone description
"""
try:
tz = pytz.timezone(timezone)
now = datetime.datetime.now(tz)
return f"Local time in {timezone}: {now.strftime('%Y-%m-%d %H:%M:%S')}"
except Exception as e:
return f"Error: {str(e)}"
@tool
def get_current_time_raw(timezone: str) -> str:
"""
Returns current local time in specified timezone as ISO 8601 string.
Args:
timezone (str): A string representing a valid timezone (e.g., 'UTC')
Returns:
str: Datetime in ISO 8601 format (YYYY-MM-DDTHH:MM:SS)
"""
try:
tz = pytz.timezone(timezone)
now = datetime.datetime.now(tz)
return now.strftime("%Y-%m-%dT%H:%M:%S")
except Exception as e:
return f"Error: {str(e)}"
# Model configuration
final_answer = FinalAnswerTool()
# model = HfApiModel(
# max_tokens=2096,
# temperature=0.5,
# model_id="gemini/gemini-2.0-flash-lite-001",
# api_key = os.environ["GEMINI_KEY"], #userdata.get("GEMINI_KEY")
# model_id="https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/",
# custom_role_conversions=None,
# )
model = LiteLLMModel(
model_id="gemini/gemini-2.0-flash",
api_key=os.getenv("GEMINI_KEY"),
max_tokens=8192,
temperature=0.7
)
# Load image tool from Hub
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
# Load prompt templates
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
# Initialize agent with all tools
agent = CodeAgent(
model=model,
tools=[final_answer, get_moon_info, get_current_time_in_timezone, get_current_time_raw, image_generation_tool],
max_steps=6,
verbosity_level=1,
prompt_templates=prompt_templates
# execution_env=E2BSandbox(
# allowed_imports=["numpy", "pandas"], # Explicitly permitted packages
# blocked_imports=["subprocess"], # Prevent system access
# ),
# safe_mode=True, # Enable safe code execution
# timeout=10, # Seconds before execution timeout
# max_memory=512, # MB memory limit
# file_system_access=False, # Disable disk write access
# network_access=False, # Block network operations
# max_code_iterations=100, # Prevent infinite loops
)
if __name__ == "__main__":
GradioUI(agent).launch()
# Change to your username and repo name
# agent.push_to_hub('sergeyo7/Garden_Magus') |