Create upload_model.py
Browse files- upload_model.py +65 -0
upload_model.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from huggingface_hub import HfApi, create_repo, Repository
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
# Set your Hugging Face username and token (with write permission)
|
| 6 |
+
USERNAME = "your-username" # replace
|
| 7 |
+
TOKEN = "hf_xxxxx" # replace
|
| 8 |
+
|
| 9 |
+
REPO_ID = f"{USERNAME}/code-review-agent"
|
| 10 |
+
LOCAL_DIR = "./code-review-agent"
|
| 11 |
+
|
| 12 |
+
# 1. Create repo on Hub (if it doesn't exist)
|
| 13 |
+
try:
|
| 14 |
+
create_repo(REPO_ID, token=TOKEN, private=False, exist_ok=True)
|
| 15 |
+
print(f"Created/verified repo: {REPO_ID}")
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(e)
|
| 18 |
+
|
| 19 |
+
# 2. Clone the repo locally
|
| 20 |
+
if not os.path.exists(LOCAL_DIR):
|
| 21 |
+
repo = Repository(local_dir=LOCAL_DIR, clone_from=REPO_ID, use_auth_token=TOKEN)
|
| 22 |
+
else:
|
| 23 |
+
# if already exists, just pull latest
|
| 24 |
+
repo = Repository(local_dir=LOCAL_DIR, clone_from=REPO_ID, use_auth_token=TOKEN)
|
| 25 |
+
repo.git_pull()
|
| 26 |
+
|
| 27 |
+
# 3. Download the model and tokenizer
|
| 28 |
+
print("Downloading distilgpt2 ...")
|
| 29 |
+
model = AutoModelForCausalLM.from_pretrained("distilgpt2")
|
| 30 |
+
tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
|
| 31 |
+
|
| 32 |
+
# 4. Save them into the local repo
|
| 33 |
+
model.save_pretrained(LOCAL_DIR)
|
| 34 |
+
tokenizer.save_pretrained(LOCAL_DIR)
|
| 35 |
+
|
| 36 |
+
# 5. Create model card (README.md)
|
| 37 |
+
readme = """---
|
| 38 |
+
language: en
|
| 39 |
+
license: mit
|
| 40 |
+
tags:
|
| 41 |
+
- code-review
|
| 42 |
+
- agent
|
| 43 |
+
- text-generation
|
| 44 |
+
---
|
| 45 |
+
|
| 46 |
+
# Code Review Agent Model
|
| 47 |
+
|
| 48 |
+
This is a small language model (distilgpt2) that can be fine‑tuned to generate helpful code review comments.
|
| 49 |
+
It is a starting point for building an AI that reviews pull requests.
|
| 50 |
+
"""
|
| 51 |
+
with open(os.path.join(LOCAL_DIR, "README.md"), "w") as f:
|
| 52 |
+
f.write(readme)
|
| 53 |
+
|
| 54 |
+
# 6. Set up Git LFS for large files
|
| 55 |
+
# The Repository class may not handle LFS automatically, so we'll do it manually
|
| 56 |
+
import subprocess
|
| 57 |
+
subprocess.run(["git", "lfs", "install"], cwd=LOCAL_DIR)
|
| 58 |
+
subprocess.run(["git", "lfs", "track", "*.bin", "*.safetensors"], cwd=LOCAL_DIR)
|
| 59 |
+
subprocess.run(["git", "add", ".gitattributes"], cwd=LOCAL_DIR)
|
| 60 |
+
|
| 61 |
+
# 7. Commit and push
|
| 62 |
+
repo.git_add()
|
| 63 |
+
repo.git_commit("Initial upload of distilgpt2 model")
|
| 64 |
+
repo.git_push()
|
| 65 |
+
print("Model pushed to:", f"https://huggingface.co/{REPO_ID}")
|