Cheikh Niang commited on
Commit
37edcb3
·
verified ·
1 Parent(s): b284628

Deploiement GuindiMa AI (interface Gradio)

Browse files
Files changed (2) hide show
  1. app.py +75 -13
  2. src/matching/stops_matcher.py +10 -2
app.py CHANGED
@@ -46,6 +46,7 @@ from intent.extract_intent import ( # noqa: E402
46
  extract_intent,
47
  extract_local,
48
  )
 
49
  from matching.stops_matcher import StopsMatcher # noqa: E402
50
  from tts.speak import ( # noqa: E402
51
  cache_complet,
@@ -285,26 +286,85 @@ def _voix_wolof(lignes: list[dict], correspondances: list[dict] | None = None) -
285
  # --------------------------------------------------------------------------- #
286
 
287
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
  def _une_carte(match: dict, etape: str = "", depart: str = "", arrivee: str = "") -> str:
289
- """Une ligne : compagnie, numéro, trajet de l'utilisateur, terminus de la ligne.
290
-
291
- Les noms d'arrêts portent leur couleur en propre (mode clair ET mode sombre) :
292
- le CSS de la page ne doit jamais les rendre illisibles sur la carte claire.
 
 
 
293
  """
294
  numero = numero_de(match["ligne"])
295
  affiche = re.sub(r"^Ligne\s*", "", str(match["ligne"]))
296
  rappel = nombre_en_wolof(numero) if numero is not None else ""
297
  entete = f"{etape} · {match['compagnie']}" if etape else match["compagnie"]
298
  trajet = ""
 
299
  if match.get("monte_a") and match.get("descend_a"):
300
- # Le trajet de l'utilisateur, en gros, juste sous le numéro de ligne.
301
- depart_texte = depart if depart else match['monte_a']
302
- arrivee_texte = arrivee if arrivee else match['descend_a']
303
  trajet = (
304
- f"<div style='color:#11301C;font-size:24px;font-weight:800;margin-top:8px;'>"
305
- f"Monte à <b style='color:#081F11;'>{depart_texte}</b> (arrêt <b style='color:#081F11;'>{match['monte_a']}</b>)"
306
- f" → descends à <b style='color:#081F11;'>{arrivee_texte}</b> (arrêt <b style='color:#081F11;'>{match['descend_a']}</b>)</div>"
307
- )
 
 
 
 
 
 
 
308
  # Les terminus de la ligne, en petit : le parcours réel du bus.
309
  ligne = (
310
  f"<div style='color:#6B7A72;font-size:13px;margin-top:6px;'>"
@@ -318,11 +378,13 @@ def _une_carte(match: dict, etape: str = "", depart: str = "", arrivee: str = ""
318
  <div style="color:{VERT};font-size:56px;font-weight:800;line-height:1.05;
319
  margin:4px 0 6px 0;">{affiche}</div>
320
  {trajet}
321
- {ligne}
322
  <div style="color:#6B7A72;font-size:13px;margin-top:6px;">
323
  {" · ".join(x for x in (rappel, match.get("categorie", "")) if x)}</div>
 
 
324
  </div>
325
- """
326
 
327
 
328
  def _carte(resultat: dict, depart: str, arrivee: str) -> str:
 
46
  extract_intent,
47
  extract_local,
48
  )
49
+ from matching.lieux import sans_accents # noqa: E402
50
  from matching.stops_matcher import StopsMatcher # noqa: E402
51
  from tts.speak import ( # noqa: E402
52
  cache_complet,
 
286
  # --------------------------------------------------------------------------- #
287
 
288
 
289
+ # Grands quartiers : leur nom dit ne désigne pas un point précis, le même dit
290
+ # peut être à plusieurs kilomètres de l'arrêt conseillé. On invite alors à affiner.
291
+ QUARTIERS_VAGUES = {
292
+ "pikine", "guediawaye", "parcelle", "parcelles", "parcelles assainies",
293
+ "keur massar", "rufisque", "yeumbeul", "thiaroye", "grand yoff", "medina",
294
+ }
295
+
296
+
297
+ def _astuce_quartier(*lieux: str) -> str:
298
+ """Un rappel quand l'utilisateur n'a nommé qu'un grand quartier.
299
+
300
+ Le fond reste clair et le texte sombre en propre : le bloc se lit pareil en
301
+ mode clair et en mode sombre.
302
+ """
303
+ normalises = {sans_accents(lieu).strip() for lieu in lieux if lieu}
304
+ if not (normalises & QUARTIERS_VAGUES):
305
+ return ""
306
+ return (
307
+ "<div style='color:#7A5A00;background:#FFF7E0;border:1px solid #F0DFA8;"
308
+ "border-radius:8px;padding:8px 12px;margin-top:10px;font-size:13px;'>"
309
+ "💡 Astuce : précise ton lieu (ex. Pikine Rue 10, Tally Boumack) "
310
+ "pour un arrêt plus juste.</div>"
311
+ )
312
+
313
+
314
+ def _bloc_arrets(match: dict) -> str:
315
+ """Le détail repliable des arrêts parcourus, montée et descente en gras."""
316
+ arrets = match.get("arrets_trajet") or []
317
+ if not arrets:
318
+ return ""
319
+ dernier = len(arrets) - 1
320
+ elements = []
321
+ for i, arret in enumerate(arrets):
322
+ if i in (0, dernier):
323
+ elements.append(
324
+ f"<li style='margin:2px 0;'><b style='color:#081F11;'>{arret}</b></li>"
325
+ )
326
+ else:
327
+ elements.append(f"<li style='margin:2px 0;color:#2C4636;'>{arret}</li>")
328
+ return (
329
+ "<details style='margin-top:10px;'>"
330
+ "<summary style='color:#0B6B37;font-size:14px;font-weight:700;"
331
+ "cursor:pointer;outline:none;'>Voir les arrêts du trajet</summary>"
332
+ "<ol style='color:#2C4636;font-size:14px;margin:8px 0 0 20px;padding:0;'>"
333
+ f"{''.join(elements)}</ol></details>"
334
+ )
335
+
336
+
337
  def _une_carte(match: dict, etape: str = "", depart: str = "", arrivee: str = "") -> str:
338
+ """Une ligne : compagnie, numéro, trajet conseillé, arrêts parcourus.
339
+
340
+ On ne prétend pas connaître l'arrêt exact de l'utilisateur : on conseille
341
+ l'arrêt le plus proche du lieu qu'il a dit, et on rappelle que tout arrêt de
342
+ la ligne parcouru convient. Les couleurs sont portées en propre par chaque
343
+ bloc (fond clair fixe + texte sombre) : la carte reste lisible en mode clair
344
+ comme en mode sombre, quel que soit le thème Gradio.
345
  """
346
  numero = numero_de(match["ligne"])
347
  affiche = re.sub(r"^Ligne\s*", "", str(match["ligne"]))
348
  rappel = nombre_en_wolof(numero) if numero is not None else ""
349
  entete = f"{etape} · {match['compagnie']}" if etape else match["compagnie"]
350
  trajet = ""
351
+ astuce = ""
352
  if match.get("monte_a") and match.get("descend_a"):
353
+ # Le lieu dit par l'utilisateur, avec l'arrêt conseillé le plus proche.
354
+ depart_texte = depart if depart else match["monte_a"]
355
+ arrivee_texte = arrivee if arrivee else match["descend_a"]
356
  trajet = (
357
+ f"<div style='color:#11301C;font-size:22px;font-weight:800;"
358
+ f"line-height:1.35;margin-top:8px;'>"
359
+ f"Monte à <b style='color:#081F11;'>{depart_texte}</b> — arrêt conseillé : "
360
+ f"<b style='color:#081F11;'>{match['monte_a']}</b> "
361
+ f"<span style='font-weight:600;font-size:15px;color:#3D5A48;'>"
362
+ f"(ou tout arrêt de la ligne {affiche} près de toi)</span><br>"
363
+ f"Descends à <b style='color:#081F11;'>{arrivee_texte}</b> — arrêt conseillé : "
364
+ f"<b style='color:#081F11;'>{match['descend_a']}</b></div>"
365
+ )
366
+ astuce = _astuce_quartier(depart_texte, arrivee_texte)
367
+ arrets = _bloc_arrets(match)
368
  # Les terminus de la ligne, en petit : le parcours réel du bus.
369
  ligne = (
370
  f"<div style='color:#6B7A72;font-size:13px;margin-top:6px;'>"
 
378
  <div style="color:{VERT};font-size:56px;font-weight:800;line-height:1.05;
379
  margin:4px 0 6px 0;">{affiche}</div>
380
  {trajet}
381
+ {astuce}
382
  <div style="color:#6B7A72;font-size:13px;margin-top:6px;">
383
  {" · ".join(x for x in (rappel, match.get("categorie", "")) if x)}</div>
384
+ {ligne}
385
+ {arrets}
386
  </div>
387
+ """
388
 
389
 
390
  def _carte(resultat: dict, depart: str, arrivee: str) -> str:
src/matching/stops_matcher.py CHANGED
@@ -79,6 +79,12 @@ class StopsMatcher:
79
  """Une ligne parcourue de l'arrêt i_dep à l'arrêt i_arr."""
80
  info, arrets = self.lignes[cle], self.arrets[cle]
81
  direct = i_dep < i_arr
 
 
 
 
 
 
82
  return {
83
  "compagnie": info["compagnie"],
84
  "ligne": info["ligne"],
@@ -88,6 +94,7 @@ class StopsMatcher:
88
  "sens": "direct" if direct else "inverse",
89
  "monte_a": joli(arrets[i_dep]),
90
  "descend_a": joli(arrets[i_arr]),
 
91
  }
92
 
93
  def _correspondances(self, depart: str, arrivee: str) -> list[dict]:
@@ -150,8 +157,9 @@ class StopsMatcher:
150
 
151
  Chaque ligne renvoyée porte : compagnie, ligne, categorie, depart et
152
  arrivee (les terminus dans le sens du trajet), sens, monte_a et
153
- descend_a (les arrêts où monter et descendre). Sans ligne directe,
154
- « correspondances » propose des trajets en deux bus.
 
155
  """
156
  depart, arrivee = depart or "", arrivee or ""
157
  depart_arret, depart_score = self._reconnu(depart)
 
79
  """Une ligne parcourue de l'arrêt i_dep à l'arrêt i_arr."""
80
  info, arrets = self.lignes[cle], self.arrets[cle]
81
  direct = i_dep < i_arr
82
+ # Tous les arrêts réellement parcourus, de la montée à la descente, dans
83
+ # le sens du trajet : le voyageur peut monter à n'importe lequel d'entre
84
+ # eux. On garde monte_a / descend_a tels quels (l'arrêt conseillé), mais
85
+ # on ne laisse pas croire que c'est le seul possible.
86
+ positions = range(i_dep, i_arr + 1) if direct else range(i_dep, i_arr - 1, -1)
87
+ arrets_trajet = [joli(arrets[i]) for i in positions]
88
  return {
89
  "compagnie": info["compagnie"],
90
  "ligne": info["ligne"],
 
94
  "sens": "direct" if direct else "inverse",
95
  "monte_a": joli(arrets[i_dep]),
96
  "descend_a": joli(arrets[i_arr]),
97
+ "arrets_trajet": arrets_trajet,
98
  }
99
 
100
  def _correspondances(self, depart: str, arrivee: str) -> list[dict]:
 
157
 
158
  Chaque ligne renvoyée porte : compagnie, ligne, categorie, depart et
159
  arrivee (les terminus dans le sens du trajet), sens, monte_a et
160
+ descend_a (les arrêts conseillés pour monter et descendre) et
161
+ arrets_trajet (tous les arrêts parcourus, de la montée à la descente).
162
+ Sans ligne directe, « correspondances » propose des trajets en deux bus.
163
  """
164
  depart, arrivee = depart or "", arrivee or ""
165
  depart_arret, depart_score = self._reconnu(depart)