Configure HF Spaces deployment
Browse files- upload_models.py +75 -0
upload_models.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
One-time uploader: push all trained model artifacts to a Hugging Face Hub
|
| 3 |
+
model repo so the deployed Streamlit Space can download them at runtime.
|
| 4 |
+
|
| 5 |
+
The Space git repo stays tiny (code + small CSVs only); the ~436 MB of models
|
| 6 |
+
live here instead. The app pulls them via snapshot_download when MODEL_REPO_ID
|
| 7 |
+
is set (see app/predictor.py).
|
| 8 |
+
|
| 9 |
+
Prerequisites:
|
| 10 |
+
pip install -U huggingface_hub
|
| 11 |
+
huggingface-cli login # or: export HF_TOKEN=hf_xxx
|
| 12 |
+
|
| 13 |
+
Usage:
|
| 14 |
+
python deploy/upload_models.py --repo-id <your-username>/indo-abusive-detector
|
| 15 |
+
|
| 16 |
+
Uploads (from saved_models/):
|
| 17 |
+
indobert_finetuned/ -> indobert_finetuned/ (config + safetensors + tokenizer)
|
| 18 |
+
lr_model.pkl, lr_tfidf.pkl, nb_model.pkl, nb_tfidf.pkl, svm_model.pkl
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
import argparse
|
| 22 |
+
import os
|
| 23 |
+
import sys
|
| 24 |
+
|
| 25 |
+
from huggingface_hub import HfApi, create_repo
|
| 26 |
+
|
| 27 |
+
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 28 |
+
SAVED = os.path.join(ROOT, "saved_models")
|
| 29 |
+
CLASSICAL = [
|
| 30 |
+
"lr_model.pkl", "lr_tfidf.pkl",
|
| 31 |
+
"nb_model.pkl", "nb_tfidf.pkl",
|
| 32 |
+
"svm_model.pkl",
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def main():
|
| 37 |
+
ap = argparse.ArgumentParser(description=__doc__)
|
| 38 |
+
ap.add_argument("--repo-id", required=True,
|
| 39 |
+
help="Target Hub model repo, e.g. user/indo-abusive-detector")
|
| 40 |
+
ap.add_argument("--private", action="store_true",
|
| 41 |
+
help="Create the repo as private (the Space then needs an HF token).")
|
| 42 |
+
args = ap.parse_args()
|
| 43 |
+
|
| 44 |
+
bert_dir = os.path.join(SAVED, "indobert_finetuned")
|
| 45 |
+
missing = [p for p in [bert_dir, *[os.path.join(SAVED, f) for f in CLASSICAL]]
|
| 46 |
+
if not os.path.exists(p)]
|
| 47 |
+
if missing:
|
| 48 |
+
sys.exit("Missing artifacts — train first:\n " + "\n ".join(missing))
|
| 49 |
+
|
| 50 |
+
api = HfApi()
|
| 51 |
+
create_repo(args.repo_id, repo_type="model", private=args.private, exist_ok=True)
|
| 52 |
+
print(f"Repo ready: {args.repo_id} (private={args.private})")
|
| 53 |
+
|
| 54 |
+
print("Uploading indobert_finetuned/ ...")
|
| 55 |
+
api.upload_folder(
|
| 56 |
+
repo_id=args.repo_id,
|
| 57 |
+
folder_path=bert_dir,
|
| 58 |
+
path_in_repo="indobert_finetuned",
|
| 59 |
+
commit_message="Add fine-tuned IndoBERTweet",
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
for fname in CLASSICAL:
|
| 63 |
+
print(f"Uploading {fname} ...")
|
| 64 |
+
api.upload_file(
|
| 65 |
+
repo_id=args.repo_id,
|
| 66 |
+
path_or_fileobj=os.path.join(SAVED, fname),
|
| 67 |
+
path_in_repo=fname,
|
| 68 |
+
commit_message=f"Add {fname}",
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
print(f"\nDone. Set MODEL_REPO_ID={args.repo_id} on the Space.")
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
main()
|