Compare commits

...
2 Commits
6 changed files with 52 additions and 17 deletions
+16
View File
@@ -32,6 +32,22 @@ async def preset():
return STEP_PRESET return STEP_PRESET
@router.post("/preset/apply")
async def apply_preset(user: User = Depends(current_user), db=Depends(get_db)):
"""一键把 Step Plan 预设落成渠道(已有同名渠道则跳过)。"""
from ...core.config import settings
rows = await repos.ChannelRepo.visible_to(db, user)
for r in rows:
if r.name == STEP_PRESET["name"]:
return {"applied": False, "reason": "已存在同名渠道"}
c = Channel(name=STEP_PRESET["name"], base_url=STEP_PRESET["base_url"],
api_key_enc=settings.step_api_key or "", models=STEP_PRESET["models"],
owner_id=user.id if not user.is_admin else None, visibility="admin")
db.add(c)
await db.commit()
return {"applied": True, "channel": _view(c)}
@router.get("") @router.get("")
async def list_channels(user: User = Depends(current_user), db=Depends(get_db)): async def list_channels(user: User = Depends(current_user), db=Depends(get_db)):
rows = await repos.ChannelRepo.visible_to(db, user) rows = await repos.ChannelRepo.visible_to(db, user)
+1 -1
View File
@@ -15,7 +15,7 @@ class CharacterIn(BaseModel):
name: str name: str
persona: str = "" persona: str = ""
greeting: str = "" greeting: str = ""
voice: str = "tongtong" voice: str = "elegantgentle-female"
avatar_url: str = "" avatar_url: str = ""
+3 -3
View File
@@ -271,11 +271,11 @@ async def tts(sid: int, mid: int, user: User = Depends(current_user),
raise HTTPException(404, "消息不存在") raise HTTPException(404, "消息不存在")
client, models = await _channel_for(db, user, s) client, models = await _channel_for(db, user, s)
model = models.get("tts") or "stepaudio-2.5-tts" model = models.get("tts") or "stepaudio-2.5-tts"
voice = "tongtong" voice = "elegantgentle-female"
if s.character_id: if s.character_id:
ch = await db.get(Character, s.character_id) ch = await db.get(Character, s.character_id)
if ch: if ch and ch.voice:
voice = ch.voice voice = ch.voice # 非法音色由 client.fix_voice 兜底
try: try:
audio = await client.tts(model, _dec(user, m.content_enc), voice) audio = await client.tts(model, _dec(user, m.content_enc), voice)
except Exception as e: except Exception as e:
+2
View File
@@ -8,6 +8,8 @@ class Settings(BaseSettings):
jwt_expire_minutes: int = 60 * 24 * 7 jwt_expire_minutes: int = 60 * 24 * 7
first_admin_user: str = "admin" first_admin_user: str = "admin"
first_admin_pass: str = "maopaoya-admin" first_admin_pass: str = "maopaoya-admin"
# 默认 Step Plan 渠道的 key(为空则种渠道但不填 key,到后台补)
step_api_key: str = ""
class Config: class Config:
env_file = ".env" env_file = ".env"
+9
View File
@@ -38,6 +38,15 @@ async def startup():
password_hash=security.hash_password(settings.first_admin_pass), password_hash=security.hash_password(settings.first_admin_pass),
kdf_salt=salt.hex(), is_admin=True)) kdf_salt=salt.hex(), is_admin=True))
await db.commit() await db.commit()
# 默认渠道:空库时种 Step Plan 预设(key 走 OMO_STEP_API_KEY)
from .models.entities import Channel
from .core.config import STEP_PRESET
chans = (await db.execute(__import__('sqlalchemy').select(Channel))).scalars().all()
if not chans:
db.add(Channel(name=STEP_PRESET["name"], base_url=STEP_PRESET["base_url"],
api_key_enc=settings.step_api_key, models=STEP_PRESET["models"],
owner_id=None, visibility="admin"))
await db.commit()
@app.exception_handler(Exception) @app.exception_handler(Exception)
+19 -11
View File
@@ -44,19 +44,27 @@ class LLMClient:
d = r.json()["data"][0] d = r.json()["data"][0]
return d.get("url") or ("data:image/png;base64," + d.get("b64_json", "")) return d.get("url") or ("data:image/png;base64," + d.get("b64_json", ""))
# 阶跃 stepaudio 系合法音色(system_voices 实测 + 官方文档)
TTS_VOICES = {"elegantgentle-female", "livelybreezy-female", "lively-girl",
"cixingnansheng", "wenrounansheng", "shenchennanyin",
"zhengpaiqingnian", "shuangkuainansheng", "ganliannvsheng",
"qinhenvsheng", "huolinvsheng"}
@staticmethod
def fix_voice(voice: str) -> str:
return voice if voice in LLMClient.TTS_VOICES else "elegantgentle-female"
async def tts(self, model: str, text: str, voice: str) -> bytes: async def tts(self, model: str, text: str, voice: str) -> bytes:
"""优先 OpenAI 兼容 /audio/speech;失败回落阶跃 stepaudio TTS 格式。""" """OpenAI 兼容 /audio/speech;input 上限 1000 字符(stepaudio-2.5-tts)。"""
headers = {"Authorization": f"Bearer {self.api_key}"} voice = self.fix_voice(voice)
try: r = await get_http().post(
r = await get_http().post(f"{self.base_url}/audio/speech", headers=headers, f"{self.base_url}/audio/speech",
json={"model": model, "input": text, "voice": voice}) headers={"Authorization": f"Bearer {self.api_key}"},
r.raise_for_status() json={"model": model, "input": text[:1000], "voice": voice,
return r.content
except httpx.HTTPError:
r = await get_http().post(f"{self.base_url}/audio/speech", headers=headers,
json={"model": model, "text": text, "voice": voice,
"response_format": "wav"}) "response_format": "wav"})
r.raise_for_status() if r.status_code >= 400:
detail = r.text[:150]
raise RuntimeError(f"{r.status_code}: {detail}")
return r.content return r.content
async def ping(self, model: str) -> tuple[bool, str]: async def ping(self, model: str) -> tuple[bool, str]: