Spaces:
Running on Zero

PythonMindset commited on
Commit
4391672
·
verified ·
1 Parent(s): ddd0714

Deploiement GuindiMa AI (interface Gradio)

Browse files
Files changed (3) hide show
  1. app.py +71 -0
  2. assets/qr_guindima.png +0 -0
  3. assets/qr_guindima.svg +2 -2
app.py CHANGED
@@ -64,6 +64,7 @@ from app.apparence import ( # noqa: E402
64
  from app.accueil import confidentialite, sections_accueil # noqa: E402
65
  from app.cartes import titre_autre_possibilite, titre_autre_possibilite_texte, titre_section # noqa: E402
66
  from app.limites import JOUR, MINUTE, LimiteDebit, LimiteParClient, cle_client, entier_env # noqa: E402
 
67
  from app.partage import ( # noqa: E402
68
  bouton_partage,
69
  bouton_texte,
@@ -1032,6 +1033,70 @@ def _octets_plan(plans: tuple[list, list]) -> tuple[bytes | None, bytes | None]:
1032
  )
1033
 
1034
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1035
  def _note_lieux_en_plus(phrase: str, depart: str, arrivee: str) -> str:
1036
  """Trois lieux sans mot d'étape : on ne devine rien, on le signale (lot T1)."""
1037
  autres = [l for l in lieux_cites(phrase) if l not in (depart, arrivee)]
@@ -2179,6 +2244,11 @@ def _pipeline(
2179
  if not exacte and (resultat["found"] or resultat.get("correspondances")):
2180
  avertissement = _bandeau_autre_compagnie(compagnie, resultat)
2181
  bandeau_compagnie = _bandeau_compagnie(compagnie_voix, compagnie_choix, mode=mode)
 
 
 
 
 
2182
  trajets_2 = resultat.get("trajets_2") or []
2183
  trouve = bool(
2184
  resultat["found"]
@@ -2204,6 +2274,7 @@ def _pipeline(
2204
  phrase,
2205
  f"{depart} → {arrivee}",
2206
  _note_lieux_en_plus(phrase, depart, arrivee)
 
2207
  + _carte(resultat, depart, arrivee, avertissement, bandeau_compagnie),
2208
  _voix_wolof(
2209
  resultat["lignes"],
 
64
  from app.accueil import confidentialite, sections_accueil # noqa: E402
65
  from app.cartes import titre_autre_possibilite, titre_autre_possibilite_texte, titre_section # noqa: E402
66
  from app.limites import JOUR, MINUTE, LimiteDebit, LimiteParClient, cle_client, entier_env # noqa: E402
67
+ from intent.ligne_nommee import ligne_nommee, numeros_du_bus # noqa: E402
68
  from app.partage import ( # noqa: E402
69
  bouton_partage,
70
  bouton_texte,
 
1033
  )
1034
 
1035
 
1036
+ def _numeros_de_la_proposition(proposition: tuple[str, dict]) -> set[int]:
1037
+ genre, trajet = proposition
1038
+ bus = [trajet] if genre == "ligne" else (trajet.get("etapes") or [trajet["etape_1"], trajet["etape_2"]])
1039
+ return {n for n in (numeros_du_bus(b.get("ligne")) for b in bus) if n is not None}
1040
+
1041
+
1042
+ def _avant(resultat: dict, genre: str, trajet: dict) -> dict:
1043
+ """Une copie du résultat où cette proposition passe en premier (voix comprise)."""
1044
+ nouveau = dict(resultat)
1045
+ lignes = list(resultat.get("lignes") or [])
1046
+ correspondances = list(resultat.get("correspondances") or [])
1047
+ trajets_2 = list(resultat.get("trajets_2") or [])
1048
+ nouveau.update(lignes=lignes, correspondances=correspondances, trajets_2=trajets_2)
1049
+ ordre = list(resultat.get("ordre") or [])
1050
+ if genre == "t2":
1051
+ trajets_2.insert(0, trajets_2.pop(trajets_2.index(trajet)))
1052
+ return nouveau
1053
+ liste = lignes if genre == "ligne" else correspondances
1054
+ index = next(i for i, t in enumerate(liste) if t is trajet)
1055
+ if not ordre and (genre == "ligne" or not lignes):
1056
+ liste.insert(0, liste.pop(index))
1057
+ return nouveau
1058
+ if not ordre:
1059
+ ordre = [("ligne", i) for i in range(len(lignes))] + [("corr", i) for i in range(len(correspondances))]
1060
+ nouveau["ordre"] = [(genre, index)] + [entree for entree in ordre if entree != (genre, index)]
1061
+ return nouveau
1062
+
1063
+
1064
+ def _ligne_demandee(
1065
+ phrase: str, resultat: dict, depart: str, arrivee: str, compagnies: set[str] | None = None
1066
+ ) -> tuple[dict, str]:
1067
+ """Lot D1 : la ligne nommée par l'usager en premier, ou une note si elle ne fait pas le trajet.
1068
+
1069
+ Jamais rien d'inventé : la ligne passe en tête seulement si le moteur la
1070
+ trouve entre ces deux lieux (parmi les propositions, ou en bus direct).
1071
+ """
1072
+ numero = ligne_nommee(phrase)
1073
+ if numero is None or not propositions(resultat):
1074
+ return resultat, ""
1075
+ for index, proposition in enumerate(propositions(resultat)):
1076
+ if numero in _numeros_de_la_proposition(proposition):
1077
+ return (resultat if index == 0 else _avant(resultat, *proposition)), ""
1078
+ trouve = None
1079
+ if hasattr(MATCHER, "direct_par_ligne"):
1080
+ try:
1081
+ trouve = MATCHER.direct_par_ligne(depart, arrivee, numero, compagnies)
1082
+ except Exception as exc: # garde-fou : la réponse normale reste
1083
+ print(f"[ligne] recherche de la ligne {numero} impossible ({type(exc).__name__})", flush=True)
1084
+ if trouve is not None:
1085
+ nouveau = dict(resultat)
1086
+ nouveau["lignes"] = [trouve] + list(resultat.get("lignes") or [])
1087
+ nouveau["found"] = True
1088
+ if resultat.get("ordre"):
1089
+ nouveau["ordre"] = [("ligne", 0)] + [
1090
+ (genre, i + 1 if genre == "ligne" else i) for genre, i in resultat["ordre"]
1091
+ ]
1092
+ return nouveau, ""
1093
+ return resultat, (
1094
+ "<div class='note-ligne' style='color:#7A3E00;background:#FFF4E5;border:1.5px solid #E8A84A;"
1095
+ "border-radius:12px;padding:8px 12px;margin:6px 0 10px;font-size:14px;'>"
1096
+ f"La ligne {numero} ne fait pas ce trajet. Voici les bus qui le font.</div>"
1097
+ )
1098
+
1099
+
1100
  def _note_lieux_en_plus(phrase: str, depart: str, arrivee: str) -> str:
1101
  """Trois lieux sans mot d'étape : on ne devine rien, on le signale (lot T1)."""
1102
  autres = [l for l in lieux_cites(phrase) if l not in (depart, arrivee)]
 
2244
  if not exacte and (resultat["found"] or resultat.get("correspondances")):
2245
  avertissement = _bandeau_autre_compagnie(compagnie, resultat)
2246
  bandeau_compagnie = _bandeau_compagnie(compagnie_voix, compagnie_choix, mode=mode)
2247
+ # Lot D1 : « dama bëgg jël bus bu cinquante et un » : la ligne nommée passe
2248
+ # en premier si elle fait le trajet ; sinon une petite note le dit.
2249
+ resultat, note_ligne = _ligne_demandee(
2250
+ phrase, resultat, depart, arrivee, {compagnie} if compagnie else None
2251
+ )
2252
  trajets_2 = resultat.get("trajets_2") or []
2253
  trouve = bool(
2254
  resultat["found"]
 
2274
  phrase,
2275
  f"{depart} → {arrivee}",
2276
  _note_lieux_en_plus(phrase, depart, arrivee)
2277
+ + note_ligne
2278
  + _carte(resultat, depart, arrivee, avertissement, bandeau_compagnie),
2279
  _voix_wolof(
2280
  resultat["lignes"],
assets/qr_guindima.png CHANGED
assets/qr_guindima.svg CHANGED