Run this model with HF Jobs

Run GD-ML/Code2World on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.

Paste one of these prompts into your coding agent:

Fine-tune on my dataset

Use HF Jobs and TRL to fine-tune https://huggingface.co/GD-ML/Code2World. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.

Run batch inference

Use HF Jobs to run batch inference with https://huggingface.co/GD-ML/Code2World on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.

Evaluate on a benchmark

Use HF Jobs to evaluate https://huggingface.co/GD-ML/Code2World. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.

Quantize for lower memory use

Use HF Jobs to create a 4-bit version of https://huggingface.co/GD-ML/Code2World. Push it to a new model repo, then reload it from there and report its memory footprint. Run `hf jobs --help` first to learn the CLI.
Code2World / render_utils.py
yhzheng1031's picture
Upload folder using huggingface_hub
4da1734 verified
Raw History Blame
1.94 kB
import os
import re
from io import BytesIO
from PIL import Image
from playwright.sync_api import sync_playwright
def extract_clean_html(text):
"""
清理模型输出,尽量提取完整 HTML。
"""
text = text.replace("```html", "").replace("```", "")
start_match = re.search(r"<!DOCTYPE html>", text, re.IGNORECASE)
end_match = re.search(r"</html>", text, re.IGNORECASE)
if start_match and end_match:
start_idx = start_match.start()
end_idx = end_match.end()
if end_idx > start_idx:
return text[start_idx:end_idx]
return text.strip()
def render_html_to_image(html, width=1080, height=2400):
"""
用 Playwright 把 HTML 渲染成 PIL.Image。
"""
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page(viewport={"width": width, "height": height})
page.set_content(html, wait_until="domcontentloaded")
screenshot_bytes = page.screenshot(full_page=False)
browser.close()
return Image.open(BytesIO(screenshot_bytes)).convert("RGB")
def save_demo_outputs(output_dir, hinted_image, html, rendered_image=None):
"""
保存 demo 输出:
- input_with_hint.png
- predicted_next_ui.html
- predicted_next_ui.png
"""
os.makedirs(output_dir, exist_ok=True)
hinted_path = os.path.join(output_dir, "input_with_hint.png")
html_path = os.path.join(output_dir, "predicted_next_ui.html")
rendered_path = os.path.join(output_dir, "predicted_next_ui.png")
hinted_image.save(hinted_path)
with open(html_path, "w", encoding="utf-8") as f:
f.write(html)
if rendered_image is not None:
rendered_image.save(rendered_path)
print(f"Saved hinted image to: {hinted_path}")
print(f"Saved HTML to: {html_path}")
if rendered_image is not None:
print(f"Saved rendered image to: {rendered_path}")