Download backend/src/api/routes/admin.py from new-arc/vector: direct link, hf CLI and curl.
- Browser
- Download file 8.6 kB
-
https://huggingface.co/spaces/new-arc/vector/resolve/main/backend/src/api/routes/admin.py
- Command line
-
hf download hf://spaces/new-arc/vector/backend/src/api/routes/admin.py
-
curl -L -o admin.py https://huggingface.co/spaces/new-arc/vector/resolve/main/backend/src/api/routes/admin.py
8.6 kB
| from __future__ import annotations | |
| from datetime import datetime | |
| from fastapi import APIRouter, Depends, HTTPException, Request | |
| from pydantic import BaseModel, Field | |
| from sqlalchemy import text | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from ...admin_auth import require_admin_user | |
| from ...ai import _get_missing_llm_key_error | |
| from ...config import NVIDIA_DEFAULT_MODEL, get_config | |
| from ...database import get_db | |
| from ...runtime_settings import ( | |
| RUNTIME_SETTING_KEYS, | |
| encrypt_setting_value, | |
| get_runtime_setting_rows, | |
| load_runtime_settings_cache, | |
| ) | |
| router = APIRouter(prefix="/admin", tags=["admin"]) | |
| LLM_MODEL_OPTIONS = [ | |
| {"label": "NVIDIA - MiniMax M3.1 Flash", "value": NVIDIA_DEFAULT_MODEL}, | |
| {"label": "Google - Gemini 3 Flash Preview", "value": "google-gla:gemini-3-flash-preview"}, | |
| {"label": "OpenAI - GPT-5.2", "value": "openai:gpt-5.2"}, | |
| {"label": "OpenRouter - Claude Sonnet 5.5", "value": "openrouter:anthropic/claude-sonnet-5.5"}, | |
| {"label": "Anthropic - Claude Sonnet 5.5", "value": "anthropic:claude-sonnet-5-5"}, | |
| {"label": "Ollama - GPT-OSS 20B", "value": "ollama:gpt-oss:20b"}, | |
| ] | |
| SETTING_METADATA = { | |
| "ASSEMBLY_AI_API_KEY": { | |
| "label": "AssemblyAI API key", | |
| "description": "Used for video transcription.", | |
| "input_type": "password", | |
| }, | |
| "LLM": { | |
| "label": "LLM model", | |
| "description": "Select a preset or enter provider:model. NVIDIA uses nvidia:vendor/model.", | |
| "input_type": "text", | |
| "options": LLM_MODEL_OPTIONS, | |
| }, | |
| "RERANK_LLM": { | |
| "label": "Reranking model (optional)", | |
| "description": "Uses LLM unless overridden. Select NVIDIA here too to replace a previously configured reranking model.", | |
| "input_type": "text", | |
| "options": LLM_MODEL_OPTIONS, | |
| }, | |
| "OPENAI_API_KEY": { | |
| "label": "OpenAI API key", | |
| "description": "Required for openai:* models.", | |
| "input_type": "password", | |
| }, | |
| "OPENROUTER_API_KEY": { | |
| "label": "OpenRouter API key", | |
| "description": "Required for openrouter:* models, including Sonnet with Luna fallback.", | |
| "input_type": "password", | |
| }, | |
| "NVIDIA_API_KEY": { | |
| "label": "NVIDIA API key", | |
| "description": "Required for nvidia:* models at integrate.api.nvidia.com. Obtain a key from build.nvidia.com; hosted trial terms apply.", | |
| "input_type": "password", | |
| }, | |
| "GOOGLE_API_KEY": { | |
| "label": "Google API key", | |
| "description": "Required for google-gla:* models and fallback YouTube metadata.", | |
| "input_type": "password", | |
| }, | |
| "ANTHROPIC_API_KEY": { | |
| "label": "Anthropic API key", | |
| "description": "Required for anthropic:* models.", | |
| "input_type": "password", | |
| }, | |
| "OLLAMA_BASE_URL": { | |
| "label": "Ollama base URL", | |
| "description": "Optional URL for local or hosted Ollama-compatible endpoints.", | |
| "input_type": "text", | |
| }, | |
| "OLLAMA_API_KEY": { | |
| "label": "Ollama API key", | |
| "description": "Optional, used by hosted Ollama-compatible providers.", | |
| "input_type": "password", | |
| }, | |
| "YOUTUBE_DATA_API_KEY": { | |
| "label": "YouTube Data API key", | |
| "description": "Optional metadata provider key.", | |
| "input_type": "password", | |
| }, | |
| "APIFY_API_TOKEN": { | |
| "label": "Apify API token", | |
| "description": "Optional YouTube download fallback provider token.", | |
| "input_type": "password", | |
| }, | |
| "PEXELS_API_KEY": { | |
| "label": "Pexels API key", | |
| "description": "Optional B-roll stock footage provider key.", | |
| "input_type": "password", | |
| }, | |
| } | |
| class RuntimeSettingsUpdate(BaseModel): | |
| updates: dict[str, str] = Field(default_factory=dict) | |
| delete_keys: list[str] = Field(default_factory=list) | |
| prefer_admin_values: dict[str, bool] = Field(default_factory=dict) | |
| def _setting_status( | |
| setting_key: str, rows: dict[str, dict[str, object]] | |
| ): | |
| env_value = get_config()._get_optional_env(setting_key) | |
| has_env = bool(env_value) | |
| row = rows.get(setting_key, {}) | |
| has_admin_value = bool(row.get("encrypted_value")) | |
| prefer_admin_value = bool(row.get("prefer_admin_value")) | |
| metadata = SETTING_METADATA[setting_key] | |
| if has_admin_value and (prefer_admin_value or not has_env): | |
| source = "admin" | |
| elif has_env: | |
| source = "environment" | |
| else: | |
| source = "unset" | |
| return { | |
| "key": setting_key, | |
| "label": metadata["label"], | |
| "description": metadata["description"], | |
| "input_type": metadata["input_type"], | |
| "options": metadata.get("options", []), | |
| "source": source, | |
| "configured": has_env or has_admin_value, | |
| "has_admin_value": has_admin_value, | |
| "has_env_value": has_env, | |
| "prefer_admin_value": prefer_admin_value, | |
| "overridden_by_env": has_env and has_admin_value and not prefer_admin_value, | |
| "updated_at": row.get("updated_at"), | |
| } | |
| async def admin_health( | |
| request: Request, db: AsyncSession = Depends(get_db) | |
| ): | |
| await require_admin_user(request, db, get_config()) | |
| return {"status": "ok"} | |
| async def get_runtime_settings(request: Request, db: AsyncSession = Depends(get_db)): | |
| await require_admin_user(request, db, get_config()) | |
| rows = await get_runtime_setting_rows(db) | |
| return { | |
| "settings": [ | |
| _setting_status(setting_key, rows) for setting_key in RUNTIME_SETTING_KEYS | |
| ] | |
| } | |
| async def update_runtime_settings( | |
| request: Request, | |
| payload: RuntimeSettingsUpdate, | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| user_id = await require_admin_user(request, db, get_config()) | |
| allowed_keys = set(RUNTIME_SETTING_KEYS) | |
| invalid_keys = [ | |
| key | |
| for key in [ | |
| *payload.updates.keys(), | |
| *payload.delete_keys, | |
| *payload.prefer_admin_values.keys(), | |
| ] | |
| if key not in allowed_keys | |
| ] | |
| if invalid_keys: | |
| raise HTTPException( | |
| status_code=400, | |
| detail=f"Unsupported setting key(s): {', '.join(sorted(set(invalid_keys)))}", | |
| ) | |
| for model_key in ("LLM", "RERANK_LLM"): | |
| model_name = payload.updates.get(model_key, "").strip() | |
| if model_name: | |
| config_error = _get_missing_llm_key_error(model_name, get_config()) | |
| if config_error and "API_KEY is not set" not in config_error: | |
| raise HTTPException(status_code=400, detail=config_error) | |
| for setting_key, raw_value in payload.updates.items(): | |
| value = raw_value.strip() | |
| if not value: | |
| continue | |
| encrypted_value = encrypt_setting_value(value) | |
| await db.execute( | |
| text( | |
| """ | |
| INSERT INTO app_settings (setting_key, encrypted_value, updated_by) | |
| VALUES (:setting_key, :encrypted_value, :updated_by) | |
| ON CONFLICT (setting_key) DO UPDATE | |
| SET encrypted_value = EXCLUDED.encrypted_value, | |
| updated_by = EXCLUDED.updated_by, | |
| updated_at = CURRENT_TIMESTAMP | |
| """ | |
| ), | |
| { | |
| "setting_key": setting_key, | |
| "encrypted_value": encrypted_value, | |
| "updated_by": user_id, | |
| }, | |
| ) | |
| for setting_key, prefer_admin_value in payload.prefer_admin_values.items(): | |
| await db.execute( | |
| text( | |
| """ | |
| UPDATE app_settings | |
| SET prefer_admin_value = :prefer_admin_value, | |
| updated_by = :updated_by, | |
| updated_at = CURRENT_TIMESTAMP | |
| WHERE setting_key = :setting_key | |
| """ | |
| ), | |
| { | |
| "setting_key": setting_key, | |
| "prefer_admin_value": prefer_admin_value, | |
| "updated_by": user_id, | |
| }, | |
| ) | |
| if payload.delete_keys: | |
| await db.execute( | |
| text( | |
| """ | |
| DELETE FROM app_settings | |
| WHERE setting_key = ANY(CAST(:setting_keys AS text[])) | |
| """ | |
| ), | |
| {"setting_keys": payload.delete_keys}, | |
| ) | |
| await db.commit() | |
| await load_runtime_settings_cache(db) | |
| rows = await get_runtime_setting_rows(db) | |
| return { | |
| "settings": [ | |
| _setting_status(setting_key, rows) for setting_key in RUNTIME_SETTING_KEYS | |
| ] | |
| } | |