Translating a user interface (UI) screenshot into executable HTML and CSS is a deceptively difficult task for artificial intelligence. While Vision-Language Models (VLMs) have made significant strides, most existing methods treat UI-to-code as a "single-pass" translation—attempting to predict the perfect code in one go.
Real-world developers do not write perfect code on the first try; they draft, render, inspect, and polish. UI2Code^N bridges this gap by reformulating UI-to-code as an interactive, closed-loop optimization problem. By employing a novel reinforcement learning method called Relative Visual Policy Optimization (RVPO), this open-source 9B-parameter model learns to refine its own code based on rendered visual feedback, ultimately rivaling much larger closed-source models like GPT-4o and Gemini 2.5 Pro.
The Problem with Single-Pass Generation
The authors argue that formulating UI-to-code as a one-shot generation is fundamentally flawed for three main reasons:
- Execution Discrepancies: Code behavior depends heavily on runtime factors—such as browser defaults, font fallbacks, and DPI scaling—that are entirely invisible in the static code itself.
- Coupled Objectives: Visual elements like spacing, alignment, and typography are tightly coupled. Changing one property often breaks another, requiring iterative adjustments.
- Non-Differentiability: You cannot backpropagate through a web browser's rendering engine. The "loss" is visual, but the model's "parameters" are discrete code tokens.
Methodology: Interactive Visual Optimization and RVPO
To mimic the human development process, the authors built a three-stage training pipeline (Continual Pre-training, Supervised Fine-tuning, and Reinforcement Learning) to train their 9B-parameter model. The core innovation lies in the RL stage, which trains the model on three distinct tasks: Drafting (writing initial code), Polishing (iterative refinement based on a rendered image), and Editing (modifying code based on natural language instructions).
Standard RL often uses an AI "judge" to give an absolute score (e.g., 0-100). However, VLM-based judges are notoriously noisy and poorly calibrated. To solve this, the authors introduce Relative Visual Policy Optimization (RVPO). RVPO shifts the focus from absolute scoring to relative preference. By sampling multiple candidates and performing pairwise comparisons, the system derives a stable, tournament-based reward signal based on win counts.
Additionally, the model employs a Think-then-Answer strategy. It generates a "thought" block before the code, allowing it to diagnose visual discrepancies and plan layout structures before writing a single line of CSS.
Datasets and Benchmarks
The model was pre-trained on 10 million image-HTML pairs and fine-tuned on 80,000 high-quality samples. To rigorously test the model, the researchers utilized established benchmarks (Design2Code, Flame-React-Eval, Web2Code) and introduced two new, challenging datasets:
- UI2Code-Real: 115 real-world webpages collected "in-the-wild" to test generalization on high-complexity layouts.
- UIPolish-bench: 200 pairs of synthetic and real-world webpages designed specifically to evaluate a model's ability to refine a rough draft into a polished final product.
Key Insights and Results
- State-of-the-Art Performance: Despite having only 9B parameters, UI2Code^N achieved an 88.6% accuracy on the Design2Code-HARD benchmark. This rivals massive proprietary models like GPT-4o and Gemini-2.5-Pro, while crushing open-source competitors like Qwen2.5-VL-72B (41.9%).
- Relative > Absolute: A major insight from the paper is that VLMs are much better comparators than evaluators. While a model might struggle to assign an accurate absolute score to a UI, it is highly accurate at determining which of two candidates is closer to the target.
- Test-Time Scaling: UI quality consistently improves with more refinement rounds. While synthetic tasks saturate quickly, real-world webpages continue to benefit from up to five rounds of polishing.
- Visual Planning Matters: Incorporating the explicit "Think" stage improved performance by 3.1%, helping the model identify major layout regions and DOM hierarchies early in the generation process.
- Sim-to-Real Transfer: While synthetic data provides clean supervision, augmenting the RL stage with real-world webpages proved critical for handling the noise and complexity of actual internet UIs.
Limitations and Future Work
While UI2Code^N represents a massive leap forward for open-source UI generation, the authors note a few limitations. First, when a UI is already high-quality, further polishing can sometimes lead to "oscillations"—minor stylistic changes that do not improve functional correctness. Second, while the model excels at specific UI edits, it is not yet a general-purpose design agent capable of following vague instructions like "make this look more professional." Finally, highly interactive JavaScript components and extremely deep DOM structures remain a frontier for future research.
The project code and models are open-sourced at https://github.com/zai-org/UI2Code_N.
Summary updated