File size: 12,536 Bytes
2ce9eab
 
 
 
7ca901a
dc128e4
7ca901a
dc128e4
 
7ca901a
2ce9eab
 
dc128e4
 
7ca901a
2ce9eab
dc128e4
2ce9eab
7ca901a
2ce9eab
6df7dd5
2ce9eab
 
 
 
 
 
6df7dd5
 
2ce9eab
 
dc128e4
2ce9eab
dc128e4
2ce9eab
 
 
 
 
dc128e4
 
 
2ce9eab
dc128e4
2ce9eab
6df7dd5
2ce9eab
6df7dd5
2ce9eab
 
6df7dd5
dc128e4
8b09855
 
2ce9eab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b09855
 
2ce9eab
8b09855
2ce9eab
dc128e4
 
2ce9eab
 
dc128e4
8b09855
 
2ce9eab
 
8b09855
7ca901a
2ce9eab
dc128e4
2ce9eab
6df7dd5
 
2ce9eab
 
 
 
6df7dd5
 
2ce9eab
 
dc128e4
 
2ce9eab
 
dc128e4
2ce9eab
 
 
 
7ca901a
2ce9eab
dc128e4
2ce9eab
6df7dd5
2ce9eab
 
 
 
 
6df7dd5
 
2ce9eab
 
dc128e4
 
2ce9eab
 
dc128e4
2ce9eab
 
 
 
7ca901a
2ce9eab
 
 
 
 
 
 
 
 
 
6df7dd5
 
 
 
 
 
