Distances routieres reelles, navigation, synthese amont lisible et file Ollama supervisee
Le rayon porte desormais sur la distance routiere (prefiltre a vol d oiseau, puis itineraire reel via OSRM par defaut et Google Routes API si une cle est fournie). Chaque station affiche distance en voiture, duree, prix, enseigne et deux liens de navigation Google Maps et Waze sur ses coordonnees exactes ; distance et duree sont mises en cache pour le futur cout de detour. La section amont devient une synthese comprehensible (pression, confiance, facteurs en langage courant, paragraphe En clair) avec les donnees techniques repliees. La file Ollama est prefiltree de facon deterministe, mesuree et exposee dans la supervision (articles en attente, age du plus ancien, analyses sur 24 h, duree moyenne, echecs par modele).
This commit is contained in:
@@ -9,6 +9,7 @@ est faite séparément, par le moteur de décision.
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import time
|
||||
|
||||
import llm
|
||||
import news
|
||||
@@ -137,6 +138,54 @@ def analyse(article, models=None):
|
||||
MAX_ARTICLES_PER_RUN = int(os.environ.get("FUEL_PROMO_MAX_PER_RUN", "3"))
|
||||
|
||||
|
||||
def relevant(article):
|
||||
"""Préfiltrage déterministe des annonces : enseigne connue **et** vocabulaire carburant."""
|
||||
text = news.normalize(article.get("title", "") + " " + (article.get("summary") or ""))
|
||||
brand_hit = any(news.normalize(brand) in text for brand in BRANDS)
|
||||
fuel_hit = any(
|
||||
term in text
|
||||
for term in (
|
||||
"carburant",
|
||||
"essence",
|
||||
"gazole",
|
||||
"diesel",
|
||||
"sp95",
|
||||
"sp98",
|
||||
"e10",
|
||||
"e85",
|
||||
"gpl",
|
||||
"prix coutant",
|
||||
"prix plafonne",
|
||||
"remise",
|
||||
"litre",
|
||||
"plein",
|
||||
"station",
|
||||
"pompe",
|
||||
)
|
||||
) and any(term in text for term in ("prix", "remise", "litre", "operation", "promotion", "offre"))
|
||||
if not fuel_hit:
|
||||
return False, "pas de vocabulaire carburant"
|
||||
if not brand_hit and "station" not in text and "pompe" not in text:
|
||||
return False, "aucune enseigne identifiable"
|
||||
return True, ""
|
||||
|
||||
|
||||
def prefilter(articles):
|
||||
keep, dropped, seen = [], [], set()
|
||||
for article in articles:
|
||||
title_key = news.normalize(article.get("title", ""))[:120]
|
||||
if title_key and title_key in seen:
|
||||
dropped.append((article, "titre déjà vu dans ce passage"))
|
||||
continue
|
||||
seen.add(title_key)
|
||||
if news.is_stale(article):
|
||||
dropped.append((article, f"article de plus de {news.MAX_AGE_DAYS} jours"))
|
||||
continue
|
||||
ok, reason = relevant(article)
|
||||
(keep if ok else dropped).append(article if ok else (article, reason))
|
||||
return keep, dropped
|
||||
|
||||
|
||||
def collect(models=None, force=False, limit=None):
|
||||
"""Recherche les opérations, n'analyse que les nouveaux articles, enregistre les annonces.
|
||||
|
||||
@@ -145,28 +194,60 @@ def collect(models=None, force=False, limit=None):
|
||||
"""
|
||||
articles, errors = gather()
|
||||
fresh = storage.save_articles(articles, "promotions")
|
||||
todo = [article for article in fresh if article.get("topic")][: limit or MAX_ARTICLES_PER_RUN]
|
||||
saved, analysed = 0, []
|
||||
candidates = [article for article in fresh if article.get("topic")]
|
||||
keep, dropped = prefilter(candidates)
|
||||
if dropped:
|
||||
storage.mark_skipped(
|
||||
[article["hash"] for article, _reason in dropped], "préfiltrage automatique (promotions)"
|
||||
)
|
||||
todo = keep[: limit or MAX_ARTICLES_PER_RUN]
|
||||
saved, analysed, durations = 0, [], []
|
||||
for article in todo:
|
||||
started = time.monotonic()
|
||||
try:
|
||||
found, _model = analyse(article, models=models)
|
||||
found, model = analyse(article, models=models)
|
||||
except Exception as exc:
|
||||
duration = round(time.monotonic() - started, 1)
|
||||
durations.append(duration)
|
||||
storage.record_llm_run(
|
||||
article["hash"],
|
||||
article.get("topic", ""),
|
||||
llm.model_order()[0],
|
||||
duration,
|
||||
ok=False,
|
||||
detail=type(exc).__name__,
|
||||
)
|
||||
errors.append(f"ollama {article['hash'][:6]}: {type(exc).__name__}")
|
||||
continue
|
||||
duration = round(time.monotonic() - started, 1)
|
||||
durations.append(duration)
|
||||
storage.record_llm_run(article["hash"], article.get("topic", ""), model, duration, ok=True)
|
||||
if found:
|
||||
saved += storage.save_promotions(found)
|
||||
analysed.append(article["hash"])
|
||||
storage.mark_analysed(analysed)
|
||||
newest = max([article.get("published_at") or "" for article in articles] or [""])
|
||||
queue = storage.llm_queue()
|
||||
detail = (
|
||||
"; ".join(errors[:3])
|
||||
if errors
|
||||
else (
|
||||
f"{saved} opération(s) · {len(analysed)} analysé(s) en {sum(durations):.0f} s · "
|
||||
f"{queue['pending']} en attente"
|
||||
)
|
||||
)
|
||||
return dict(
|
||||
status="ok" if articles else "error",
|
||||
articles=len(articles),
|
||||
new=len(todo),
|
||||
analysed=len(analysed),
|
||||
promotions=saved,
|
||||
skipped=len(dropped),
|
||||
durations=durations,
|
||||
errors=errors[:6],
|
||||
queue=queue,
|
||||
last_observation_at=newest or None,
|
||||
detail="; ".join(errors[:3]) if errors else f"{saved} opération(s) enregistrée(s)",
|
||||
detail=detail,
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user