Spaces:
Running
Running
| """ | |
| MCP Server CEREMA — Données foncières et friches pour la transition écologique. | |
| Point d'entrée Gradio exposant les tools MCP pour Sofia V2. | |
| Les données (DV3F + Cartofriches) sont chargées en mémoire au démarrage. | |
| Les friches sont pré-agrégées à toutes les mailles (commune, EPCI, département, région, national). | |
| Endpoint MCP : http://localhost:7860/gradio_api/mcp/sse | |
| Schema : http://localhost:7860/gradio_api/mcp/schema | |
| """ | |
| import logging | |
| import gradio as gr | |
| from data_loader import init_all | |
| from server import ( | |
| rechercher_friches, | |
| statistiques_prix_foncier, | |
| evolution_prix, | |
| statistiques_friches, | |
| diagnostic_foncier_territoire, | |
| ) | |
| # Configuration du logging | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s [%(levelname)s] %(name)s: %(message)s", | |
| ) | |
| logger = logging.getLogger(__name__) | |
| # --- Chargement des données au démarrage --- | |
| logger.info("Démarrage du serveur MCP CEREMA...") | |
| init_all() | |
| logger.info("Données chargées. Construction de l'interface Gradio...") | |
| # --- Interfaces Gradio pour chaque tool --- | |
| # Tool 1 : Recherche de friches | |
| iface_friches = gr.Interface( | |
| fn=rechercher_friches, | |
| inputs=[ | |
| gr.Textbox(label="Code INSEE commune", placeholder="ex: 13055, 59350, 75056"), | |
| gr.Textbox(label="Code département", placeholder="ex: 13, 59, 75"), | |
| gr.Dropdown( | |
| label="Type de friche", | |
| choices=["", "industrielle", "habitat", "commerciale", "ferroviaire", | |
| "militaire", "hospitalière", "logistique", "agro-industrielle", | |
| "équipement public", "carrière ou mine"], | |
| value="", | |
| ), | |
| gr.Number(label="Surface minimale (m²)", value=0), | |
| gr.Dropdown( | |
| label="Statut", | |
| choices=["", "sans projet", "avec projet", "potentielle", "reconvertie"], | |
| value="", | |
| ), | |
| ], | |
| outputs=gr.Markdown(label="Résultats"), | |
| title="Recherche de friches", | |
| description="Recherche des friches disponibles sur un territoire (base Cartofriches du CEREMA).", | |
| api_name="rechercher_friches", | |
| ) | |
| # Tool 2 : Statistiques de prix foncier | |
| iface_prix = gr.Interface( | |
| fn=statistiques_prix_foncier, | |
| inputs=[ | |
| gr.Textbox(label="Code INSEE commune", placeholder="ex: 13055, 59350"), | |
| gr.Textbox(label="Code département", placeholder="ex: 13, 59"), | |
| gr.Dropdown( | |
| label="Type de bien", | |
| choices=["tous", "maison", "appartement"], | |
| value="tous", | |
| ), | |
| gr.Textbox(label="Année", value="2024", placeholder="2010 à 2024"), | |
| ], | |
| outputs=gr.Markdown(label="Résultats"), | |
| title="Prix foncier", | |
| description="Statistiques de prix et volumes de transactions immobilières (DV3F, CEREMA/DGFiP).", | |
| api_name="statistiques_prix_foncier", | |
| ) | |
| # Tool 3 : Évolution des prix | |
| iface_evolution = gr.Interface( | |
| fn=evolution_prix, | |
| inputs=[ | |
| gr.Textbox(label="Code INSEE commune", placeholder="ex: 13055"), | |
| gr.Textbox(label="Code département", placeholder="ex: 13"), | |
| gr.Dropdown( | |
| label="Type de bien", | |
| choices=["maison", "appartement"], | |
| value="maison", | |
| ), | |
| ], | |
| outputs=gr.Markdown(label="Résultats"), | |
| title="Évolution des prix", | |
| description="Évolution temporelle des prix fonciers de 2010 à 2024 (DV3F, CEREMA/DGFiP).", | |
| api_name="evolution_prix", | |
| ) | |
| # Tool 4 : Statistiques agrégées de friches (multi-échelle) | |
| iface_stats_friches = gr.Interface( | |
| fn=statistiques_friches, | |
| inputs=[ | |
| gr.Textbox(label="Code INSEE commune", placeholder="ex: 13055 — retourne aussi EPCI, département, région"), | |
| gr.Textbox(label="Code SIREN EPCI", placeholder="ex: 200054807 (Aix-Marseille-Provence)"), | |
| gr.Textbox(label="Code département", placeholder="ex: 13, 59"), | |
| gr.Textbox(label="Code région", placeholder="ex: 93 (PACA), 32 (Hauts-de-France)"), | |
| gr.Dropdown( | |
| label="Échelle", | |
| choices=["", "commune", "epci", "departement", "region", "national"], | |
| value="", | |
| ), | |
| ], | |
| outputs=gr.Markdown(label="Résultats"), | |
| title="Statistiques friches (multi-échelle)", | |
| description="Statistiques agrégées des friches : commune, EPCI, département, région ou national. " | |
| "Si une commune est fournie, les stats aux échelles supérieures sont aussi affichées.", | |
| api_name="statistiques_friches", | |
| ) | |
| # Tool 5 : Diagnostic foncier territorial (multi-échelle) | |
| iface_diagnostic = gr.Interface( | |
| fn=diagnostic_foncier_territoire, | |
| inputs=[ | |
| gr.Textbox(label="Code INSEE commune", placeholder="ex: 13055"), | |
| gr.Textbox(label="Code SIREN EPCI", placeholder="ex: 200054807"), | |
| gr.Textbox(label="Code département", placeholder="ex: 13"), | |
| gr.Textbox(label="Code région", placeholder="ex: 93 (PACA)"), | |
| ], | |
| outputs=gr.Markdown(label="Résultats"), | |
| title="Diagnostic foncier", | |
| description="Diagnostic complet croisant friches et marché foncier à toutes les échelles (Cartofriches + DV3F).", | |
| api_name="diagnostic_foncier_territoire", | |
| ) | |
| # --- Application Gradio combinée --- | |
| demo = gr.TabbedInterface( | |
| [iface_friches, iface_prix, iface_evolution, iface_stats_friches, iface_diagnostic], | |
| tab_names=["🏗️ Friches", "💰 Prix foncier", "📈 Évolution prix", | |
| "📊 Stats friches", "🔍 Diagnostic territorial"], | |
| title="MCP CEREMA — Données foncières & friches pour la transition écologique", | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch( | |
| mcp_server=True, | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| ) | |