Aniket2006 commited on
Commit
15baaaf
·
1 Parent(s): f11af9a

Fix: extract Sentinel-2 vegetation_indices_summary and SAR crop_health

Browse files
Files changed (2) hide show
  1. app.py +11 -10
  2. context_aggregator.py +10 -9
app.py CHANGED
@@ -317,18 +317,19 @@ def build_llm_prompt(query: str, context: Dict, history: List[Dict] = None) -> s
317
  vi_lines.append(f"- {name}: {mean:.4f}{change_str}" if isinstance(mean, float) else f"- {name}: {mean}")
318
  context_parts.append("\n".join(vi_lines))
319
 
320
- # SAR bands
321
  if context.get("sar_bands"):
322
  sar = context["sar_bands"]
323
- vv = sar.get('VV')
324
- vh = sar.get('VH')
325
- vv_val = vv.get('mean', vv) if isinstance(vv, dict) else (vv if vv else 'N/A')
326
- vh_val = vh.get('mean', vh) if isinstance(vh, dict) else (vh if vh else 'N/A')
327
- context_parts.append(f"""## SAR Backscatter (Sentinel-1)
328
- - VV: {vv_val} dB
329
- - VH: {vh_val} dB
330
- - VV/VH Ratio: {sar.get('VV_VH_ratio', 'N/A')}
331
- - Interpretation: {sar.get('interpretation', 'N/A')}""")
 
332
 
333
  # Soil indicators
334
  if context.get("soil_indicators"):
 
317
  vi_lines.append(f"- {name}: {mean:.4f}{change_str}" if isinstance(mean, float) else f"- {name}: {mean}")
318
  context_parts.append("\n".join(vi_lines))
319
 
320
+ # SAR Analysis (crop health from SAR data)
321
  if context.get("sar_bands"):
322
  sar = context["sar_bands"]
323
+ health = sar.get("crop_health", "unknown")
324
+ stress = sar.get("stress_score", 0)
325
+ summary = sar.get("summary", "")
326
+ recs = sar.get("recommendations", [])
327
+ context_parts.append(f"""## SAR Crop Health Analysis
328
+ - Overall Health: {health}
329
+ - Stress Score: {stress:.2f if isinstance(stress, float) else stress}
330
+ - Confidence: {sar.get('confidence', 0)}%
331
+ - Summary: {summary[:200] if summary else 'N/A'}
332
+ - Top Recommendation: {recs[0] if recs else 'No specific recommendations'}""")
333
 
334
  # Soil indicators
335
  if context.get("soil_indicators"):
context_aggregator.py CHANGED
@@ -234,15 +234,15 @@ class ContextAggregator:
234
  # =========================================================================
235
 
236
  def _extract_sar_bands(self, data: Dict) -> Dict:
237
- """Extract SAR band values."""
238
- sar = data.get("sar_bands", {})
239
  return {
240
- "VV": sar.get("vv"),
241
- "VH": sar.get("vh"),
242
- "VV_VH_ratio": sar.get("ratio"),
243
- "VV_trend": sar.get("vv_trend", "stable"),
244
- "VH_trend": sar.get("vh_trend", "stable"),
245
- "interpretation": self._interpret_sar(sar.get("vv"), sar.get("vh"))
246
  }
247
 
248
  def _interpret_sar(self, vv: Optional[float], vh: Optional[float]) -> str:
@@ -260,7 +260,8 @@ class ContextAggregator:
260
 
261
  def _extract_vegetation_indices(self, data: Dict) -> Dict:
262
  """Extract and structure vegetation indices."""
263
- veg = data.get("vegetation_indices", {})
 
264
 
265
  result = {}
266
  for idx in ["ndvi", "evi", "ndre", "reci", "ndwi", "smi", "psri", "pri", "mcari"]:
 
234
  # =========================================================================
235
 
236
  def _extract_sar_bands(self, data: Dict) -> Dict:
237
+ """Extract SAR analysis data."""
238
+ # SAR API returns: status, crop_health, health_summary, average_stress_score, etc.
239
  return {
240
+ "crop_health": data.get("crop_health", "unknown"),
241
+ "confidence": data.get("confidence_score", 0),
242
+ "stress_score": data.get("average_stress_score", 0),
243
+ "summary": data.get("summary", ""),
244
+ "health_summary": data.get("health_summary", {}),
245
+ "recommendations": data.get("recommendations", [])
246
  }
247
 
248
  def _interpret_sar(self, vv: Optional[float], vh: Optional[float]) -> str:
 
260
 
261
  def _extract_vegetation_indices(self, data: Dict) -> Dict:
262
  """Extract and structure vegetation indices."""
263
+ # Try both possible keys from API response
264
+ veg = data.get("vegetation_indices", {}) or data.get("vegetation_indices_summary", {})
265
 
266
  result = {}
267
  for idx in ["ndvi", "evi", "ndre", "reci", "ndwi", "smi", "psri", "pri", "mcari"]: