Spaces:
Sleeping
Sleeping
Create download_models.py
Browse files- download_models.py +35 -0
download_models.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import urllib.request
|
| 2 |
+
import sys
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
MODELS_DIR = Path("models")
|
| 6 |
+
MODELS_DIR.mkdir(exist_ok=True)
|
| 7 |
+
|
| 8 |
+
BASE = "https://huggingface.co/Xenova/clip-vit-base-patch32/resolve/main"
|
| 9 |
+
|
| 10 |
+
FILES = [
|
| 11 |
+
(f"{BASE}/onnx/vision_model.onnx", MODELS_DIR / "clip_visual.onnx"),
|
| 12 |
+
(f"{BASE}/onnx/text_model.onnx", MODELS_DIR / "clip_text.onnx"),
|
| 13 |
+
(f"{BASE}/tokenizer.json", MODELS_DIR / "tokenizer.json"),
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
def progress(block, block_size, total):
|
| 17 |
+
done = block * block_size
|
| 18 |
+
if total > 0:
|
| 19 |
+
pct = min(done / total * 100, 100)
|
| 20 |
+
sys.stdout.write(f"\r {pct:.1f}%")
|
| 21 |
+
sys.stdout.flush()
|
| 22 |
+
|
| 23 |
+
for url, dest in FILES:
|
| 24 |
+
if dest.exists():
|
| 25 |
+
print(f"[=] {dest.name} already exists")
|
| 26 |
+
continue
|
| 27 |
+
print(f"[*] Downloading {dest.name}...")
|
| 28 |
+
try:
|
| 29 |
+
urllib.request.urlretrieve(url, dest, reporthook=progress)
|
| 30 |
+
print(f"\n[+] {dest.name} ({dest.stat().st_size // 1024 // 1024}MB)")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"\n[-] Failed: {e}")
|
| 33 |
+
raise
|
| 34 |
+
|
| 35 |
+
print("\n[DONE] Models ready")
|