feat: update user preferences and system settings management

This commit is contained in:
2026-02-03 16:37:05 +08:00
parent 555bf38b71
commit 5a22351a66
21 changed files with 417 additions and 153 deletions

View File

@@ -14,7 +14,7 @@ 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
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, is_local_model_enabled
from schemas.user import User, UserCreate, Token, PasswordChange, AliyunKeyUpdate, AliyunKeyVerifyResponse, UserPreferences, UserPreferencesResponse
router = APIRouter(prefix="/auth", tags=["authentication"])
@@ -246,7 +246,17 @@ async def get_preferences(
db: Session = Depends(get_db)
):
prefs = get_user_preferences(db, current_user.id)
return UserPreferencesResponse(**prefs)
local_enabled = is_local_model_enabled(db)
available_backends = ["aliyun"]
if local_enabled or current_user.is_superuser:
available_backends.append("local")
return {
"default_backend": prefs.get("default_backend", "aliyun"),
"onboarding_completed": prefs.get("onboarding_completed", False),
"available_backends": available_backends
}
@router.put("/preferences")
@limiter.limit("10/minute")
@@ -256,10 +266,24 @@ async def update_preferences(
current_user: Annotated[User, Depends(get_current_user)],
db: Session = Depends(get_db)
):
user = update_user_preferences(db, current_user.id, preferences.dict())
if not user:
if preferences.default_backend == "local":
local_enabled = is_local_model_enabled(db)
if not local_enabled and not current_user.is_superuser:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Local model is not available. Please contact administrator."
)
updated_user = update_user_preferences(
db,
current_user.id,
preferences.model_dump()
)
if not updated_user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
return {"message": "Preferences updated"}
return {"message": "Preferences updated successfully"}

View File

@@ -285,14 +285,27 @@ async def create_custom_voice_job(
db: Session = Depends(get_db)
):
from core.security import decrypt_api_key
from db.crud import get_user_preferences, is_local_model_enabled
backend_type = req_data.backend or settings.DEFAULT_BACKEND
if backend_type == "aliyun":
if not current_user.aliyun_api_key:
raise HTTPException(status_code=400, detail="Aliyun API key not configured. Please set your API key first.")
user_api_key = decrypt_api_key(current_user.aliyun_api_key)
if not user_api_key:
raise HTTPException(status_code=400, detail="Invalid Aliyun API key. Please update your API key.")
user_prefs = get_user_preferences(db, current_user.id)
preferred_backend = user_prefs.get("default_backend", "aliyun")
local_enabled = is_local_model_enabled(db)
can_use_local = local_enabled or current_user.is_superuser
backend_type = req_data.backend if hasattr(req_data, 'backend') and req_data.backend else preferred_backend
if backend_type == "local" and not can_use_local:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
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."
)
try:
validate_text_length(req_data.text)
@@ -362,14 +375,27 @@ async def create_voice_design_job(
db: Session = Depends(get_db)
):
from core.security import decrypt_api_key
from db.crud import get_user_preferences, is_local_model_enabled
backend_type = req_data.backend or settings.DEFAULT_BACKEND
if backend_type == "aliyun":
if not current_user.aliyun_api_key:
raise HTTPException(status_code=400, detail="Aliyun API key not configured. Please set your API key first.")
user_api_key = decrypt_api_key(current_user.aliyun_api_key)
if not user_api_key:
raise HTTPException(status_code=400, detail="Invalid Aliyun API key. Please update your API key.")
user_prefs = get_user_preferences(db, current_user.id)
preferred_backend = user_prefs.get("default_backend", "aliyun")
local_enabled = is_local_model_enabled(db)
can_use_local = local_enabled or current_user.is_superuser
backend_type = req_data.backend if hasattr(req_data, 'backend') and req_data.backend else preferred_backend
if backend_type == "local" and not can_use_local:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
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."
)
try:
validate_text_length(req_data.text)
@@ -450,14 +476,27 @@ async def create_voice_clone_job(
db: Session = Depends(get_db)
):
from core.security import decrypt_api_key
from db.crud import get_user_preferences, is_local_model_enabled
backend_type = backend or settings.DEFAULT_BACKEND
if backend_type == "aliyun":
if not current_user.aliyun_api_key:
raise HTTPException(status_code=400, detail="Aliyun API key not configured. Please set your API key first.")
user_api_key = decrypt_api_key(current_user.aliyun_api_key)
if not user_api_key:
raise HTTPException(status_code=400, detail="Invalid Aliyun API key. Please update your API key.")
user_prefs = get_user_preferences(db, current_user.id)
preferred_backend = user_prefs.get("default_backend", "aliyun")
local_enabled = is_local_model_enabled(db)
can_use_local = local_enabled or current_user.is_superuser
backend_type = backend if backend else preferred_backend
if backend_type == "local" and not can_use_local:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
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."
)
try:
validate_text_length(text)

