# ╔══════════════════════════════════════════════════════════════════╗ # ║ Pro Voice Studio (XTTS + NLP + Resemble Enhance Integration) ║ # ╚══════════════════════════════════════════════════════════════════╝ import os, sys, time, json, uuid, shutil import uvicorn from fastapi import FastAPI, Form, File, UploadFile, HTTPException from fastapi.responses import HTMLResponse, FileResponse, JSONResponse from fastapi.middleware.cors import CORSMiddleware import torch import torchaudio # === Model 1 Imports (NLP) === import pyarabic.araby as araby # === Model 2 Imports (TTS) === from TTS.api import TTS # === Model 3 Imports (Enhancement/Super Resolution) === from resemble_enhance.enhancer.inference import enhance # ── Env ────────────────────────────────────────────────────────────── os.environ["COQUI_TOS_AGREED"] = "1" os.environ.setdefault("HF_HOME", "/data/huggingface") VOICE_LIB = "/data/voice_library" OUTPUT_DIR = "/data/outputs" HISTORY_FILE = "/data/history.json" for d in [VOICE_LIB, OUTPUT_DIR]: os.makedirs(d, exist_ok=True) # ── Hardware ───────────────────────────────────────────────────────── device = "cuda" if torch.cuda.is_available() else "cpu" print(f"[*] Pipeline running on {device.upper()} …") # ── Load Core TTS ──────────────────────────────────────────────────── print("[*] Loading Model 2: XTTS v2 …") xtts_engine = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device) print("[✓] XTTS Model ready.") # ── Text Normalization Wrapper (Model 1) ───────────────────────────── def normalize_arabic_text(text: str) -> str: """Pre-process text to guide the TTS model better.""" text = araby.strip_tashkeel(text) # Optional: Can be modified based on need text = araby.normalize_ligature(text) # Add subtle punctuation spacing to give the acoustic model breathing room text = text.replace("،", "، ").replace(".", ". ") return " ".join(text.split()) # ── Audio Enhancer Wrapper (Model 3) ───────────────────────────────── def enhance_audio_quality(input_path: str, output_path: str): """Pass XTTS output through Resemble Enhance for ElevenLabs-like quality.""" print("[*] Running Model 3: Resemble Enhance (Super Resolution)...") dwav, sr = torchaudio.load(input_path) # Apply inference enhancement (NFE=32 is a good balance of speed/quality) hwav, new_sr = enhance(dwav=dwav, sr=sr, device=device, nfe=32) # Save the polished audio torchaudio.save(output_path, hwav, new_sr) print("[✓] Enhancement complete.") # ── History helpers ────────────────────────────────────────────────── def load_history(): if os.path.exists(HISTORY_FILE): try: return json.load(open(HISTORY_FILE)) except: pass return [] def save_history(h): json.dump(h, open(HISTORY_FILE, "w"), ensure_ascii=False, indent=2) # ── FastAPI app ────────────────────────────────────────────────────── app = FastAPI(title="Pro Voice Studio") app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]) LANGUAGES = { "ar": "العربية", "en": "English", "es": "Español", "fr": "Français", "de": "Deutsch", "it": "Italiano", "pt": "Português","ru": "Русский", "zh-cn": "中文", "ja": "日本語" } # ══════════════════════════════════════════════════════════════════════ # HTML Frontend # ══════════════════════════════════════════════════════════════════════ HTML = r"""