#!/usr/bin/env python3 """Deploy SakThai Engine LoRA Adapter to HF Inference Endpoint Run this AFTER the Kaggle notebook finishes training. The adapter will be at: Nanthasit/sakthai-1.5b-lora-kaggle Steps: 1. Merge LoRA into base model 2. Fix config.json (remove null quantization_config) 3. Upload merged model to HF Hub 4. Create Inference Endpoint """ import json, os, torch from huggingface_hub import HfApi, login, hf_hub_download from transformers import AutoModelForCausalLM, AutoTokenizer from peft import PeftModel # ─── Config ───────────────────────────────────────────── HF_TOKEN = os.environ["HF_TOKEN"] BASE_MODEL = "Qwen/Qwen2.5-1.5B-Instruct" ADAPTER_REPO = "Nanthasit/sakthai-1.5b-lora-kaggle" # <- from Kaggle training MERGED_REPO = "Nanthasit/sakthai-1.5b-merged-kaggle" # <- upload target ENDPOINT_NAME = "sakthai-1-5b-endpoint-v2" # <- new endpoint # ─── Auth ─────────────────────────────────────────────── login(token=HF_TOKEN, add_to_git_credential=True) api = HfApi() # ─── 1. Merge LoRA ────────────────────────────────────── print("Loading base model...") base = AutoModelForCausalLM.from_pretrained( BASE_MODEL, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True ) tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True) print("Loading adapter...") peft = PeftModel.from_pretrained(base, ADAPTER_REPO) merged = peft.merge_and_unload() print("Saving merged model...") merged.save_pretrained("./merged-output") tokenizer.save_pretrained("./merged-output") # ─── 2. Fix config.json ──────────────────────────────── config_path = "./merged-output/config.json" config = json.loads(open(config_path).read()) config.pop("quantization_config", None) # <- required fix! config["torch_dtype"] = "bfloat16" with open(config_path, "w") as f: json.dump(config, f, indent=2) print("config.json fixed") # ─── 3. Upload to Hub ─────────────────────────────────── api.create_repo(repo_id=MERGED_REPO, repo_type="model", exist_ok=True) api.upload_folder( folder_path="./merged-output", repo_id=MERGED_REPO, commit_message="Merged model from Kaggle LoRA training", ) print(f"Merged model: https://huggingface.co/{MERGED_REPO}") # ─── 4. Create Inference Endpoint ─────────────────────── endpoint = api.create_inference_endpoint( name=ENDPOINT_NAME, repository=MERGED_REPO, accelerator="gpu", instance_type="nvidia-a10g", instance_size="x1", region="us-east-1", vendor="aws", framework="pytorch", task="text-generation", min_replica=0, # scale-to-zero max_replica=1, scale_to_zero_timeout=15, ) print(f"Endpoint: {endpoint.url}") print("✅ Done! Ready for inference.")