Use generated short id for llm asr tts

This commit is contained in:
Xin Wang
2026-02-12 19:05:50 +08:00
parent 14991af1bf
commit 28ca003662
6 changed files with 98 additions and 27 deletions

17
api/app/id_generator.py Normal file
View 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__}")