Tracy André commited on
Commit
7691575
·
1 Parent(s): af6888e
Files changed (2) hide show
  1. app.py +1 -1
  2. mcp_server.py +61 -11
app.py CHANGED
@@ -10,4 +10,4 @@ os.environ.setdefault("HF_TOKEN", os.environ.get("HF_TOKEN"))
10
  os.environ.setdefault("DATASET_ID", "HackathonCRA/2024")
11
 
12
  demo = create_mcp_interface()
13
- demo.launch(mcp_server=True)
 
10
  os.environ.setdefault("DATASET_ID", "HackathonCRA/2024")
11
 
12
  demo = create_mcp_interface()
13
+ demo.launch(share=True)
mcp_server.py CHANGED
@@ -93,7 +93,7 @@ analyzer = WeedPressureAnalyzer()
93
  # These functions provide structured data access for LLMs and can be exposed as MCP resources
94
  # To activate: uncomment the @gr.mcp.resource decorators below
95
 
96
- @gr.mcp.resource("agricultural://plots")
97
  def get_available_plots_resource() -> str:
98
  """Get list of all available agricultural plots from the dataset"""
99
  try:
@@ -102,7 +102,7 @@ def get_available_plots_resource() -> str:
102
  except Exception as e:
103
  return f"Error loading plots: {str(e)}"
104
 
105
- @gr.mcp.resource("agricultural://crops")
106
  def get_available_crops_resource() -> str:
107
  """Get list of all crop types in the dataset"""
108
  try:
@@ -111,7 +111,7 @@ def get_available_crops_resource() -> str:
111
  except Exception as e:
112
  return f"Error loading crops: {str(e)}"
113
 
114
- @gr.mcp.resource("agricultural://years")
115
  def get_available_years_resource() -> str:
116
  """Get range of years available in the dataset"""
117
  try:
@@ -121,7 +121,7 @@ def get_available_years_resource() -> str:
121
  except Exception as e:
122
  return f"Error loading years: {str(e)}"
123
 
124
- @gr.mcp.resource("agricultural://dataset-info")
125
  def get_dataset_info() -> str:
126
  """Get comprehensive information about the agricultural dataset"""
127
  try:
@@ -144,7 +144,7 @@ Agricultural Dataset Information:
144
  except Exception as e:
145
  return f"Error loading dataset info: {str(e)}"
146
 
147
- @gr.mcp.resource("agricultural://plot/{plot_name}")
148
  def get_plot_info(plot_name: str) -> str:
149
  """Get detailed information about a specific agricultural plot"""
150
  try:
@@ -171,7 +171,7 @@ Plot Information: {plot_name}
171
  except Exception as e:
172
  return f"Error loading plot info: {str(e)}"
173
 
174
- @gr.mcp.resource("agricultural://crop/{crop_type}")
175
  def get_crop_info(crop_type: str) -> str:
176
  """Get information about a specific crop type and its cultivation patterns"""
177
  try:
@@ -199,7 +199,7 @@ Crop Information: {crop_type}
199
  except Exception as e:
200
  return f"Error loading crop info: {str(e)}"
201
 
202
- @gr.mcp.resource("agricultural://year/{year}")
203
  def get_year_summary(year: int) -> str:
204
  """Get summary of agricultural activities for a specific year"""
205
  try:
@@ -228,7 +228,7 @@ Year Summary: {year}
228
  except Exception as e:
229
  return f"Error loading year summary: {str(e)}"
230
 
231
- @gr.mcp.resource("agricultural://herbicide-usage")
232
  def get_herbicide_usage_summary() -> str:
233
  """Get comprehensive summary of herbicide usage patterns"""
234
  try:
@@ -268,7 +268,7 @@ Herbicide Usage Summary:
268
  except Exception as e:
269
  return f"Error loading herbicide usage summary: {str(e)}"
270
 
271
- @gr.mcp.resource("agricultural://predictions/2025-2027")
272
  def get_predictions_summary() -> str:
273
  """Get summary of weed pressure predictions for 2025-2027"""
274
  try:
