94 lines
2.9 KiB
Python
94 lines
2.9 KiB
Python
import io
|
|
import os
|
|
import soundfile as sf
|
|
from typing import Optional, List
|
|
from fastapi import FastAPI, HTTPException, Response
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import FileResponse
|
|
from pydantic import BaseModel, Field
|
|
|
|
from vocoloom.voice_manager import VoiceManager
|
|
|
|
app = FastAPI(
|
|
title="VocoLoom Voice Platform API",
|
|
description="High-fidelity Multi-Voice AI Speech Synthesis Engine & Microservice",
|
|
version="1.0.0"
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Global manager instance
|
|
manager: Optional[VoiceManager] = None
|
|
|
|
def get_manager() -> VoiceManager:
|
|
global manager
|
|
if manager is None:
|
|
manager = VoiceManager()
|
|
return manager
|
|
|
|
class SynthesizeRequest(BaseModel):
|
|
voice_id: str = Field(..., description="ID der gewünschten Stimme (z.B. 'juergen_seiferth')")
|
|
text: str = Field(..., description="Der zu synthetisierende Text")
|
|
speed: Optional[float] = Field(None, ge=0.5, le=2.0, description="Sprechgeschwindigkeit (Standard: voice default)")
|
|
use_rvc: bool = Field(False, description="Zusätzliche RVC-Veredelung aktivieren")
|
|
rvc_index: float = Field(0.70, ge=0.0, le=1.0, description="RVC Index Stärke")
|
|
|
|
@app.get("/health")
|
|
def health():
|
|
mgr = get_manager()
|
|
return {
|
|
"status": "ok",
|
|
"device": mgr.device,
|
|
"voices_count": len(mgr.voices),
|
|
"active_voice": mgr.active_voice_id
|
|
}
|
|
|
|
@app.get("/api/v1/voices")
|
|
def list_voices():
|
|
mgr = get_manager()
|
|
return [v.to_dict() for v in mgr.list_voices()]
|
|
|
|
@app.get("/api/v1/voices/{voice_id}")
|
|
def get_voice(voice_id: str):
|
|
mgr = get_manager()
|
|
voice = mgr.get_voice(voice_id)
|
|
if not voice:
|
|
raise HTTPException(status_code=404, detail=f"Stimme '{voice_id}' nicht gefunden.")
|
|
return voice.to_dict()
|
|
|
|
@app.get("/api/v1/voices/{voice_id}/avatar")
|
|
def get_avatar(voice_id: str):
|
|
mgr = get_manager()
|
|
voice = mgr.get_voice(voice_id)
|
|
if not voice or not voice.avatar_path or not os.path.exists(voice.avatar_path):
|
|
raise HTTPException(status_code=404, detail="Avatar nicht vorhanden.")
|
|
return FileResponse(voice.avatar_path, media_type="image/png")
|
|
|
|
@app.post("/api/v1/synthesize")
|
|
def synthesize_speech(req: SynthesizeRequest):
|
|
mgr = get_manager()
|
|
try:
|
|
audio, sr, status = mgr.synthesize(
|
|
voice_id=req.voice_id,
|
|
text=req.text,
|
|
speed=req.speed,
|
|
use_rvc=req.use_rvc,
|
|
rvc_index=req.rvc_index
|
|
)
|
|
buf = io.BytesIO()
|
|
sf.write(buf, audio, sr, format="WAV")
|
|
buf.seek(0)
|
|
return Response(
|
|
content=buf.read(),
|
|
media_type="audio/wav",
|
|
headers={"Content-Disposition": f'inline; filename="vocoloom_{req.voice_id}.wav"'}
|
|
)
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|