mmrech commited on
Commit
1c55cfc
·
verified ·
1 Parent(s): e4a4f4c

Upload merge_stage4_fixed.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. merge_stage4_fixed.py +35 -0
merge_stage4_fixed.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # /// script
3
+ # requires-python = ">=3.10"
4
+ # dependencies = ["torch", "transformers>=4.40.0", "peft>=0.10.0", "accelerate", "huggingface_hub>=0.21.0", "pillow", "qwen-vl-utils"]
5
+ # ///
6
+ import os, torch
7
+ from huggingface_hub import login, HfApi
8
+ from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
9
+ from peft import PeftModel
10
+ from pathlib import Path
11
+
12
+ login(token=os.environ.get("HF_TOKEN"))
13
+ api = HfApi()
14
+
15
+ print("Loading base model...")
16
+ base = Qwen2VLForConditionalGeneration.from_pretrained(
17
+ "Qwen/Qwen2-VL-2B-Instruct", torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True)
18
+ processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", trust_remote_code=True)
19
+
20
+ print("Loading Stage 4 adapter...")
21
+ model = PeftModel.from_pretrained(base, "mmrech/pitvqa-qwen2vl-unified-v2", adapter_name="stage4", subfolder="stage4")
22
+
23
+ print("Merging...")
24
+ merged = model.merge_and_unload()
25
+
26
+ print("Saving...")
27
+ out = Path("./pitvqa-merged")
28
+ out.mkdir(exist_ok=True)
29
+ merged.save_pretrained(out)
30
+ processor.save_pretrained(out)
31
+
32
+ print("Uploading...")
33
+ api.create_repo("mmrech/pitvqa-qwen2vl-merged", exist_ok=True)
34
+ api.upload_folder(folder_path=str(out), repo_id="mmrech/pitvqa-qwen2vl-merged", repo_type="model")
35
+ print("Done! https://huggingface.co/mmrech/pitvqa-qwen2vl-merged")