Spaces:
Running
Running
Jin Zhu
commited on
Commit
·
94acd9c
1
Parent(s):
2af30bb
Update app.py
Browse files- src/app.py +22 -0
src/app.py
CHANGED
|
@@ -68,8 +68,30 @@ def load_model(from_pretrained, base_model, cache_dir, device):
|
|
| 68 |
print("✅ Successfully authenticated with HF token")
|
| 69 |
except Exception as e:
|
| 70 |
print(f"⚠️ HF login warning: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
else:
|
| 72 |
cache_dir = cache_dir
|
|
|
|
| 73 |
with st.spinner("🔄 Loading model... This may take a moment on first launch."):
|
| 74 |
model = ComputeScore.from_pretrained(
|
| 75 |
from_pretrained,
|
|
|
|
| 68 |
print("✅ Successfully authenticated with HF token")
|
| 69 |
except Exception as e:
|
| 70 |
print(f"⚠️ HF login warning: {e}")
|
| 71 |
+
|
| 72 |
+
# 🔥 新增:从 HF Hub 下载模型
|
| 73 |
+
# 检查是否是 HF Hub 路径(格式:username/repo-name)
|
| 74 |
+
is_hf_hub = '/' in from_pretrained and not from_pretrained.startswith('.')
|
| 75 |
+
if is_hf_hub:
|
| 76 |
+
from huggingface_hub import snapshot_download
|
| 77 |
+
print(f"📥 Downloading model from HuggingFace Hub: {from_pretrained}")
|
| 78 |
+
try:
|
| 79 |
+
# 下载整个仓库到本地
|
| 80 |
+
local_model_path = snapshot_download(
|
| 81 |
+
repo_id=from_pretrained,
|
| 82 |
+
cache_dir=cache_dir,
|
| 83 |
+
token=hf_token,
|
| 84 |
+
repo_type="model"
|
| 85 |
+
)
|
| 86 |
+
print(f"✅ Model downloaded to: {local_model_path}")
|
| 87 |
+
# 使用下载后的本地路径
|
| 88 |
+
from_pretrained = local_model_path
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"❌ Failed to download model: {e}")
|
| 91 |
+
raise
|
| 92 |
else:
|
| 93 |
cache_dir = cache_dir
|
| 94 |
+
|
| 95 |
with st.spinner("🔄 Loading model... This may take a moment on first launch."):
|
| 96 |
model = ComputeScore.from_pretrained(
|
| 97 |
from_pretrained,
|