Download app/routers/ingest.py from acpcchatbotadmin/acpcchatbot_testing_first: direct link, hf CLI and curl.
- Browser
- Download file 2.16 kB
-
https://huggingface.co/spaces/acpcchatbotadmin/acpcchatbot_testing_first/resolve/main/app/routers/ingest.py
- Command line
-
hf download hf://spaces/acpcchatbotadmin/acpcchatbot_testing_first/app/routers/ingest.py
-
curl -L -o ingest.py https://huggingface.co/spaces/acpcchatbotadmin/acpcchatbot_testing_first/resolve/main/app/routers/ingest.py
2.16 kB
| import logging | |
| from fastapi import APIRouter, HTTPException, Header | |
| from app.retrieval.ingestion import ingest_file, ingest_directory, reset_collection | |
| from app.schemas.ingest import IngestFileRequest, IngestDirectoryRequest, IngestResponse | |
| from app.config import settings | |
| router = APIRouter(prefix="/api/ingest", tags=["ingest"]) | |
| logger = logging.getLogger(__name__) | |
| def _verify_ingest_key(x_api_key: str | None) -> None: | |
| """Protect ingest endpoints with an API key if configured.""" | |
| if settings.ingest_api_key and x_api_key != settings.ingest_api_key: | |
| raise HTTPException(status_code=401, detail="Invalid or missing ingest API key.") | |
| async def ingest_single_file( | |
| request: IngestFileRequest, | |
| x_api_key: str | None = Header(default=None), | |
| ): | |
| _verify_ingest_key(x_api_key) | |
| try: | |
| chunks = ingest_file(request.file_path) | |
| return IngestResponse(success=True, total_documents=1, total_chunks=chunks) | |
| except Exception as e: | |
| logger.error("Ingest file error: %s", e) | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def ingest_dir( | |
| request: IngestDirectoryRequest, | |
| x_api_key: str | None = Header(default=None), | |
| ): | |
| _verify_ingest_key(x_api_key) | |
| try: | |
| result = ingest_directory(request.directory_path, recursive=request.recursive) | |
| return IngestResponse(success=True, **result) | |
| except Exception as e: | |
| logger.error("Ingest directory error: %s", e) | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def reset(x_api_key: str | None = Header(default=None)): | |
| _verify_ingest_key(x_api_key) | |
| try: | |
| reset_collection() | |
| return IngestResponse( | |
| success=True, | |
| message=f"Collection '{settings.collection_name}' reset successfully.", | |
| ) | |
| except Exception as e: | |
| logger.error("Reset error: %s", e) | |
| raise HTTPException(status_code=500, detail=str(e)) | |