Instructions to use GD-ML/Code2World with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use GD-ML/Code2World with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="GD-ML/Code2World") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoProcessor, AutoModelForImageTextToText processor = AutoProcessor.from_pretrained("GD-ML/Code2World") model = AutoModelForImageTextToText.from_pretrained("GD-ML/Code2World") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use GD-ML/Code2World with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "GD-ML/Code2World" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "GD-ML/Code2World", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/GD-ML/Code2World
- SGLang
How to use GD-ML/Code2World with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "GD-ML/Code2World" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "GD-ML/Code2World", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "GD-ML/Code2World" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "GD-ML/Code2World", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use GD-ML/Code2World with Docker Model Runner:
docker model run hf.co/GD-ML/Code2World
| 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}") |