Update app.py
Browse files
app.py
CHANGED
|
@@ -13,16 +13,57 @@ from helper import (
|
|
| 13 |
ensure_wav_format,
|
| 14 |
)
|
| 15 |
import wave
|
|
|
|
| 16 |
|
| 17 |
app = Flask(__name__)
|
| 18 |
CORS(app)
|
| 19 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
| 20 |
|
| 21 |
device = "cpu"
|
| 22 |
-
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
|
| 23 |
-
active_tasks = {}
|
| 24 |
|
| 25 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
MAX_AUDIO_SIZE_MB = 15
|
| 27 |
|
| 28 |
|
|
@@ -80,7 +121,6 @@ def generate_voice():
|
|
| 80 |
def process_vox(user_id, text, video, audio_base64, task_id):
|
| 81 |
temp_audio_path = None
|
| 82 |
try:
|
| 83 |
-
# Handle audio or video input
|
| 84 |
if audio_base64:
|
| 85 |
if audio_base64.startswith("data:audio/"):
|
| 86 |
audio_base64 = audio_base64.split(",", 1)[1]
|
|
@@ -91,7 +131,6 @@ def process_vox(user_id, text, video, audio_base64, task_id):
|
|
| 91 |
temp_audio_path = video_to_audio(video, output_path=temp_audio_path)
|
| 92 |
|
| 93 |
temp_audio_path = ensure_wav_format(temp_audio_path)
|
| 94 |
-
|
| 95 |
valid, msg = validate_audio_file(temp_audio_path, MAX_AUDIO_SIZE_MB)
|
| 96 |
if not valid:
|
| 97 |
raise Exception(f"Invalid audio file: {msg}")
|
|
@@ -111,7 +150,6 @@ def process_vox(user_id, text, video, audio_base64, task_id):
|
|
| 111 |
duration = f"{dura:.2f}"
|
| 112 |
title = text[:20]
|
| 113 |
|
| 114 |
-
# Upload + save metadata
|
| 115 |
audio_url = save_to_dataset_repo(file_path, f"user/data/audios/{file_name}", file_name)
|
| 116 |
active_tasks[task_id].update(
|
| 117 |
{
|
|
|
|
| 13 |
ensure_wav_format,
|
| 14 |
)
|
| 15 |
import wave
|
| 16 |
+
import shutil
|
| 17 |
|
| 18 |
app = Flask(__name__)
|
| 19 |
CORS(app)
|
| 20 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
| 21 |
|
| 22 |
device = "cpu"
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# ============================================================
|
| 25 |
+
# MODEL PATH CONFIGURATION
|
| 26 |
+
# ============================================================
|
| 27 |
+
|
| 28 |
+
# Mounted dataset path inside Hugging Face Space
|
| 29 |
+
DATASET_MODEL_DIR = "/datasets/EllenBeta/Xtts_2/model"
|
| 30 |
+
|
| 31 |
+
# Local cache fallback path
|
| 32 |
+
LOCAL_MODEL_DIR = os.path.expanduser("~/.local/share/tts/xtts_v2_model")
|
| 33 |
+
|
| 34 |
+
# If model already exists in dataset, use it directly (no download)
|
| 35 |
+
if os.path.exists(DATASET_MODEL_DIR) and os.listdir(DATASET_MODEL_DIR):
|
| 36 |
+
print(f"✅ Loading model directly from dataset: {DATASET_MODEL_DIR}")
|
| 37 |
+
model_path = DATASET_MODEL_DIR
|
| 38 |
+
else:
|
| 39 |
+
print("⬇️ Downloading XTTS model (first-time only)...")
|
| 40 |
+
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
|
| 41 |
+
|
| 42 |
+
# Save the model locally for re-use
|
| 43 |
+
os.makedirs(LOCAL_MODEL_DIR, exist_ok=True)
|
| 44 |
+
src_model_dir = tts.model_dir
|
| 45 |
+
|
| 46 |
+
# Copy model to dataset path for future Space restarts
|
| 47 |
+
if os.path.exists(src_model_dir):
|
| 48 |
+
print(f"📦 Copying model files to dataset for reuse: {DATASET_MODEL_DIR}")
|
| 49 |
+
os.makedirs(DATASET_MODEL_DIR, exist_ok=True)
|
| 50 |
+
for item in os.listdir(src_model_dir):
|
| 51 |
+
s = os.path.join(src_model_dir, item)
|
| 52 |
+
d = os.path.join(DATASET_MODEL_DIR, item)
|
| 53 |
+
if os.path.isdir(s):
|
| 54 |
+
shutil.copytree(s, d, dirs_exist_ok=True)
|
| 55 |
+
else:
|
| 56 |
+
shutil.copy2(s, d)
|
| 57 |
+
|
| 58 |
+
model_path = src_model_dir
|
| 59 |
+
print("✅ Model cached successfully.")
|
| 60 |
+
|
| 61 |
+
# Initialize TTS from cached or dataset path
|
| 62 |
+
tts = TTS(model_path=model_path).to(device)
|
| 63 |
+
|
| 64 |
+
# ============================================================
|
| 65 |
+
|
| 66 |
+
active_tasks = {}
|
| 67 |
MAX_AUDIO_SIZE_MB = 15
|
| 68 |
|
| 69 |
|
|
|
|
| 121 |
def process_vox(user_id, text, video, audio_base64, task_id):
|
| 122 |
temp_audio_path = None
|
| 123 |
try:
|
|
|
|
| 124 |
if audio_base64:
|
| 125 |
if audio_base64.startswith("data:audio/"):
|
| 126 |
audio_base64 = audio_base64.split(",", 1)[1]
|
|
|
|
| 131 |
temp_audio_path = video_to_audio(video, output_path=temp_audio_path)
|
| 132 |
|
| 133 |
temp_audio_path = ensure_wav_format(temp_audio_path)
|
|
|
|
| 134 |
valid, msg = validate_audio_file(temp_audio_path, MAX_AUDIO_SIZE_MB)
|
| 135 |
if not valid:
|
| 136 |
raise Exception(f"Invalid audio file: {msg}")
|
|
|
|
| 150 |
duration = f"{dura:.2f}"
|
| 151 |
title = text[:20]
|
| 152 |
|
|
|
|
| 153 |
audio_url = save_to_dataset_repo(file_path, f"user/data/audios/{file_name}", file_name)
|
| 154 |
active_tasks[task_id].update(
|
| 155 |
{
|