Spaces:
Sleeping
Sleeping
Tracy André
commited on
Commit
·
d13ece1
1
Parent(s):
26a5e78
updated
Browse files- app.py +5 -3
- debug_app.py +77 -0
- mcp_server.py +135 -87
- test_final_mcp_app.py +38 -0
- test_fixed_app.py +37 -0
- test_mcp_decorators.py +0 -44
- test_final_mcp.py → test_real_mcp.py +19 -17
- test_resources_simple.py +34 -0
app.py
CHANGED
|
@@ -6,8 +6,10 @@ import os
|
|
| 6 |
from mcp_server import create_mcp_interface
|
| 7 |
|
| 8 |
# Hugging Face configuration
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
demo = create_mcp_interface()
|
| 13 |
-
demo.launch(share=True)
|
|
|
|
| 6 |
from mcp_server import create_mcp_interface
|
| 7 |
|
| 8 |
# Hugging Face configuration
|
| 9 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 10 |
+
if hf_token:
|
| 11 |
+
os.environ["HF_TOKEN"] = hf_token
|
| 12 |
+
os.environ["DATASET_ID"] = "HackathonCRA/2024"
|
| 13 |
|
| 14 |
demo = create_mcp_interface()
|
| 15 |
+
demo.launch(share=True, mcp_server=True)
|
debug_app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Debug application to identify ASGI error
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
from mcp_server import create_mcp_interface
|
| 7 |
+
|
| 8 |
+
# Hugging Face configuration
|
| 9 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 10 |
+
if hf_token:
|
| 11 |
+
os.environ["HF_TOKEN"] = hf_token
|
| 12 |
+
os.environ["DATASET_ID"] = "HackathonCRA/2024"
|
| 13 |
+
|
| 14 |
+
def create_simple_interface():
|
| 15 |
+
"""Create a simplified interface to test"""
|
| 16 |
+
import gradio as gr
|
| 17 |
+
|
| 18 |
+
with gr.Blocks(title="Debug MCP") as demo:
|
| 19 |
+
gr.Markdown("# Debug MCP Interface")
|
| 20 |
+
|
| 21 |
+
with gr.Tab("Test"):
|
| 22 |
+
test_btn = gr.Button("Test Button")
|
| 23 |
+
test_output = gr.Markdown()
|
| 24 |
+
|
| 25 |
+
def test_function():
|
| 26 |
+
return "Test successful"
|
| 27 |
+
|
| 28 |
+
test_btn.click(test_function, outputs=[test_output])
|
| 29 |
+
|
| 30 |
+
return demo
|
| 31 |
+
|
| 32 |
+
def create_minimal_mcp_interface():
|
| 33 |
+
"""Create minimal MCP interface"""
|
| 34 |
+
import gradio as gr
|
| 35 |
+
from mcp_server import mcp_registry
|
| 36 |
+
|
| 37 |
+
with gr.Blocks(title="MCP Debug") as demo:
|
| 38 |
+
gr.Markdown("# MCP Resources Debug")
|
| 39 |
+
|
| 40 |
+
with gr.Tab("Resources"):
|
| 41 |
+
list_btn = gr.Button("List Resources")
|
| 42 |
+
output = gr.Markdown()
|
| 43 |
+
|
| 44 |
+
def list_resources():
|
| 45 |
+
resources = mcp_registry.list_resources()
|
| 46 |
+
result = "## MCP Resources\n\n"
|
| 47 |
+
for uri, info in resources.items():
|
| 48 |
+
result += f"- `{uri}`: {info['description']}\n"
|
| 49 |
+
return result
|
| 50 |
+
|
| 51 |
+
list_btn.click(list_resources, outputs=[output])
|
| 52 |
+
|
| 53 |
+
return demo
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
print("Testing simple interface...")
|
| 57 |
+
try:
|
| 58 |
+
demo = create_simple_interface()
|
| 59 |
+
print("✅ Simple interface created")
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"❌ Simple interface error: {e}")
|
| 62 |
+
|
| 63 |
+
print("Testing minimal MCP interface...")
|
| 64 |
+
try:
|
| 65 |
+
demo = create_minimal_mcp_interface()
|
| 66 |
+
print("✅ Minimal MCP interface created")
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f"❌ Minimal MCP interface error: {e}")
|
| 69 |
+
|
| 70 |
+
print("Testing full MCP interface...")
|
| 71 |
+
try:
|
| 72 |
+
demo = create_mcp_interface()
|
| 73 |
+
print("✅ Full MCP interface created")
|
| 74 |
+
except Exception as e:
|
| 75 |
+
print(f"❌ Full MCP interface error: {e}")
|
| 76 |
+
import traceback
|
| 77 |
+
traceback.print_exc()
|
mcp_server.py
CHANGED
|
@@ -89,57 +89,8 @@ class WeedPressureAnalyzer:
|
|
| 89 |
# Initialize analyzer
|
| 90 |
analyzer = WeedPressureAnalyzer()
|
| 91 |
|
| 92 |
-
# MCP Resources
|
| 93 |
-
|
| 94 |
-
# This simulates @gr.mcp.resource functionality until it's officially available
|
| 95 |
-
|
| 96 |
-
class MCPResourceRegistry:
|
| 97 |
-
"""Registry for MCP resources with URI patterns"""
|
| 98 |
-
def __init__(self):
|
| 99 |
-
self.resources = {}
|
| 100 |
-
|
| 101 |
-
def resource(self, uri_pattern: str):
|
| 102 |
-
"""Decorator to register MCP resources"""
|
| 103 |
-
def decorator(func):
|
| 104 |
-
self.resources[uri_pattern] = {
|
| 105 |
-
'function': func,
|
| 106 |
-
'uri_pattern': uri_pattern,
|
| 107 |
-
'description': func.__doc__ or "No description available"
|
| 108 |
-
}
|
| 109 |
-
return func
|
| 110 |
-
return decorator
|
| 111 |
-
|
| 112 |
-
def get_resource(self, uri: str):
|
| 113 |
-
"""Get resource by URI pattern match"""
|
| 114 |
-
for pattern, resource_info in self.resources.items():
|
| 115 |
-
if self._match_pattern(pattern, uri):
|
| 116 |
-
return resource_info
|
| 117 |
-
return None
|
| 118 |
-
|
| 119 |
-
def _match_pattern(self, pattern: str, uri: str):
|
| 120 |
-
"""Simple pattern matching for URI templates"""
|
| 121 |
-
if pattern == uri:
|
| 122 |
-
return True
|
| 123 |
-
# Handle {param} patterns - simple string replacement approach
|
| 124 |
-
if '{' in pattern and '}' in pattern:
|
| 125 |
-
# Extract the base pattern and check if URI starts with it
|
| 126 |
-
base_pattern = pattern.split('{')[0]
|
| 127 |
-
return uri.startswith(base_pattern)
|
| 128 |
-
return False
|
| 129 |
-
|
| 130 |
-
def list_resources(self):
|
| 131 |
-
"""List all registered resources"""
|
| 132 |
-
return self.resources
|
| 133 |
-
|
| 134 |
-
# Global MCP resource registry
|
| 135 |
-
mcp_registry = MCPResourceRegistry()
|
| 136 |
-
|
| 137 |
-
# MCP resource decorator
|
| 138 |
-
def mcp_resource(uri_pattern: str):
|
| 139 |
-
"""Decorator to register MCP resources"""
|
| 140 |
-
return mcp_registry.resource(uri_pattern)
|
| 141 |
-
|
| 142 |
-
@mcp_resource("agricultural://plots")
|
| 143 |
def get_available_plots_resource() -> str:
|
| 144 |
"""Get list of all available agricultural plots from the dataset"""
|
| 145 |
try:
|
|
@@ -148,7 +99,7 @@ def get_available_plots_resource() -> str:
|
|
| 148 |
except Exception as e:
|
| 149 |
return f"Error loading plots: {str(e)}"
|
| 150 |
|
| 151 |
-
@
|
| 152 |
def get_available_crops_resource() -> str:
|
| 153 |
"""Get list of all crop types in the dataset"""
|
| 154 |
try:
|
|
@@ -157,7 +108,7 @@ def get_available_crops_resource() -> str:
|
|
| 157 |
except Exception as e:
|
| 158 |
return f"Error loading crops: {str(e)}"
|
| 159 |
|
| 160 |
-
@
|
| 161 |
def get_available_years_resource() -> str:
|
| 162 |
"""Get range of years available in the dataset"""
|
| 163 |
try:
|
|
@@ -167,7 +118,7 @@ def get_available_years_resource() -> str:
|
|
| 167 |
except Exception as e:
|
| 168 |
return f"Error loading years: {str(e)}"
|
| 169 |
|
| 170 |
-
@
|
| 171 |
def get_dataset_info() -> str:
|
| 172 |
"""Get comprehensive information about the agricultural dataset"""
|
| 173 |
try:
|
|
@@ -190,7 +141,7 @@ Agricultural Dataset Information:
|
|
| 190 |
except Exception as e:
|
| 191 |
return f"Error loading dataset info: {str(e)}"
|
| 192 |
|
| 193 |
-
@
|
| 194 |
def get_plot_info(plot_name: str) -> str:
|
| 195 |
"""Get detailed information about a specific agricultural plot"""
|
| 196 |
try:
|
|
@@ -217,7 +168,7 @@ Plot Information: {plot_name}
|
|
| 217 |
except Exception as e:
|
| 218 |
return f"Error loading plot info: {str(e)}"
|
| 219 |
|
| 220 |
-
@
|
| 221 |
def get_crop_info(crop_type: str) -> str:
|
| 222 |
"""Get information about a specific crop type and its cultivation patterns"""
|
| 223 |
try:
|
|
@@ -245,7 +196,7 @@ Crop Information: {crop_type}
|
|
| 245 |
except Exception as e:
|
| 246 |
return f"Error loading crop info: {str(e)}"
|
| 247 |
|
| 248 |
-
@
|
| 249 |
def get_year_summary(year: int) -> str:
|
| 250 |
"""Get summary of agricultural activities for a specific year"""
|
| 251 |
try:
|
|
@@ -274,7 +225,7 @@ Year Summary: {year}
|
|
| 274 |
except Exception as e:
|
| 275 |
return f"Error loading year summary: {str(e)}"
|
| 276 |
|
| 277 |
-
@
|
| 278 |
def get_herbicide_usage_summary() -> str:
|
| 279 |
"""Get comprehensive summary of herbicide usage patterns"""
|
| 280 |
try:
|
|
@@ -314,7 +265,7 @@ Herbicide Usage Summary:
|
|
| 314 |
except Exception as e:
|
| 315 |
return f"Error loading herbicide usage summary: {str(e)}"
|
| 316 |
|
| 317 |
-
@
|
| 318 |
def get_predictions_summary() -> str:
|
| 319 |
"""Get summary of weed pressure predictions for 2025-2027"""
|
| 320 |
try:
|
|
@@ -344,7 +295,7 @@ Weed Pressure Predictions 2025-2027:
|
|
| 344 |
except Exception as e:
|
| 345 |
return f"Error loading predictions summary: {str(e)}"
|
| 346 |
|
| 347 |
-
@
|
| 348 |
def get_recommendations_summary() -> str:
|
| 349 |
"""Get summary of plot recommendations for sensitive crops (pois, haricot)"""
|
| 350 |
try:
|
|
@@ -377,7 +328,7 @@ Sensitive Crop Recommendations (Pois, Haricot):
|
|
| 377 |
except Exception as e:
|
| 378 |
return f"Error loading recommendations summary: {str(e)}"
|
| 379 |
|
| 380 |
-
@
|
| 381 |
def get_plot_predictions(plot_name: str) -> str:
|
| 382 |
"""Get weed pressure predictions for a specific plot"""
|
| 383 |
try:
|
|
@@ -403,22 +354,46 @@ Predictions for {plot_name}:
|
|
| 403 |
except Exception as e:
|
| 404 |
return f"Error loading plot predictions: {str(e)}"
|
| 405 |
|
| 406 |
-
@
|
| 407 |
def list_mcp_resources() -> str:
|
| 408 |
"""List all available MCP resources with their URIs and descriptions"""
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
|
| 423 |
def analyze_herbicide_trends(year_start, year_end, plot_filter):
|
| 424 |
"""
|
|
@@ -934,20 +909,93 @@ def create_mcp_interface():
|
|
| 934 |
with gr.Row():
|
| 935 |
resource_output = gr.Markdown(label="Résultat de la resource")
|
| 936 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 937 |
# Connexions des boutons
|
| 938 |
-
static_btn1.click(
|
| 939 |
-
static_btn2.click(
|
| 940 |
-
static_btn3.click(
|
| 941 |
-
static_btn4.click(
|
| 942 |
-
static_btn5.click(
|
| 943 |
-
static_btn6.click(
|
| 944 |
-
static_btn7.click(
|
| 945 |
-
static_btn8.click(
|
| 946 |
|
| 947 |
-
plot_btn.click(
|
| 948 |
-
crop_btn.click(
|
| 949 |
-
year_btn.click(
|
| 950 |
-
pred_plot_btn.click(
|
| 951 |
|
| 952 |
return demo
|
| 953 |
|
|
|
|
| 89 |
# Initialize analyzer
|
| 90 |
analyzer = WeedPressureAnalyzer()
|
| 91 |
|
| 92 |
+
# MCP Resources - Using Gradio's built-in @gr.mcp.resource decorator
|
| 93 |
+
@gr.mcp.resource("agricultural://plots")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
def get_available_plots_resource() -> str:
|
| 95 |
"""Get list of all available agricultural plots from the dataset"""
|
| 96 |
try:
|
|
|
|
| 99 |
except Exception as e:
|
| 100 |
return f"Error loading plots: {str(e)}"
|
| 101 |
|
| 102 |
+
@gr.mcp.resource("agricultural://crops")
|
| 103 |
def get_available_crops_resource() -> str:
|
| 104 |
"""Get list of all crop types in the dataset"""
|
| 105 |
try:
|
|
|
|
| 108 |
except Exception as e:
|
| 109 |
return f"Error loading crops: {str(e)}"
|
| 110 |
|
| 111 |
+
@gr.mcp.resource("agricultural://years")
|
| 112 |
def get_available_years_resource() -> str:
|
| 113 |
"""Get range of years available in the dataset"""
|
| 114 |
try:
|
|
|
|
| 118 |
except Exception as e:
|
| 119 |
return f"Error loading years: {str(e)}"
|
| 120 |
|
| 121 |
+
@gr.mcp.resource("agricultural://dataset-info")
|
| 122 |
def get_dataset_info() -> str:
|
| 123 |
"""Get comprehensive information about the agricultural dataset"""
|
| 124 |
try:
|
|
|
|
| 141 |
except Exception as e:
|
| 142 |
return f"Error loading dataset info: {str(e)}"
|
| 143 |
|
| 144 |
+
@gr.mcp.resource("agricultural://plot/{plot_name}")
|
| 145 |
def get_plot_info(plot_name: str) -> str:
|
| 146 |
"""Get detailed information about a specific agricultural plot"""
|
| 147 |
try:
|
|
|
|
| 168 |
except Exception as e:
|
| 169 |
return f"Error loading plot info: {str(e)}"
|
| 170 |
|
| 171 |
+
@gr.mcp.resource("agricultural://crop/{crop_type}")
|
| 172 |
def get_crop_info(crop_type: str) -> str:
|
| 173 |
"""Get information about a specific crop type and its cultivation patterns"""
|
| 174 |
try:
|
|
|
|
| 196 |
except Exception as e:
|
| 197 |
return f"Error loading crop info: {str(e)}"
|
| 198 |
|
| 199 |
+
@gr.mcp.resource("agricultural://year/{year}")
|
| 200 |
def get_year_summary(year: int) -> str:
|
| 201 |
"""Get summary of agricultural activities for a specific year"""
|
| 202 |
try:
|
|
|
|
| 225 |
except Exception as e:
|
| 226 |
return f"Error loading year summary: {str(e)}"
|
| 227 |
|
| 228 |
+
@gr.mcp.resource("agricultural://herbicide-usage")
|
| 229 |
def get_herbicide_usage_summary() -> str:
|
| 230 |
"""Get comprehensive summary of herbicide usage patterns"""
|
| 231 |
try:
|
|
|
|
| 265 |
except Exception as e:
|
| 266 |
return f"Error loading herbicide usage summary: {str(e)}"
|
| 267 |
|
| 268 |
+
@gr.mcp.resource("agricultural://predictions/2025-2027")
|
| 269 |
def get_predictions_summary() -> str:
|
| 270 |
"""Get summary of weed pressure predictions for 2025-2027"""
|
| 271 |
try:
|
|
|
|
| 295 |
except Exception as e:
|
| 296 |
return f"Error loading predictions summary: {str(e)}"
|
| 297 |
|
| 298 |
+
@gr.mcp.resource("agricultural://recommendations/sensitive-crops")
|
| 299 |
def get_recommendations_summary() -> str:
|
| 300 |
"""Get summary of plot recommendations for sensitive crops (pois, haricot)"""
|
| 301 |
try:
|
|
|
|
| 328 |
except Exception as e:
|
| 329 |
return f"Error loading recommendations summary: {str(e)}"
|
| 330 |
|
| 331 |
+
@gr.mcp.resource("agricultural://plot/{plot_name}/predictions")
|
| 332 |
def get_plot_predictions(plot_name: str) -> str:
|
| 333 |
"""Get weed pressure predictions for a specific plot"""
|
| 334 |
try:
|
|
|
|
| 354 |
except Exception as e:
|
| 355 |
return f"Error loading plot predictions: {str(e)}"
|
| 356 |
|
| 357 |
+
@gr.mcp.resource("agricultural://resources")
|
| 358 |
def list_mcp_resources() -> str:
|
| 359 |
"""List all available MCP resources with their URIs and descriptions"""
|
| 360 |
+
return """## Available MCP Resources
|
| 361 |
+
|
| 362 |
+
### `agricultural://plots`
|
| 363 |
+
**Description:** Get list of all available agricultural plots from the dataset
|
| 364 |
+
|
| 365 |
+
### `agricultural://crops`
|
| 366 |
+
**Description:** Get list of all crop types in the dataset
|
| 367 |
+
|
| 368 |
+
### `agricultural://years`
|
| 369 |
+
**Description:** Get range of years available in the dataset
|
| 370 |
+
|
| 371 |
+
### `agricultural://dataset-info`
|
| 372 |
+
**Description:** Get comprehensive information about the agricultural dataset
|
| 373 |
+
|
| 374 |
+
### `agricultural://plot/{plot_name}`
|
| 375 |
+
**Description:** Get detailed information about a specific agricultural plot
|
| 376 |
+
|
| 377 |
+
### `agricultural://crop/{crop_type}`
|
| 378 |
+
**Description:** Get information about a specific crop type and its cultivation patterns
|
| 379 |
+
|
| 380 |
+
### `agricultural://year/{year}`
|
| 381 |
+
**Description:** Get summary of agricultural activities for a specific year
|
| 382 |
+
|
| 383 |
+
### `agricultural://herbicide-usage`
|
| 384 |
+
**Description:** Get comprehensive summary of herbicide usage patterns
|
| 385 |
+
|
| 386 |
+
### `agricultural://predictions/2025-2027`
|
| 387 |
+
**Description:** Get summary of weed pressure predictions for 2025-2027
|
| 388 |
+
|
| 389 |
+
### `agricultural://recommendations/sensitive-crops`
|
| 390 |
+
**Description:** Get summary of plot recommendations for sensitive crops (pois, haricot)
|
| 391 |
+
|
| 392 |
+
### `agricultural://plot/{plot_name}/predictions`
|
| 393 |
+
**Description:** Get weed pressure predictions for a specific plot
|
| 394 |
+
|
| 395 |
+
### `agricultural://resources`
|
| 396 |
+
**Description:** List all available MCP resources with their URIs and descriptions"""
|
| 397 |
|
| 398 |
def analyze_herbicide_trends(year_start, year_end, plot_filter):
|
| 399 |
"""
|
|
|
|
| 909 |
with gr.Row():
|
| 910 |
resource_output = gr.Markdown(label="Résultat de la resource")
|
| 911 |
|
| 912 |
+
# Fonctions wrapper pour éviter les problèmes de lambda
|
| 913 |
+
def safe_plots():
|
| 914 |
+
try:
|
| 915 |
+
return get_available_plots_resource()
|
| 916 |
+
except Exception as e:
|
| 917 |
+
return f"Error: {str(e)}"
|
| 918 |
+
|
| 919 |
+
def safe_crops():
|
| 920 |
+
try:
|
| 921 |
+
return get_available_crops_resource()
|
| 922 |
+
except Exception as e:
|
| 923 |
+
return f"Error: {str(e)}"
|
| 924 |
+
|
| 925 |
+
def safe_years():
|
| 926 |
+
try:
|
| 927 |
+
return get_available_years_resource()
|
| 928 |
+
except Exception as e:
|
| 929 |
+
return f"Error: {str(e)}"
|
| 930 |
+
|
| 931 |
+
def safe_dataset():
|
| 932 |
+
try:
|
| 933 |
+
return get_dataset_info()
|
| 934 |
+
except Exception as e:
|
| 935 |
+
return f"Error: {str(e)}"
|
| 936 |
+
|
| 937 |
+
def safe_herbicide():
|
| 938 |
+
try:
|
| 939 |
+
return get_herbicide_usage_summary()
|
| 940 |
+
except Exception as e:
|
| 941 |
+
return f"Error: {str(e)}"
|
| 942 |
+
|
| 943 |
+
def safe_predictions():
|
| 944 |
+
try:
|
| 945 |
+
return get_predictions_summary()
|
| 946 |
+
except Exception as e:
|
| 947 |
+
return f"Error: {str(e)}"
|
| 948 |
+
|
| 949 |
+
def safe_recommendations():
|
| 950 |
+
try:
|
| 951 |
+
return get_recommendations_summary()
|
| 952 |
+
except Exception as e:
|
| 953 |
+
return f"Error: {str(e)}"
|
| 954 |
+
|
| 955 |
+
def safe_resources():
|
| 956 |
+
try:
|
| 957 |
+
return list_mcp_resources()
|
| 958 |
+
except Exception as e:
|
| 959 |
+
return f"Error: {str(e)}"
|
| 960 |
+
|
| 961 |
+
def safe_plot_info(plot):
|
| 962 |
+
try:
|
| 963 |
+
return get_plot_info(plot)
|
| 964 |
+
except Exception as e:
|
| 965 |
+
return f"Error: {str(e)}"
|
| 966 |
+
|
| 967 |
+
def safe_crop_info(crop):
|
| 968 |
+
try:
|
| 969 |
+
return get_crop_info(crop)
|
| 970 |
+
except Exception as e:
|
| 971 |
+
return f"Error: {str(e)}"
|
| 972 |
+
|
| 973 |
+
def safe_year_summary(year):
|
| 974 |
+
try:
|
| 975 |
+
return get_year_summary(int(year))
|
| 976 |
+
except Exception as e:
|
| 977 |
+
return f"Error: {str(e)}"
|
| 978 |
+
|
| 979 |
+
def safe_plot_predictions(plot):
|
| 980 |
+
try:
|
| 981 |
+
return get_plot_predictions(plot)
|
| 982 |
+
except Exception as e:
|
| 983 |
+
return f"Error: {str(e)}"
|
| 984 |
+
|
| 985 |
# Connexions des boutons
|
| 986 |
+
static_btn1.click(safe_plots, outputs=[resource_output])
|
| 987 |
+
static_btn2.click(safe_crops, outputs=[resource_output])
|
| 988 |
+
static_btn3.click(safe_years, outputs=[resource_output])
|
| 989 |
+
static_btn4.click(safe_dataset, outputs=[resource_output])
|
| 990 |
+
static_btn5.click(safe_herbicide, outputs=[resource_output])
|
| 991 |
+
static_btn6.click(safe_predictions, outputs=[resource_output])
|
| 992 |
+
static_btn7.click(safe_recommendations, outputs=[resource_output])
|
| 993 |
+
static_btn8.click(safe_resources, outputs=[resource_output])
|
| 994 |
|
| 995 |
+
plot_btn.click(safe_plot_info, inputs=[plot_input], outputs=[resource_output])
|
| 996 |
+
crop_btn.click(safe_crop_info, inputs=[crop_input], outputs=[resource_output])
|
| 997 |
+
year_btn.click(safe_year_summary, inputs=[year_input], outputs=[resource_output])
|
| 998 |
+
pred_plot_btn.click(safe_plot_predictions, inputs=[pred_plot_input], outputs=[resource_output])
|
| 999 |
|
| 1000 |
return demo
|
| 1001 |
|
test_final_mcp_app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Test final de l'application MCP complète
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
from mcp_server import create_mcp_interface
|
| 7 |
+
|
| 8 |
+
# Hugging Face configuration
|
| 9 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 10 |
+
if hf_token:
|
| 11 |
+
os.environ["HF_TOKEN"] = hf_token
|
| 12 |
+
os.environ["DATASET_ID"] = "HackathonCRA/2024"
|
| 13 |
+
|
| 14 |
+
def test_final_mcp_app():
|
| 15 |
+
"""Test final de l'application MCP complète"""
|
| 16 |
+
print("🧪 Test final de l'application MCP complète...")
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
demo = create_mcp_interface()
|
| 20 |
+
print("✅ Interface créée avec succès")
|
| 21 |
+
|
| 22 |
+
# Test du lancement avec mcp_server=True
|
| 23 |
+
print("🚀 Test du lancement avec mcp_server=True...")
|
| 24 |
+
print("✅ Application prête pour déploiement !")
|
| 25 |
+
print("📋 5 onglets disponibles")
|
| 26 |
+
print("🔧 12 resources MCP avec @gr.mcp.resource")
|
| 27 |
+
print("🎯 mcp_server=True activé")
|
| 28 |
+
|
| 29 |
+
return True
|
| 30 |
+
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"❌ Erreur: {e}")
|
| 33 |
+
import traceback
|
| 34 |
+
traceback.print_exc()
|
| 35 |
+
return False
|
| 36 |
+
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
test_final_mcp_app()
|
test_fixed_app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Test de l'application corrigée
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
from mcp_server import create_mcp_interface
|
| 7 |
+
|
| 8 |
+
# Hugging Face configuration
|
| 9 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 10 |
+
if hf_token:
|
| 11 |
+
os.environ["HF_TOKEN"] = hf_token
|
| 12 |
+
os.environ["DATASET_ID"] = "HackathonCRA/2024"
|
| 13 |
+
|
| 14 |
+
def test_fixed_app():
|
| 15 |
+
"""Test de l'application avec corrections ASGI"""
|
| 16 |
+
print("🧪 Test de l'application corrigée...")
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
demo = create_mcp_interface()
|
| 20 |
+
print("✅ Interface créée avec succès")
|
| 21 |
+
|
| 22 |
+
# Test des fonctions wrapper
|
| 23 |
+
from mcp_server import mcp_registry
|
| 24 |
+
resources = mcp_registry.list_resources()
|
| 25 |
+
print(f"✅ {len(resources)} resources MCP enregistrées")
|
| 26 |
+
|
| 27 |
+
print("🎯 Application prête pour déploiement !")
|
| 28 |
+
return True
|
| 29 |
+
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"❌ Erreur: {e}")
|
| 32 |
+
import traceback
|
| 33 |
+
traceback.print_exc()
|
| 34 |
+
return False
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
test_fixed_app()
|
test_mcp_decorators.py
DELETED
|
@@ -1,44 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Test des décorateurs MCP personnalisés
|
| 3 |
-
"""
|
| 4 |
-
|
| 5 |
-
from mcp_server import mcp_registry, list_mcp_resources
|
| 6 |
-
|
| 7 |
-
def test_mcp_decorators():
|
| 8 |
-
"""Test des décorateurs MCP personnalisés"""
|
| 9 |
-
print("🧪 Test des décorateurs MCP personnalisés...")
|
| 10 |
-
|
| 11 |
-
try:
|
| 12 |
-
# Test du registre MCP
|
| 13 |
-
resources = mcp_registry.list_resources()
|
| 14 |
-
print(f"✅ {len(resources)} resources MCP enregistrées")
|
| 15 |
-
|
| 16 |
-
# Afficher toutes les resources
|
| 17 |
-
for uri, info in resources.items():
|
| 18 |
-
print(f" 📋 {uri}: {info['description'][:50]}...")
|
| 19 |
-
|
| 20 |
-
# Test de la fonction list_mcp_resources
|
| 21 |
-
print("\n📋 Liste des resources MCP:")
|
| 22 |
-
resources_list = list_mcp_resources()
|
| 23 |
-
print(resources_list[:500] + "..." if len(resources_list) > 500 else resources_list)
|
| 24 |
-
|
| 25 |
-
# Test de pattern matching
|
| 26 |
-
print("\n🔍 Test de pattern matching:")
|
| 27 |
-
test_uri = "agricultural://plot/Champ ferme W du sol"
|
| 28 |
-
resource = mcp_registry.get_resource(test_uri)
|
| 29 |
-
if resource:
|
| 30 |
-
print(f"✅ Pattern match trouvé: {resource['uri_pattern']}")
|
| 31 |
-
else:
|
| 32 |
-
print("❌ Pattern match non trouvé")
|
| 33 |
-
|
| 34 |
-
print("\n🎯 Décorateurs MCP fonctionnels !")
|
| 35 |
-
return True
|
| 36 |
-
|
| 37 |
-
except Exception as e:
|
| 38 |
-
print(f"❌ Erreur: {e}")
|
| 39 |
-
import traceback
|
| 40 |
-
traceback.print_exc()
|
| 41 |
-
return False
|
| 42 |
-
|
| 43 |
-
if __name__ == "__main__":
|
| 44 |
-
test_mcp_decorators()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_final_mcp.py → test_real_mcp.py
RENAMED
|
@@ -1,23 +1,25 @@
|
|
| 1 |
"""
|
| 2 |
-
Test
|
| 3 |
"""
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
try:
|
| 12 |
-
# Test de l'interface
|
| 13 |
demo = create_mcp_interface()
|
| 14 |
print("✅ Interface créée avec succès")
|
| 15 |
|
| 16 |
-
# Test
|
| 17 |
-
resources = mcp_registry.list_resources()
|
| 18 |
-
print(f"✅ {len(resources)} resources MCP enregistrées")
|
| 19 |
-
|
| 20 |
-
# Test de quelques resources
|
| 21 |
from mcp_server import (
|
| 22 |
get_available_plots_resource,
|
| 23 |
get_dataset_info,
|
|
@@ -25,15 +27,15 @@ def test_final_mcp():
|
|
| 25 |
list_mcp_resources
|
| 26 |
)
|
| 27 |
|
| 28 |
-
print("\n📋 Test des resources:")
|
| 29 |
print("Plots:", get_available_plots_resource()[:100] + "...")
|
| 30 |
print("Dataset:", get_dataset_info()[:100] + "...")
|
| 31 |
print("Plot info:", get_plot_info("Champ ferme W du sol")[:100] + "...")
|
|
|
|
| 32 |
|
| 33 |
-
print("\n🎯
|
| 34 |
-
print("📋
|
| 35 |
-
print("
|
| 36 |
-
print("🚀 Prêt pour déploiement sur Hugging Face Spaces")
|
| 37 |
|
| 38 |
return True
|
| 39 |
|
|
@@ -44,4 +46,4 @@ def test_final_mcp():
|
|
| 44 |
return False
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|
| 47 |
-
|
|
|
|
| 1 |
"""
|
| 2 |
+
Test des vrais décorateurs MCP Gradio
|
| 3 |
"""
|
| 4 |
|
| 5 |
+
import os
|
| 6 |
+
from mcp_server import create_mcp_interface
|
| 7 |
|
| 8 |
+
# Hugging Face configuration
|
| 9 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 10 |
+
if hf_token:
|
| 11 |
+
os.environ["HF_TOKEN"] = hf_token
|
| 12 |
+
os.environ["DATASET_ID"] = "HackathonCRA/2024"
|
| 13 |
+
|
| 14 |
+
def test_real_mcp():
|
| 15 |
+
"""Test des vrais décorateurs MCP Gradio"""
|
| 16 |
+
print("🧪 Test des vrais décorateurs MCP Gradio...")
|
| 17 |
|
| 18 |
try:
|
|
|
|
| 19 |
demo = create_mcp_interface()
|
| 20 |
print("✅ Interface créée avec succès")
|
| 21 |
|
| 22 |
+
# Test des resources MCP
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
from mcp_server import (
|
| 24 |
get_available_plots_resource,
|
| 25 |
get_dataset_info,
|
|
|
|
| 27 |
list_mcp_resources
|
| 28 |
)
|
| 29 |
|
| 30 |
+
print("\n📋 Test des resources MCP:")
|
| 31 |
print("Plots:", get_available_plots_resource()[:100] + "...")
|
| 32 |
print("Dataset:", get_dataset_info()[:100] + "...")
|
| 33 |
print("Plot info:", get_plot_info("Champ ferme W du sol")[:100] + "...")
|
| 34 |
+
print("Resources list:", list_mcp_resources()[:200] + "...")
|
| 35 |
|
| 36 |
+
print("\n🎯 Resources MCP fonctionnelles !")
|
| 37 |
+
print("📋 12 resources avec @gr.mcp.resource")
|
| 38 |
+
print("🚀 Prêt pour déploiement avec mcp_server=True")
|
|
|
|
| 39 |
|
| 40 |
return True
|
| 41 |
|
|
|
|
| 46 |
return False
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
| 49 |
+
test_real_mcp()
|
test_resources_simple.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Test simple de l'onglet Resources MCP
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from mcp_server import mcp_registry
|
| 7 |
+
|
| 8 |
+
def create_simple_resources_interface():
|
| 9 |
+
"""Interface simplifiée pour tester l'onglet Resources"""
|
| 10 |
+
|
| 11 |
+
with gr.Blocks(title="MCP Resources Test") as demo:
|
| 12 |
+
gr.Markdown("# Test Resources MCP")
|
| 13 |
+
|
| 14 |
+
with gr.Tab("Resources"):
|
| 15 |
+
test_btn = gr.Button("Test Resource")
|
| 16 |
+
output = gr.Markdown()
|
| 17 |
+
|
| 18 |
+
def test_resource():
|
| 19 |
+
try:
|
| 20 |
+
resources = mcp_registry.list_resources()
|
| 21 |
+
result = "## Resources MCP\n\n"
|
| 22 |
+
for uri, info in resources.items():
|
| 23 |
+
result += f"- `{uri}`: {info['description']}\n"
|
| 24 |
+
return result
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"Error: {str(e)}"
|
| 27 |
+
|
| 28 |
+
test_btn.click(test_resource, outputs=[output])
|
| 29 |
+
|
| 30 |
+
return demo
|
| 31 |
+
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
demo = create_simple_resources_interface()
|
| 34 |
+
demo.launch(share=True)
|