Instructions to use Huggyb/screenshot2react-qwen2.5vl-3b-lora with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use Huggyb/screenshot2react-qwen2.5vl-3b-lora with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-VL-3B-Instruct") model = PeftModel.from_pretrained(base_model, "Huggyb/screenshot2react-qwen2.5vl-3b-lora") - Notebooks
- Google Colab
- Kaggle
screenshot2react: Qwen2.5-VL-3B LoRA
This is a LoRA adapter for Qwen/Qwen2.5-VL-3B-Instruct. It turns a UI screenshot into a React component plus its CSS, written as two code blocks:
```jsx
import React from 'react';
...
export default App;
```
```css
.container { ... }
```
The adapter was trained with a hand-written PyTorch training loop. Code, evaluation pipeline and details: GitHub repo.
Results
All 80 screenshots of Flame-Eval-React, greedy decoding:
- Each answer is bundled, rendered in headless Chromium and screenshotted.
- The screenshot is compared with the input image using CLIP (does it look like the same page?) and SSIM (is the layout the same?).
- Pages that don't render score 0.
*= rendered pages only.
| Format | JSX parses | Renders | CLIP | SSIM | CLIP* | SSIM* | |
|---|---|---|---|---|---|---|---|
| Base Qwen2.5-VL-3B | 100% | 100% | 74/80 | 0.769 | 0.773 | 0.832 | 0.836 |
| This adapter | 100% | 99% | 78/80 | 0.838 | 0.796 | 0.860 | 0.817 |
| Reference answers | 100% | 99% | 79/80 | 0.957 | 0.938 | 0.969 | 0.950 |
Two unrelated pages score about CLIP 0.70 / SSIM 0.78, so that's the bottom of the scale.
Summary:
- More pages render: on the hardest level, 22/22 instead of 18/22.
- The pages are visually closer to the screenshot: CLIP is better on 50 of 73 pages.
- The answers are fuller: 682 tokens on average, against 367 for base.
- Layout did not improve: SSIM on rendered pages is about even with base.
Rendering used React 18.3.1 and react-router-dom v5, which match the era of the training data. Under React 19, this adapter renders 72/80.
How to use
import torch
from PIL import Image
from transformers import AutoProcessor, AutoModelForImageTextToText
from peft import PeftModel
BASE = "Qwen/Qwen2.5-VL-3B-Instruct"
ADAPTER = "Huggyb/screenshot2react-qwen2.5vl-3b-lora"
processor = AutoProcessor.from_pretrained(BASE, max_pixels=1280 * 28 * 28) # same as training
model = AutoModelForImageTextToText.from_pretrained(BASE, dtype=torch.bfloat16, device_map="cuda")
model = PeftModel.from_pretrained(model, ADAPTER).merge_and_unload().eval()
SYSTEM_PROMPT = (
"You are a frontend developer who converts UI screenshots into React code.\n"
"Given a screenshot, write a single React functional component that reproduces it.\n"
"Style elements with className and put all custom styles in the CSS block.\n"
"End the component with an export default statement.\n"
"Output exactly two code blocks, JSX first and CSS second, with no text before, between or after them.\n"
"Format:\n"
"```jsx\n...\n```\n"
"```css\n...\n```"
)
USER_PROMPT = "Generate the React component and CSS for this UI screenshot."
image = Image.open("screenshot.png").convert("RGB")
messages = [
{"role": "system", "content": [{"type": "text", "text": SYSTEM_PROMPT}]},
{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": USER_PROMPT}]},
]
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = processor(text=[text], images=[image], return_tensors="pt").to(model.device)
with torch.no_grad():
output = model.generate(**inputs, max_new_tokens=4096, do_sample=False)
answer = processor.batch_decode(output[:, inputs["input_ids"].shape[1]:], skip_special_tokens=True)[0]
print(answer)
Use the same system prompt: the adapter was trained with it.
Training
| Data | Flame-Waterfall-React single-image file: 155,125 training examples (β€ 4,096 tokens), 500 held-out for validation |
| LoRA | r 16, alpha 32, dropout 0.05, on q_proj, k_proj, v_proj, o_proj of the language model (7.4 M parameters, 0.2%) |
| Loss | answer tokens only (prompt, image and padding masked) |
| Optimization | AdamW, lr 1e-4, weight decay 0.01, 5% warmup + cosine, gradient clipping 1.0, batch 1 Γ 16 accumulation, 9,695 steps (1 epoch) |
| Precision | bf16 base model with bf16 autocast, gradient checkpointing |
| Hardware | 1Γ H100 80 GB, about 34 hours |
Validation loss fell steadily from 0.193 (step 500) to 0.140 (final), with no sign of overfitting. The full log is in train.log.
Limitations
- The layout is the weak spot. Multi-column arrangements are often stacked vertically.
- The code is dated. It uses the libraries of older React projects:
@material-ui/core(MUI v4),formik/yupandreact-routerv5. Running the output may need those older versions, or React 18. - Generated, not checked. The output can contain runtime errors. Review it before using it.
- Small test set. The evaluation has 80 pages, so differences of 1β2 pages are noise.
- License. The adapter is derived from Qwen2.5-VL-3B-Instruct and follows its Qwen Research License.
- Downloads last month
- 20
Model tree for Huggyb/screenshot2react-qwen2.5vl-3b-lora
Base model
Qwen/Qwen2.5-VL-3B-Instruct