Spaces:
Runtime error
Runtime error
| import asyncio | |
| import pandas as pd | |
| from huggingface_hub import HfApi, hf_hub_download | |
| from mcp.server import Server | |
| from mcp.types import Resource, ResourceResponse | |
| import os | |
| server = Server("csv-provider") | |
| HF_TOKEN = os.environ.get("HF_TOKEN") | |
| api = HfApi() | |
| # Resource: csv://repo_id/path/to/file.csv | |
| async def get_csv(repo_id: str, path: str) -> ResourceResponse: | |
| try: | |
| # Télécharger le fichier CSV depuis le dataset privé | |
| local_path = hf_hub_download( | |
| repo_id=repo_id, | |
| filename=path, | |
| repo_type="dataset", | |
| token=HF_TOKEN | |
| ) | |
| # Charger et prévisualiser le CSV | |
| df = pd.read_csv(local_path) | |
| preview = df.head(5).to_string() | |
| return ResourceResponse( | |
| resource=Resource( | |
| uri=f"csv://{repo_id}/{path}", | |
| name=f"CSV: {path}", | |
| description=f"Aperçu des données du dataset {repo_id}", | |
| mimeType="text/plain", | |
| text=preview, | |
| ) | |
| ) | |
| except Exception as e: | |
| return ResourceResponse( | |
| resource=Resource( | |
| uri=f"csv://{repo_id}/{path}", | |
| name=f"Erreur CSV", | |
| description=str(e), | |
| mimeType="text/plain", | |
| text=f"Impossible de lire {repo_id}/{path} : {e}", | |
| ) | |
| ) | |
| if __name__ == "__main__": | |
| asyncio.run(server.run()) | |