angeetoile commited on
Commit
39e6f82
·
verified ·
1 Parent(s): 38095bc

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +31 -17
main.py CHANGED
@@ -584,38 +584,52 @@ async def chat_bot(input_data: ChatInput):
584
  intent = detect_intent(query)
585
  k_data = search_knowledge(query)
586
  s_data = search_stats(query)
587
- print(f"--- DEBUG STATS POUR {query} ---")
588
- print(f"Contenu envoyé à l'IA : '{s_data}'")
589
- print("---------------------------------")
 
 
590
  prediction_ctx = ""
591
- pred = None
592
- if intent["needs_prediction"] and intent["city"]:
593
- pred = predict_pm25_for_city(intent["city"], days=7)
594
- if pred:
 
 
 
595
  lines = []
596
- for d in pred['days'][:7]:
597
  lines.append(
598
- f"- {d['date']} : {d['temp_max']}°C, {d['condition']}, "
599
- f"Pluie: {d['precip']}mm, Vent: {d['vent']}km/h, Humidité: {d['humidite']}%, "
600
  f"PM2.5 prévu: {d['pm25']} ({d['niveau']})"
601
  )
602
- prediction_ctx = "\n".join(lines)
603
  else:
604
- prediction_ctx = "Désolé, les prévisions sur 7 jours ne sont pas disponibles pour cette localité."
605
  prompt = build_prompt(
606
  user_query=query,
607
  stats_ctx=s_data,
608
  knowledge_ctx=k_data,
609
  prediction_ctx=prediction_ctx,
610
- search_ctx=""
611
  )
612
-
613
  response = call_google(prompt) or call_openrouter(prompt)
614
- return {"response": response.strip(), "prediction": pred}
 
 
 
 
 
 
 
 
615
 
616
  except Exception as e:
617
- print(f"Erreur : {e}") # C'est ici que tu voyais l'erreur
618
- return {"response": "Désolé, j'ai un petit souci technique. ✨"}
 
 
 
619
  @app.get("/api/weather-at-coords")
620
  async def get_weather_at_coords(lat: float, lon: float):
621
  prediction = predict_pm25_for_city(city_name="", days=1, user_lat=lat, user_lon=lon)
 
584
  intent = detect_intent(query)
585
  k_data = search_knowledge(query)
586
  s_data = search_stats(query)
587
+ s_internet = ""
588
+ if intent["needs_search"]:
589
+ print(f"--- ACTIVATION RECHERCHE WEB POUR : {query} ---")
590
+ s_internet = search_internet(query) or "Aucun résultat web récent trouvé."
591
+
592
  prediction_ctx = ""
593
+ pred_data = None
594
+ target_city = intent["city"]
595
+
596
+ if target_city:
597
+ pred_results = predict_pm25_for_city(target_city, days=7)
598
+ if pred_results:
599
+ pred_data = pred_results
600
  lines = []
601
+ for d in pred_results['days']:
602
  lines.append(
603
+ f"- {d['date']} ({target_city}): {d['temp_max']}°C, {d['condition']}, "
 
604
  f"PM2.5 prévu: {d['pm25']} ({d['niveau']})"
605
  )
606
+ prediction_ctx = "\n".join(lines)
607
  else:
608
+ prediction_ctx = f"Données de prévision indisponibles pour {target_city}."
609
  prompt = build_prompt(
610
  user_query=query,
611
  stats_ctx=s_data,
612
  knowledge_ctx=k_data,
613
  prediction_ctx=prediction_ctx,
614
+ search_ctx=s_internet # Le bloc Web est maintenant rempli !
615
  )
 
616
  response = call_google(prompt) or call_openrouter(prompt)
617
+
618
+ if not response:
619
+ raise Exception("Les services de génération IA sont indisponibles.")
620
+ return {
621
+ "response": response.strip(),
622
+ "prediction": pred_data,
623
+ "detected_city": target_city,
624
+ "source_web": True if s_internet else False
625
+ }
626
 
627
  except Exception as e:
628
+ print(f"ERREUR CRITIQUE CHATBOT: {str(e)}")
629
+ return {
630
+ "response": "Désolé, je rencontre une difficulté technique pour analyser cette demande. ✨",
631
+ "prediction": None
632
+ }
633
  @app.get("/api/weather-at-coords")
634
  async def get_weather_at_coords(lat: float, lon: float):
635
  prediction = predict_pm25_for_city(city_name="", days=1, user_lat=lat, user_lon=lon)