Spaces:
Sleeping
Sleeping
Upload tools/idfm_disruptions_final.py with huggingface_hub
Browse files
tools/idfm_disruptions_final.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# --- User Defined Logic ---
|
| 7 |
+
import requests
|
| 8 |
+
import json
|
| 9 |
+
|
| 10 |
+
def idfm_disruptions_final(mode: str, token: str, query: str = "") -> str:
|
| 11 |
+
"""Consulte les perturbations IDFM via l'API PRIM."""
|
| 12 |
+
url = "https://prim.iledefrance-mobilites.fr/marketplace/disruptions_bulk/disruptions/v2"
|
| 13 |
+
headers = {"apikey": token}
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 17 |
+
response.raise_for_status()
|
| 18 |
+
data = response.json()
|
| 19 |
+
|
| 20 |
+
# L'API peut retourner soit une liste directe, soit un objet avec une clé
|
| 21 |
+
if isinstance(data, dict) and "disruptions" in data:
|
| 22 |
+
disruptions = data["disruptions"]
|
| 23 |
+
elif isinstance(data, list):
|
| 24 |
+
disruptions = data
|
| 25 |
+
else:
|
| 26 |
+
disruptions = []
|
| 27 |
+
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"Erreur API: {str(e)}"
|
| 30 |
+
|
| 31 |
+
if mode == "stats":
|
| 32 |
+
total = len(disruptions)
|
| 33 |
+
result = f"Total de perturbations: {total}\n\n"
|
| 34 |
+
if total > 0:
|
| 35 |
+
result += "Échantillon des 5 premières:\n"
|
| 36 |
+
for i, d in enumerate(disruptions[:5], 1):
|
| 37 |
+
ligne = d.get("impactedObject", {}).get("name", "N/A") if isinstance(d.get("impactedObject"), dict) else "N/A"
|
| 38 |
+
titre = d.get("title", d.get("message", "Sans titre"))
|
| 39 |
+
result += f"{i}. {ligne}: {titre}\n"
|
| 40 |
+
return result
|
| 41 |
+
|
| 42 |
+
elif mode == "find":
|
| 43 |
+
if not query:
|
| 44 |
+
return "Erreur: query requis pour le mode find"
|
| 45 |
+
q_lower = query.lower()
|
| 46 |
+
matches = []
|
| 47 |
+
|
| 48 |
+
for d in disruptions:
|
| 49 |
+
# Recherche dans tous les champs texte
|
| 50 |
+
search_str = json.dumps(d, ensure_ascii=False).lower()
|
| 51 |
+
if q_lower in search_str:
|
| 52 |
+
matches.append(d)
|
| 53 |
+
|
| 54 |
+
if not matches:
|
| 55 |
+
return f"Aucune perturbation trouvée pour '{query}'"
|
| 56 |
+
|
| 57 |
+
result = f"Trouvé {len(matches)} perturbation(s) pour '{query}':\n\n"
|
| 58 |
+
for i, d in enumerate(matches[:10], 1):
|
| 59 |
+
ligne = d.get("impactedObject", {}).get("name", "N/A") if isinstance(d.get("impactedObject"), dict) else "N/A"
|
| 60 |
+
titre = d.get("title", d.get("message", "Sans titre"))
|
| 61 |
+
severite = d.get("severity", "N/A")
|
| 62 |
+
result += f"{i}. [{severite}] {ligne}: {titre}\n"
|
| 63 |
+
|
| 64 |
+
if len(matches) > 10:
|
| 65 |
+
result += f"\n... et {len(matches) - 10} autres"
|
| 66 |
+
|
| 67 |
+
return result
|
| 68 |
+
|
| 69 |
+
elif mode == "list":
|
| 70 |
+
if not disruptions:
|
| 71 |
+
return "Aucune perturbation"
|
| 72 |
+
result = f"Liste des perturbations (10 premières sur {len(disruptions)}):\n\n"
|
| 73 |
+
for i, d in enumerate(disruptions[:10], 1):
|
| 74 |
+
ligne = d.get("impactedObject", {}).get("name", "N/A") if isinstance(d.get("impactedObject"), dict) else "N/A"
|
| 75 |
+
titre = d.get("title", d.get("message", "Sans titre"))
|
| 76 |
+
result += f"{i}. {ligne}: {titre}\n"
|
| 77 |
+
return result
|
| 78 |
+
|
| 79 |
+
else:
|
| 80 |
+
return f"Mode non supporté: {mode}. Utilisez stats, find ou list"
|
| 81 |
+
|
| 82 |
+
# --- Interface Factory ---
|
| 83 |
+
def create_interface():
|
| 84 |
+
return gr.Interface(
|
| 85 |
+
fn=idfm_disruptions_final,
|
| 86 |
+
inputs=[gr.Textbox(label=k) for k in ['mode', 'token', 'query']],
|
| 87 |
+
outputs=gr.Textbox(label="Résumé formaté des perturbations"),
|
| 88 |
+
title="idfm-disruptions-final",
|
| 89 |
+
description="Auto-generated tool: idfm-disruptions-final"
|
| 90 |
+
)
|