""" Screenshot -> React, with Gemma 3 27B IT VLM + a LoRA adapter. Upload a UI screenshot; the model writes a React component, and the app renders it live in a sandboxed iframe. The training data (Reubencf/frontend-react-dataset) leans on Tailwind, framer-motion and lucide-react, so the preview resolves those imports to UMD globals instead of stripping them. """ import html import json import os import re import tempfile import threading import gradio as gr import spaces import torch from peft import PeftModel from transformers import AutoProcessor, Gemma3ForConditionalGeneration, TextIteratorStreamer # `unsloth/gemma-3-27b-it` is an ungated mirror, so the Space runs without a token. # Set BASE_MODEL_ID=google/gemma-3-27b-it (plus an HF_TOKEN secret) to use the canonical repo. # # Both are Space variables, so dropping back to the 4B pair is a settings change rather than # a code change: BASE_MODEL_ID=unsloth/gemma-3-4b-it and # ADAPTER_ID=Reubencf/gemma-3-4b-it-vlm-react-screenshot-to-code. BASE_MODEL_ID = os.environ.get("BASE_MODEL_ID", "unsloth/gemma-3-27b-it") ADAPTER_ID = os.environ.get( "ADAPTER_ID", "Reubencf/gemma-3-27b-it-vlm-react-screenshot-to-code" ) MODEL_LABEL = os.environ.get("MODEL_LABEL", "Gemma 3 27B IT VLM") # 27B in bfloat16 is ~55GB of weights, which does not fit ZeroGPU's default 48GB slice. # `xlarge` is a full RTX Pro 6000 Blackwell at 96GB — and costs 2x quota per call. GPU_SIZE = os.environ.get("GPU_SIZE", "xlarge") DEFAULT_INSTRUCTION = ( "Convert this screenshot into a single self-contained React component.\n" "Use Tailwind CSS utility classes for styling. Return only the component code." ) # -------------------------------------------------------------------------------------- # Model # -------------------------------------------------------------------------------------- processor = AutoProcessor.from_pretrained(BASE_MODEL_ID) # Build entirely on CPU, then move once. # # torch_device="cpu" is load-bearing on ZeroGPU: PEFT picks the adapter's load device via # infer_device(), which returns "cuda" because torch.cuda.is_available() reports True at # startup even though no GPU is attached yet. safetensors then materialises straight onto # CUDA and dies with "No CUDA GPUs are available". Module .to("cuda") is patched by # `spaces` and replayed when a GPU attaches; a direct safetensors CUDA load is not. # # The same constraint rules out loading 4-bit to save memory: bitsandbytes quantises during # from_pretrained and needs a real GPU to do it, which does not exist at module scope here. model = Gemma3ForConditionalGeneration.from_pretrained( BASE_MODEL_ID, dtype=torch.bfloat16, device_map="cpu" ) model = PeftModel.from_pretrained(model, ADAPTER_ID, torch_device="cpu") # Deliberately not merged. merge_and_unload() would rebuild all 62 layers' q/v projections # on CPU, briefly holding a second copy of weights that are already ~55GB. The adapter is # rank 4 on two projections, so leaving it live costs a pair of tiny matmuls per layer. model = model.eval().to("cuda") DEVICE = "cuda" _eot = processor.tokenizer.convert_tokens_to_ids("") EOS_IDS = [i for i in {processor.tokenizer.eos_token_id, _eot} if i is not None] # -------------------------------------------------------------------------------------- # Pulling code out of the raw generation # -------------------------------------------------------------------------------------- FENCED = re.compile( r"```[ \t]*(?:jsx|tsx|js|javascript|typescript|react)?[ \t]*\n(.*?)```", re.S | re.I ) UNCLOSED = re.compile( r"```[ \t]*(?:jsx|tsx|js|javascript|typescript|react)?[ \t]*\n(.*)\Z", re.S | re.I ) def extract_code(raw: str) -> str: """Return just the code from a generation that may be wrapped in prose and fences.""" raw = raw.strip() blocks = FENCED.findall(raw) if blocks: return max(blocks, key=len).strip() truncated = UNCLOSED.search(raw) # generation cut off mid-block if truncated: return truncated.group(1).strip() return raw # -------------------------------------------------------------------------------------- # Rewriting ESM into something that runs in a plain page # -------------------------------------------------------------------------------------- IMPORT_STMT = re.compile( r"^[ \t]*import\s+(?:(?P[\s\S]*?)\s+from\s+)?" r"['\"](?P[^'\"]+)['\"][ \t]*;?[ \t]*$", re.M, ) REQUIRE = re.compile( r"^[ \t]*(?:const|let|var)\s+(?P[\s\S]*?)\s*=\s*require\(" r"['\"](?P[^'\"]+)['\"]\)[ \t]*;?[ \t]*$", re.M, ) EXPORT_DEFAULT_DECL = re.compile( r"^[ \t]*export\s+default\s+(?=(?:async\s+)?(?:function|class)\b)", re.M ) EXPORT_DEFAULT_DECL_NAME = re.compile( r"^[ \t]*export\s+default\s+(?:async\s+)?(?:function|class)\s+([A-Za-z_$][\w$]*)", re.M ) EXPORT_DEFAULT_ANON_FN = re.compile( r"^[ \t]*export\s+default\s+((?:async\s+)?function\s*\()", re.M ) EXPORT_DEFAULT_NAME = re.compile( r"^[ \t]*export\s+default\s+([A-Za-z_$][\w$]*)[ \t]*;?[ \t]*$", re.M ) EXPORT_DEFAULT_EXPR = re.compile(r"^[ \t]*export\s+default\s+", re.M) EXPORT_NAMED = re.compile(r"^[ \t]*export\s+(?=(?:const|let|var|function|class|async)\b)", re.M) EXPORT_LIST = re.compile(r"^[ \t]*export\s*\{[^}]*\}[ \t]*;?[ \t]*$", re.M) DECL_NAME = re.compile( r"^[ \t]*(?:export\s+default\s+|export\s+)?" r"(?:async\s+)?(?:function|class|const|let|var)\s+([A-Z][\w$]*)", re.M, ) IDENT = re.compile(r"^[A-Za-z_$][\w$]*$") ANON_NAME = "__GeneratedComponent" def _parse_clause(clause: str): """Split an import clause into (default_name, namespace_name, [(imported, local)]).""" default_name = namespace = None named: list[tuple[str, str]] = [] braces = re.search(r"\{([\s\S]*)\}", clause) if braces: for part in braces.group(1).split(","): part = part.strip() if not part: continue part = re.sub(r"^type\s+", "", part) # `{ type Foo }` alias = re.match(r"^([A-Za-z_$][\w$]*)\s+as\s+([A-Za-z_$][\w$]*)$", part) if alias: named.append((alias.group(1), alias.group(2))) elif IDENT.match(part): named.append((part, part)) clause = clause[: braces.start()] + clause[braces.end():] for part in clause.split(","): part = part.strip() if not part: continue ns = re.match(r"^\*\s+as\s+([A-Za-z_$][\w$]*)$", part) if ns: namespace = ns.group(1) elif IDENT.match(part): default_name = part return default_name, namespace, named def _rebind(clause: str | None, mod: str, bound: set[str]) -> str: """Turn one import/require into `const` bindings backed by CDN globals.""" if clause is None: # side-effect import, e.g. `import './styles.css'` return "" clause = clause.strip() if clause.startswith("type "): # `import type { Props } from ...` return "" default_name, namespace, named = _parse_clause(clause) mod_js = json.dumps(mod) lines = [] if namespace: lines.append(f"const {namespace} = window.__ns({mod_js});") bound.add(namespace) if default_name: lines.append(f"const {default_name} = window.__default({mod_js});") bound.add(default_name) if named: spec = ", ".join(f"{src}: {dst}" if src != dst else src for src, dst in named) lines.append(f"const {{ {spec} }} = window.__ns({mod_js});") bound.update(dst for _, dst in named) return "\n".join(lines) def to_browser_module(code: str) -> tuple[str, str]: """Rewrite ESM-flavoured React so Babel-standalone can run it. -> (code, component).""" bound: set[str] = set() code = IMPORT_STMT.sub(lambda m: _rebind(m.group("clause"), m.group("mod"), bound), code) code = REQUIRE.sub(lambda m: _rebind(m.group("clause"), m.group("mod"), bound), code) code = EXPORT_LIST.sub("", code) # Resolve the default export's name before `export` prefixes are stripped — afterwards # there's no way to tell the default component from any other exported one. name = None named_default = EXPORT_DEFAULT_NAME.search(code) if named_default: name = named_default.group(1) code = EXPORT_DEFAULT_NAME.sub("", code) else: decl = EXPORT_DEFAULT_DECL_NAME.search(code) if decl: name = decl.group(1) if name is None and EXPORT_DEFAULT_ANON_FN.search(code): code = EXPORT_DEFAULT_ANON_FN.sub(rf"const {ANON_NAME} = \1", code, count=1) name = ANON_NAME code = EXPORT_DEFAULT_DECL.sub("", code) code = EXPORT_NAMED.sub("", code) # Anything still `export default ` is an anonymous arrow/class expression. if EXPORT_DEFAULT_EXPR.search(code): code = EXPORT_DEFAULT_EXPR.sub(f"const {ANON_NAME} = ", code, count=1) name = ANON_NAME if name is None: # Skip the `const React = window.__default(...)` lines we just generated. name = next( (m.group(1) for m in DECL_NAME.finditer(code) if m.group(1) not in bound), "App", ) # Bare `useState(...)` with no import resolves off `window` (see the preview preamble), # rather than a `const` here that could redeclare something the code already binds. return code.strip(), name # -------------------------------------------------------------------------------------- # Live preview # -------------------------------------------------------------------------------------- PREVIEW_DOC = """

""" MOUNT = """window.ReactDOM.createRoot(document.getElementById('root')).render( window.React.createElement( window.__ErrorBoundary, null, window.React.createElement(/*__NAME__*/) ) );""" # Code that already calls ReactDOM...render() mounts itself; don't mount it twice. SELF_MOUNTING = re.compile(r"ReactDOM(?:Client)?\s*\.\s*(?:createRoot|render)\s*\(") PLACEHOLDER = """
Upload a screenshot and hit  Generate  — the rendered React lands here.
""" def build_preview(code: str) -> str: if not code.strip(): return PLACEHOLDER body, name = to_browser_module(code) mount = "" if SELF_MOUNTING.search(body) else MOUNT.replace("/*__NAME__*/", name) # A literal in the code would close the host