Fix 429 rate-limit: use hf_hub_download instead of raw requests
Browse files- app.py +4 -3
- download_models.py +57 -55
app.py
CHANGED
|
@@ -8,7 +8,6 @@ import sys
|
|
| 8 |
import json
|
| 9 |
import time
|
| 10 |
import base64
|
| 11 |
-
import subprocess
|
| 12 |
from dataclasses import dataclass
|
| 13 |
from typing import Any, List, Optional, Tuple, Dict
|
| 14 |
from unicodedata import normalize as uni_normalize
|
|
@@ -17,11 +16,13 @@ import numpy as np
|
|
| 17 |
from num2words import num2words
|
| 18 |
import gradio as gr
|
| 19 |
import onnxruntime as ort
|
|
|
|
| 20 |
|
| 21 |
# Download models if missing
|
| 22 |
if not os.path.exists("onnx_models/text_encoder.onnx") or os.path.getsize("onnx_models/text_encoder.onnx") < 1000:
|
| 23 |
-
print("Models missing or invalid,
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
# ============================================================
|
| 27 |
# Vocabulary & Normalization
|
|
|
|
| 8 |
import json
|
| 9 |
import time
|
| 10 |
import base64
|
|
|
|
| 11 |
from dataclasses import dataclass
|
| 12 |
from typing import Any, List, Optional, Tuple, Dict
|
| 13 |
from unicodedata import normalize as uni_normalize
|
|
|
|
| 16 |
from num2words import num2words
|
| 17 |
import gradio as gr
|
| 18 |
import onnxruntime as ort
|
| 19 |
+
from download_models import download_blue_models, download_renikud
|
| 20 |
|
| 21 |
# Download models if missing
|
| 22 |
if not os.path.exists("onnx_models/text_encoder.onnx") or os.path.getsize("onnx_models/text_encoder.onnx") < 1000:
|
| 23 |
+
print("Models missing or invalid, downloading via huggingface_hub...")
|
| 24 |
+
download_blue_models()
|
| 25 |
+
download_renikud()
|
| 26 |
|
| 27 |
# ============================================================
|
| 28 |
# Vocabulary & Normalization
|
download_models.py
CHANGED
|
@@ -1,60 +1,62 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
return
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
total_size = int(response.headers.get('content-length', 0))
|
| 29 |
-
with open(path, 'wb') as f, tqdm(
|
| 30 |
-
desc=path, total=total_size, unit='iB', unit_scale=True, unit_divisor=1024,
|
| 31 |
-
) as bar:
|
| 32 |
-
for data in response.iter_content(chunk_size=8192):
|
| 33 |
-
size = f.write(data)
|
| 34 |
-
bar.update(size)
|
| 35 |
-
|
| 36 |
-
print(f"Downloaded {path}: {os.path.getsize(path)} bytes")
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
# Public model repo: notmax123/blue-onnx
|
| 40 |
-
BASE = "https://huggingface.co/notmax123/blue-onnx/resolve/main"
|
| 41 |
-
|
| 42 |
-
MODELS = {
|
| 43 |
-
"onnx_models/text_encoder.onnx": f"{BASE}/text_encoder.onnx?download=true",
|
| 44 |
-
"onnx_models/reference_encoder.onnx": f"{BASE}/reference_encoder.onnx?download=true",
|
| 45 |
-
"onnx_models/vector_estimator.onnx": f"{BASE}/vector_estimator.onnx?download=true",
|
| 46 |
-
"onnx_models/vocoder.onnx": f"{BASE}/vocoder.onnx?download=true",
|
| 47 |
-
"onnx_models/duration_predictor.onnx": f"{BASE}/duration_predictor.onnx?download=true",
|
| 48 |
-
"onnx_models/length_pred_style.onnx": f"{BASE}/length_pred_style.onnx?download=true",
|
| 49 |
-
"onnx_models/stats.npz": f"{BASE}/stats.npz?download=true",
|
| 50 |
-
"onnx_models/uncond.npz": f"{BASE}/uncond.npz?download=true",
|
| 51 |
-
"renikud.onnx": "https://huggingface.co/thewh1teagle/renikud/resolve/main/model.onnx?download=true",
|
| 52 |
-
}
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
try:
|
| 58 |
-
download_file(url, path, token=token)
|
| 59 |
-
except Exception as e:
|
| 60 |
-
print(f"Failed to download {path}: {e}")
|
|
|
|
| 1 |
import os
|
| 2 |
+
import shutil
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
|
| 5 |
+
BLUE_REPO = "notmax123/blue-onnx"
|
| 6 |
+
RENIKUD_REPO = "thewh1teagle/renikud"
|
| 7 |
+
|
| 8 |
+
BLUE_FILES = [
|
| 9 |
+
"text_encoder.onnx",
|
| 10 |
+
"reference_encoder.onnx",
|
| 11 |
+
"vector_estimator.onnx",
|
| 12 |
+
"vocoder.onnx",
|
| 13 |
+
"duration_predictor.onnx",
|
| 14 |
+
"length_pred_style.onnx",
|
| 15 |
+
"stats.npz",
|
| 16 |
+
"uncond.npz",
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
def _is_valid(path, min_bytes=100_000):
|
| 20 |
+
return os.path.exists(path) and os.path.getsize(path) >= min_bytes
|
| 21 |
+
|
| 22 |
+
def download_blue_models(dest_dir="onnx_models"):
|
| 23 |
+
os.makedirs(dest_dir, exist_ok=True)
|
| 24 |
+
token = os.environ.get("HF_TOKEN") or None
|
| 25 |
+
|
| 26 |
+
for filename in BLUE_FILES:
|
| 27 |
+
dest = os.path.join(dest_dir, filename)
|
| 28 |
+
if _is_valid(dest):
|
| 29 |
+
print(f"Already present: {dest} ({os.path.getsize(dest):,} bytes)")
|
| 30 |
+
continue
|
| 31 |
+
print(f"Downloading {BLUE_REPO}/{filename} …")
|
| 32 |
+
try:
|
| 33 |
+
cached = hf_hub_download(
|
| 34 |
+
repo_id=BLUE_REPO,
|
| 35 |
+
filename=filename,
|
| 36 |
+
repo_type="model",
|
| 37 |
+
token=token,
|
| 38 |
+
)
|
| 39 |
+
shutil.copy2(cached, dest)
|
| 40 |
+
print(f" → {dest} ({os.path.getsize(dest):,} bytes)")
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f" FAILED {filename}: {e}")
|
| 43 |
+
raise
|
| 44 |
|
| 45 |
+
def download_renikud(dest="renikud.onnx"):
|
| 46 |
+
if _is_valid(dest, min_bytes=1_000_000):
|
| 47 |
+
print(f"Already present: {dest} ({os.path.getsize(dest):,} bytes)")
|
| 48 |
return
|
| 49 |
+
token = os.environ.get("HF_TOKEN") or None
|
| 50 |
+
print(f"Downloading renikud model …")
|
| 51 |
+
cached = hf_hub_download(
|
| 52 |
+
repo_id=RENIKUD_REPO,
|
| 53 |
+
filename="model.onnx",
|
| 54 |
+
repo_type="model",
|
| 55 |
+
token=token,
|
| 56 |
+
)
|
| 57 |
+
shutil.copy2(cached, dest)
|
| 58 |
+
print(f" → {dest} ({os.path.getsize(dest):,} bytes)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
+
download_blue_models()
|
| 62 |
+
download_renikud()
|
|
|
|
|
|
|
|
|
|
|
|