nraptisss commited on
Commit
77ca79f
·
verified ·
1 Parent(s): bd023d4

Upload merge_and_push.py

Browse files
Files changed (1) hide show
  1. merge_and_push.py +56 -0
merge_and_push.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Merge LoRA adapters into the base model and push to Hugging Face Hub.
3
+ Run this after train.py completes successfully.
4
+ """
5
+
6
+ import torch
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer
8
+ from peft import PeftModel
9
+
10
+ # ============================================================================
11
+ # CONFIGURATION
12
+ # ============================================================================
13
+
14
+ BASE_MODEL = "Qwen/Qwen2.5-7B-Instruct"
15
+ ADAPTER_PATH = "./qwen2.5-7b-telecom-intent-lora"
16
+ MERGED_OUTPUT = "./qwen2.5-7b-telecom-intent-merged"
17
+ HUB_MODEL_ID = "nraptisss/qwen2.5-7b-telecom-intent-merged"
18
+
19
+
20
+ def merge_and_push():
21
+ """Load adapters, merge, save locally, and push to hub."""
22
+ print(f"Loading base model: {BASE_MODEL}")
23
+ base_model = AutoModelForCausalLM.from_pretrained(
24
+ BASE_MODEL,
25
+ torch_dtype=torch.float16,
26
+ device_map="auto",
27
+ trust_remote_code=True,
28
+ )
29
+
30
+ tokenizer = AutoTokenizer.from_pretrained(
31
+ BASE_MODEL,
32
+ trust_remote_code=True,
33
+ )
34
+ if tokenizer.pad_token is None:
35
+ tokenizer.pad_token = tokenizer.eos_token
36
+
37
+ print(f"Loading adapters from: {ADAPTER_PATH}")
38
+ model = PeftModel.from_pretrained(base_model, ADAPTER_PATH)
39
+
40
+ print("Merging adapters into base model...")
41
+ model = model.merge_and_unload()
42
+
43
+ print(f"Saving merged model to: {MERGED_OUTPUT}")
44
+ model.save_pretrained(MERGED_OUTPUT)
45
+ tokenizer.save_pretrained(MERGED_OUTPUT)
46
+
47
+ print(f"Pushing merged model to hub: {HUB_MODEL_ID}")
48
+ model.push_to_hub(HUB_MODEL_ID, private=False)
49
+ tokenizer.push_to_hub(HUB_MODEL_ID, private=False)
50
+
51
+ print("Done! Model available at:")
52
+ print(f" https://huggingface.co/{HUB_MODEL_ID}")
53
+
54
+
55
+ if __name__ == "__main__":
56
+ merge_and_push()