@@ -298,7 +298,7 @@ Weed Pressure Predictions 2025-2027:
298
  except Exception as e:
299
  return f"Error loading predictions summary: {str(e)}"
300
 
301
- @gr.mcp.resource("agricultural://recommendations/sensitive-crops")
302
  def get_recommendations_summary() -> str:
303
  """Get summary of plot recommendations for sensitive crops (pois, haricot)"""
304
  try:
@@ -331,7 +331,7 @@ Sensitive Crop Recommendations (Pois, Haricot):
331
  except Exception as e:
332
  return f"Error loading recommendations summary: {str(e)}"
333
 
334
- @gr.mcp.resource("agricultural://plot/{plot_name}/predictions")
335
  def get_plot_predictions(plot_name: str) -> str:
336
  """Get weed pressure predictions for a specific plot"""
337
  try:
@@ -834,6 +834,56 @@ def create_mcp_interface():
834
  inputs=[data_year_start, data_year_end, data_plot_filter, data_crop_filter, data_intervention_filter],
835
  outputs=[data_plot, data_summary]
836
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
837
 
838
  return demo
839
 
 
 
93
  # These functions provide structured data access for LLMs and can be exposed as MCP resources
94
  # To activate: uncomment the @gr.mcp.resource decorators below
95
 
96
+ # @gr.mcp.resource("agricultural://plots")
97
  def get_available_plots_resource() -> str:
98
  """Get list of all available agricultural plots from the dataset"""
99
  try:
 
102
  except Exception as e:
103
  return f"Error loading plots: {str(e)}"
104
 
105
+ # @gr.mcp.resource("agricultural://crops")
106
  def get_available_crops_resource() -> str:
107
  """Get list of all crop types in the dataset"""
108
  try:
 
111
  except Exception as e:
112
  return f"Error loading crops: {str(e)}"
113
 
114
+ # @gr.mcp.resource("agricultural://years")
115
  def get_available_years_resource() -> str:
116
  """Get range of years available in the dataset"""
117
  try:
 
121
  except Exception as e:
122
  return f"Error loading years: {str(e)}"
123
 
124
+ # @gr.mcp.resource("agricultural://dataset-info")
125
  def get_dataset_info() -> str:
126
  """Get comprehensive information about the agricultural dataset"""
127
  try:
 
144
  except Exception as e:
145
  return f"Error loading dataset info: {str(e)}"
146
 
147
+ # @gr.mcp.resource("agricultural://plot/{plot_name}")
148
  def get_plot_info(plot_name: str) -> str:
149
  """Get detailed information about a specific agricultural plot"""
150
  try:
 
171
  except Exception as e:
172
  return f"Error loading plot info: {str(e)}"
173
 
174
+ # @gr.mcp.resource("agricultural://crop/{crop_type}")
175
  def get_crop_info(crop_type: str) -> str:
176
  """Get information about a specific crop type and its cultivation patterns"""
177
  try:
 
199
  except Exception as e:
200
  return f"Error loading crop info: {str(e)}"
201
 
202
+ # @gr.mcp.resource("agricultural://year/{year}")
203
  def get_year_summary(year: int) -> str:
204
  """Get summary of agricultural activities for a specific year"""
205
  try:
 
228
  except Exception as e:
229
  return f"Error loading year summary: {str(e)}"
230
 
231
+ # @gr.mcp.resource("agricultural://herbicide-usage")
232
  def get_herbicide_usage_summary() -> str:
233
  """Get comprehensive summary of herbicide usage patterns"""
234
  try:
 
268
  except Exception as e:
269
  return f"Error loading herbicide usage summary: {str(e)}"
270
 
271
+ # @gr.mcp.resource("agricultural://predictions/2025-2027")
272
  def get_predictions_summary() -> str:
273
  """Get summary of weed pressure predictions for 2025-2027"""
274
  try:
 
298
  except Exception as e:
299
  return f"Error loading predictions summary: {str(e)}"
300
 
301
+ # @gr.mcp.resource("agricultural://recommendations/sensitive-crops")
302
  def get_recommendations_summary() -> str:
