Spaces:
Runtime error
Runtime error
Eddyhzd
commited on
Commit
·
05183f1
1
Parent(s):
f14db60
ADD: ajout du mcp
Browse files- csv_provider.py +50 -0
- mcp.json +10 -0
csv_provider.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 4 |
+
from mcp.server import Server
|
| 5 |
+
from mcp.types import Resource, ResourceResponse
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
server = Server("csv-provider")
|
| 9 |
+
|
| 10 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 11 |
+
api = HfApi()
|
| 12 |
+
|
| 13 |
+
# Resource: csv://repo_id/path/to/file.csv
|
| 14 |
+
@server.resource("csv://{repo_id}/{path}")
|
| 15 |
+
async def get_csv(repo_id: str, path: str) -> ResourceResponse:
|
| 16 |
+
try:
|
| 17 |
+
# Télécharger le fichier CSV depuis le dataset privé
|
| 18 |
+
local_path = hf_hub_download(
|
| 19 |
+
repo_id=repo_id,
|
| 20 |
+
filename=path,
|
| 21 |
+
repo_type="dataset",
|
| 22 |
+
token=HF_TOKEN
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Charger et prévisualiser le CSV
|
| 26 |
+
df = pd.read_csv(local_path)
|
| 27 |
+
preview = df.head(5).to_string()
|
| 28 |
+
|
| 29 |
+
return ResourceResponse(
|
| 30 |
+
resource=Resource(
|
| 31 |
+
uri=f"csv://{repo_id}/{path}",
|
| 32 |
+
name=f"CSV: {path}",
|
| 33 |
+
description=f"Aperçu des données du dataset {repo_id}",
|
| 34 |
+
mimeType="text/plain",
|
| 35 |
+
text=preview,
|
| 36 |
+
)
|
| 37 |
+
)
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return ResourceResponse(
|
| 40 |
+
resource=Resource(
|
| 41 |
+
uri=f"csv://{repo_id}/{path}",
|
| 42 |
+
name=f"Erreur CSV",
|
| 43 |
+
description=str(e),
|
| 44 |
+
mimeType="text/plain",
|
| 45 |
+
text=f"Impossible de lire {repo_id}/{path} : {e}",
|
| 46 |
+
)
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
asyncio.run(server.run())
|
mcp.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"providers": {
|
| 3 |
+
"csv": {
|
| 4 |
+
"command": "python",
|
| 5 |
+
"args": [
|
| 6 |
+
"csv_provider.py"
|
| 7 |
+
]
|
| 8 |
+
}
|
| 9 |
+
}
|
| 10 |
+
}
|