import os os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True") import spaces # noqa: E402 (must precede torch) import html as html_lib # noqa: E402 import re # noqa: E402 import tempfile # noqa: E402 import time # noqa: E402 from threading import Thread # noqa: E402 import gradio as gr # noqa: E402 import torch # noqa: E402 from PIL import Image # noqa: E402 from transformers import ( # noqa: E402 AutoProcessor, Qwen3VLForConditionalGeneration, TextIteratorStreamer, ) MODEL_ID = "microsoft/AesCode-32B" processor = AutoProcessor.from_pretrained(MODEL_ID) model = Qwen3VLForConditionalGeneration.from_pretrained( MODEL_ID, dtype=torch.bfloat16, attn_implementation="sdpa" ).to("cuda") model.eval() # --- Prompting: copied 1:1 from github.com/microsoft/AesCode inference/prompts.py --- SYSTEM_PROMPT = ( "You create polished standalone HTML/CSS infographics, slides, and editorial layouts. " "You receive a generation prompt and a reference image. " "The generation prompt is the primary contract: follow it exactly wherever it specifies required text, charts, tables, or data. " "The reference image is visual guidance only: reuse strong ideas in hierarchy, palette, density, composition, and typography, but do not treat it as a screenshot to copy. " "Its visible text, numbers, and labels may be incomplete, inaccurate, or stylistically decorative, so do not trust them over the prompt or inline data contracts. " "HTML output constraints: " "(1) Produce exactly one complete document with all CSS in a single inline