Upload hf_space_cap_checker.py
Browse files- hf_space_cap_checker.py +109 -0
hf_space_cap_checker.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import re
|
| 3 |
+
import ast
|
| 4 |
+
import json # 🌟 加上这一行
|
| 5 |
+
|
| 6 |
+
def calculate_dynamic_size():
|
| 7 |
+
# ==========================================
|
| 8 |
+
# 1. 真实链接配置 (使用 raw 原始文件下载链接)
|
| 9 |
+
# ==========================================
|
| 10 |
+
py_url = "https://huggingface.co/spaces/waveshaker/universal-lora-studio/resolve/main/model_config.py"
|
| 11 |
+
json_url = "https://huggingface.co/waveshaker/comfy_models/resolve/main/studio_config.json"
|
| 12 |
+
|
| 13 |
+
print("🔍 1. 正在从 Hugging Face 下载并解析 model_config.py ...")
|
| 14 |
+
try:
|
| 15 |
+
py_res = requests.get(py_url, timeout=15)
|
| 16 |
+
py_res.raise_for_status()
|
| 17 |
+
py_text = py_res.text
|
| 18 |
+
tree = ast.parse(py_text)
|
| 19 |
+
local_config = {}
|
| 20 |
+
for node in ast.walk(tree):
|
| 21 |
+
if isinstance(node, ast.Assign) and getattr(node.targets[0], 'id', '') == 'MODEL_CONFIG':
|
| 22 |
+
local_config = ast.literal_eval(node.value)
|
| 23 |
+
break
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"❌ 解析 model_config.py 失败: {e}")
|
| 26 |
+
return
|
| 27 |
+
|
| 28 |
+
print("🔍 2. 正在从 Hugging Face 下载并解析 studio_config.json ...")
|
| 29 |
+
try:
|
| 30 |
+
json_res = requests.get(json_url, timeout=15)
|
| 31 |
+
json_res.raise_for_status()
|
| 32 |
+
json_str = json_res.text
|
| 33 |
+
# 🌟 容错修复:你截图里的 JSON 在 krea2 的 unets 里少了一个逗号,这里自动补上
|
| 34 |
+
json_str = re.sub(r'\}\s*\{', '}, {', json_str)
|
| 35 |
+
remote_config = json.loads(json_str)
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"⚠️ 解析 studio_config.json 失败: {e},将仅使用本地兜底配置。")
|
| 38 |
+
remote_config = {}
|
| 39 |
+
|
| 40 |
+
# ==========================================
|
| 41 |
+
# 2. 提取并去重 (利用 Set)
|
| 42 |
+
# ==========================================
|
| 43 |
+
unique_files = set()
|
| 44 |
+
def add_file(repo, subfolder, filename):
|
| 45 |
+
if repo and filename:
|
| 46 |
+
clean_sub = subfolder.strip("/") if subfolder else ""
|
| 47 |
+
unique_files.add((repo, clean_sub, filename))
|
| 48 |
+
|
| 49 |
+
# 提取 py 配置
|
| 50 |
+
for model_name, model_data in local_config.get("models", {}).items():
|
| 51 |
+
# 1. TE & VAE
|
| 52 |
+
for repo, path in model_data.get("companion_downloads", []):
|
| 53 |
+
parts = path.split("/")
|
| 54 |
+
add_file(repo, "/".join(parts[:-1]), parts[-1])
|
| 55 |
+
# 2. 兜底 UNet
|
| 56 |
+
for name, info in model_data.get("fallback_unets", {}).items():
|
| 57 |
+
add_file(info.get("repo"), info.get("subfolder"), info.get("filename"))
|
| 58 |
+
# 3. 兜底 LoRA
|
| 59 |
+
for name, info in model_data.get("fallback_base_loras", {}).items():
|
| 60 |
+
add_file(info.get("repo"), info.get("subfolder"), info.get("filename"))
|
| 61 |
+
|
| 62 |
+
# 提取远程 JSON
|
| 63 |
+
for root_key, data in remote_config.items():
|
| 64 |
+
for category in ["unets", "base_loras"]:
|
| 65 |
+
for item in data.get(category, []):
|
| 66 |
+
add_file(item.get("repo"), item.get("subfolder"), item.get("filename"))
|
| 67 |
+
|
| 68 |
+
print(f"\n🧹 去重后共发现 {len(unique_files)} 个独立文件。开始极速计算尺寸...\n")
|
| 69 |
+
|
| 70 |
+
# ==========================================
|
| 71 |
+
# 3. 极速查询 API 并计算
|
| 72 |
+
# ==========================================
|
| 73 |
+
total_bytes = 0
|
| 74 |
+
folder_cache = {}
|
| 75 |
+
|
| 76 |
+
def get_file_size(repo, subfolder, filename):
|
| 77 |
+
cache_key = f"{repo}/{subfolder}"
|
| 78 |
+
if cache_key not in folder_cache:
|
| 79 |
+
api_url = f"https://huggingface.co/api/models/{repo}/tree/main/{subfolder}"
|
| 80 |
+
try:
|
| 81 |
+
res = requests.get(api_url, timeout=10)
|
| 82 |
+
if res.status_code == 200:
|
| 83 |
+
folder_cache[cache_key] = {
|
| 84 |
+
item['path'].split('/')[-1]: item.get('size', 0)
|
| 85 |
+
for item in res.json()
|
| 86 |
+
}
|
| 87 |
+
else:
|
| 88 |
+
folder_cache[cache_key] = {}
|
| 89 |
+
except:
|
| 90 |
+
folder_cache[cache_key] = {}
|
| 91 |
+
return folder_cache[cache_key].get(filename, 0)
|
| 92 |
+
|
| 93 |
+
for repo, subfolder, filename in sorted(unique_files):
|
| 94 |
+
size = get_file_size(repo, subfolder, filename)
|
| 95 |
+
if size > 0:
|
| 96 |
+
total_bytes += size
|
| 97 |
+
print(f"✅ {repo:<25} | {filename[:45]:<45} | {size / (1024**3):>5.2f} GB")
|
| 98 |
+
else:
|
| 99 |
+
print(f"❌ {repo:<25} | {filename[:45]:<45} | 获取失败")
|
| 100 |
+
|
| 101 |
+
total_gb = total_bytes / (1024 ** 3)
|
| 102 |
+
print(f"\n" + "="*70)
|
| 103 |
+
print(f"📊 动态计算完成!")
|
| 104 |
+
print(f"💾 去重后文件数: {len(unique_files)}")
|
| 105 |
+
print(f"💾 总计需要下载: {total_gb:.2f} GB")
|
| 106 |
+
print(f"="*70)
|
| 107 |
+
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
calculate_dynamic_size()
|