Spaces:
Running
Running
Update scripts/download_model.py
Browse files- scripts/download_model.py +94 -18
scripts/download_model.py
CHANGED
|
@@ -5,35 +5,71 @@ Script to download the Phi-3.5-mini-instruct model for local development
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
| 8 |
-
|
|
|
|
| 9 |
import argparse
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
def
|
| 12 |
-
"""
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
try:
|
| 22 |
-
# Download
|
| 23 |
model_path = hf_hub_download(
|
| 24 |
-
repo_id=
|
| 25 |
-
filename=
|
| 26 |
local_dir=local_dir,
|
| 27 |
-
local_dir_use_symlinks=False
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
print(f"β Error downloading model: {e}")
|
| 35 |
return None
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def main():
|
| 38 |
parser = argparse.ArgumentParser(description="Download Phi-3.5-mini-instruct model")
|
| 39 |
parser.add_argument("--repo", default="Thetima4/Phi-3.5-mini-instruct-Q4_K_M-GGUF",
|
|
@@ -42,13 +78,53 @@ def main():
|
|
| 42 |
help="Model filename")
|
| 43 |
parser.add_argument("--dir", default="./models",
|
| 44 |
help="Local directory to save the model")
|
|
|
|
|
|
|
| 45 |
|
| 46 |
args = parser.parse_args()
|
| 47 |
|
| 48 |
print("π Saem's Tunes AI Model Downloader")
|
| 49 |
print("=" * 50)
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
main()
|
|
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
| 8 |
+
import sys
|
| 9 |
+
from huggingface_hub import hf_hub_download, snapshot_download
|
| 10 |
import argparse
|
| 11 |
+
import requests
|
| 12 |
+
import hashlib
|
| 13 |
+
import time
|
| 14 |
|
| 15 |
+
def calculate_file_hash(filepath):
|
| 16 |
+
"""Calculate MD5 hash of a file"""
|
| 17 |
+
hash_md5 = hashlib.md5()
|
| 18 |
+
with open(filepath, "rb") as f:
|
| 19 |
+
for chunk in iter(lambda: f.read(4096), b""):
|
| 20 |
+
hash_md5.update(chunk)
|
| 21 |
+
return hash_md5.hexdigest()
|
| 22 |
+
|
| 23 |
+
def download_with_progress(repo_id, filename, local_dir):
|
| 24 |
+
"""Download file with progress tracking"""
|
| 25 |
+
print(f"π₯ Downloading {filename} from {repo_id}")
|
| 26 |
|
| 27 |
try:
|
| 28 |
+
# Download with progress
|
| 29 |
model_path = hf_hub_download(
|
| 30 |
+
repo_id=repo_id,
|
| 31 |
+
filename=filename,
|
| 32 |
local_dir=local_dir,
|
| 33 |
+
local_dir_use_symlinks=False,
|
| 34 |
+
resume_download=True
|
| 35 |
)
|
| 36 |
|
| 37 |
+
# Verify download
|
| 38 |
+
if os.path.exists(model_path):
|
| 39 |
+
file_size = os.path.getsize(model_path) / (1024 * 1024 * 1024) # GB
|
| 40 |
+
file_hash = calculate_file_hash(model_path)
|
| 41 |
+
|
| 42 |
+
print(f"β
Download completed successfully!")
|
| 43 |
+
print(f"π File: {model_path}")
|
| 44 |
+
print(f"π Size: {file_size:.2f} GB")
|
| 45 |
+
print(f"π Hash: {file_hash}")
|
| 46 |
+
|
| 47 |
+
return model_path
|
| 48 |
+
else:
|
| 49 |
+
print(f"β Downloaded file not found: {model_path}")
|
| 50 |
+
return None
|
| 51 |
+
|
| 52 |
except Exception as e:
|
| 53 |
print(f"β Error downloading model: {e}")
|
| 54 |
return None
|
| 55 |
|
| 56 |
+
def check_disk_space(required_gb=3):
|
| 57 |
+
"""Check if there's enough disk space"""
|
| 58 |
+
try:
|
| 59 |
+
stat = os.statvfs('/')
|
| 60 |
+
free_gb = (stat.f_bavail * stat.f_frsize) / (1024 ** 3)
|
| 61 |
+
|
| 62 |
+
print(f"πΎ Available disk space: {free_gb:.1f} GB")
|
| 63 |
+
print(f"πΎ Required disk space: {required_gb} GB")
|
| 64 |
+
|
| 65 |
+
if free_gb < required_gb:
|
| 66 |
+
print(f"β Not enough disk space. Need {required_gb} GB, have {free_gb:.1f} GB")
|
| 67 |
+
return False
|
| 68 |
+
return True
|
| 69 |
+
except:
|
| 70 |
+
print("β οΈ Could not check disk space")
|
| 71 |
+
return True
|
| 72 |
+
|
| 73 |
def main():
|
| 74 |
parser = argparse.ArgumentParser(description="Download Phi-3.5-mini-instruct model")
|
| 75 |
parser.add_argument("--repo", default="Thetima4/Phi-3.5-mini-instruct-Q4_K_M-GGUF",
|
|
|
|
| 78 |
help="Model filename")
|
| 79 |
parser.add_argument("--dir", default="./models",
|
| 80 |
help="Local directory to save the model")
|
| 81 |
+
parser.add_argument("--force", action="store_true",
|
| 82 |
+
help="Force download even if file exists")
|
| 83 |
|
| 84 |
args = parser.parse_args()
|
| 85 |
|
| 86 |
print("π Saem's Tunes AI Model Downloader")
|
| 87 |
print("=" * 50)
|
| 88 |
|
| 89 |
+
# Check disk space
|
| 90 |
+
if not check_disk_space(3):
|
| 91 |
+
sys.exit(1)
|
| 92 |
+
|
| 93 |
+
# Create local directory if it doesn't exist
|
| 94 |
+
os.makedirs(args.dir, exist_ok=True)
|
| 95 |
+
|
| 96 |
+
# Check if file already exists
|
| 97 |
+
local_path = os.path.join(args.dir, args.file)
|
| 98 |
+
if os.path.exists(local_path) and not args.force:
|
| 99 |
+
print(f"β
Model already exists: {local_path}")
|
| 100 |
+
file_size = os.path.getsize(local_path) / (1024 * 1024 * 1024)
|
| 101 |
+
print(f"π Size: {file_size:.2f} GB")
|
| 102 |
+
return local_path
|
| 103 |
+
|
| 104 |
+
# Download the model
|
| 105 |
+
start_time = time.time()
|
| 106 |
+
model_path = download_with_progress(args.repo, args.file, args.dir)
|
| 107 |
+
download_time = time.time() - start_time
|
| 108 |
+
|
| 109 |
+
if model_path:
|
| 110 |
+
print(f"β±οΈ Download time: {download_time:.1f} seconds")
|
| 111 |
+
print(f"π Model ready for use!")
|
| 112 |
+
|
| 113 |
+
# Create model info file
|
| 114 |
+
info_file = os.path.join(args.dir, "model_info.txt")
|
| 115 |
+
with open(info_file, 'w') as f:
|
| 116 |
+
f.write(f"Model: {args.repo}\n")
|
| 117 |
+
f.write(f"File: {args.file}\n")
|
| 118 |
+
f.write(f"Downloaded: {time.ctime()}\n")
|
| 119 |
+
f.write(f"Path: {model_path}\n")
|
| 120 |
+
f.write(f"Size: {os.path.getsize(model_path) / (1024**3):.2f} GB\n")
|
| 121 |
+
|
| 122 |
+
print(f"π Model info saved to: {info_file}")
|
| 123 |
+
else:
|
| 124 |
+
print("β Model download failed")
|
| 125 |
+
sys.exit(1)
|
| 126 |
+
|
| 127 |
+
return model_path
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
| 130 |
main()
|