Spaces:
Paused
Paused
File size: 21,894 Bytes
15d3766 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 | """
api.py β FastAPI front-door for the Avatar Renderer Pod
==========================================================
* POST /render β returns {jobId, statusUrl, async} (expects server-side file paths)
* POST /render-upload β upload avatar + audio, returns {jobId, statusUrl, async} (browser-friendly)
* GET /status/{id} β returns either {"state": "..."} or the MP4 file
* GET /avatars β list available models and system capabilities
* GET /health/live β liveness probe (200 OK)
* GET /health/ready β readiness probe (checks Celery broker if present)
* POST /text-to-audio β synthesize text to speech via Chatterbox
"""
from __future__ import annotations
import json
import shutil
import uuid
from pathlib import Path
from typing import Optional
from fastapi import BackgroundTasks, FastAPI, File, Form, HTTPException, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, Field
from .settings import Settings # pydantic-based env loader
settings = Settings()
# βββββββββββββββββββ TTS imports βββββββββββββββββββββββββββββββββββββββββ #
try:
from .tts.chatterbox_client import (
ChatterboxTtsError,
tts_wav_bytes_async,
tts_wav_base64_async,
chatterbox_health_async,
)
tts_available = True
except ImportError:
tts_available = False
# βββββββββββββββββββ Celery optional ββββββββββββββββββββββββββββββββββββββ #
celery_available = False
try:
from celery import Celery
from celery.result import AsyncResult
celery_app = Celery(
"avatar_renderer",
broker=settings.CELERY_BROKER_URL,
backend=settings.CELERY_BACKEND_URL or settings.CELERY_BROKER_URL,
)
celery_available = bool(settings.CELERY_BROKER_URL)
except ImportError:
celery_app = None # type: ignore
# import pipeline after Celery to avoid GPU init on health checks
from .pipeline import render_pipeline # noqa: E402
# βββββββββββββββββββββββββ FastAPI setup ββββββββββββββββββββββββββββββββββ #
app = FastAPI(
title="avatar-renderer-svc",
version="0.1.0",
description="Generate a lip-synced avatar video (REST faΓ§ade)",
)
# βββββββββββββββββββββββββββββ CORS setup ββββββββββββββββββββββββββββββββββ #
app.add_middleware(
CORSMiddleware,
allow_origins=[
"*",
"http://localhost:3000",
"http://localhost:3001",
"https://*.vercel.app",
"https://vercel.app",
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# βββββββββββββββββ Static frontend (HF Spaces / Docker) βββββββββββββββββββ #
_STATIC_DIR = Path(__file__).resolve().parent.parent / "static"
if _STATIC_DIR.is_dir():
app.mount("/static", StaticFiles(directory=str(_STATIC_DIR)), name="static")
@app.get("/", response_class=HTMLResponse, include_in_schema=False)
def serve_frontend():
index = _STATIC_DIR / "index.html"
if index.exists():
return HTMLResponse(index.read_text())
return HTMLResponse("<h1>Avatar Renderer MCP</h1><p>API is running. Visit <a href='/docs'>/docs</a></p>")
WORK_ROOT = Path("/tmp/avatar-jobs")
WORK_ROOT.mkdir(parents=True, exist_ok=True)
# βββββββββββββββββββββββββββ Pydantic models ββββββββββββββββββββββββββββββ #
class RenderBody(BaseModel):
avatarPath: str = Field(..., alias="avatarPath", description="Path to PNG/JPG portrait image")
audioPath: str = Field(..., alias="audioPath", description="Path to WAV/MP3 audio file")
driverVideo: Optional[str] = Field(None, alias="driverVideo", description="Optional MP4 for head pose")
visemeJson: Optional[str] = Field(None, alias="visemeJson", description="Optional phoneme alignment JSON")
qualityMode: str = Field(
default="auto",
alias="qualityMode",
description=(
"Rendering quality mode: 'real_time' (Wav2Lip/MuseTalk), "
"'high_quality' (FOMM+Diff2Lip/LatentSync+GFPGAN), "
"'cinematic' (Hallo3 DiT), '3d' (Gaussian Splatting), or 'auto'"
)
)
enhancements: Optional[list] = Field(
default=None,
description=(
"List of enhancement names to apply. Options: "
"'emotion_expressions', 'musetalk_lipsync', 'eye_gaze_blink', "
"'liveportrait_driver', 'latentsync_lipsync', 'hallo3_cinematic', "
"'cosyvoice_tts', 'viseme_guided', 'gesture_animation', "
"'gaussian_splatting'. Use ['all'] for all available."
)
)
transcript: Optional[str] = Field(
None,
description="Optional text transcript of the audio (enables emotion detection and gesture sync)"
)
class TextToAudioRequest(BaseModel):
text: str = Field(..., description="Text to synthesize into speech")
voice: Optional[str] = Field(None, description="Voice profile: 'female', 'male', or 'neutral'")
language: Optional[str] = Field(None, description="Language code (ISO 639-1, e.g., 'en', 'it', 'fr')")
temperature: Optional[float] = Field(0.7, description="Temperature for TTS generation (0.0-1.0)", ge=0.0, le=1.0)
cfg_weight: Optional[float] = Field(0.4, description="CFG weight for TTS generation (0.0-1.0)", ge=0.0, le=1.0)
exaggeration: Optional[float] = Field(0.3, description="Exaggeration for TTS generation (0.0-1.0)", ge=0.0, le=1.0)
speed: Optional[float] = Field(1.0, description="Speed for TTS generation (0.5-2.0)", ge=0.5, le=2.0)
output_format: Optional[str] = Field("file", description="Output format: 'file' (WAV file) or 'base64' (base64-encoded WAV)")
class TextToAudioResponse(BaseModel):
status: str = Field(..., description="Status of the request ('success' or 'error')")
audio_path: Optional[str] = Field(None, description="Path to the generated WAV file (if output_format='file')")
audio_base64: Optional[str] = Field(None, description="Base64-encoded WAV audio (if output_format='base64')")
error: Optional[str] = Field(None, description="Error message (if status='error')")
# βββββββββββββββββββββββββ Celery vs Thread task βββββββββββββββββββββββββββ #
if celery_available:
@celery_app.task(name="render_video_task")
def _render_video_task(payload: dict):
render_pipeline(
face_image=payload["avatar_path"],
audio=payload["audio_path"],
reference_video=payload.get("driver_video"),
viseme_json=payload.get("viseme_json"),
quality_mode=payload.get("quality_mode", "auto"),
out_path=payload["out_path"],
enhancements=payload.get("enhancements"),
transcript=payload.get("transcript"),
)
else:
def _render_video_thread(payload: dict):
render_pipeline(
face_image=payload["avatar_path"],
audio=payload["audio_path"],
reference_video=payload.get("driver_video"),
viseme_json=payload.get("viseme_json"),
quality_mode=payload.get("quality_mode", "auto"),
out_path=payload["out_path"],
enhancements=payload.get("enhancements"),
transcript=payload.get("transcript"),
)
# mark success for readiness
(WORK_ROOT / payload["job_id"] / "done").touch()
# βββββββββββββββββββββββββββββ REST endpoints ββββββββββββββββββββββββββββββ #
@app.post("/render")
def render_job(body: RenderBody, bg: BackgroundTasks):
"""Start a render job and return jobId + status URL."""
job_id = str(uuid.uuid4())
job_dir = WORK_ROOT / job_id
job_dir.mkdir(parents=True, exist_ok=True)
out_mp4 = job_dir / "out.mp4"
# Resolve enhancements: per-request or default from settings
active_enhancements = body.enhancements
if active_enhancements is None and settings.DEFAULT_ENHANCEMENTS:
active_enhancements = [e.strip() for e in settings.DEFAULT_ENHANCEMENTS.split(",") if e.strip()]
payload = {
"job_id": job_id,
"avatar_path": body.avatarPath,
"audio_path": body.audioPath,
"driver_video": body.driverVideo,
"viseme_json": body.visemeJson,
"quality_mode": body.qualityMode,
"out_path": str(out_mp4),
"enhancements": active_enhancements,
"transcript": body.transcript,
}
# save original request
(job_dir / "request.json").write_text(json.dumps(body.dict(by_alias=True), indent=2))
if celery_available:
task = _render_video_task.delay(payload) # type: ignore
(job_dir / "celery_id").write_text(task.id)
async_mode = True
else:
bg.add_task(_render_video_thread, payload)
async_mode = False
return {
"jobId": job_id,
"statusUrl": f"/status/{job_id}",
"async": async_mode,
}
@app.post("/render-upload")
async def render_upload(
bg: BackgroundTasks,
avatar: UploadFile = File(...),
audio: UploadFile = File(...),
qualityMode: str = Form("auto"),
enhancements: Optional[str] = Form(None),
transcript: Optional[str] = Form(None),
):
"""Upload avatar image + audio, start render job, return jobId + status URL.
enhancements: Comma-separated list of enhancement names (e.g., 'emotion_expressions,eye_gaze_blink')
transcript: Optional text transcript of the audio
"""
job_id = str(uuid.uuid4())
job_dir = WORK_ROOT / job_id
job_dir.mkdir(parents=True, exist_ok=True)
# Save uploaded files
avatar_path = job_dir / f"avatar_{avatar.filename}"
audio_path = job_dir / f"audio_{audio.filename}"
out_mp4 = job_dir / "out.mp4"
with avatar_path.open("wb") as f:
shutil.copyfileobj(avatar.file, f)
with audio_path.open("wb") as f:
shutil.copyfileobj(audio.file, f)
# Parse enhancements from comma-separated form field
active_enhancements = None
if enhancements:
active_enhancements = [e.strip() for e in enhancements.split(",") if e.strip()]
elif settings.DEFAULT_ENHANCEMENTS:
active_enhancements = [e.strip() for e in settings.DEFAULT_ENHANCEMENTS.split(",") if e.strip()]
payload = {
"job_id": job_id,
"avatar_path": str(avatar_path),
"audio_path": str(audio_path),
"quality_mode": qualityMode,
"out_path": str(out_mp4),
"enhancements": active_enhancements,
"transcript": transcript,
}
# save upload metadata
(job_dir / "upload.json").write_text(
json.dumps(
{
"avatar_filename": avatar.filename,
"audio_filename": audio.filename,
"quality_mode": qualityMode,
"enhancements": active_enhancements,
},
indent=2,
)
)
if celery_available:
task = _render_video_task.delay(payload) # type: ignore
(job_dir / "celery_id").write_text(task.id)
async_mode = True
else:
bg.add_task(_render_video_thread, payload)
async_mode = False
return {
"jobId": job_id,
"statusUrl": f"/status/{job_id}",
"async": async_mode,
}
@app.get("/status/{job_id}")
def get_status(job_id: str):
"""Fetch job state or return the completed MP4."""
job_dir = WORK_ROOT / job_id
if not job_dir.exists():
raise HTTPException(404, "job not found")
out_mp4 = job_dir / "out.mp4"
if out_mp4.exists():
return FileResponse(out_mp4, media_type="video/mp4")
if celery_available:
celery_id_path = job_dir / "celery_id"
if not celery_id_path.exists():
raise HTTPException(500, "job metadata missing")
task_id = celery_id_path.read_text()
task = AsyncResult(task_id, app=celery_app)
return {"state": task.state}
else:
done_marker = job_dir / "done"
state = "finished" if done_marker.exists() else "processing"
return {"state": state}
# βββββββββββββββββββββββ Text-to-Audio endpoint ββββββββββββββββββββββββββββββ #
@app.post("/text-to-audio", response_model=TextToAudioResponse)
async def text_to_audio(body: TextToAudioRequest):
"""
Convert text to speech using the Chatterbox TTS service.
This endpoint generates audio from text and returns either:
- A WAV file path (if output_format='file')
- Base64-encoded WAV audio (if output_format='base64')
The generated audio can be used with the avatar rendering pipeline.
"""
if not tts_available:
raise HTTPException(
503,
"TTS service is not available. Please check the Chatterbox TTS server configuration."
)
try:
# Determine output format
output_format = body.output_format or "file"
# Get voice and language (use defaults from settings if not specified)
voice = body.voice or settings.CHATTERBOX_DEFAULT_VOICE
language = body.language or settings.CHATTERBOX_DEFAULT_LANGUAGE
if output_format == "base64":
# Return base64-encoded WAV
audio_base64 = await tts_wav_base64_async(
body.text,
voice=voice,
language=language,
temperature=body.temperature or 0.7,
cfg_weight=body.cfg_weight or 0.4,
exaggeration=body.exaggeration or 0.3,
speed=body.speed or 1.0,
)
return TextToAudioResponse(
status="success",
audio_base64=audio_base64,
)
else:
# Generate WAV and save to file
wav_bytes = await tts_wav_bytes_async(
body.text,
voice=voice,
language=language,
temperature=body.temperature or 0.7,
cfg_weight=body.cfg_weight or 0.4,
exaggeration=body.exaggeration or 0.3,
speed=body.speed or 1.0,
)
# Save to a file in the work directory
audio_id = str(uuid.uuid4())
audio_dir = WORK_ROOT / f"tts-{audio_id}"
audio_dir.mkdir(parents=True, exist_ok=True)
audio_path = audio_dir / "audio.wav"
audio_path.write_bytes(wav_bytes)
return TextToAudioResponse(
status="success",
audio_path=str(audio_path),
)
except ChatterboxTtsError as exc:
return TextToAudioResponse(
status="error",
error=f"TTS generation failed: {str(exc)}",
)
except Exception as exc:
return TextToAudioResponse(
status="error",
error=f"Unexpected error: {str(exc)}",
)
# ββββββββββββββββββββββ Health & Readiness probes ββββββββββββββββββββββββββ #
@app.get("/health/live")
def liveness():
return JSONResponse({"status": "alive"})
@app.get("/health/ready")
def readiness():
if celery_available:
try:
celery_app.control.ping(timeout=1)
except Exception as err:
raise HTTPException(503, f"celery ping failed: {err}") from err
return JSONResponse({"status": "ready"})
@app.get("/health/tts")
async def tts_health():
"""Check the health of the Chatterbox TTS service."""
if not tts_available:
raise HTTPException(
503,
"TTS service is not available. TTS module could not be imported."
)
try:
health_status = await chatterbox_health_async()
return JSONResponse({
"status": "healthy",
"tts_server": settings.CHATTERBOX_URL,
"details": health_status,
})
except ChatterboxTtsError as exc:
raise HTTPException(
503,
f"TTS health check failed: {str(exc)}"
) from exc
except Exception as exc:
raise HTTPException(
503,
f"Unexpected TTS health check error: {str(exc)}"
) from exc
@app.get("/avatars")
def list_avatars():
"""List available avatar models and their status.
This endpoint provides health status for all avatar rendering models,
indicating which models are available and ready for use.
"""
import torch
from pathlib import Path
# Model checkpoint paths from settings
model_checks = {
"fomm": {
"name": "First Order Motion Model",
"path": settings.FOMM_CKPT_DIR / "vox-cpk.pth.tar",
"purpose": "Head pose and expression generation",
"required_for": "high_quality"
},
"diff2lip": {
"name": "Diff2Lip",
"path": settings.DIFF2LIP_CKPT_DIR / "Diff2Lip.pth",
"purpose": "Photorealistic lip synchronization",
"required_for": "high_quality"
},
"sadtalker": {
"name": "SadTalker",
"path": settings.SADTALKER_CKPT_DIR / "sadtalker.pth",
"purpose": "Talking head generation (fallback)",
"required_for": "real_time"
},
"wav2lip": {
"name": "Wav2Lip",
"path": settings.WAV2LIP_CKPT,
"purpose": "Lip synchronization GAN (fallback)",
"required_for": "real_time"
},
"gfpgan": {
"name": "GFPGAN",
"path": settings.GFPGAN_CKPT,
"purpose": "Face enhancement",
"required_for": "both"
}
}
models_status = {}
for model_id, info in model_checks.items():
path = Path(info["path"])
models_status[model_id] = {
"name": info["name"],
"purpose": info["purpose"],
"required_for": info["required_for"],
"available": path.exists(),
"path": str(path)
}
# System capabilities
cuda_available = torch.cuda.is_available()
gpu_count = torch.cuda.device_count() if cuda_available else 0
gpu_info = []
if cuda_available:
for i in range(gpu_count):
try:
gpu_info.append({
"id": i,
"name": torch.cuda.get_device_name(i),
"memory_total_gb": round(torch.cuda.get_device_properties(i).total_memory / 1024**3, 2)
})
except Exception:
pass
# Determine rendering modes available
high_quality_ready = (
models_status["fomm"]["available"] and
models_status["diff2lip"]["available"] and
cuda_available
)
real_time_ready = (
models_status["sadtalker"]["available"] and
models_status["wav2lip"]["available"]
)
# Enhancements status
enhancements_info = []
try:
from .enhancements import registry as enhancement_registry
enhancements_info = enhancement_registry.get_info_all()
except ImportError:
pass
return JSONResponse({
"status": "ready",
"models": models_status,
"system": {
"cuda_available": cuda_available,
"gpu_count": gpu_count,
"gpus": gpu_info,
"celery_enabled": celery_available,
"tts_enabled": tts_available
},
"rendering_modes": {
"high_quality": {
"available": high_quality_ready,
"description": "FOMM + Diff2Lip pipeline for best quality (requires GPU)",
"models": ["fomm", "diff2lip", "gfpgan"]
},
"real_time": {
"available": real_time_ready,
"description": "SadTalker + Wav2Lip pipeline for faster processing",
"models": ["sadtalker", "wav2lip", "gfpgan"]
},
"cinematic": {
"available": any(e["name"] == "hallo3_cinematic" and e["available"] for e in enhancements_info),
"description": "Hallo3 Diffusion Transformer for cinematic quality (slow, GPU intensive)",
"models": ["hallo3"]
},
"3d": {
"available": any(e["name"] == "gaussian_splatting" and e["available"] for e in enhancements_info),
"description": "InsTaG 3D Gaussian Splatting for real-time 3D avatars",
"models": ["instag"]
}
},
"enhancements": enhancements_info,
"tts": {
"available": tts_available,
"server_url": settings.CHATTERBOX_URL if tts_available else None,
"default_voice": settings.CHATTERBOX_DEFAULT_VOICE if tts_available else None,
"default_language": settings.CHATTERBOX_DEFAULT_LANGUAGE if tts_available else None,
"description": "Chatterbox TTS for text-to-speech synthesis"
}
}) |