CenaForge: primeira versão pública
Gerador de vídeos narrativos (Godot + FFmpeg), painel Next.js, serviço de voz, Clipador de lives e Publicador para YouTube, TikTok e Instagram. Inclui a leitura das visualizações dos posts para o Dashboard e o botão "Publicar no <rede>" nos cartões das publicações. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,212 @@
|
||||
"""Processo de clonagem de voz do CenaForge (Chatterbox Multilingual, licença MIT).
|
||||
|
||||
Roda num venv próprio (voice/.venv-clone): o Chatterbox fixa torch 2.6, numpy < 2 e um transformers
|
||||
diferente do serviço de voz. O serviço de voz (porta 4510) chama este processo pela rede local.
|
||||
|
||||
Contrato (só 127.0.0.1, cabeçalho X-Service-Key = VOICE_SERVICE_KEY):
|
||||
- GET /health → {"status": "loading" | "ok" | "error", "device", "error"?}
|
||||
- POST /synthesize JSON {text, reference, language, exaggeration, cfgWeight} → audio/wav (24 kHz, mono, PCM 16)
|
||||
`reference` precisa estar dentro de <VOICE_MODELS_DIR>/clone/.
|
||||
|
||||
Pesos: baixados do Hugging Face (ResembleAI/chatterbox) na primeira subida, para data/voice/models/clone-hf.
|
||||
Todo áudio gerado leva a marca d'água inaudível do Chatterbox (PerTh).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hmac
|
||||
import io
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
from pathlib import Path
|
||||
|
||||
VOICE_ROOT = Path(__file__).resolve().parents[1]
|
||||
PROJECT_ROOT = VOICE_ROOT.parent
|
||||
MAX_TEXT_CHARS = 2000
|
||||
MAX_CHUNK_CHARS = 280 # o Chatterbox perde qualidade em trechos longos: gera por frase e junta
|
||||
PAUSE_S = 0.18
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s", stream=sys.stdout)
|
||||
log = logging.getLogger("cenaforge_clone")
|
||||
|
||||
|
||||
def _read_env() -> dict[str, str]:
|
||||
"""voice/.env (o dev.ts já repassa, mas rodar à mão também funciona)."""
|
||||
values: dict[str, str] = {}
|
||||
env_file = VOICE_ROOT / ".env"
|
||||
if env_file.is_file():
|
||||
for line in env_file.read_text(encoding="utf-8").splitlines():
|
||||
if "=" in line and not line.lstrip().startswith("#"):
|
||||
name, value = line.split("=", 1)
|
||||
values[name.strip()] = value.strip()
|
||||
return {**values, **{k: v for k, v in os.environ.items() if k.startswith("VOICE_")}}
|
||||
|
||||
|
||||
ENV = _read_env()
|
||||
KEY = ENV.get("VOICE_SERVICE_KEY", "")
|
||||
PORT = int(ENV.get("VOICE_CLONE_PORT", "4511"))
|
||||
_models = ENV.get("VOICE_MODELS_DIR", "")
|
||||
MODELS_DIR = (PROJECT_ROOT / _models).resolve() if _models and not Path(_models).is_absolute() else Path(
|
||||
_models or PROJECT_ROOT / "data" / "voice" / "models"
|
||||
).resolve()
|
||||
CLONE_DIR = (MODELS_DIR / "clone").resolve()
|
||||
# Cache do Hugging Face dentro de data/ (não no perfil do usuário): apagar data/voice/models remove tudo.
|
||||
os.environ.setdefault("HF_HOME", str(MODELS_DIR / "clone-hf"))
|
||||
# Windows sem modo desenvolvedor não cria symlink: o cache funciona copiando, e o aviso só polui o terminal.
|
||||
os.environ.setdefault("HF_HUB_DISABLE_SYMLINKS_WARNING", "1")
|
||||
# O download pelo protocolo Xet travou nesta máquina (256 MB em 9 min, arquivo de 2 GB parado); HTTP comum baixou tudo.
|
||||
os.environ.setdefault("HF_HUB_DISABLE_XET", "1")
|
||||
# Uma linha por arquivo baixado do Hugging Face enche o terminal do pnpm dev.
|
||||
logging.getLogger("httpx").setLevel(logging.WARNING)
|
||||
|
||||
|
||||
class Engine:
|
||||
"""Carrega o modelo uma vez (em segundo plano) e serializa as gerações (uma placa, um modelo)."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.status = "loading"
|
||||
self.error: str | None = None
|
||||
self.device = "cpu"
|
||||
self.model = None
|
||||
self._lock = threading.Lock()
|
||||
self._conds_key: tuple | None = None
|
||||
|
||||
def load(self) -> None:
|
||||
try:
|
||||
import torch
|
||||
from chatterbox.mtl_tts import ChatterboxMultilingualTTS
|
||||
|
||||
wanted = ENV.get("VOICE_DEVICE", "auto")
|
||||
self.device = "cuda" if wanted != "cpu" and torch.cuda.is_available() else "cpu"
|
||||
started = time.monotonic()
|
||||
self.model = ChatterboxMultilingualTTS.from_pretrained(device=torch.device(self.device))
|
||||
self.status = "ok"
|
||||
log.info("clonagem pronta em %s (%.0f s)", self.device, time.monotonic() - started)
|
||||
except Exception as exc: # noqa: BLE001 - qualquer falha de carga vira estado "error" no /health
|
||||
self.status, self.error = "error", f"{type(exc).__name__}: {exc}"
|
||||
log.exception("falha ao carregar o Chatterbox")
|
||||
|
||||
def synthesize(self, text: str, reference: Path, language: str, exaggeration: float, cfg_weight: float) -> bytes:
|
||||
import numpy as np
|
||||
import soundfile
|
||||
|
||||
with self._lock:
|
||||
model = self.model
|
||||
stamp = reference.stat()
|
||||
key = (str(reference), stamp.st_mtime_ns, stamp.st_size, exaggeration)
|
||||
if key != self._conds_key:
|
||||
# Condicionamento da voz (timbre da referência): caro, então só refaz quando muda.
|
||||
model.prepare_conditionals(str(reference), exaggeration=exaggeration)
|
||||
self._conds_key = key
|
||||
pieces = []
|
||||
silence = np.zeros(int(model.sr * PAUSE_S), dtype=np.float32)
|
||||
for chunk in split_text(text):
|
||||
wav = model.generate(chunk, language_id=language, exaggeration=exaggeration, cfg_weight=cfg_weight)
|
||||
pieces.extend([wav.squeeze(0).detach().cpu().numpy().astype(np.float32), silence])
|
||||
audio = np.concatenate(pieces[:-1]) if pieces else silence
|
||||
buffer = io.BytesIO()
|
||||
soundfile.write(buffer, np.clip(audio, -1.0, 1.0), model.sr, format="WAV", subtype="PCM_16")
|
||||
return buffer.getvalue()
|
||||
|
||||
|
||||
def split_text(text: str) -> list[str]:
|
||||
"""Quebra em frases e junta as curtas até ~280 caracteres (frase longa demais é cortada nas vírgulas)."""
|
||||
sentences = [s.strip() for s in re.split(r"(?<=[.!?…])\s+", text.strip()) if s.strip()]
|
||||
chunks: list[str] = []
|
||||
for sentence in sentences:
|
||||
parts = [sentence] if len(sentence) <= MAX_CHUNK_CHARS else [p.strip() for p in re.split(r"(?<=[,;:])\s+", sentence)]
|
||||
for part in parts:
|
||||
if chunks and len(chunks[-1]) + 1 + len(part) <= MAX_CHUNK_CHARS:
|
||||
chunks[-1] = f"{chunks[-1]} {part}"
|
||||
else:
|
||||
chunks.append(part[:MAX_CHUNK_CHARS * 2])
|
||||
return chunks
|
||||
|
||||
|
||||
ENGINE = Engine()
|
||||
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
server_version = "cenaforge-clone"
|
||||
|
||||
def log_message(self, format: str, *args) -> None: # noqa: A002 - assinatura da stdlib
|
||||
return # sem log de acesso (o texto do usuário não vai para o terminal)
|
||||
|
||||
def _send(self, status: int, body: bytes, content_type: str = "application/json") -> None:
|
||||
self.send_response(status)
|
||||
self.send_header("Content-Type", content_type)
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.send_header("Cache-Control", "no-store")
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def _error(self, status: int, message: str) -> None:
|
||||
self._send(status, json.dumps({"error": message}, ensure_ascii=False).encode("utf-8"))
|
||||
|
||||
def _authorized(self) -> bool:
|
||||
given = self.headers.get("X-Service-Key", "")
|
||||
return bool(KEY) and hmac.compare_digest(given.encode("utf-8"), KEY.encode("utf-8"))
|
||||
|
||||
def do_GET(self) -> None: # noqa: N802 - nome da stdlib
|
||||
if self.path != "/health":
|
||||
return self._error(404, "rota desconhecida")
|
||||
payload = {"status": ENGINE.status, "device": ENGINE.device}
|
||||
if ENGINE.error:
|
||||
payload["error"] = ENGINE.error
|
||||
self._send(200, json.dumps(payload).encode("utf-8"))
|
||||
|
||||
def do_POST(self) -> None: # noqa: N802 - nome da stdlib
|
||||
if self.path != "/synthesize":
|
||||
return self._error(404, "rota desconhecida")
|
||||
if not self._authorized():
|
||||
return self._error(401, "chave ausente ou inválida")
|
||||
if ENGINE.status != "ok":
|
||||
return self._error(503, f"modelo ainda {'carregando' if ENGINE.status == 'loading' else 'com erro'}")
|
||||
try:
|
||||
length = int(self.headers.get("Content-Length", "0"))
|
||||
if not 0 < length <= 64 * 1024:
|
||||
raise ValueError("corpo vazio ou grande demais")
|
||||
body = json.loads(self.rfile.read(length))
|
||||
text = str(body["text"]).strip()
|
||||
if not text or len(text) > MAX_TEXT_CHARS:
|
||||
raise ValueError(f"texto vazio ou acima de {MAX_TEXT_CHARS} caracteres")
|
||||
reference = Path(str(body["reference"])).resolve()
|
||||
if CLONE_DIR not in reference.parents or not reference.is_file():
|
||||
raise ValueError("áudio de referência fora de data/voice/models/clone ou ausente")
|
||||
language = str(body.get("language", "pt"))[:5]
|
||||
exaggeration = min(1.5, max(0.25, float(body.get("exaggeration", 0.5))))
|
||||
cfg_weight = min(1.0, max(0.0, float(body.get("cfgWeight", 0.5))))
|
||||
except (ValueError, KeyError, TypeError, json.JSONDecodeError) as exc:
|
||||
return self._error(400, str(exc))
|
||||
try:
|
||||
wav = ENGINE.synthesize(text, reference, language, exaggeration, cfg_weight)
|
||||
except Exception as exc: # noqa: BLE001 - falha do modelo vira 500 com o tipo do erro
|
||||
log.exception("falha ao gerar a voz clonada")
|
||||
return self._error(500, f"{type(exc).__name__}: {exc}")
|
||||
self._send(200, wav, "audio/wav")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if len(KEY) < 32:
|
||||
log.error("VOICE_SERVICE_KEY ausente: rode o setup.bat")
|
||||
sys.exit(1)
|
||||
if not 4500 <= PORT <= 4599:
|
||||
log.error("VOICE_CLONE_PORT=%s fora da faixa 4500–4599 do CenaForge", PORT)
|
||||
sys.exit(1)
|
||||
threading.Thread(target=ENGINE.load, name="load-model", daemon=True).start()
|
||||
server = ThreadingHTTPServer(("127.0.0.1", PORT), Handler)
|
||||
log.info("clonagem ouvindo em 127.0.0.1:%d (modelo carregando…)", PORT)
|
||||
try:
|
||||
server.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user