Spaces:
Sleeping
Sleeping
Fix: Force re-download of incomplete models
Browse files- src/download_models.py +20 -17
src/download_models.py
CHANGED
|
@@ -1,16 +1,14 @@
|
|
| 1 |
# src/download_models.py
|
| 2 |
import os
|
|
|
|
| 3 |
from huggingface_hub import snapshot_download
|
| 4 |
|
| 5 |
-
# ---
|
| 6 |
-
# Ini memberitahu pustaka huggingface untuk menggunakan /data/.cache sebagai
|
| 7 |
-
# folder cache, yang berada di dalam persistent storage yang bisa kita tulis.
|
| 8 |
cache_dir = "/data/.cache"
|
| 9 |
os.environ['HF_HOME'] = cache_dir
|
| 10 |
os.makedirs(cache_dir, exist_ok=True)
|
| 11 |
print(f"Hugging Face home/cache directory set to: {os.environ['HF_HOME']}")
|
| 12 |
|
| 13 |
-
|
| 14 |
# Path utama untuk menyimpan model final
|
| 15 |
MODEL_STORAGE_PATH = "/data/models"
|
| 16 |
|
|
@@ -24,35 +22,40 @@ MODELS_TO_DOWNLOAD = {
|
|
| 24 |
|
| 25 |
def main():
|
| 26 |
print("==================================================")
|
| 27 |
-
print("Memulai proses download model dengan
|
| 28 |
print("==================================================")
|
| 29 |
|
| 30 |
for model_key, model_id in MODELS_TO_DOWNLOAD.items():
|
| 31 |
-
print(f"\n--->
|
| 32 |
|
| 33 |
-
# Tentukan direktori tujuan untuk model ini
|
| 34 |
local_dir_path = os.path.join(MODEL_STORAGE_PATH, model_key)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
-
|
| 44 |
-
# variabel lingkungan HF_HOME yang telah kita atur untuk cache.
|
| 45 |
snapshot_download(
|
| 46 |
repo_id=model_id,
|
| 47 |
local_dir=local_dir_path,
|
| 48 |
local_dir_use_symlinks=False,
|
| 49 |
-
resume_download=True
|
| 50 |
)
|
| 51 |
print(f"---> {model_key.upper()} berhasil diunduh ke {local_dir_path}")
|
| 52 |
except Exception as e:
|
| 53 |
print(f"[ERROR] Gagal mengunduh {model_key.upper()}: {e}")
|
| 54 |
-
# Kita biarkan skrip berlanjut jika satu model gagal,
|
| 55 |
-
# agar tidak menghentikan seluruh proses build.
|
| 56 |
pass
|
| 57 |
|
| 58 |
print("\n==================================================")
|
|
|
|
| 1 |
# src/download_models.py
|
| 2 |
import os
|
| 3 |
+
import shutil
|
| 4 |
from huggingface_hub import snapshot_download
|
| 5 |
|
| 6 |
+
# --- Atur Direktori Cache ---
|
|
|
|
|
|
|
| 7 |
cache_dir = "/data/.cache"
|
| 8 |
os.environ['HF_HOME'] = cache_dir
|
| 9 |
os.makedirs(cache_dir, exist_ok=True)
|
| 10 |
print(f"Hugging Face home/cache directory set to: {os.environ['HF_HOME']}")
|
| 11 |
|
|
|
|
| 12 |
# Path utama untuk menyimpan model final
|
| 13 |
MODEL_STORAGE_PATH = "/data/models"
|
| 14 |
|
|
|
|
| 22 |
|
| 23 |
def main():
|
| 24 |
print("==================================================")
|
| 25 |
+
print("Memulai proses download model dengan verifikasi...")
|
| 26 |
print("==================================================")
|
| 27 |
|
| 28 |
for model_key, model_id in MODELS_TO_DOWNLOAD.items():
|
| 29 |
+
print(f"\n---> Memeriksa {model_key.upper()} ({model_id})")
|
| 30 |
|
|
|
|
| 31 |
local_dir_path = os.path.join(MODEL_STORAGE_PATH, model_key)
|
| 32 |
|
| 33 |
+
# PERBAIKAN: Verifikasi kelengkapan model.
|
| 34 |
+
# Cara sederhana adalah memeriksa keberadaan file bobot model.
|
| 35 |
+
is_complete = False
|
| 36 |
+
if os.path.exists(local_dir_path):
|
| 37 |
+
if os.path.exists(os.path.join(local_dir_path, "model.safetensors")) or \
|
| 38 |
+
os.path.exists(os.path.join(local_dir_path, "pytorch_model.bin")):
|
| 39 |
+
is_complete = True
|
| 40 |
|
| 41 |
+
if is_complete:
|
| 42 |
+
print(f"---> Model {model_key.upper()} sudah lengkap. Melewati unduhan.")
|
| 43 |
+
continue
|
| 44 |
+
elif os.path.exists(local_dir_path) and not is_complete:
|
| 45 |
+
print(f"---> Model {model_key.upper()} tidak lengkap. Menghapus folder lama untuk mengunduh ulang...")
|
| 46 |
+
shutil.rmtree(local_dir_path)
|
| 47 |
+
|
| 48 |
try:
|
| 49 |
+
print(f"---> Mengunduh {model_key.upper()}...")
|
|
|
|
| 50 |
snapshot_download(
|
| 51 |
repo_id=model_id,
|
| 52 |
local_dir=local_dir_path,
|
| 53 |
local_dir_use_symlinks=False,
|
| 54 |
+
resume_download=True
|
| 55 |
)
|
| 56 |
print(f"---> {model_key.upper()} berhasil diunduh ke {local_dir_path}")
|
| 57 |
except Exception as e:
|
| 58 |
print(f"[ERROR] Gagal mengunduh {model_key.upper()}: {e}")
|
|
|
|
|
|
|
| 59 |
pass
|
| 60 |
|
| 61 |
print("\n==================================================")
|