Use generated short id for llm asr tts
This commit is contained in:
17
api/app/id_generator.py
Normal file
17
api/app/id_generator.py
Normal file
@@ -0,0 +1,17 @@
|
||||
import uuid
|
||||
from typing import Any, Type
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
|
||||
def short_id(prefix: str, size: int = 8) -> str:
|
||||
return f"{prefix}_{uuid.uuid4().hex[:size]}"
|
||||
|
||||
|
||||
def unique_short_id(prefix: str, db: Session, model_cls: Type[Any], size: int = 8) -> str:
|
||||
for _ in range(10):
|
||||
candidate = short_id(prefix, size=size)
|
||||
exists = db.query(model_cls.id).filter(model_cls.id == candidate).first()
|
||||
if not exists:
|
||||
return candidate
|
||||
raise RuntimeError(f"failed to generate unique id for {model_cls.__name__}")
|
||||
@@ -1,6 +1,5 @@
|
||||
import os
|
||||
import time
|
||||
import uuid
|
||||
from typing import List, Optional
|
||||
|
||||
import httpx
|
||||
@@ -8,6 +7,7 @@ from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..db import get_db
|
||||
from ..id_generator import unique_short_id
|
||||
from ..models import ASRModel
|
||||
from ..schemas import (
|
||||
ASRModelCreate, ASRModelUpdate, ASRModelOut,
|
||||
@@ -72,7 +72,7 @@ def get_asr_model(id: str, db: Session = Depends(get_db)):
|
||||
def create_asr_model(data: ASRModelCreate, db: Session = Depends(get_db)):
|
||||
"""创建ASR模型"""
|
||||
asr_model = ASRModel(
|
||||
id=data.id or str(uuid.uuid4())[:8],
|
||||
id=unique_short_id("asr", db, ASRModel),
|
||||
user_id=1, # 默认用户
|
||||
name=data.name,
|
||||
vendor=data.vendor,
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List, Optional
|
||||
import uuid
|
||||
import httpx
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
from ..db import get_db
|
||||
from ..id_generator import unique_short_id
|
||||
from ..models import LLMModel
|
||||
from ..schemas import (
|
||||
LLMModelCreate, LLMModelUpdate, LLMModelOut,
|
||||
@@ -53,7 +53,7 @@ def get_llm_model(id: str, db: Session = Depends(get_db)):
|
||||
def create_llm_model(data: LLMModelCreate, db: Session = Depends(get_db)):
|
||||
"""创建LLM模型"""
|
||||
llm_model = LLMModel(
|
||||
id=data.id or str(uuid.uuid4())[:8],
|
||||
id=unique_short_id("llm", db, LLMModel),
|
||||
user_id=1, # 默认用户
|
||||
name=data.name,
|
||||
vendor=data.vendor,
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import base64
|
||||
import os
|
||||
import uuid
|
||||
from typing import Optional
|
||||
|
||||
import httpx
|
||||
@@ -8,6 +7,7 @@ from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..db import get_db
|
||||
from ..id_generator import unique_short_id
|
||||
from ..models import Voice
|
||||
from ..schemas import VoiceCreate, VoiceOut, VoicePreviewRequest, VoicePreviewResponse, VoiceUpdate
|
||||
|
||||
@@ -78,7 +78,7 @@ def create_voice(data: VoiceCreate, db: Session = Depends(get_db)):
|
||||
voice_key = raw_id if ":" in raw_id else f"{model}:{raw_id}"
|
||||
|
||||
voice = Voice(
|
||||
id=data.id or str(uuid.uuid4())[:8],
|
||||
id=unique_short_id("tts", db, Voice),
|
||||
user_id=1,
|
||||
name=data.name,
|
||||
vendor=vendor,
|
||||
|
||||
Reference in New Issue
Block a user