elanuk commited on
Commit
df7fc24
·
verified ·
1 Parent(s): e9dace8

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +36 -8
server.py CHANGED
@@ -295,22 +295,50 @@ async def generate_weather_based_alert(weather_data: dict, crop: str, crop_stage
295
 
296
  async def generate_ai_alert(latitude: float, longitude: float, crop: str,
297
  crop_stage: str, village: str, district: str) -> Optional[dict]:
298
- """Generate AI-powered weather alert using OpenAI"""
299
  if not openai_key:
300
  logger.warning("No OpenAI API key - skipping AI alert generation")
301
  return None
302
 
303
  try:
304
- ai_alert = await alert_generation_tools.predict_weather_alert(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
  latitude=latitude,
306
- longitude=longitude,
307
- api_key=openai_key
308
  )
309
 
310
- # Extract and enhance AI response
311
- alert_description = ai_alert.get('alert', 'Weather update for agricultural activities')
312
- impact_description = ai_alert.get('impact', 'Monitor crops regularly')
313
- recommendations = ai_alert.get('recommendations', 'Continue routine farming activities')
 
 
 
 
 
 
314
 
315
  # Create enhanced alert message
316
  alert_message = f"🤖 AI Weather Alert for {village}, {district}: {alert_description}"
 
295
 
296
  async def generate_ai_alert(latitude: float, longitude: float, crop: str,
297
  crop_stage: str, village: str, district: str) -> Optional[dict]:
298
+ """Generate AI-powered weather alert using available tools"""
299
  if not openai_key:
300
  logger.warning("No OpenAI API key - skipping AI alert generation")
301
  return None
302
 
303
  try:
304
+ # First get weather data for the AI context
305
+ current_weather_data = await open_meteo.get_current_weather(
306
+ latitude=latitude, longitude=longitude
307
+ )
308
+ forecast_data = await open_meteo.get_weather_forecast(
309
+ latitude=latitude, longitude=longitude, days=7
310
+ )
311
+
312
+ # Prepare weather context for AI
313
+ current_weather = current_weather_data.get('current_weather', {})
314
+ daily_forecast = forecast_data.get('daily', {})
315
+
316
+ weather_context = {
317
+ 'temperature': current_weather.get('temperature', 25),
318
+ 'windspeed': current_weather.get('windspeed', 10),
319
+ 'precipitation_forecast': daily_forecast.get('precipitation_sum', [0, 0, 0])[:3]
320
+ }
321
+
322
+ # Use the generate_weather_alert function from your tools
323
+ ai_alert = await alert_generation_tools.generate_weather_alert(
324
+ crop=crop,
325
+ weather_data=weather_context,
326
+ growth_stage=crop_stage,
327
+ api_key=openai_key,
328
  latitude=latitude,
329
+ longitude=longitude
 
330
  )
331
 
332
+ # Extract AI response (adjust based on your actual function return format)
333
+ if isinstance(ai_alert, dict):
334
+ alert_description = ai_alert.get('alert', 'Weather update for agricultural activities')
335
+ impact_description = ai_alert.get('impact', 'Monitor crops regularly')
336
+ recommendations = ai_alert.get('recommendations', 'Continue routine farming activities')
337
+ else:
338
+ # If it returns a string or other format
339
+ alert_description = str(ai_alert)
340
+ impact_description = 'Monitor crops regularly'
341
+ recommendations = 'Follow weather updates and maintain good practices'
342
 
343
  # Create enhanced alert message
344
  alert_message = f"🤖 AI Weather Alert for {village}, {district}: {alert_description}"