View File

@@ -15,9 +15,12 @@ from db.crud import (
list_users,
create_user_by_admin,
update_user,
delete_user
delete_user,
get_system_setting,
update_system_setting,
is_local_model_enabled
)
from schemas.user import User, UserCreateByAdmin, UserUpdate, UserListResponse
from schemas.user import User, UserCreateByAdmin, UserUpdate, UserListResponse, SystemSettingsUpdate, SystemSettingsResponse
router = APIRouter(prefix="/users", tags=["users"])
limiter = Limiter(key_func=get_remote_address)
@@ -167,3 +170,42 @@ async def delete_user_by_id(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
@router.get("/system/settings", response_model=SystemSettingsResponse)
async def get_system_settings(
current_user: Annotated[User, Depends(require_superuser)],
db: Session = Depends(get_db)
):
local_enabled = is_local_model_enabled(db)
return {"local_model_enabled": local_enabled}
@router.put("/system/settings")
async def update_system_settings(
settings: SystemSettingsUpdate,
current_user: Annotated[User, Depends(require_superuser)],
db: Session = Depends(get_db)
):
from db.models import User
from datetime import datetime
update_system_setting(db, "local_model_enabled", {"enabled": settings.local_model_enabled})
if not settings.local_model_enabled:
users = db.query(User).filter(User.is_superuser == False).all()
migrated_count = 0
for user in users:
prefs = user.user_preferences or {}
if prefs.get("default_backend") == "local":
prefs["default_backend"] = "aliyun"
user.user_preferences = prefs
user.updated_at = datetime.utcnow()
migrated_count += 1
db.commit()
return {
"message": "System settings updated",
"users_migrated": migrated_count
}
return {"message": "System settings updated", "users_migrated": 0}

View File

@@ -3,7 +3,7 @@ from typing import Optional, List, Dict, Any
from datetime import datetime
from sqlalchemy.orm import Session
from db.models import User, Job, VoiceCache
from db.models import User, Job, VoiceCache, SystemSettings
def get_user_by_username(db: Session, username: str) -> Optional[User]:
return db.query(User).filter(User.username == username).first()
@@ -233,7 +233,7 @@ def delete_cache_entry(db: Session, cache_id: int, user_id: int) -> bool:
def get_user_preferences(db: Session, user_id: int) -> dict:
user = get_user_by_id(db, user_id)
if not user or not user.user_preferences:
return {"default_backend": "local", "onboarding_completed": False}
return {"default_backend": "aliyun", "onboarding_completed": False}
return user.user_preferences
def update_user_preferences(db: Session, user_id: int, preferences: dict) -> Optional[User]:
@@ -245,3 +245,27 @@ def update_user_preferences(db: Session, user_id: int, preferences: dict) -> Opt
db.commit()
db.refresh(user)
return user
def get_system_setting(db: Session, key: str) -> Optional[dict]:
setting = db.query(SystemSettings).filter(SystemSettings.key == key).first()
if not setting:
return None
return setting.value
def update_system_setting(db: Session, key: str, value: dict) -> SystemSettings:
setting = db.query(SystemSettings).filter(SystemSettings.key == key).first()
if setting:
setting.value = value
setting.updated_at = datetime.utcnow()
else:
setting = SystemSettings(key=key, value=value, updated_at=datetime.utcnow())
db.add(setting)
db.commit()
db.refresh(setting)
return setting
def is_local_model_enabled(db: Session) -> bool:
setting = get_system_setting(db, "local_model_enabled")
if not setting:
return False
return setting.get("enabled", False)

View File

@@ -21,7 +21,7 @@ class User(Base):
is_active = Column(Boolean, default=True, nullable=False)
is_superuser = Column(Boolean, default=False, nullable=False)
aliyun_api_key = Column(Text, nullable=True)
user_preferences = Column(JSON, nullable=True, default=lambda: {"default_backend": "local", "onboarding_completed": False})
user_preferences = Column(JSON, nullable=True, default=lambda: {"default_backend": "aliyun", "onboarding_completed": False})
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
@@ -68,3 +68,11 @@ class VoiceCache(Base):
__table_args__ = (
Index('idx_user_hash', 'user_id', 'ref_audio_hash'),
)
class SystemSettings(Base):
__tablename__ = "system_settings"
id = Column(Integer, primary_key=True, index=True)
key = Column(String(100), unique=True, nullable=False, index=True)
value = Column(JSON, nullable=False)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)

