JohanDL commited on
Commit
fe5070c
Β·
1 Parent(s): ab40017

Adding initial files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ unsloth_trained_weights/** filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # svg_compare_gradio.py
3
+ # ------------------------------------------------------------
4
+ import re, os, torch, cairosvg, lpips, clip, gradio as gr
5
+ from io import BytesIO
6
+ from pathlib import Path
7
+ from PIL import Image
8
+ from unsloth import FastLanguageModel
9
+ from transformers import BitsAndBytesConfig, AutoTokenizer
10
+ import gradio as gr
11
+ import spaces
12
+
13
+ # ---------- paths YOU may want to edit ----------------------
14
+ ADAPTER_DIR = "unsloth_trained_weights/checkpoint-1700" # LoRA ckpt
15
+ BASE_MODEL = "Qwen/Qwen2.5-Coder-7B-Instruct"
16
+ MAX_NEW = 512
17
+ DEVICE = "cuda" # if torch.cuda.is_available() else "cpu"
18
+
19
+ # ---------- utils -------------------------------------------
20
+ SVG_PAT = re.compile(r"<svg[^>]*>.*?</svg>", re.S | re.I)
21
+ def extract_svg(txt:str):
22
+ m = list(SVG_PAT.finditer(txt))
23
+ return m[-1].group(0) if m else None # last match βœ”
24
+
25
+ def svg2pil(svg:str):
26
+ try:
27
+ png = cairosvg.svg2png(bytestring=svg.encode())
28
+ return Image.open(BytesIO(png)).convert("RGB")
29
+ except Exception:
30
+ return None
31
+
32
+ # ---------- backbone loaders (CLIP + LPIPS) -----------------
33
+ _CLIP,_PREP,_LP=None,None,None
34
+ @spaces.GPU
35
+ def _load_backbones():
36
+ global _CLIP,_PREP,_LP
37
+ if _CLIP is None:
38
+ _CLIP,_PREP = clip.load("ViT-L/14", device=DEVICE); _CLIP.eval()
39
+ if _LP is None:
40
+ _LP = lpips.LPIPS(net="vgg").to(DEVICE).eval()
41
+
42
+ @spaces.GPU
43
+ @torch.no_grad()
44
+ def fused_sim(a:Image.Image,b:Image.Image,Ξ±=.5):
45
+ _load_backbones()
46
+ ta,tb = _PREP(a).unsqueeze(0).to(DEVICE), _PREP(b).unsqueeze(0).to(DEVICE)
47
+ fa = _CLIP.encode_image(ta); fa/=fa.norm(dim=-1,keepdim=True)
48
+ fb = _CLIP.encode_image(tb); fb/=fb.norm(dim=-1,keepdim=True)
49
+ clip_sim=(([email protected]).item()+1)/2
50
+ lp_sim = 1 - _LP(ta,tb,normalize=True).item()
51
+ return Ξ±*clip_sim + (1-Ξ±)*lp_sim
52
+
53
+ # ---------- load models once at startup ---------------------
54
+ bnb_cfg = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_use_double_quant=True)
55
+ print("Loading BASE …")
56
+ base, tok = FastLanguageModel.from_pretrained(
57
+ BASE_MODEL, max_seq_length=2048,
58
+ load_in_4bit=True, quantization_config=bnb_cfg, device_map="auto")
59
+ tok.pad_token = tok.eos_token
60
+
61
+ print("Loading LoRA …")
62
+ lora, _ = FastLanguageModel.from_pretrained(
63
+ ADAPTER_DIR, max_seq_length=2048,
64
+ load_in_4bit=True, quantization_config=bnb_cfg, device_map="auto")
65
+
66
+ def build_prompt(desc:str):
67
+ msgs=[{"role":"system","content":"You are an SVG illustrator."},
68
+ {"role":"user",
69
+ "content":f"ONLY reply with a valid, complete <svg>…</svg> file that depicts: {desc}"}]
70
+ return tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
71
+
72
+ @spaces.GPU
73
+ @torch.no_grad()
74
+ def draw(model, desc:str):
75
+ prompt = build_prompt(desc)
76
+ ids = tok(prompt, return_tensors="pt").to(DEVICE)
77
+ out = model.generate(**ids, max_new_tokens=MAX_NEW,
78
+ do_sample=True, temperature=.7, top_p=.8)
79
+ txt = tok.decode(out[0], skip_special_tokens=True)
80
+ svg = extract_svg(txt)
81
+ img = svg2pil(svg) if svg else None
82
+ return img, svg or "(no SVG found)"
83
+
84
+ # ---------- gradio interface --------------------------------
85
+ def compare(desc):
86
+ img_base, svg_base = draw(base, desc)
87
+ img_lora, svg_lora = draw(lora, desc)
88
+ # sim = (fused_sim(img_lora, img_base) if img_base and img_lora else float("nan"))
89
+ caption = "Thanks for trying our model 😊"
90
+ return img_base, img_lora, caption, svg_base, svg_lora
91
+
92
+ with gr.Blocks(css="body{background:#111;color:#eee}") as demo:
93
+ gr.Markdown("## πŸ–ŒοΈ Qwen-2.5 SVG Generator β€” base vs GRPO-LoRA")
94
+ gr.Markdown(
95
+ "Type an image **description** (e.g. *a purple forest at dusk*). "
96
+ "Click **Generate** to see what the base model and your fine-tuned LoRA produce."
97
+ )
98
+ inp = gr.Textbox(label="Description", placeholder="a purple forest at dusk")
99
+ btn = gr.Button("Generate")
100
+ with gr.Row():
101
+ out_base = gr.Image(label="Base model", type="pil")
102
+ out_lora = gr.Image(label="LoRA-tuned model", type="pil")
103
+ sim_lbl = gr.Markdown()
104
+ with gr.Accordion("βš™οΈ Raw SVG code", open=False):
105
+ svg_base_box = gr.Textbox(label="Base SVG", lines=6)
106
+ svg_lora_box = gr.Textbox(label="LoRA SVG", lines=6)
107
+ btn.click(compare, inp, [out_base, out_lora, sim_lbl, svg_base_box, svg_lora_box])
108
+
109
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ───────── core inference stack ─────────
2
+ torch==2.6.0 # install the CUDA wheel that matches your driver ✱
3
+ bitsandbytes>=0.45.2 # 0.45.2 adds official PyTorch-2.6 support :contentReference[oaicite:0]{index=0}
4
+ transformers==4.51.3 # version Unsloth 2025.3.19 is patched for :contentReference[oaicite:1]{index=1}
5
+ accelerate>=0.29.3
6
+ unsloth==2025.3.19 # LoRA loader / fast-Qwen runtime :contentReference[oaicite:2]{index=2}
7
+ xformers==0.0.29.post2
8
+
9
+ # ───────── SVG β†’ PNG rendering ─────────
10
+ cairosvg==2.7.1
11
+ Pillow>=10.0.0
12
+
13
+ # ───────── similarity metrics ──────────
14
+ lpips==0.1.4 # perceptual distance metric :contentReference[oaicite:3]{index=3}
15
+ git+https://github.com/openai/CLIP.git # CLIP ViT-L/14 encoder
16
+
17
+ # ───────── demo UI ─────────
18
+ gradio>=4.26.0 # any 4.x is fine
19
+
20
+ # ───────── misc utils ─────────
21
+ tqdm>=4.66.0
22
+ sentencepiece>=0.1.99 # Qwen tokenizer dep
unsloth_trained_weights/checkpoint-1700/README.md ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:eb9250c3303bd88c2738f2919476f4c71ed8213168ba6a5c6b3c20c0836906ec
3
+ size 5116
unsloth_trained_weights/checkpoint-1700/adapter_config.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5fb59157e70efc6193c7541feb93aace07ec5d19a817d65b684ed78fac8aea7b
3
+ size 870
unsloth_trained_weights/checkpoint-1700/adapter_model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:32ccac6f130776abb6423c62ad307124c6d251a677c4899c2918021bdde52101
3
+ size 80792096
unsloth_trained_weights/checkpoint-1700/added_tokens.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a32bc0d990603eb93fd389dc164fe6fbfc442db2ab3a65e4a99a249433ab11f6
3
+ size 632
unsloth_trained_weights/checkpoint-1700/merges.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8831e4f1a044471340f7c0a83d7bd71306a5b867e95fd870f74d0c5308a904d5
3
+ size 1671853
unsloth_trained_weights/checkpoint-1700/optimizer.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e1a45eb226b9cd649d636129ae320083d65cd1d5aa494dc44007d03e7ef2db88
3
+ size 41460084
unsloth_trained_weights/checkpoint-1700/rng_state.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b0eaa64b57c5826845c620a42cef5b62998f73702ecb1f3c96a5d26dfcc40d3a
3
+ size 14244
unsloth_trained_weights/checkpoint-1700/scheduler.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f23016e8799aefc8d86dfb9fde925b215c21b54eda565485261eac9bc2a47837
3
+ size 1064
unsloth_trained_weights/checkpoint-1700/special_tokens_map.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bd1a530e68d3e09aa7a45be45eff6f3f6930f6111d227ac57fb75073adf954ec
3
+ size 613
unsloth_trained_weights/checkpoint-1700/tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a11add1ccce473bcd467355ba4e231ba727eee54501528658b0d6fa18112957e
3
+ size 11422253
unsloth_trained_weights/checkpoint-1700/tokenizer_config.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8b7949e2b40053c85fc46c183c5af6a460cc402edee3ca0b7e44c01d3a572aad
3
+ size 7542
unsloth_trained_weights/checkpoint-1700/trainer_state.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:808aa42d0c7dd762a1fe1f6fd1a5d3c5952eb158f734888eb69ce454dc4cdb87
3
+ size 614237
unsloth_trained_weights/checkpoint-1700/training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b5035a1ee88000b6e11ceac38cf8db6c936b5f91805d43272d89febd096f3c1a
3
+ size 5880
unsloth_trained_weights/checkpoint-1700/vocab.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ca10d7e9fb3ed18575dd1e277a2579c16d108e32f27439684afa0e10b1440910
3
+ size 2776833