feat: add admin usage statistics and LLM configuration management
This commit is contained in:
22
qwen3-tts-backend/api/admin.py
Normal file
22
qwen3-tts-backend/api/admin.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from api.users import require_superuser
|
||||
from db.database import get_db
|
||||
from db.crud import get_usage_stats
|
||||
from schemas.user import User
|
||||
|
||||
router = APIRouter(prefix="/admin", tags=["admin"])
|
||||
|
||||
|
||||
@router.get("/usage")
|
||||
async def get_usage_statistics(
|
||||
date_from: Optional[datetime] = Query(None),
|
||||
date_to: Optional[datetime] = Query(None),
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_superuser),
|
||||
):
|
||||
return get_usage_stats(db, date_from=date_from, date_to=date_to)
|
||||
@@ -14,9 +14,9 @@ from core.security import (
|
||||
decode_access_token
|
||||
)
|
||||
from db.database import get_db
|
||||
from db.crud import get_user_by_username, get_user_by_email, create_user, change_user_password, update_user_aliyun_key, get_user_preferences, update_user_preferences, can_user_use_local_model, update_user_llm_config
|
||||
from schemas.user import User, UserCreate, Token, PasswordChange, AliyunKeyUpdate, AliyunKeyVerifyResponse, UserPreferences, UserPreferencesResponse
|
||||
from schemas.audiobook import LLMConfigUpdate, LLMConfigResponse
|
||||
from db.crud import get_user_by_username, get_user_by_email, create_user, change_user_password, get_user_preferences, update_user_preferences, can_user_use_local_model, get_system_setting
|
||||
from schemas.user import User, UserCreate, Token, PasswordChange, AliyunKeyVerifyResponse, UserPreferences, UserPreferencesResponse
|
||||
from schemas.audiobook import LLMConfigResponse
|
||||
|
||||
router = APIRouter(prefix="/auth", tags=["authentication"])
|
||||
|
||||
@@ -137,70 +137,6 @@ async def change_password(
|
||||
|
||||
return user
|
||||
|
||||
@router.post("/aliyun-key", response_model=User)
|
||||
@limiter.limit("5/minute")
|
||||
async def set_aliyun_key(
|
||||
request: Request,
|
||||
key_data: AliyunKeyUpdate,
|
||||
current_user: Annotated[User, Depends(get_current_user)],
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
from core.security import encrypt_api_key
|
||||
from core.tts_service import AliyunTTSBackend
|
||||
|
||||
api_key = key_data.api_key.strip()
|
||||
|
||||
aliyun_backend = AliyunTTSBackend(api_key=api_key, region=settings.ALIYUN_REGION)
|
||||
health = await aliyun_backend.health_check()
|
||||
|
||||
if not health.get("available", False):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Invalid Aliyun API key. Please check your API key and try again."
|
||||
)
|
||||
|
||||
encrypted_key = encrypt_api_key(api_key)
|
||||
|
||||
user = update_user_aliyun_key(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
encrypted_api_key=encrypted_key
|
||||
)
|
||||
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="User not found"
|
||||
)
|
||||
|
||||
return user
|
||||
|
||||
@router.delete("/aliyun-key")
|
||||
@limiter.limit("5/minute")
|
||||
async def delete_aliyun_key(
|
||||
request: Request,
|
||||
current_user: Annotated[User, Depends(get_current_user)],
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
user = update_user_aliyun_key(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
encrypted_api_key=None
|
||||
)
|
||||
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="User not found"
|
||||
)
|
||||
|
||||
prefs = get_user_preferences(db, current_user.id)
|
||||
if prefs.get("default_backend") == "aliyun":
|
||||
prefs["default_backend"] = "local"
|
||||
update_user_preferences(db, current_user.id, prefs)
|
||||
|
||||
return {"message": "Aliyun API key deleted", "preferences_updated": True}
|
||||
|
||||
@router.get("/aliyun-key/verify", response_model=AliyunKeyVerifyResponse)
|
||||
@limiter.limit("10/minute")
|
||||
async def verify_aliyun_key(
|
||||
@@ -211,33 +147,20 @@ async def verify_aliyun_key(
|
||||
from core.security import decrypt_api_key
|
||||
from core.tts_service import AliyunTTSBackend
|
||||
|
||||
if not current_user.aliyun_api_key:
|
||||
return AliyunKeyVerifyResponse(
|
||||
valid=False,
|
||||
message="No Aliyun API key configured"
|
||||
)
|
||||
|
||||
api_key = decrypt_api_key(current_user.aliyun_api_key)
|
||||
encrypted = get_system_setting(db, "aliyun_api_key")
|
||||
if not encrypted:
|
||||
return AliyunKeyVerifyResponse(valid=False, message="No Aliyun API key configured")
|
||||
|
||||
api_key = decrypt_api_key(encrypted)
|
||||
if not api_key:
|
||||
return AliyunKeyVerifyResponse(
|
||||
valid=False,
|
||||
message="Failed to decrypt API key"
|
||||
)
|
||||
return AliyunKeyVerifyResponse(valid=False, message="Failed to decrypt API key")
|
||||
|
||||
aliyun_backend = AliyunTTSBackend(api_key=api_key, region=settings.ALIYUN_REGION)
|
||||
health = await aliyun_backend.health_check()
|
||||
|
||||
if health.get("available", False):
|
||||
return AliyunKeyVerifyResponse(
|
||||
valid=True,
|
||||
message="Aliyun API key is valid and working"
|
||||
)
|
||||
else:
|
||||
return AliyunKeyVerifyResponse(
|
||||
valid=False,
|
||||
message="Aliyun API key is not working. Please check your API key."
|
||||
)
|
||||
return AliyunKeyVerifyResponse(valid=True, message="Aliyun API key is valid and working")
|
||||
return AliyunKeyVerifyResponse(valid=False, message="Aliyun API key is not working.")
|
||||
|
||||
@router.get("/preferences", response_model=UserPreferencesResponse)
|
||||
@limiter.limit("30/minute")
|
||||
@@ -288,61 +211,15 @@ async def update_preferences(
|
||||
return {"message": "Preferences updated successfully"}
|
||||
|
||||
|
||||
@router.put("/llm-config")
|
||||
@limiter.limit("10/minute")
|
||||
async def set_llm_config(
|
||||
request: Request,
|
||||
config: LLMConfigUpdate,
|
||||
current_user: Annotated[User, Depends(get_current_user)],
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
from core.security import encrypt_api_key
|
||||
from core.llm_service import LLMService
|
||||
|
||||
api_key = config.api_key.strip()
|
||||
base_url = config.base_url.strip()
|
||||
model = config.model.strip()
|
||||
|
||||
# Validate LLM config by sending a test request
|
||||
llm = LLMService(base_url=base_url, api_key=api_key, model=model)
|
||||
try:
|
||||
await llm.chat("You are a test assistant.", "Reply with 'ok'.")
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"LLM API validation failed: {e}"
|
||||
)
|
||||
|
||||
encrypted_key = encrypt_api_key(api_key)
|
||||
update_user_llm_config(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
llm_api_key=encrypted_key,
|
||||
llm_base_url=base_url,
|
||||
llm_model=model,
|
||||
)
|
||||
return {"message": "LLM config updated successfully"}
|
||||
|
||||
|
||||
@router.get("/llm-config", response_model=LLMConfigResponse)
|
||||
@limiter.limit("30/minute")
|
||||
async def get_llm_config(
|
||||
request: Request,
|
||||
current_user: Annotated[User, Depends(get_current_user)],
|
||||
):
|
||||
return LLMConfigResponse(
|
||||
base_url=current_user.llm_base_url,
|
||||
model=current_user.llm_model,
|
||||
has_key=bool(current_user.llm_api_key),
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/llm-config")
|
||||
@limiter.limit("10/minute")
|
||||
async def delete_llm_config(
|
||||
request: Request,
|
||||
current_user: Annotated[User, Depends(get_current_user)],
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
update_user_llm_config(db, user_id=current_user.id, clear=True)
|
||||
return {"message": "LLM config deleted"}
|
||||
return LLMConfigResponse(
|
||||
base_url=get_system_setting(db, "llm_base_url"),
|
||||
model=get_system_setting(db, "llm_model"),
|
||||
has_key=bool(get_system_setting(db, "llm_api_key")),
|
||||
)
|
||||
|
||||
@@ -72,9 +72,10 @@ async def process_custom_voice_job(
|
||||
|
||||
user_api_key = None
|
||||
if backend_type == "aliyun":
|
||||
user = db.query(User).filter(User.id == user_id).first()
|
||||
if user and user.aliyun_api_key:
|
||||
user_api_key = decrypt_api_key(user.aliyun_api_key)
|
||||
from db.crud import get_system_setting
|
||||
encrypted = get_system_setting(db, "aliyun_api_key")
|
||||
if encrypted:
|
||||
user_api_key = decrypt_api_key(encrypted)
|
||||
|
||||
backend = await TTSServiceFactory.get_backend(backend_type, user_api_key)
|
||||
|
||||
@@ -134,9 +135,10 @@ async def process_voice_design_job(
|
||||
|
||||
user_api_key = None
|
||||
if backend_type == "aliyun":
|
||||
user = db.query(User).filter(User.id == user_id).first()
|
||||
if user and user.aliyun_api_key:
|
||||
user_api_key = decrypt_api_key(user.aliyun_api_key)
|
||||
from db.crud import get_system_setting
|
||||
encrypted = get_system_setting(db, "aliyun_api_key")
|
||||
if encrypted:
|
||||
user_api_key = decrypt_api_key(encrypted)
|
||||
|
||||
backend = await TTSServiceFactory.get_backend(backend_type, user_api_key)
|
||||
|
||||
@@ -201,9 +203,10 @@ async def process_voice_clone_job(
|
||||
from core.security import decrypt_api_key
|
||||
user_api_key = None
|
||||
if backend_type == "aliyun":
|
||||
user = db.query(User).filter(User.id == user_id).first()
|
||||
if user and user.aliyun_api_key:
|
||||
user_api_key = decrypt_api_key(user.aliyun_api_key)
|
||||
from db.crud import get_system_setting
|
||||
encrypted = get_system_setting(db, "aliyun_api_key")
|
||||
if encrypted:
|
||||
user_api_key = decrypt_api_key(encrypted)
|
||||
|
||||
with open(ref_audio_path, 'rb') as f:
|
||||
ref_audio_data = f.read()
|
||||
@@ -352,11 +355,13 @@ async def create_custom_voice_job(
|
||||
detail="Local model is not available. Please contact administrator."
|
||||
)
|
||||
|
||||
if backend_type == "aliyun" and not current_user.aliyun_api_key:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Aliyun API key not configured. Please set your API key in Settings."
|
||||
)
|
||||
if backend_type == "aliyun":
|
||||
from db.crud import get_system_setting
|
||||
if not get_system_setting(db, "aliyun_api_key"):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Aliyun API key not configured. Please contact administrator."
|
||||
)
|
||||
|
||||
try:
|
||||
validate_text_length(req_data.text)
|
||||
@@ -459,11 +464,13 @@ async def create_voice_design_job(
|
||||
detail="Local model is not available. Please contact administrator."
|
||||
)
|
||||
|
||||
if backend_type == "aliyun" and not current_user.aliyun_api_key:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Aliyun API key not configured. Please set your API key in Settings."
|
||||
)
|
||||
if backend_type == "aliyun":
|
||||
from db.crud import get_system_setting
|
||||
if not get_system_setting(db, "aliyun_api_key"):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Aliyun API key not configured. Please contact administrator."
|
||||
)
|
||||
|
||||
try:
|
||||
validate_text_length(req_data.text)
|
||||
@@ -562,11 +569,13 @@ async def create_voice_clone_job(
|
||||
detail="Local model is not available. Please contact administrator."
|
||||
)
|
||||
|
||||
if backend_type == "aliyun" and not current_user.aliyun_api_key:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Aliyun API key not configured. Please set your API key in Settings."
|
||||
)
|
||||
if backend_type == "aliyun":
|
||||
from db.crud import get_system_setting
|
||||
if not get_system_setting(db, "aliyun_api_key"):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Aliyun API key not configured. Please contact administrator."
|
||||
)
|
||||
|
||||
ref_audio_data = None
|
||||
ref_audio_hash = None
|
||||
|
||||
@@ -17,7 +17,8 @@ from db.crud import (
|
||||
update_user,
|
||||
delete_user
|
||||
)
|
||||
from schemas.user import User, UserCreateByAdmin, UserUpdate, UserListResponse
|
||||
from schemas.user import User, UserCreateByAdmin, UserUpdate, UserListResponse, AliyunKeyUpdate, AliyunKeyVerifyResponse
|
||||
from schemas.audiobook import LLMConfigUpdate, LLMConfigResponse
|
||||
|
||||
router = APIRouter(prefix="/users", tags=["users"])
|
||||
limiter = Limiter(key_func=get_remote_address)
|
||||
@@ -177,3 +178,115 @@ async def delete_user_by_id(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="User not found"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/system/aliyun-key")
|
||||
@limiter.limit("5/minute")
|
||||
async def set_system_aliyun_key(
|
||||
request: Request,
|
||||
key_data: AliyunKeyUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_superuser)
|
||||
):
|
||||
from core.security import encrypt_api_key
|
||||
from core.tts_service import AliyunTTSBackend
|
||||
from db.crud import set_system_setting
|
||||
|
||||
api_key = key_data.api_key.strip()
|
||||
aliyun_backend = AliyunTTSBackend(api_key=api_key, region=settings.ALIYUN_REGION)
|
||||
health = await aliyun_backend.health_check()
|
||||
if not health.get("available", False):
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid Aliyun API key.")
|
||||
set_system_setting(db, "aliyun_api_key", encrypt_api_key(api_key))
|
||||
return {"message": "Aliyun API key updated"}
|
||||
|
||||
|
||||
@router.delete("/system/aliyun-key")
|
||||
@limiter.limit("5/minute")
|
||||
async def delete_system_aliyun_key(
|
||||
request: Request,
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_superuser)
|
||||
):
|
||||
from db.crud import delete_system_setting
|
||||
delete_system_setting(db, "aliyun_api_key")
|
||||
return {"message": "Aliyun API key deleted"}
|
||||
|
||||
|
||||
@router.get("/system/aliyun-key/verify", response_model=AliyunKeyVerifyResponse)
|
||||
@limiter.limit("10/minute")
|
||||
async def verify_system_aliyun_key(
|
||||
request: Request,
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_superuser)
|
||||
):
|
||||
from core.security import decrypt_api_key
|
||||
from core.tts_service import AliyunTTSBackend
|
||||
from db.crud import get_system_setting
|
||||
|
||||
encrypted = get_system_setting(db, "aliyun_api_key")
|
||||
if not encrypted:
|
||||
return AliyunKeyVerifyResponse(valid=False, message="No Aliyun API key configured")
|
||||
api_key = decrypt_api_key(encrypted)
|
||||
if not api_key:
|
||||
return AliyunKeyVerifyResponse(valid=False, message="Failed to decrypt API key")
|
||||
aliyun_backend = AliyunTTSBackend(api_key=api_key, region=settings.ALIYUN_REGION)
|
||||
health = await aliyun_backend.health_check()
|
||||
if health.get("available", False):
|
||||
return AliyunKeyVerifyResponse(valid=True, message="Aliyun API key is valid and working")
|
||||
return AliyunKeyVerifyResponse(valid=False, message="Aliyun API key is not working.")
|
||||
|
||||
|
||||
@router.put("/system/llm-config")
|
||||
@limiter.limit("10/minute")
|
||||
async def set_system_llm_config(
|
||||
request: Request,
|
||||
config: LLMConfigUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_superuser)
|
||||
):
|
||||
from core.security import encrypt_api_key
|
||||
from core.llm_service import LLMService
|
||||
from db.crud import set_system_setting
|
||||
|
||||
api_key = config.api_key.strip()
|
||||
base_url = config.base_url.strip()
|
||||
model = config.model.strip()
|
||||
llm = LLMService(base_url=base_url, api_key=api_key, model=model)
|
||||
try:
|
||||
await llm.chat("You are a test assistant.", "Reply with 'ok'.")
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"LLM API validation failed: {e}")
|
||||
set_system_setting(db, "llm_api_key", encrypt_api_key(api_key))
|
||||
set_system_setting(db, "llm_base_url", base_url)
|
||||
set_system_setting(db, "llm_model", model)
|
||||
return {"message": "LLM config updated"}
|
||||
|
||||
|
||||
@router.get("/system/llm-config", response_model=LLMConfigResponse)
|
||||
@limiter.limit("30/minute")
|
||||
async def get_system_llm_config(
|
||||
request: Request,
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_superuser)
|
||||
):
|
||||
from db.crud import get_system_setting
|
||||
return LLMConfigResponse(
|
||||
base_url=get_system_setting(db, "llm_base_url"),
|
||||
model=get_system_setting(db, "llm_model"),
|
||||
has_key=bool(get_system_setting(db, "llm_api_key")),
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/system/llm-config")
|
||||
@limiter.limit("10/minute")
|
||||
async def delete_system_llm_config(
|
||||
request: Request,
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_superuser)
|
||||
):
|
||||
from db.crud import delete_system_setting
|
||||
delete_system_setting(db, "llm_api_key")
|
||||
delete_system_setting(db, "llm_base_url")
|
||||
delete_system_setting(db, "llm_model")
|
||||
return {"message": "LLM config deleted"}
|
||||
|
||||
Reference in New Issue
Block a user