Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,21 @@ import uuid
|
|
| 3 |
import shutil
|
| 4 |
import secrets
|
| 5 |
import traceback
|
|
|
|
| 6 |
from fastapi import FastAPI, Depends, HTTPException, status, File, UploadFile, Form
|
| 7 |
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
| 8 |
from fastapi.responses import FileResponse
|
| 9 |
from TTS.api import TTS
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# --- KONFIGURASI FOLDER TEMP ---
|
| 12 |
TEMP_DIR = "/tmp/coqui_data"
|
| 13 |
AUDIO_DIR = "/tmp/audio_output"
|
|
@@ -20,8 +30,8 @@ os.environ["COQUI_TOS_AGREED"] = "1"
|
|
| 20 |
|
| 21 |
app = FastAPI(
|
| 22 |
title="PasBlast Dynamic Coqui API",
|
| 23 |
-
description="API Komplet dengan Dynamic Model Loading. Memanfaatkan 16GB RAM untuk menjalankan berbagai model secara leluasa.",
|
| 24 |
-
version="2.0.
|
| 25 |
)
|
| 26 |
security = HTTPBasic()
|
| 27 |
|
|
@@ -119,26 +129,21 @@ def generate_tts(
|
|
| 119 |
tts = get_tts_instance(model_name)
|
| 120 |
output_path = os.path.join(AUDIO_DIR, f"tts_{uuid.uuid4().hex}.wav")
|
| 121 |
|
| 122 |
-
# Validasi dan auto-fallback speaker jika model membutuhkan speaker
|
| 123 |
if hasattr(tts, "speakers") and tts.speakers:
|
| 124 |
if speaker not in tts.speakers:
|
| 125 |
print(f"Speaker '{speaker}' tidak valid untuk model {model_name}. Menggunakan default.")
|
| 126 |
speaker = tts.speakers[0]
|
| 127 |
|
| 128 |
-
# Validasi language
|
| 129 |
if hasattr(tts, "languages") and tts.languages:
|
| 130 |
if language not in tts.languages:
|
| 131 |
language = tts.languages[0]
|
| 132 |
|
| 133 |
try:
|
| 134 |
print(f"Memproses TTS menggunakan {model_name}...")
|
| 135 |
-
|
| 136 |
-
# Eksekusi berdasarkan apakah model XTTS atau VITS/FastPitch biasa
|
| 137 |
kwargs = {"text": text, "file_path": output_path}
|
| 138 |
if speaker: kwargs["speaker"] = speaker
|
| 139 |
if language: kwargs["language"] = language
|
| 140 |
|
| 141 |
-
# XTTS mendukung parameter tuning tambahan, model lama mungkin tidak
|
| 142 |
if "xtts" in model_name.lower():
|
| 143 |
kwargs.update({"split_sentences": True, "temperature": temperature, "length_penalty": length_penalty, "repetition_penalty": repetition_penalty})
|
| 144 |
|
|
@@ -152,7 +157,7 @@ def generate_tts(
|
|
| 152 |
@app.post("/tts_voice_clone", tags=["Core Generation"])
|
| 153 |
def generate_tts_voice_clone(
|
| 154 |
text: str = Form(...),
|
| 155 |
-
model_name: str = Form("tts_models/multilingual/multi-dataset/xtts_v2", description="Harus model yang mendukung kloning
|
| 156 |
language: str = Form("id"),
|
| 157 |
reference_audio: UploadFile = File(...),
|
| 158 |
temperature: float = Form(0.75),
|
|
@@ -175,7 +180,6 @@ def generate_tts_voice_clone(
|
|
| 175 |
tts.tts_to_file(**kwargs)
|
| 176 |
return FileResponse(output_path, media_type="audio/wav", filename="pasblast_cloned.wav")
|
| 177 |
except Exception as e:
|
| 178 |
-
error_trace = traceback.format_exc()
|
| 179 |
raise HTTPException(status_code=500, detail=f"Gagal memproses Voice Cloning: {str(e)}")
|
| 180 |
|
| 181 |
@app.post("/voice_conversion", tags=["Core Generation"])
|
|
|
|
| 3 |
import shutil
|
| 4 |
import secrets
|
| 5 |
import traceback
|
| 6 |
+
import torch # Ditambahkan untuk melakukan patch sistem
|
| 7 |
from fastapi import FastAPI, Depends, HTTPException, status, File, UploadFile, Form
|
| 8 |
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
| 9 |
from fastapi.responses import FileResponse
|
| 10 |
from TTS.api import TTS
|
| 11 |
|
| 12 |
+
# --- PATCH PYTORCH 2.6+ ---
|
| 13 |
+
# Memaksa PyTorch untuk mengizinkan pemuatan objek kustom Coqui TTS
|
| 14 |
+
original_torch_load = torch.load
|
| 15 |
+
def patched_torch_load(*args, **kwargs):
|
| 16 |
+
kwargs['weights_only'] = False
|
| 17 |
+
return original_torch_load(*args, **kwargs)
|
| 18 |
+
torch.load = patched_torch_load
|
| 19 |
+
# --------------------------
|
| 20 |
+
|
| 21 |
# --- KONFIGURASI FOLDER TEMP ---
|
| 22 |
TEMP_DIR = "/tmp/coqui_data"
|
| 23 |
AUDIO_DIR = "/tmp/audio_output"
|
|
|
|
| 30 |
|
| 31 |
app = FastAPI(
|
| 32 |
title="PasBlast Dynamic Coqui API",
|
| 33 |
+
description="API Komplet dengan Dynamic Model Loading dan PyTorch Patch. Memanfaatkan 16GB RAM untuk menjalankan berbagai model secara leluasa.",
|
| 34 |
+
version="2.0.1"
|
| 35 |
)
|
| 36 |
security = HTTPBasic()
|
| 37 |
|
|
|
|
| 129 |
tts = get_tts_instance(model_name)
|
| 130 |
output_path = os.path.join(AUDIO_DIR, f"tts_{uuid.uuid4().hex}.wav")
|
| 131 |
|
|
|
|
| 132 |
if hasattr(tts, "speakers") and tts.speakers:
|
| 133 |
if speaker not in tts.speakers:
|
| 134 |
print(f"Speaker '{speaker}' tidak valid untuk model {model_name}. Menggunakan default.")
|
| 135 |
speaker = tts.speakers[0]
|
| 136 |
|
|
|
|
| 137 |
if hasattr(tts, "languages") and tts.languages:
|
| 138 |
if language not in tts.languages:
|
| 139 |
language = tts.languages[0]
|
| 140 |
|
| 141 |
try:
|
| 142 |
print(f"Memproses TTS menggunakan {model_name}...")
|
|
|
|
|
|
|
| 143 |
kwargs = {"text": text, "file_path": output_path}
|
| 144 |
if speaker: kwargs["speaker"] = speaker
|
| 145 |
if language: kwargs["language"] = language
|
| 146 |
|
|
|
|
| 147 |
if "xtts" in model_name.lower():
|
| 148 |
kwargs.update({"split_sentences": True, "temperature": temperature, "length_penalty": length_penalty, "repetition_penalty": repetition_penalty})
|
| 149 |
|
|
|
|
| 157 |
@app.post("/tts_voice_clone", tags=["Core Generation"])
|
| 158 |
def generate_tts_voice_clone(
|
| 159 |
text: str = Form(...),
|
| 160 |
+
model_name: str = Form("tts_models/multilingual/multi-dataset/xtts_v2", description="Harus model yang mendukung kloning"),
|
| 161 |
language: str = Form("id"),
|
| 162 |
reference_audio: UploadFile = File(...),
|
| 163 |
temperature: float = Form(0.75),
|
|
|
|
| 180 |
tts.tts_to_file(**kwargs)
|
| 181 |
return FileResponse(output_path, media_type="audio/wav", filename="pasblast_cloned.wav")
|
| 182 |
except Exception as e:
|
|
|
|
| 183 |
raise HTTPException(status_code=500, detail=f"Gagal memproses Voice Cloning: {str(e)}")
|
| 184 |
|
| 185 |
@app.post("/voice_conversion", tags=["Core Generation"])
|