Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# π
|
| 2 |
# Must be first to patch environment correctly
|
| 3 |
try:
|
| 4 |
import spaces
|
|
@@ -26,10 +26,11 @@ import gc
|
|
| 26 |
import sys
|
| 27 |
import types
|
| 28 |
import logging
|
|
|
|
| 29 |
from threading import Thread, Lock
|
| 30 |
from huggingface_hub import snapshot_download, hf_hub_download
|
| 31 |
|
| 32 |
-
# π‘οΈ 1. SILENCE & ENV (
|
| 33 |
logging.getLogger("transformers").setLevel(logging.ERROR)
|
| 34 |
logging.getLogger("TTS").setLevel(logging.ERROR)
|
| 35 |
os.environ["CT2_VERBOSE"] = "0"
|
|
@@ -37,7 +38,7 @@ os.environ["ORT_LOGGING_LEVEL"] = "3"
|
|
| 37 |
os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
|
| 38 |
os.environ["GRADIO_SERVER_PORT"] = "7860"
|
| 39 |
|
| 40 |
-
# π οΈ 2. COMPATIBILITY PATCHES (
|
| 41 |
if "torchaudio.backend" not in sys.modules:
|
| 42 |
backend = types.ModuleType("torchaudio.backend")
|
| 43 |
common = types.ModuleType("torchaudio.backend.common")
|
|
@@ -77,14 +78,13 @@ except Exception: pass
|
|
| 77 |
|
| 78 |
# π¦ 3. AI LIBRARIES
|
| 79 |
import chatterbox_utils
|
| 80 |
-
# We import types/classes but do NOT instantiate them at top-level
|
| 81 |
from faster_whisper import WhisperModel
|
| 82 |
from TTS.api import TTS
|
| 83 |
from df.enhance import init_df
|
| 84 |
import deep_translator
|
| 85 |
|
| 86 |
-
# FORCE BUILD TRIGGER: 17:
|
| 87 |
-
#
|
| 88 |
|
| 89 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
| 90 |
MODELS = {"stt": None, "translate": None, "tts": None, "denoiser": None}
|
|
@@ -93,7 +93,7 @@ WARMUP_STATUS = {"complete": False, "in_progress": False}
|
|
| 93 |
WARMUP_LOCK = Lock()
|
| 94 |
|
| 95 |
def activate_gpu_models(action):
|
| 96 |
-
"""
|
| 97 |
global MODELS, WARMUP_STATUS
|
| 98 |
local_only = WARMUP_STATUS["complete"]
|
| 99 |
|
|
@@ -102,7 +102,7 @@ def activate_gpu_models(action):
|
|
| 102 |
try: stt_on_gpu = MODELS["stt"] is not None and MODELS["stt"].model.device == "cuda"
|
| 103 |
except: pass
|
| 104 |
if not stt_on_gpu:
|
| 105 |
-
print(f"ποΈ [
|
| 106 |
try:
|
| 107 |
if MODELS["stt"]: del MODELS["stt"]; gc.collect(); torch.cuda.empty_cache()
|
| 108 |
MODELS["stt"] = WhisperModel(
|
|
@@ -123,14 +123,13 @@ def activate_gpu_models(action):
|
|
| 123 |
tts_on_gpu = "cuda" in curr
|
| 124 |
except: pass
|
| 125 |
if MODELS["tts"] is None or not tts_on_gpu:
|
| 126 |
-
print(f"π [
|
| 127 |
try:
|
| 128 |
if MODELS["tts"] is None:
|
| 129 |
MODELS["tts"] = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2", gpu=True)
|
| 130 |
else: MODELS["tts"].to("cuda")
|
| 131 |
except: pass
|
| 132 |
|
| 133 |
-
# In v109, we only load chatterbox into CUDA when needed
|
| 134 |
chatterbox_utils.load_chatterbox(device="cuda")
|
| 135 |
|
| 136 |
if MODELS["denoiser"] is None:
|
|
@@ -139,9 +138,9 @@ def activate_gpu_models(action):
|
|
| 139 |
if MODELS["translate"] is None: MODELS["translate"] = "active"
|
| 140 |
|
| 141 |
def release_gpu_models():
|
| 142 |
-
"""
|
| 143 |
global MODELS
|
| 144 |
-
print("π§Ή [
|
| 145 |
try:
|
| 146 |
if MODELS["stt"] and MODELS["stt"].model.device == "cuda":
|
| 147 |
del MODELS["stt"]
|
|
@@ -156,41 +155,37 @@ def release_gpu_models():
|
|
| 156 |
time.sleep(0.5)
|
| 157 |
|
| 158 |
def warmup_task():
|
| 159 |
-
"""
|
| 160 |
global WARMUP_STATUS
|
| 161 |
if WARMUP_STATUS["complete"] or WARMUP_STATUS["in_progress"]: return
|
| 162 |
WARMUP_STATUS["in_progress"] = True
|
| 163 |
-
print("\nπ₯ ---
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
print(f"β
--- CACHE SYNCED: v109 --- \n")
|
| 181 |
-
except Exception as e:
|
| 182 |
-
print(f"β Warmup Warning (might be fine): {e}")
|
| 183 |
-
finally: WARMUP_STATUS["in_progress"] = False
|
| 184 |
|
| 185 |
@spaces.GPU(duration=150)
|
| 186 |
def core_process(request_dict):
|
| 187 |
action = request_dict.get("action")
|
| 188 |
-
print(f"--- [
|
| 189 |
|
| 190 |
-
# Wait for
|
| 191 |
waited = 0
|
| 192 |
-
while not WARMUP_STATUS["complete"] and waited <
|
| 193 |
-
if waited % 10 == 0: print(f"β³
|
| 194 |
time.sleep(1)
|
| 195 |
waited += 1
|
| 196 |
|
|
@@ -212,9 +207,8 @@ def core_process(request_dict):
|
|
| 212 |
|
| 213 |
elif action == "tts":
|
| 214 |
text = request_dict.get("text")
|
| 215 |
-
lang = request_dict.get("lang", "en")
|
| 216 |
XTTS_MAP = {"en": "en", "de": "de", "fr": "fr", "es": "es", "it": "it", "pl": "pl", "pt": "pt", "tr": "tr", "ru": "ru", "nl": "nl", "cs": "cs", "ar": "ar", "hu": "hu", "ko": "ko", "hi": "hi", "zh": "zh-cn"}
|
| 217 |
-
clean_lang = lang.strip().lower().split('-')[0]
|
| 218 |
mapped_lang = XTTS_MAP.get(clean_lang) or ("zh-cn" if clean_lang == "zh" else None)
|
| 219 |
|
| 220 |
if mapped_lang:
|
|
@@ -247,10 +241,10 @@ def core_process(request_dict):
|
|
| 247 |
res = {"text": stt_res.get("text"), "translated": translated, "audio": tts_res.get("audio")}
|
| 248 |
else: res = {"error": f"Unknown action: {action}"}
|
| 249 |
except Exception as e:
|
| 250 |
-
print(f"β
|
| 251 |
res = {"error": str(e)}
|
| 252 |
finally:
|
| 253 |
-
print(f"--- [
|
| 254 |
release_gpu_models()
|
| 255 |
return res
|
| 256 |
|
|
@@ -259,7 +253,7 @@ async def lifespan(app: FastAPI):
|
|
| 259 |
Thread(target=warmup_task, daemon=True).start()
|
| 260 |
yield
|
| 261 |
|
| 262 |
-
# π
|
| 263 |
app = FastAPI(lifespan=lifespan)
|
| 264 |
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
| 265 |
|
|
@@ -268,21 +262,21 @@ async def api_process(request: Request):
|
|
| 268 |
try:
|
| 269 |
req_data = await request.json()
|
| 270 |
if req_data.get("action") == "health":
|
| 271 |
-
return {"status": "awake", "warm": WARMUP_STATUS["complete"], "v": "
|
| 272 |
return core_process(req_data)
|
| 273 |
except Exception as e: return {"error": str(e)}
|
| 274 |
|
| 275 |
@app.get("/health")
|
| 276 |
-
def health(): return {"status": "ok", "warm": WARMUP_STATUS["complete"], "v": "
|
| 277 |
|
| 278 |
def gradio_fn(req_json):
|
| 279 |
try: return json.dumps(core_process(json.loads(req_json)))
|
| 280 |
except Exception as e: return json.dumps({"error": str(e)})
|
| 281 |
|
| 282 |
-
demo = gr.Interface(fn=gradio_fn, inputs="text", outputs="text", title="π AI Engine
|
| 283 |
demo.queue()
|
| 284 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 285 |
|
| 286 |
if __name__ == "__main__":
|
| 287 |
-
print("π [
|
| 288 |
uvicorn.run(app, host="0.0.0.0", port=7860, log_level="error")
|
|
|
|
| 1 |
+
# π V110: ZEROGPU RESILIENT STARTUP
|
| 2 |
# Must be first to patch environment correctly
|
| 3 |
try:
|
| 4 |
import spaces
|
|
|
|
| 26 |
import sys
|
| 27 |
import types
|
| 28 |
import logging
|
| 29 |
+
import traceback
|
| 30 |
from threading import Thread, Lock
|
| 31 |
from huggingface_hub import snapshot_download, hf_hub_download
|
| 32 |
|
| 33 |
+
# π‘οΈ 1. SILENCE & ENV (v110)
|
| 34 |
logging.getLogger("transformers").setLevel(logging.ERROR)
|
| 35 |
logging.getLogger("TTS").setLevel(logging.ERROR)
|
| 36 |
os.environ["CT2_VERBOSE"] = "0"
|
|
|
|
| 38 |
os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
|
| 39 |
os.environ["GRADIO_SERVER_PORT"] = "7860"
|
| 40 |
|
| 41 |
+
# π οΈ 2. COMPATIBILITY PATCHES (v110)
|
| 42 |
if "torchaudio.backend" not in sys.modules:
|
| 43 |
backend = types.ModuleType("torchaudio.backend")
|
| 44 |
common = types.ModuleType("torchaudio.backend.common")
|
|
|
|
| 78 |
|
| 79 |
# π¦ 3. AI LIBRARIES
|
| 80 |
import chatterbox_utils
|
|
|
|
| 81 |
from faster_whisper import WhisperModel
|
| 82 |
from TTS.api import TTS
|
| 83 |
from df.enhance import init_df
|
| 84 |
import deep_translator
|
| 85 |
|
| 86 |
+
# FORCE BUILD TRIGGER: 17:40:00 Jan 21 2026
|
| 87 |
+
# v110: Resilient Warmup. No blocks.
|
| 88 |
|
| 89 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
| 90 |
MODELS = {"stt": None, "translate": None, "tts": None, "denoiser": None}
|
|
|
|
| 93 |
WARMUP_LOCK = Lock()
|
| 94 |
|
| 95 |
def activate_gpu_models(action):
|
| 96 |
+
"""v110: Safe Deferred Activation"""
|
| 97 |
global MODELS, WARMUP_STATUS
|
| 98 |
local_only = WARMUP_STATUS["complete"]
|
| 99 |
|
|
|
|
| 102 |
try: stt_on_gpu = MODELS["stt"] is not None and MODELS["stt"].model.device == "cuda"
|
| 103 |
except: pass
|
| 104 |
if not stt_on_gpu:
|
| 105 |
+
print(f"ποΈ [v110] Init Whisper (MIG-Ready: int8_float16)...")
|
| 106 |
try:
|
| 107 |
if MODELS["stt"]: del MODELS["stt"]; gc.collect(); torch.cuda.empty_cache()
|
| 108 |
MODELS["stt"] = WhisperModel(
|
|
|
|
| 123 |
tts_on_gpu = "cuda" in curr
|
| 124 |
except: pass
|
| 125 |
if MODELS["tts"] is None or not tts_on_gpu:
|
| 126 |
+
print(f"π [v110] Init XTTS-v2 (GPU)...")
|
| 127 |
try:
|
| 128 |
if MODELS["tts"] is None:
|
| 129 |
MODELS["tts"] = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2", gpu=True)
|
| 130 |
else: MODELS["tts"].to("cuda")
|
| 131 |
except: pass
|
| 132 |
|
|
|
|
| 133 |
chatterbox_utils.load_chatterbox(device="cuda")
|
| 134 |
|
| 135 |
if MODELS["denoiser"] is None:
|
|
|
|
| 138 |
if MODELS["translate"] is None: MODELS["translate"] = "active"
|
| 139 |
|
| 140 |
def release_gpu_models():
|
| 141 |
+
"""v110: Quiet Release"""
|
| 142 |
global MODELS
|
| 143 |
+
print("π§Ή [v110] Releasing.")
|
| 144 |
try:
|
| 145 |
if MODELS["stt"] and MODELS["stt"].model.device == "cuda":
|
| 146 |
del MODELS["stt"]
|
|
|
|
| 155 |
time.sleep(0.5)
|
| 156 |
|
| 157 |
def warmup_task():
|
| 158 |
+
"""v110: Resilient Warmup (Independent Downloads)"""
|
| 159 |
global WARMUP_STATUS
|
| 160 |
if WARMUP_STATUS["complete"] or WARMUP_STATUS["in_progress"]: return
|
| 161 |
WARMUP_STATUS["in_progress"] = True
|
| 162 |
+
print("\nπ₯ --- V110: ZEROGPU RESILIENT WARMUP ---")
|
| 163 |
+
|
| 164 |
+
# 1. Faster-Whisper
|
| 165 |
+
try: snapshot_download("Systran/faster-whisper-large-v3")
|
| 166 |
+
except Exception as e: print(f"β οΈ Whisper download: {e}")
|
| 167 |
+
|
| 168 |
+
# 2. XTTS-v2
|
| 169 |
+
try: snapshot_download("coqui/XTTS-v2")
|
| 170 |
+
except Exception as e: print(f"β οΈ XTTS download: {e}")
|
| 171 |
+
|
| 172 |
+
# 3. Chatterbox
|
| 173 |
+
try: chatterbox_utils.warmup_chatterbox()
|
| 174 |
+
except Exception as e: print(f"β οΈ Chatterbox download: {e}")
|
| 175 |
+
|
| 176 |
+
WARMUP_STATUS["complete"] = True
|
| 177 |
+
print(f"β
--- SYSTEM READY: v110 --- \n")
|
| 178 |
+
WARMUP_STATUS["in_progress"] = False
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
@spaces.GPU(duration=150)
|
| 181 |
def core_process(request_dict):
|
| 182 |
action = request_dict.get("action")
|
| 183 |
+
print(f"--- [v110] π PROCESSING: {action} ---")
|
| 184 |
|
| 185 |
+
# Wait for thermal preparation
|
| 186 |
waited = 0
|
| 187 |
+
while not WARMUP_STATUS["complete"] and waited < 60:
|
| 188 |
+
if waited % 10 == 0: print(f"β³ Syncing assets... ({waited}s)")
|
| 189 |
time.sleep(1)
|
| 190 |
waited += 1
|
| 191 |
|
|
|
|
| 207 |
|
| 208 |
elif action == "tts":
|
| 209 |
text = request_dict.get("text")
|
|
|
|
| 210 |
XTTS_MAP = {"en": "en", "de": "de", "fr": "fr", "es": "es", "it": "it", "pl": "pl", "pt": "pt", "tr": "tr", "ru": "ru", "nl": "nl", "cs": "cs", "ar": "ar", "hu": "hu", "ko": "ko", "hi": "hi", "zh": "zh-cn"}
|
| 211 |
+
clean_lang = (request_dict.get("lang") or "en").strip().lower().split('-')[0]
|
| 212 |
mapped_lang = XTTS_MAP.get(clean_lang) or ("zh-cn" if clean_lang == "zh" else None)
|
| 213 |
|
| 214 |
if mapped_lang:
|
|
|
|
| 241 |
res = {"text": stt_res.get("text"), "translated": translated, "audio": tts_res.get("audio")}
|
| 242 |
else: res = {"error": f"Unknown action: {action}"}
|
| 243 |
except Exception as e:
|
| 244 |
+
print(f"β Processing Fault: {traceback.format_exc()}")
|
| 245 |
res = {"error": str(e)}
|
| 246 |
finally:
|
| 247 |
+
print(f"--- [v110] β¨ FINISHED ---")
|
| 248 |
release_gpu_models()
|
| 249 |
return res
|
| 250 |
|
|
|
|
| 253 |
Thread(target=warmup_task, daemon=True).start()
|
| 254 |
yield
|
| 255 |
|
| 256 |
+
# π FastAPI & Gradio Unified
|
| 257 |
app = FastAPI(lifespan=lifespan)
|
| 258 |
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
| 259 |
|
|
|
|
| 262 |
try:
|
| 263 |
req_data = await request.json()
|
| 264 |
if req_data.get("action") == "health":
|
| 265 |
+
return {"status": "awake", "warm": WARMUP_STATUS["complete"], "v": "110"}
|
| 266 |
return core_process(req_data)
|
| 267 |
except Exception as e: return {"error": str(e)}
|
| 268 |
|
| 269 |
@app.get("/health")
|
| 270 |
+
def health(): return {"status": "ok", "warm": WARMUP_STATUS["complete"], "v": "110"}
|
| 271 |
|
| 272 |
def gradio_fn(req_json):
|
| 273 |
try: return json.dumps(core_process(json.loads(req_json)))
|
| 274 |
except Exception as e: return json.dumps({"error": str(e)})
|
| 275 |
|
| 276 |
+
demo = gr.Interface(fn=gradio_fn, inputs="text", outputs="text", title="π AI Engine v110")
|
| 277 |
demo.queue()
|
| 278 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 279 |
|
| 280 |
if __name__ == "__main__":
|
| 281 |
+
print("π [v110] Starting Resilient Server...")
|
| 282 |
uvicorn.run(app, host="0.0.0.0", port=7860, log_level="error")
|