File size: 1,956 Bytes
bbe01fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# backend/app/api/admin.py
# Admin-only endpoints. Not exposed in public docs.
# Used exclusively by the retrain_reranker GitHub Actions workflow to pull
# the live interaction SQLite DB without needing HF Space persistent-storage
# access credentials beyond what the workflow already holds.

import os

from fastapi import APIRouter, Depends, HTTPException, Request, status
from fastapi.responses import FileResponse
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer

from app.core.config import get_settings
from app.core.logging import get_logger

router = APIRouter()
logger = get_logger(__name__)
_bearer = HTTPBearer()


def _require_admin(credentials: HTTPAuthorizationCredentials = Depends(_bearer)) -> None:
    settings = get_settings()
    if not settings.ADMIN_TOKEN:
        raise HTTPException(
            status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
            detail="Admin access not configured on this instance.",
        )
    if credentials.credentials != settings.ADMIN_TOKEN:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Invalid admin token.",
        )


@router.get("/db", dependencies=[Depends(_require_admin)])
async def export_db(request: Request) -> FileResponse:
    """
    Stream the SQLite interaction log to the caller.
    Used by the retrain_reranker GitHub Actions workflow to pull the live DB
    for triplet generation without direct access to HF Space storage volumes.
    """
    settings = get_settings()
    db_path = settings.DB_PATH

    if not os.path.exists(db_path):
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Interaction log database not yet initialised.",
        )

    logger.info("Admin DB export requested from %s", request.client)
    return FileResponse(
        path=db_path,
        filename="sqlite.db",
        media_type="application/octet-stream",
    )