Download app.py from 1990two/XTOOLS: direct link, hf CLI and curl.
- Browser
- Download file 1.6 kB
-
https://huggingface.co/spaces/1990two/XTOOLS/resolve/main/app.py
- Command line
-
hf download hf://spaces/1990two/XTOOLS/app.py
-
curl -L -o app.py https://huggingface.co/spaces/1990two/XTOOLS/resolve/main/app.py
1.6 kB
| import importlib | |
| from fastapi import FastAPI, HTTPException | |
| from registry import TOOLS, BY_NAME | |
| app = FastAPI(title="XTOOLS", description="90two tool server") | |
| _registry_cache = {} | |
| def _load(tool_name: str): | |
| if tool_name not in _registry_cache: | |
| mod = importlib.import_module(f"tools.{tool_name}") | |
| _registry_cache[tool_name] = mod.run | |
| return _registry_cache[tool_name] | |
| def _validate(tool_name: str, params: dict) -> dict: | |
| spec = BY_NAME[tool_name] | |
| cleaned = {} | |
| for p in spec["params"]: | |
| name = p["name"] | |
| if name in params: | |
| cleaned[name] = params[name] | |
| elif p["required"]: | |
| raise HTTPException(status_code=422, detail=f"Missing required param: {name}") | |
| else: | |
| cleaned[name] = p["default"] | |
| return cleaned | |
| def health(): | |
| return {"status": "ok", "tool_count": len(TOOLS)} | |
| def get_registry(): | |
| """Full tool manifest -- name, category, auth type, and param schema for every tool.""" | |
| return {"tools": TOOLS} | |
| def call_tool(tool_name: str, params: dict = None): | |
| if tool_name not in BY_NAME: | |
| raise HTTPException(status_code=404, detail=f"Unknown tool: {tool_name}") | |
| params = params or {} | |
| cleaned = _validate(tool_name, params) | |
| run_fn = _load(tool_name) | |
| try: | |
| return run_fn(cleaned) | |
| except ValueError as e: | |
| raise HTTPException(status_code=400, detail=str(e)) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"{type(e).__name__}: {e}") | |