View File

@@ -1,37 +0,0 @@
import sqlite3
import json
import os
DB_PATH = os.path.join(os.path.dirname(__file__), "qwen_tts.db")
def migrate():
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
try:
cursor.execute("SELECT user_preferences FROM users LIMIT 1")
print("Column user_preferences already exists, skipping migration")
except sqlite3.OperationalError:
print("Adding user_preferences column to users table...")
cursor.execute("""
ALTER TABLE users
ADD COLUMN user_preferences TEXT DEFAULT NULL
""")
cursor.execute("SELECT id FROM users")
user_ids = cursor.fetchall()
default_prefs = json.dumps({"default_backend": "local", "onboarding_completed": False})
for (user_id,) in user_ids:
cursor.execute(
"UPDATE users SET user_preferences = ? WHERE id = ?",
(default_prefs, user_id)
)
conn.commit()
print(f"Migration completed: Added user_preferences column and initialized {len(user_ids)} users")
conn.close()
if __name__ == "__main__":
migrate()

View File

@@ -120,9 +120,16 @@ class AliyunKeyVerifyResponse(BaseModel):
message: str
class UserPreferences(BaseModel):
default_backend: str = Field(default="local", pattern="^(local|aliyun)$")
default_backend: str = Field(default="aliyun", pattern="^(local|aliyun)$")
onboarding_completed: bool = Field(default=False)
class UserPreferencesResponse(BaseModel):
default_backend: str
onboarding_completed: bool
default_backend: str = Field(default="aliyun", pattern="^(local|aliyun)$")
onboarding_completed: bool = Field(default=False)
available_backends: list[str] = Field(default=["aliyun"])
class SystemSettingsUpdate(BaseModel):
local_model_enabled: bool
class SystemSettingsResponse(BaseModel):
local_model_enabled: bool

View File