2ce9eab
6df7dd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc128e4
6df7dd5
 
 
 
 
7691575
2ce9eab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ca901a
2ce9eab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""
MCP Server for Agricultural Weed Pressure Analysis
Integrates tools, resources and prompts from the mcp/ folder
"""

import gradio as gr
import pandas as pd
import numpy as np
import plotly.express as px
from data_loader import AgriculturalDataLoader
from agricultural_mcp.tools import WeedPressureAnalyzer, analyze_herbicide_trends, predict_future_weed_pressure, recommend_sensitive_crop_plots, explore_raw_data, get_available_plots, get_available_crops, get_available_interventions
from agricultural_mcp.resources import AgriculturalResources
import warnings
warnings.filterwarnings('ignore')

# Initialize analyzer and resources
analyzer = WeedPressureAnalyzer()
resources = AgriculturalResources()

def create_mcp_interface():
    """
    Create the main MCP interface with 5 tabs:
    1. Analyse Tendances - IFT herbicide analysis
    2. Prédictions - Weed pressure predictions 2025-2027
    3. Recommandations - Sensitive crop recommendations
    4. Exploration Données - Raw data exploration
    5. MCP Resources - Display resources.py content
    """
    
    with gr.Blocks(title="Serveur MCP - Analyse Pression Adventices", theme=gr.themes.Soft()) as demo:
        gr.Markdown("# 🌾 Serveur MCP - Analyse Pression Adventices")
        gr.Markdown("""
        **Analyse de la pression adventices et recommandations pour cultures sensibles**
        
        Ce serveur MCP (Model Context Protocol) fournit des outils d'analyse pour :
        - Calculer l'IFT (Indice de Fréquence de Traitement) des herbicides
        - Prédire la pression adventices pour 2025-2027
        - Recommander des parcelles pour cultures sensibles (pois, haricot)
        - Explorer les données agricoles avec filtres avancés
        """)
        
        with gr.Tabs():
            # Tab 1: Analyse Tendances
            with gr.Tab("📈 Analyse Tendances"):
                gr.Markdown("### Analyse des tendances IFT herbicides")
                gr.Markdown("""
                **Méthode de calcul IFT :**
                - IFT = Nombre d'applications herbicides / Surface de la parcelle
                - Analyse de l'évolution temporelle par parcelle
                - Classification des niveaux de risque : Faible (IFT < 1.0), Modéré (1.0 ≤ IFT < 2.0), Élevé (IFT ≥ 2.0)
                """)
                
                with gr.Row():
                    with gr.Column():
                        year_start = gr.Slider(
                            minimum=2014, 
                            maximum=2025, 
                            value=2014, 
                            step=1, 
                            label="Année de début"
                        )
                        year_end = gr.Slider(
                            minimum=2014, 
                            maximum=2025, 
                            value=2025, 
                            step=1, 
                            label="Année de fin"
                        )
                        plot_filter = gr.Dropdown(
                            choices=get_available_plots(), 
                            value="Toutes", 
                            label="Filtrer par parcelle"
                        )
                        analyze_btn = gr.Button("📊 Analyser les Tendances", variant="primary")
                
                with gr.Row():
                    trend_plot = gr.Plot()
                    trend_summary = gr.Markdown()
                
                analyze_btn.click(
                    analyze_herbicide_trends, 
                    inputs=[year_start, year_end, plot_filter], 
                    outputs=[trend_plot, trend_summary]
                )
            
            # Tab 2: Prédictions
            with gr.Tab("🔮 Prédictions"):
                gr.Markdown("### Prédictions de pression adventices 2025-2027")
                gr.Markdown("""
                **Méthode de prédiction :**
                - Régression linéaire sur les données IFT historiques
                - Extrapolation pour les années 2025-2027
                - Classification des niveaux de risque basée sur l'IFT prédit
                - Prise en compte de l'historique des cultures récentes
                """)
                
                with gr.Row():
                    predict_btn = gr.Button("🔮 Générer les Prédictions", variant="primary")
                
                with gr.Row():
                    pred_plot = gr.Plot()
                    pred_summary = gr.Markdown()
                
                predict_btn.click(
                    predict_future_weed_pressure, 
                    outputs=[pred_plot, pred_summary]
                )
            
            # Tab 3: Recommandations
            with gr.Tab("🌱 Recommandations"):
                gr.Markdown("### Recommandations pour cultures sensibles")
                gr.Markdown("""
                **Critères de recommandation :**
                - Parcelles avec IFT prédit < 1.0 (faible pression adventices)
                - Score de recommandation : 100 - (IFT_prédit × 30)
                - Cultures sensibles : pois, haricot
                - Prise en compte de l'historique cultural récent
                """)
                
                with gr.Row():
                    recommend_btn = gr.Button("🌱 Générer les Recommandations", variant="primary")
                
                with gr.Row():
                    rec_plot = gr.Plot()
                    rec_summary = gr.Markdown()
                
                recommend_btn.click(
                    recommend_sensitive_crop_plots, 
                    outputs=[rec_plot, rec_summary]
                )
            
            # Tab 4: Exploration Données
            with gr.Tab("🔍 Exploration Données"):
                gr.Markdown("### Exploration des données brutes")
                gr.Markdown("""
                **Filtres disponibles :**
                - Années : 2014-2025
                - Parcelles : 106 parcelles disponibles
                - Cultures : 42 types de cultures
                - Types d'intervention : Herbicides, Fertilisation, Semis, etc.
                """)
                
                with gr.Row():
                    with gr.Column():
                        data_year_start = gr.Slider(
                            minimum=2014, 
                            maximum=2025, 
                            value=2014, 
                            step=1, 
                            label="Année de début"
                        )
                        data_year_end = gr.Slider(
                            minimum=2014, 
                            maximum=2025, 
                            value=2025, 
                            step=1, 
                            label="Année de fin"
                        )
                        data_plot_filter = gr.Dropdown(
                            choices=get_available_plots(), 
                            value="Toutes", 
                            label="Filtrer par parcelle"
                        )
                        data_crop_filter = gr.Dropdown(
                            choices=get_available_crops(), 
                            value="Toutes", 
                            label="Filtrer par culture"
                        )
                        data_intervention_filter = gr.Dropdown(
                            choices=get_available_interventions(), 
                            value="Toutes", 
                            label="Filtrer par type d'intervention"
                        )
                        explore_btn = gr.Button("🔍 Explorer les Données", variant="primary")
                
                with gr.Row():
                    data_plot = gr.Plot()
                    data_summary = gr.Markdown()
                
                explore_btn.click(
                    explore_raw_data, 
                    inputs=[data_year_start, data_year_end, data_plot_filter, data_crop_filter, data_intervention_filter], 
                    outputs=[data_plot, data_summary]
                )
            
            # Tab 5: MCP Resources
            with gr.Tab("🔧 MCP Resources"):
                gr.Markdown("### Resources MCP disponibles")
                gr.Markdown("""
                **Resources MCP exposées :**
                - `exploitation://{siret}` - Informations d'exploitation
                - `parcelle://{numparcell}` - Informations de parcelle
                - `intervention://{rang}` - Informations d'intervention
                - `intrant://{codeamm}` - Informations d'intrant
                - `materiel://{id}` - Informations de matériel
                """)
                
                with gr.Row():
                    with gr.Column():
                        gr.Markdown("#### Test des Resources MCP")
                        
                        # Test Exploitation
                        with gr.Row():
                            siret_input = gr.Textbox(
                                label="SIRET Exploitation", 
                                value="18560001000016",
                                placeholder="18560001000016"
                            )
                            siret_btn = gr.Button("🏢 Test Exploitation", variant="secondary")
                        
                        # Test Parcelle
                        with gr.Row():
                            parcelle_input = gr.Textbox(
                                label="Numéro Parcelle", 
                                value="12",
                                placeholder="12"
                            )
                            parcelle_btn = gr.Button("🏞️ Test Parcelle", variant="secondary")
                        
                        # Test Intervention
                        with gr.Row():
                            intervention_input = gr.Textbox(
                                label="Rang Intervention", 
                                value="1",
                                placeholder="1"
                            )
                            intervention_btn = gr.Button("⚙️ Test Intervention", variant="secondary")
                        
                        # Test Intrant
                        with gr.Row():
                            intrant_input = gr.Textbox(
                                label="Code AMM Intrant", 
                                value="9100296",
                                placeholder="9100296"
                            )
                            intrant_btn = gr.Button("🧪 Test Intrant", variant="secondary")
                        
                        # Test Matériel
                        with gr.Row():
                            materiel_input = gr.Textbox(
                                label="ID Matériel", 
                                value="1",
                                placeholder="1"
                            )
                            materiel_btn = gr.Button("🔧 Test Matériel", variant="secondary")
                    
                    with gr.Column():
                        gr.Markdown("#### Résultat")
                        resource_output = gr.JSON(label="Résultat de la resource")
                
                # Connexions des boutons
                siret_btn.click(
                    lambda siret: resources.get_exploitation(siret), 
                    inputs=[siret_input], 
                    outputs=[resource_output]
                )
                parcelle_btn.click(
                    lambda parcelle: resources.get_parcelle(parcelle), 
                    inputs=[parcelle_input], 
                    outputs=[resource_output]
                )
                intervention_btn.click(
                    lambda intervention: resources.get_intervention(intervention), 
                    inputs=[intervention_input], 
                    outputs=[resource_output]
                )
                intrant_btn.click(
                    lambda intrant: resources.get_intrant(intrant), 
                    inputs=[intrant_input], 
                    outputs=[resource_output]
                )
                materiel_btn.click(
                    lambda materiel: resources.get_materiel(materiel), 
                    inputs=[materiel_input], 
                    outputs=[resource_output]
                )
    
    return demo