Views
No views yet

Qwen/Qwen2.5-VL-3B-Instructq_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj| Metric | Value |
|---|---|
| Final Eval Loss | 0.180 |
| Final Eval Accuracy | 94.6% |
| Best Train Loss | 0.133 |
| Best Train Accuracy | 95.9% |
1pip install transformers torch pillow qwen-vl-utils torchvision
2# Optional but recommended:
3pip install flash-attn --no-build-isolation1from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
2from qwen_vl_utils import process_vision_info
3from PIL import Image
4import torch
5
6# Load model
7model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
8 "Aquiles-ai/Qwen2.5-VL-3B-Instruct-Img2Code",
9 dtype=torch.bfloat16,
10 attn_implementation="flash_attention_2", # Requires flash-attn
11 device_map="auto"
12)
13
14# Without flash-attn:
15# model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
16# "Aquiles-ai/Qwen2.5-VL-3B-Instruct-Img2Code",
17# dtype="auto",
18# device_map="auto"
19# )
20
21# Load processor
22processor = AutoProcessor.from_pretrained(
23 "Aquiles-ai/Qwen2.5-VL-3B-Instruct-Img2Code",
24 min_pixels=256*28*28,
25 max_pixels=1280*28*28
26)
27
28# Load image
29image = Image.open("screenshot.jpg")
30
31# Prepare messages
32messages = [
33 {
34 "role": "user",
35 "content": [
36 {
37 "type": "image",
38 "image": image,
39 },
40 {"type": "text", "text": "Generate the HTML/CSS code for this webpage screenshot."},
41 ],
42 }
43]
44
45
46text = processor.apply_chat_template(
47 messages, tokenize=False, add_generation_prompt=True
48)
49image_inputs, video_inputs = process_vision_info(messages)
50inputs = processor(
51 text=[text],
52 images=image_inputs,
53 videos=video_inputs,
54 padding=True,
55 return_tensors="pt",
56)
57inputs = inputs.to("cuda")
58
59# Inference: Generation of the output
60generated_ids = model.generate(**inputs, max_new_tokens=2048)
61generated_ids_trimmed = [
62 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
63]
64output_text = processor.batch_decode(
65 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
66)
67print(output_text)1from transformers import TextIteratorStreamer
2from threading import Thread
3
4# ... (previous loading code)
5
6streamer = TextIteratorStreamer(
7 processor,
8 skip_prompt=True,
9 skip_special_tokens=True
10)
11
12generation_kwargs = dict(
13 inputs,
14 streamer=streamer,
15 max_new_tokens=2048
16)
17
18thread = Thread(target=model.generate, kwargs=generation_kwargs)
19thread.start()
20
21print("Generating code:")
22for new_text in streamer:
23 print(new_text, end="", flush=True)
24
25thread.join()1vllm serve Aquiles-ai/Qwen2.5-VL-3B-Instruct-Img2Code \
2 --host 0.0.0.0 \
3 --port 8000 \
4 --api-key dummyapikey \
5 --mm-encoder-tp-mode data \
6 --limit-mm-per-prompt '{"image":2,"video":0}' \
7 --max-model-len=16384 \
8 --gpu-memory-utilization=0.901from openai import OpenAI
2import base64
3
4def encode_image(image_path):
5 with open(image_path, "rb") as image_file:
6 return base64.b64encode(image_file.read()).decode("utf-8")
7
8client = OpenAI(api_key="dummyapikey", base_url="http://127.0.0.1:8000/v1")
9image_base64 = encode_image("screenshot.jpg")
10
11stream = client.chat.completions.create(
12 model="Aquiles-ai/Qwen2.5-VL-3B-Instruct-Img2Code",
13 messages=[{
14 "role": "user",
15 "content": [
16 {"type": "text", "text": "Generate the HTML/CSS code for this webpage screenshot."},
17 {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}}
18 ]
19 }],
20 max_tokens=2048,
21 stream=True
22)
23
24for chunk in stream:
25 if chunk.choices[0].delta.content:
26 print(chunk.choices[0].delta.content, end="", flush=True)pip install aquiles-rag1@misc{aquiles-qwen-img2code,
2 author = {Aquiles-ai},
3 title = {Qwen2.5-VL-3B-Instruct-Img2Code: Automated Webpage Screenshot to Code Generation},
4 year = {2025},
5 publisher = {HuggingFace},
6 url = {https://huggingface.co/Aquiles-ai/Qwen2.5-VL-3B-Instruct-Img2Code}
7}