@@ -0,0 +1,70 @@
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker
from datetime import datetime
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
from config import settings
engine = create_engine(
settings.DATABASE_URL,
connect_args={"check_same_thread": False} if "sqlite" in settings.DATABASE_URL else {}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def upgrade():
db = SessionLocal()
try:
db.execute(text("""
CREATE TABLE IF NOT EXISTS system_settings (
id INTEGER PRIMARY KEY,
key VARCHAR(100) UNIQUE NOT NULL,
value JSON NOT NULL,
updated_at DATETIME NOT NULL
)
"""))
db.execute(text("""
CREATE INDEX IF NOT EXISTS ix_system_settings_key ON system_settings (key)
"""))
result = db.execute(text(
"SELECT COUNT(*) as count FROM system_settings WHERE key = 'local_model_enabled'"
))
count = result.fetchone()[0]
if count == 0:
db.execute(text(
"INSERT INTO system_settings (key, value, updated_at) VALUES "
"('local_model_enabled', '{\"enabled\": false}', :now)"
), {"now": datetime.utcnow()})
db.execute(text("""
UPDATE users
SET user_preferences = json_set(
COALESCE(user_preferences, '{}'),
'$.default_backend',
'aliyun'
)
WHERE json_extract(user_preferences, '$.default_backend') IS NULL
OR json_extract(user_preferences, '$.default_backend') = 'local'
"""))
db.commit()
print("Migration completed successfully!")
print("- Created system_settings table")
print("- Added local_model_enabled setting (default: false)")
print("- Updated user preferences to use aliyun backend by default")
except Exception as e:
db.rollback()
print(f"Migration failed: {e}")
raise
finally:
db.close()
if __name__ == "__main__":
upgrade()

View File

@@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/qwen.svg" />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="preload" href="/fonts/noto-serif-regular.woff2" as="font" type="font/woff2" crossorigin>
<title>Qwen3-TTS-WebUI</title>

View File

@@ -21,6 +21,7 @@
"@radix-ui/react-separator": "^1.1.8",
"@radix-ui/react-slider": "^1.3.6",
"@radix-ui/react-slot": "^1.2.4",
"@radix-ui/react-switch": "^1.2.6",
"@radix-ui/react-tabs": "^1.1.13",
"@radix-ui/react-tooltip": "^1.2.8",
"axios": "^1.13.3",
@@ -2019,6 +2020,35 @@
}
}
},
"node_modules/@radix-ui/react-switch": {
"version": "1.2.6",
"resolved": "https://registry.npmjs.org/@radix-ui/react-switch/-/react-switch-1.2.6.tgz",
"integrity": "sha512-bByzr1+ep1zk4VubeEVViV592vu2lHE2BZY5OnzehZqOOgogN80+mNtCqPkhn2gklJqOpxWgPoYTSnhBCqpOXQ==",
"license": "MIT",
"dependencies": {
"@radix-ui/primitive": "1.1.3",
"@radix-ui/react-compose-refs": "1.1.2",
"@radix-ui/react-context": "1.1.2",
"@radix-ui/react-primitive": "2.1.3",
"@radix-ui/react-use-controllable-state": "1.2.2",
"@radix-ui/react-use-previous": "1.1.1",
"@radix-ui/react-use-size": "1.1.1"
},
"peerDependencies": {
"@types/react": "*",
"@types/react-dom": "*",
"react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
"react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
},
"peerDependenciesMeta": {
"@types/react": {
"optional": true
},
"@types/react-dom": {
"optional": true
}
}
},
"node_modules/@radix-ui/react-tabs": {
"version": "1.1.13",
"resolved": "https://registry.npmjs.org/@radix-ui/react-tabs/-/react-tabs-1.1.13.tgz",

View File

@@ -23,6 +23,7 @@
"@radix-ui/react-separator": "^1.1.8",
"@radix-ui/react-slider": "^1.3.6",
"@radix-ui/react-slot": "^1.2.4",
"@radix-ui/react-switch": "^1.2.6",
"@radix-ui/react-tabs": "^1.1.13",
"@radix-ui/react-tooltip": "^1.2.8",
"axios": "^1.13.3",

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

View File

@@ -1 +1,15 @@
Redirecting to /@lobehub/icons-static-svg@1.78.0/icons/qwen.svg
<svg width="200" height="200" viewBox="0 0 200 200" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M174.82 108.75L155.38 75L165.64 57.75C166.46 56.31 166.46 54.53 165.64 53.09L155.38 35.84C154.86 34.91 153.87 34.33 152.78 34.33H114.88L106.14 19.03C105.62 18.1 104.63 17.52 103.54 17.52H83.3C82.21 17.52 81.22 18.1 80.7 19.03L61.26 52.77H41.02C39.93 52.77 38.94 53.35 38.42 54.28L28.16 71.53C27.34 72.97 27.34 74.75 28.16 76.19L45.52 107.5L36.78 122.8C35.96 124.24 35.96 126.02 36.78 127.46L47.04 144.71C47.56 145.64 48.55 146.22 49.64 146.22H87.54L96.28 161.52C96.8 162.45 97.79 163.03 98.88 163.03H119.12C120.21 163.03 121.2 162.45 121.72 161.52L141.16 127.78H158.52C159.61 127.78 160.6 127.2 161.12 126.27L171.38 109.02C172.2 107.58 172.2 105.8 171.38 104.36L174.82 108.75Z" fill="url(#paint0_radial)"/>
<path d="M119.12 163.03H98.88L87.54 144.71H49.64L61.26 126.39H80.7L38.42 55.29H61.26L83.3 19.03L93.56 37.35L83.3 55.29H161.58L151.32 72.54L170.76 106.28H151.32L141.16 88.34L101.18 163.03H119.12Z" fill="white"/>
<path d="M127.86 79.83H76.14L101.18 122.11L127.86 79.83Z" fill="url(#paint1_radial)"/>
<defs>
<radialGradient id="paint0_radial" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(100 100) rotate(90) scale(100)">
<stop stop-color="#665CEE"/>
<stop offset="1" stop-color="#332E91"/>
</radialGradient>
<radialGradient id="paint1_radial" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(100 100) rotate(90) scale(100)">
<stop stop-color="#665CEE"/>
<stop offset="1" stop-color="#332E91"/>
</radialGradient>
</defs>
</svg>

Before

Width:  |  Height:  |  Size: 63 B

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@@ -33,7 +33,6 @@ const formSchema = z.object({
top_k: z.number().min(1).max(100).optional(),
top_p: z.number().min(0).max(1).optional(),
repetition_penalty: z.number().min(0).max(2).optional(),
backend: z.string().optional(),
})
type FormData = z.infer<typeof formSchema>
@@ -77,16 +76,9 @@ const CustomVoiceForm = forwardRef<CustomVoiceFormHandle>((_props, ref) => {
top_k: 20,
top_p: 0.7,
repetition_penalty: 1.05,
backend: preferences?.default_backend || 'local',
},
})
useEffect(() => {
if (preferences?.default_backend) {
setValue('backend', preferences.default_backend)
}
}, [preferences?.default_backend, setValue])
useImperativeHandle(ref, () => ({
loadParams: (params: any) => {
setValue('text', params.text || '')
@@ -142,22 +134,6 @@ const CustomVoiceForm = forwardRef<CustomVoiceFormHandle>((_props, ref) => {
return (
<form onSubmit={handleSubmit(onSubmit)} className="space-y-2">
<div className="space-y-0.5">
<Label></Label>
<Select
value={watch('backend')}
onValueChange={(value: string) => setValue('backend', value)}
>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="local"></SelectItem>
<SelectItem value="aliyun"> API</SelectItem>
</SelectContent>
</Select>
</div>
<div className="space-y-0.5">
<IconLabel icon={Globe2} tooltip="语言" required />
<Select

View File

@@ -38,7 +38,6 @@ const formSchema = z.object({
top_k: z.number().min(1).max(100).optional(),
top_p: z.number().min(0).max(1).optional(),
repetition_penalty: z.number().min(0).max(2).optional(),
backend: z.string().optional(),
})
type FormData = z.infer<typeof formSchema>
@@ -78,16 +77,9 @@ function VoiceCloneForm() {
top_k: 20,
top_p: 0.7,
repetition_penalty: 1.05,
backend: preferences?.default_backend || 'local',
} as Partial<FormData>,
})
useEffect(() => {
if (preferences?.default_backend) {
setValue('backend', preferences.default_backend)
}
}, [preferences?.default_backend, setValue])
useEffect(() => {
const fetchData = async () => {
try {
@@ -243,22 +235,6 @@ function VoiceCloneForm() {
<div className={step === 2 ? 'block space-y-4' : 'hidden'}>
{/* Step 2: Synthesis Options */}
<div className="space-y-0.5">
<Label></Label>
<Select
value={watch('backend')}
onValueChange={(value: string) => setValue('backend', value)}
>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="local"></SelectItem>
<SelectItem value="aliyun"> API</SelectItem>
</SelectContent>
</Select>
</div>
<div className="space-y-0.5">
<IconLabel icon={Globe2} tooltip="语言(可选)" />
<Select

View File

@@ -32,7 +32,6 @@ const formSchema = z.object({
top_k: z.number().min(1).max(100).optional(),
top_p: z.number().min(0).max(1).optional(),
repetition_penalty: z.number().min(0).max(2).optional(),
backend: z.string().optional(),
})
type FormData = z.infer<typeof formSchema>
@@ -74,16 +73,9 @@ const VoiceDesignForm = forwardRef<VoiceDesignFormHandle>((_props, ref) => {
top_k: 20,
top_p: 0.7,
repetition_penalty: 1.05,
backend: preferences?.default_backend || 'local',
},
})
useEffect(() => {
if (preferences?.default_backend) {
setValue('backend', preferences.default_backend)
}
}, [preferences?.default_backend, setValue])
useImperativeHandle(ref, () => ({
loadParams: (params: any) => {
setValue('text', params.text || '')
@@ -94,7 +86,6 @@ const VoiceDesignForm = forwardRef<VoiceDesignFormHandle>((_props, ref) => {
setValue('top_k', params.top_k || 20)
setValue('top_p', params.top_p || 0.7)
setValue('repetition_penalty', params.repetition_penalty || 1.05)
setValue('backend', params.backend || 'local')
}
}))
@@ -133,22 +124,6 @@ const VoiceDesignForm = forwardRef<VoiceDesignFormHandle>((_props, ref) => {
return (
<form onSubmit={handleSubmit(onSubmit)} className="space-y-2">
<div className="space-y-0.5">
<Label></Label>
<Select
value={watch('backend')}
onValueChange={(value: string) => setValue('backend', value)}
>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="local"></SelectItem>
<SelectItem value="aliyun"> API</SelectItem>
</SelectContent>
</Select>
</div>
<div className="space-y-0.5">
<IconLabel icon={Globe2} tooltip="语言" required />
<Select

View File

@@ -0,0 +1,27 @@
import * as React from "react"
import * as SwitchPrimitives from "@radix-ui/react-switch"
import { cn } from "@/lib/utils"
const Switch = React.forwardRef<
React.ElementRef<typeof SwitchPrimitives.Root>,
React.ComponentPropsWithoutRef<typeof SwitchPrimitives.Root>
>(({ className, ...props }, ref) => (
<SwitchPrimitives.Root
className={cn(
"peer inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=unchecked]:bg-input",
className
)}
{...props}
ref={ref}
>
<SwitchPrimitives.Thumb
className={cn(
"pointer-events-none block h-5 w-5 rounded-full bg-background shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-5 data-[state=unchecked]:translate-x-0"
)}
/>
</SwitchPrimitives.Root>
))
Switch.displayName = SwitchPrimitives.Root.displayName
export { Switch }

View File

@@ -9,6 +9,7 @@ interface UserPreferencesContextType {
updatePreferences: (prefs: Partial<UserPreferences>) => Promise<void>
hasAliyunKey: boolean
refetchPreferences: () => Promise<void>
isBackendAvailable: (backend: string) => boolean
}
const UserPreferencesContext = createContext<UserPreferencesContextType | undefined>(undefined)
@@ -43,7 +44,7 @@ export function UserPreferencesProvider({ children }: { children: ReactNode }) {
if (cached) {
setPreferences(JSON.parse(cached))
} else {
setPreferences({ default_backend: 'local', onboarding_completed: false })
setPreferences({ default_backend: 'aliyun', onboarding_completed: false })
}
} finally {
setIsLoading(false)
@@ -72,6 +73,13 @@ export function UserPreferencesProvider({ children }: { children: ReactNode }) {
}
}
const isBackendAvailable = (backend: string) => {
if (!preferences?.available_backends) {
return backend === 'aliyun'
}
return preferences.available_backends.includes(backend)
}
return (
<UserPreferencesContext.Provider
value={{
@@ -80,6 +88,7 @@ export function UserPreferencesProvider({ children }: { children: ReactNode }) {
updatePreferences,
hasAliyunKey,
refetchPreferences: fetchPreferences,
isBackendAvailable,
}}
>
{children}

View File

@@ -1,5 +1,5 @@
import axios from 'axios'
import type { LoginRequest, LoginResponse, User, PasswordChangeRequest, UserPreferences } from '@/types/auth'
import type { LoginRequest, LoginResponse, User, PasswordChangeRequest, UserPreferences, SystemSettings } from '@/types/auth'
import type { Job, JobCreateResponse, JobListResponse, JobType } from '@/types/job'
import type { Language, Speaker, CustomVoiceForm, VoiceDesignForm, VoiceCloneForm } from '@/types/tts'
import type { UserCreateRequest, UserUpdateRequest, UserListResponse } from '@/types/user'
@@ -209,6 +209,15 @@ export const authApi = {
)
return response.data
},
getSystemSettings: async (): Promise<SystemSettings> => {
const response = await apiClient.get<SystemSettings>('/users/system/settings')
return response.data
},
updateSystemSettings: async (settings: { local_model_enabled: boolean }): Promise<void> => {
await apiClient.put('/users/system/settings', settings)
},
}
export const ttsApi = {

View File

@@ -1,4 +1,4 @@
import { useState } from 'react'
import { useState, useEffect } from 'react'
import { useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import * as z from 'zod'
@@ -10,6 +10,7 @@ import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
import { Switch } from '@/components/ui/switch'
import {
Form,
FormControl,
@@ -32,11 +33,12 @@ type ApiKeyFormValues = z.infer<typeof apiKeySchema>
export default function Settings() {
const { user } = useAuth()
const { preferences, hasAliyunKey, updatePreferences, refetchPreferences } = useUserPreferences()
const { preferences, hasAliyunKey, updatePreferences, refetchPreferences, isBackendAvailable } = useUserPreferences()
const [showApiKey, setShowApiKey] = useState(false)
const [isLoading, setIsLoading] = useState(false)
const [showPasswordDialog, setShowPasswordDialog] = useState(false)
const [isPasswordLoading, setIsPasswordLoading] = useState(false)
const [localModelEnabled, setLocalModelEnabled] = useState(false)
const form = useForm<ApiKeyFormValues>({
resolver: zodResolver(apiKeySchema),
@@ -45,6 +47,34 @@ export default function Settings() {
},
})
useEffect(() => {
if (user?.is_superuser) {
fetchSystemSettings()
}
}, [user])
const fetchSystemSettings = async () => {
try {
const settings = await authApi.getSystemSettings()
setLocalModelEnabled(settings.local_model_enabled)
} catch (error) {
console.error('Failed to fetch system settings:', error)
}
}
const handleToggleLocalModel = async (enabled: boolean) => {
try {
await authApi.updateSystemSettings({ local_model_enabled: enabled })
setLocalModelEnabled(enabled)
toast.success(`本地模型已${enabled ? '启用' : '禁用'}`)
await refetchPreferences()
} catch (error) {
toast.error('更新失败,请重试')
console.error('Failed to update system settings:', error)
}
}
const handleBackendChange = async (value: string) => {
try {
await updatePreferences({ default_backend: value as 'local' | 'aliyun' })
@@ -141,11 +171,20 @@ export default function Settings() {
value={preferences.default_backend}
onValueChange={handleBackendChange}
>
<div className="flex items-center space-x-3 border rounded-lg p-4 hover:bg-accent/50 cursor-pointer">
<RadioGroupItem value="local" id="backend-local" />
<div className={`flex items-center space-x-3 border rounded-lg p-4 ${
!isBackendAvailable('local') ? 'opacity-50' : 'hover:bg-accent/50 cursor-pointer'
}`}>
<RadioGroupItem
value="local"
id="backend-local"
disabled={!isBackendAvailable('local')}
/>
<Label htmlFor="backend-local" className="flex-1 cursor-pointer">
<div className="font-medium"></div>
<div className="text-sm text-muted-foreground">使 Qwen3-TTS </div>
<div className="text-sm text-muted-foreground">
使 Qwen3-TTS
{!isBackendAvailable('local') && ' (管理员未启用)'}
</div>
</Label>
</div>
<div className="flex items-center space-x-3 border rounded-lg p-4 hover:bg-accent/50 cursor-pointer">
@@ -249,6 +288,32 @@ export default function Settings() {
</CardContent>
</Card>
{user.is_superuser && (
<Card>
<CardHeader>
<CardTitle></CardTitle>
<CardDescription></CardDescription>
</CardHeader>
<CardContent>
<div className="space-y-4">
<div className="flex items-center justify-between">
<div className="space-y-0.5">
<Label htmlFor="local-model-toggle"></Label>
<p className="text-sm text-muted-foreground">
使 Qwen3-TTS
</p>
</div>
<Switch
id="local-model-toggle"
checked={localModelEnabled}
onCheckedChange={handleToggleLocalModel}
/>
</div>
</div>
</CardContent>
</Card>
)}
<Card>
<CardHeader>
<CardTitle></CardTitle>

View File

@@ -33,4 +33,9 @@ export interface PasswordChangeRequest {
export interface UserPreferences {
default_backend: 'local' | 'aliyun'
onboarding_completed: boolean
available_backends?: string[]
}
export interface SystemSettings {
local_model_enabled: boolean
}