303
  """Get summary of plot recommendations for sensitive crops (pois, haricot)"""
304
  try:
 
331
  except Exception as e:
332
  return f"Error loading recommendations summary: {str(e)}"
333
 
334
+ # @gr.mcp.resource("agricultural://plot/{plot_name}/predictions")
335
  def get_plot_predictions(plot_name: str) -> str:
336
  """Get weed pressure predictions for a specific plot"""
337
  try:
 
834
  inputs=[data_year_start, data_year_end, data_plot_filter, data_crop_filter, data_intervention_filter],
835
  outputs=[data_plot, data_summary]
836
  )
837
+
838
+ with gr.Tab("🔧 Resources MCP"):
839
+ gr.Markdown("### Resources MCP disponibles pour les LLM")
840
+ gr.Markdown("""
841
+ Ces resources fournissent un accès structuré aux données agricoles pour les LLM via le protocole MCP.
842
+ """)
843
+
844
+ with gr.Row():
845
+ with gr.Column():
846
+ gr.Markdown("#### Resources statiques")
847
+ static_btn1 = gr.Button("📋 Plots disponibles", variant="secondary")
848
+ static_btn2 = gr.Button("🌾 Cultures disponibles", variant="secondary")
849
+ static_btn3 = gr.Button("📅 Années disponibles", variant="secondary")
850
+ static_btn4 = gr.Button("📊 Info dataset", variant="secondary")
851
+ static_btn5 = gr.Button("🧪 Usage herbicides", variant="secondary")
852
+ static_btn6 = gr.Button("🔮 Prédictions 2025-2027", variant="secondary")
853
+ static_btn7 = gr.Button("🌱 Recommandations", variant="secondary")
854
+
855
+ with gr.Column():
856
+ gr.Markdown("#### Resources paramétrées")
857
+ with gr.Row():
858
+ plot_input = gr.Textbox(label="Nom de parcelle", placeholder="Champ ferme W du sol")
859
+ plot_btn = gr.Button("🏞️ Info parcelle", variant="secondary")
860
+ with gr.Row():
861
+ crop_input = gr.Textbox(label="Type de culture", placeholder="blé tendre hiver")
862
+ crop_btn = gr.Button("🌾 Info culture", variant="secondary")
863
+ with gr.Row():
864
+ year_input = gr.Number(label="Année", value=2023, precision=0)
865
+ year_btn = gr.Button("📅 Résumé année", variant="secondary")
866
+ with gr.Row():
867
+ pred_plot_input = gr.Textbox(label="Parcelle pour prédictions", placeholder="Etang 5")
868
+ pred_plot_btn = gr.Button("🔮 Prédictions parcelle", variant="secondary")
869
+
870
+ with gr.Row():
871
+ resource_output = gr.Markdown(label="Résultat de la resource")
872
+
873
+ # Connexions des boutons
874
+ static_btn1.click(lambda: get_available_plots_resource(), outputs=[resource_output])
875
+ static_btn2.click(lambda: get_available_crops_resource(), outputs=[resource_output])
876
+ static_btn3.click(lambda: get_available_years_resource(), outputs=[resource_output])
877
+ static_btn4.click(lambda: get_dataset_info(), outputs=[resource_output])
878
+ static_btn5.click(lambda: get_herbicide_usage_summary(), outputs=[resource_output])
879
+ static_btn6.click(lambda: get_predictions_summary(), outputs=[resource_output])
880
+ static_btn7.click(lambda: get_recommendations_summary(), outputs=[resource_output])
881
+
882
+ plot_btn.click(lambda plot: get_plot_info(plot), inputs=[plot_input], outputs=[resource_output])
883
+ crop_btn.click(lambda crop: get_crop_info(crop), inputs=[crop_input], outputs=[resource_output])
884
+ year_btn.click(lambda year: get_year_summary(int(year)), inputs=[year_input], outputs=[resource_output])
885
+ pred_plot_btn.click(lambda plot: get_plot_predictions(plot), inputs=[pred_plot_input], outputs=[resource_output])
886
 
887
  return demo
888
 
889
+