Nanthasit commited on
Commit
c769aad
·
verified ·
1 Parent(s): 847c591

Endpoint deployment script for merged model

Browse files
Files changed (1) hide show
  1. deploy-endpoint.py +79 -0
deploy-endpoint.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Deploy SakThai Engine LoRA Adapter to HF Inference Endpoint
3
+
4
+ Run this AFTER the Kaggle notebook finishes training.
5
+ The adapter will be at: Nanthasit/sakthai-1.5b-lora-kaggle
6
+
7
+ Steps:
8
+ 1. Merge LoRA into base model
9
+ 2. Fix config.json (remove null quantization_config)
10
+ 3. Upload merged model to HF Hub
11
+ 4. Create Inference Endpoint
12
+ """
13
+
14
+ import json, os, torch
15
+ from huggingface_hub import HfApi, login, hf_hub_download
16
+ from transformers import AutoModelForCausalLM, AutoTokenizer
17
+ from peft import PeftModel
18
+
19
+ # ─── Config ─────────────────────────────────────────────
20
+ HF_TOKEN = os.environ["HF_TOKEN"]
21
+ BASE_MODEL = "Qwen/Qwen2.5-1.5B-Instruct"
22
+ ADAPTER_REPO = "Nanthasit/sakthai-1.5b-lora-kaggle" # <- from Kaggle training
23
+ MERGED_REPO = "Nanthasit/sakthai-1.5b-merged-kaggle" # <- upload target
24
+ ENDPOINT_NAME = "sakthai-1-5b-endpoint-v2" # <- new endpoint
25
+
26
+ # ─── Auth ───────────────────────────────────────────────
27
+ login(token=HF_TOKEN, add_to_git_credential=True)
28
+ api = HfApi()
29
+
30
+ # ─── 1. Merge LoRA ──────────────────────────────────────
31
+ print("Loading base model...")
32
+ base = AutoModelForCausalLM.from_pretrained(
33
+ BASE_MODEL, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True
34
+ )
35
+ tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
36
+
37
+ print("Loading adapter...")
38
+ peft = PeftModel.from_pretrained(base, ADAPTER_REPO)
39
+ merged = peft.merge_and_unload()
40
+
41
+ print("Saving merged model...")
42
+ merged.save_pretrained("./merged-output")
43
+ tokenizer.save_pretrained("./merged-output")
44
+
45
+ # ─── 2. Fix config.json ────────────────────────────────
46
+ config_path = "./merged-output/config.json"
47
+ config = json.loads(open(config_path).read())
48
+ config.pop("quantization_config", None) # <- required fix!
49
+ config["torch_dtype"] = "bfloat16"
50
+ with open(config_path, "w") as f:
51
+ json.dump(config, f, indent=2)
52
+ print("config.json fixed")
53
+
54
+ # ─── 3. Upload to Hub ───────────────────────────────────
55
+ api.create_repo(repo_id=MERGED_REPO, repo_type="model", exist_ok=True)
56
+ api.upload_folder(
57
+ folder_path="./merged-output",
58
+ repo_id=MERGED_REPO,
59
+ commit_message="Merged model from Kaggle LoRA training",
60
+ )
61
+ print(f"Merged model: https://huggingface.co/{MERGED_REPO}")
62
+
63
+ # ─── 4. Create Inference Endpoint ───────────────────────
64
+ endpoint = api.create_inference_endpoint(
65
+ name=ENDPOINT_NAME,
66
+ repository=MERGED_REPO,
67
+ accelerator="gpu",
68
+ instance_type="nvidia-a10g",
69
+ instance_size="x1",
70
+ region="us-east-1",
71
+ vendor="aws",
72
+ framework="pytorch",
73
+ task="text-generation",
74
+ min_replica=0, # scale-to-zero
75
+ max_replica=1,
76
+ scale_to_zero_timeout=15,
77
+ )
78
+ print(f"Endpoint: {endpoint.url}")
79
+ print("✅ Done! Ready for inference.")