from __future__ import annotations from typing import Literal from fastapi import APIRouter, HTTPException, Query from app.config import CURATED_MODELS, CURATED_RERANKERS, STARTER_PRESETS from app.engine.hardware import detect_hardware from app.engine.hf_hub import search_hf_datasets, search_hf_models from app.engine.ingest import inspect_hf_dataset from app.schemas import ( HFDatasetSearchResponse, HFInspectRequest, HFInspectResponse, HFModelSearchResponse, ) router = APIRouter(prefix="/api", tags=["datasets"]) @router.get("/hardware") def get_hardware(): return { "hardware": detect_hardware().model_dump(), "models": CURATED_MODELS, "rerankers": CURATED_RERANKERS, "presets": STARTER_PRESETS, } @router.get("/hf/datasets/search", response_model=HFDatasetSearchResponse) def search_datasets( q: str = Query(default="", max_length=120), limit: int = Query(default=12, ge=1, le=30), ): return search_hf_datasets(query=q, limit=limit) @router.get("/hf/models/search", response_model=HFModelSearchResponse) def search_models( q: str = Query(default="", max_length=120), task: Literal["embedding", "reranker"] = Query(default="embedding"), limit: int = Query(default=15, ge=1, le=30), ): return search_hf_models(query=q, task=task, limit=limit) @router.post("/hf/inspect", response_model=HFInspectResponse) def inspect_dataset(req: HFInspectRequest): try: return inspect_hf_dataset( dataset_id=req.dataset_id.strip(), config=req.config, split=req.split, sample_rows=req.sample_rows, ) except Exception as exc: raise HTTPException( status_code=400, detail=f"Failed to inspect Hugging Face dataset '{req.dataset_id}': {exc}", ) from exc