Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,8 +23,8 @@ HF_TOKEN = (
|
|
| 23 |
)
|
| 24 |
|
| 25 |
# Model configuration
|
| 26 |
-
|
| 27 |
-
MODEL_DIR = os.getenv("MODEL_DIR", "/data/
|
| 28 |
MAX_TEXT_LENGTH = 1000
|
| 29 |
DEFAULT_LANGUAGE = "en"
|
| 30 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -49,48 +49,62 @@ os.makedirs(MODEL_DIR, exist_ok=True)
|
|
| 49 |
try:
|
| 50 |
from huggingface_hub import snapshot_download
|
| 51 |
|
| 52 |
-
# Download model if not already present
|
| 53 |
-
if not Path(MODEL_DIR, "
|
| 54 |
-
print(f"Downloading
|
| 55 |
snapshot_download(
|
| 56 |
-
repo_id=
|
| 57 |
local_dir=MODEL_DIR,
|
| 58 |
token=HF_TOKEN,
|
| 59 |
)
|
| 60 |
-
print("
|
| 61 |
except Exception as exc:
|
| 62 |
print(f"Warning: Could not download model: {exc}")
|
| 63 |
# Continue anyway - model might already be present
|
| 64 |
|
| 65 |
-
# Initialize
|
| 66 |
try:
|
| 67 |
-
from
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
raise FileNotFoundError(
|
| 72 |
-
f"
|
| 73 |
)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
| 81 |
)
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
except Exception as exc:
|
| 84 |
-
raise RuntimeError(f"Failed to load
|
| 85 |
|
| 86 |
# Initialize FastAPI app
|
| 87 |
-
app = FastAPI(title="
|
| 88 |
|
| 89 |
|
| 90 |
class GenerateRequest(BaseModel):
|
| 91 |
text: str = Field(..., min_length=1, max_length=MAX_TEXT_LENGTH)
|
| 92 |
speaker_wav: str = Field(..., description="HTTPS URL or base64-encoded audio")
|
| 93 |
-
language: Optional[str] = Field(DEFAULT_LANGUAGE, description="ISO code,
|
|
|
|
| 94 |
|
| 95 |
|
| 96 |
def _require_api_key(x_api_key: Optional[str]):
|
|
@@ -143,13 +157,15 @@ def _temp_speaker_file(speaker_wav: str) -> str:
|
|
| 143 |
def _preprocess_audio_wav(
|
| 144 |
path: str,
|
| 145 |
target_sr: int = 24000,
|
| 146 |
-
target_peak: float = 0.98
|
|
|
|
| 147 |
) -> str:
|
| 148 |
"""
|
| 149 |
-
|
| 150 |
- convert to mono
|
| 151 |
- resample to target_sr
|
| 152 |
- peak-normalize to target_peak (avoid clipping)
|
|
|
|
| 153 |
"""
|
| 154 |
wav, sr = torchaudio.load(path)
|
| 155 |
|
|
@@ -163,6 +179,11 @@ def _preprocess_audio_wav(
|
|
| 163 |
wav = resampler(wav)
|
| 164 |
sr = target_sr
|
| 165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
# Peak normalize
|
| 167 |
peak = wav.abs().max().item() if wav.numel() else 0.0
|
| 168 |
if peak > 0:
|
|
@@ -204,39 +225,74 @@ def _cleanup_files(*files: str):
|
|
| 204 |
|
| 205 |
|
| 206 |
def _run_generate_job(job_id: str, payload: Dict[str, str]):
|
| 207 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
speaker_file = None
|
|
|
|
| 209 |
output_file = None
|
| 210 |
_set_job(job_id, status="processing")
|
| 211 |
|
| 212 |
try:
|
|
|
|
| 213 |
speaker_file = _temp_speaker_file(payload["speaker_wav"])
|
| 214 |
speaker_file = _preprocess_audio_wav(speaker_file)
|
| 215 |
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
tempfile.gettempdir(),
|
| 218 |
-
f"
|
| 219 |
)
|
| 220 |
|
| 221 |
-
|
| 222 |
-
|
|
|
|
| 223 |
text=payload["text"],
|
| 224 |
-
output_path=
|
| 225 |
-
|
| 226 |
-
|
|
|
|
| 227 |
)
|
| 228 |
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
if not Path(output_file).exists():
|
| 232 |
raise RuntimeError(
|
| 233 |
f"TTS generation failed: output file was not created at {output_file}"
|
| 234 |
)
|
| 235 |
|
| 236 |
-
|
|
|
|
| 237 |
_set_job(job_id, status="completed", output_file=output_file)
|
|
|
|
| 238 |
except Exception as exc:
|
| 239 |
-
_cleanup_files(speaker_file, output_file)
|
| 240 |
_set_job(job_id, status="error", error=str(exc))
|
| 241 |
|
| 242 |
|
|
@@ -244,7 +300,12 @@ def _run_generate_job(job_id: str, payload: Dict[str, str]):
|
|
| 244 |
def health(x_api_key: Optional[str] = Header(default=None)):
|
| 245 |
"""Health check endpoint."""
|
| 246 |
_require_api_key(x_api_key)
|
| 247 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
|
| 250 |
@app.post("/generate")
|
|
@@ -254,15 +315,19 @@ def generate(
|
|
| 254 |
x_api_key: Optional[str] = Header(default=None),
|
| 255 |
):
|
| 256 |
"""
|
| 257 |
-
Generate speech from text using voice cloning.
|
| 258 |
Returns job information for async processing.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
"""
|
| 260 |
_require_api_key(x_api_key)
|
| 261 |
|
| 262 |
job_id = str(uuid.uuid4())
|
| 263 |
_set_job(job_id, status="queued")
|
| 264 |
|
| 265 |
-
# Offload the
|
| 266 |
background_tasks.add_task(_run_generate_job, job_id, payload.dict())
|
| 267 |
|
| 268 |
return JSONResponse(
|
|
@@ -336,11 +401,20 @@ def job_result(
|
|
| 336 |
def root():
|
| 337 |
"""API root with available endpoints."""
|
| 338 |
return {
|
| 339 |
-
"name": "
|
|
|
|
|
|
|
| 340 |
"endpoints": [
|
| 341 |
"/health",
|
| 342 |
"/generate",
|
| 343 |
"/status/{job_id}",
|
| 344 |
"/result/{job_id}"
|
| 345 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
}
|
|
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
# Model configuration
|
| 26 |
+
OPENVOICE_REPO = "myshell-ai/OpenVoiceV2"
|
| 27 |
+
MODEL_DIR = os.getenv("MODEL_DIR", "/data/openvoice")
|
| 28 |
MAX_TEXT_LENGTH = 1000
|
| 29 |
DEFAULT_LANGUAGE = "en"
|
| 30 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 49 |
try:
|
| 50 |
from huggingface_hub import snapshot_download
|
| 51 |
|
| 52 |
+
# Download OpenVoice model if not already present
|
| 53 |
+
if not Path(MODEL_DIR, "converter").exists():
|
| 54 |
+
print(f"Downloading OpenVoice model from {OPENVOICE_REPO}...")
|
| 55 |
snapshot_download(
|
| 56 |
+
repo_id=OPENVOICE_REPO,
|
| 57 |
local_dir=MODEL_DIR,
|
| 58 |
token=HF_TOKEN,
|
| 59 |
)
|
| 60 |
+
print("OpenVoice model download complete.")
|
| 61 |
except Exception as exc:
|
| 62 |
print(f"Warning: Could not download model: {exc}")
|
| 63 |
# Continue anyway - model might already be present
|
| 64 |
|
| 65 |
+
# Initialize OpenVoice
|
| 66 |
try:
|
| 67 |
+
from openvoice import se_extractor
|
| 68 |
+
from openvoice.api import BaseSpeakerTTS, ToneColorConverter
|
| 69 |
|
| 70 |
+
# Initialize base TTS model (MeloTTS)
|
| 71 |
+
ckpt_converter = os.path.join(MODEL_DIR, "converter")
|
| 72 |
+
|
| 73 |
+
if not Path(ckpt_converter).exists():
|
| 74 |
raise FileNotFoundError(
|
| 75 |
+
f"Converter checkpoint not found at {ckpt_converter}. Model may not be downloaded."
|
| 76 |
)
|
| 77 |
|
| 78 |
+
# Initialize TTS and Tone Color Converter
|
| 79 |
+
base_speaker_tts = BaseSpeakerTTS(
|
| 80 |
+
f'{MODEL_DIR}/base_speakers/EN/config.json',
|
| 81 |
+
device=DEVICE
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
tone_color_converter = ToneColorConverter(
|
| 85 |
+
f'{ckpt_converter}/config.json',
|
| 86 |
+
device=DEVICE
|
| 87 |
)
|
| 88 |
+
|
| 89 |
+
# Load source speaker embedding (default voice)
|
| 90 |
+
source_se = torch.load(
|
| 91 |
+
f'{MODEL_DIR}/base_speakers/EN/en_default_se.pth',
|
| 92 |
+
map_location=DEVICE
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
print("OpenVoice model loaded successfully.")
|
| 96 |
except Exception as exc:
|
| 97 |
+
raise RuntimeError(f"Failed to load OpenVoice model: {exc}") from exc
|
| 98 |
|
| 99 |
# Initialize FastAPI app
|
| 100 |
+
app = FastAPI(title="openvoice-api", version="1.0.0")
|
| 101 |
|
| 102 |
|
| 103 |
class GenerateRequest(BaseModel):
|
| 104 |
text: str = Field(..., min_length=1, max_length=MAX_TEXT_LENGTH)
|
| 105 |
speaker_wav: str = Field(..., description="HTTPS URL or base64-encoded audio")
|
| 106 |
+
language: Optional[str] = Field(DEFAULT_LANGUAGE, description="ISO code: en, es, fr, zh, ja, ko")
|
| 107 |
+
speed: Optional[float] = Field(1.0, ge=0.5, le=2.0, description="Speech speed (0.5-2.0)")
|
| 108 |
|
| 109 |
|
| 110 |
def _require_api_key(x_api_key: Optional[str]):
|
|
|
|
| 157 |
def _preprocess_audio_wav(
|
| 158 |
path: str,
|
| 159 |
target_sr: int = 24000,
|
| 160 |
+
target_peak: float = 0.98,
|
| 161 |
+
min_duration: float = 3.0
|
| 162 |
) -> str:
|
| 163 |
"""
|
| 164 |
+
Preprocess audio for optimal voice cloning:
|
| 165 |
- convert to mono
|
| 166 |
- resample to target_sr
|
| 167 |
- peak-normalize to target_peak (avoid clipping)
|
| 168 |
+
- ensure minimum duration (OpenVoice works better with 3-10s audio)
|
| 169 |
"""
|
| 170 |
wav, sr = torchaudio.load(path)
|
| 171 |
|
|
|
|
| 179 |
wav = resampler(wav)
|
| 180 |
sr = target_sr
|
| 181 |
|
| 182 |
+
# Check duration (OpenVoice recommends 3-10 seconds)
|
| 183 |
+
duration = wav.shape[1] / sr
|
| 184 |
+
if duration < min_duration:
|
| 185 |
+
print(f"Warning: Reference audio is {duration:.2f}s. OpenVoice works best with 3-10s audio.")
|
| 186 |
+
|
| 187 |
# Peak normalize
|
| 188 |
peak = wav.abs().max().item() if wav.numel() else 0.0
|
| 189 |
if peak > 0:
|
|
|
|
| 225 |
|
| 226 |
|
| 227 |
def _run_generate_job(job_id: str, payload: Dict[str, str]):
|
| 228 |
+
"""
|
| 229 |
+
Background job for TTS generation using OpenVoice.
|
| 230 |
+
Two-step process:
|
| 231 |
+
1. Generate base speech with BaseSpeakerTTS
|
| 232 |
+
2. Apply target voice characteristics with ToneColorConverter
|
| 233 |
+
"""
|
| 234 |
speaker_file = None
|
| 235 |
+
temp_audio = None
|
| 236 |
output_file = None
|
| 237 |
_set_job(job_id, status="processing")
|
| 238 |
|
| 239 |
try:
|
| 240 |
+
# Step 1: Prepare reference audio and extract speaker embedding
|
| 241 |
speaker_file = _temp_speaker_file(payload["speaker_wav"])
|
| 242 |
speaker_file = _preprocess_audio_wav(speaker_file)
|
| 243 |
|
| 244 |
+
# Extract target speaker embedding
|
| 245 |
+
target_se, _ = se_extractor.get_se(
|
| 246 |
+
speaker_file,
|
| 247 |
+
tone_color_converter,
|
| 248 |
+
vad=True # Voice activity detection for better extraction
|
| 249 |
+
)
|
| 250 |
+
|
| 251 |
+
# Step 2: Generate base speech with default voice
|
| 252 |
+
temp_audio = os.path.join(
|
| 253 |
tempfile.gettempdir(),
|
| 254 |
+
f"openvoice-temp-{uuid.uuid4()}.wav"
|
| 255 |
)
|
| 256 |
|
| 257 |
+
speed = float(payload.get("speed", 1.0))
|
| 258 |
+
|
| 259 |
+
base_speaker_tts.tts(
|
| 260 |
text=payload["text"],
|
| 261 |
+
output_path=temp_audio,
|
| 262 |
+
speaker='default',
|
| 263 |
+
language=payload.get("language", "en").upper(),
|
| 264 |
+
speed=speed
|
| 265 |
)
|
| 266 |
|
| 267 |
+
# Step 3: Apply target voice characteristics
|
| 268 |
+
output_file = os.path.join(
|
| 269 |
+
tempfile.gettempdir(),
|
| 270 |
+
f"openvoice-{uuid.uuid4()}.wav"
|
| 271 |
+
)
|
| 272 |
|
| 273 |
+
# Encode with watermark (set to False if not needed)
|
| 274 |
+
encode_message = "@MyShell"
|
| 275 |
+
|
| 276 |
+
tone_color_converter.convert(
|
| 277 |
+
audio_src_path=temp_audio,
|
| 278 |
+
src_se=source_se,
|
| 279 |
+
tgt_se=target_se,
|
| 280 |
+
output_path=output_file,
|
| 281 |
+
message=encode_message
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
+
# Verify output exists
|
| 285 |
if not Path(output_file).exists():
|
| 286 |
raise RuntimeError(
|
| 287 |
f"TTS generation failed: output file was not created at {output_file}"
|
| 288 |
)
|
| 289 |
|
| 290 |
+
# Cleanup intermediate files
|
| 291 |
+
_cleanup_files(speaker_file, temp_audio)
|
| 292 |
_set_job(job_id, status="completed", output_file=output_file)
|
| 293 |
+
|
| 294 |
except Exception as exc:
|
| 295 |
+
_cleanup_files(speaker_file, temp_audio, output_file)
|
| 296 |
_set_job(job_id, status="error", error=str(exc))
|
| 297 |
|
| 298 |
|
|
|
|
| 300 |
def health(x_api_key: Optional[str] = Header(default=None)):
|
| 301 |
"""Health check endpoint."""
|
| 302 |
_require_api_key(x_api_key)
|
| 303 |
+
return {
|
| 304 |
+
"status": "ok",
|
| 305 |
+
"model": "openvoice-v2",
|
| 306 |
+
"device": DEVICE,
|
| 307 |
+
"supported_languages": ["en", "es", "fr", "zh", "ja", "ko"]
|
| 308 |
+
}
|
| 309 |
|
| 310 |
|
| 311 |
@app.post("/generate")
|
|
|
|
| 315 |
x_api_key: Optional[str] = Header(default=None),
|
| 316 |
):
|
| 317 |
"""
|
| 318 |
+
Generate speech from text using voice cloning with OpenVoice.
|
| 319 |
Returns job information for async processing.
|
| 320 |
+
|
| 321 |
+
OpenVoice uses a two-step process:
|
| 322 |
+
1. Generate base speech with MeloTTS
|
| 323 |
+
2. Apply voice characteristics from reference audio
|
| 324 |
"""
|
| 325 |
_require_api_key(x_api_key)
|
| 326 |
|
| 327 |
job_id = str(uuid.uuid4())
|
| 328 |
_set_job(job_id, status="queued")
|
| 329 |
|
| 330 |
+
# Offload the synthesis to background task
|
| 331 |
background_tasks.add_task(_run_generate_job, job_id, payload.dict())
|
| 332 |
|
| 333 |
return JSONResponse(
|
|
|
|
| 401 |
def root():
|
| 402 |
"""API root with available endpoints."""
|
| 403 |
return {
|
| 404 |
+
"name": "openvoice-api",
|
| 405 |
+
"version": "2.0.0",
|
| 406 |
+
"model": "OpenVoice V2",
|
| 407 |
"endpoints": [
|
| 408 |
"/health",
|
| 409 |
"/generate",
|
| 410 |
"/status/{job_id}",
|
| 411 |
"/result/{job_id}"
|
| 412 |
],
|
| 413 |
+
"features": [
|
| 414 |
+
"Voice cloning with 3-10s reference audio",
|
| 415 |
+
"Multi-language support (EN, ES, FR, ZH, JA, KO)",
|
| 416 |
+
"Adjustable speech speed (0.5-2.0x)",
|
| 417 |
+
"Fast CPU performance (5-10x faster than IndexTTS2)"
|
| 418 |
+
]
|
| 419 |
}
|
| 420 |
+
|