Spaces:
Running
Running
Upload 2 files
Browse files- app.py +46 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
model_id = "zai-org/UI2Code_N"
|
| 6 |
+
|
| 7 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForImageTextToText.from_pretrained(
|
| 9 |
+
model_id, torch_dtype=torch.bfloat16, device_map="auto"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
def generate_code(image, prompt):
|
| 13 |
+
messages = [{
|
| 14 |
+
"role": "user",
|
| 15 |
+
"content": [
|
| 16 |
+
{"type": "image", "image": image},
|
| 17 |
+
{"type": "text", "text": prompt},
|
| 18 |
+
]
|
| 19 |
+
}]
|
| 20 |
+
|
| 21 |
+
inputs = processor.apply_chat_template(
|
| 22 |
+
messages, tokenize=True, add_generation_prompt=True,
|
| 23 |
+
return_dict=True, return_tensors="pt"
|
| 24 |
+
).to(model.device)
|
| 25 |
+
|
| 26 |
+
generated_ids = model.generate(**inputs, max_new_tokens=4096)
|
| 27 |
+
output = processor.decode(
|
| 28 |
+
generated_ids[0][inputs["input_ids"].shape[1]:],
|
| 29 |
+
skip_special_tokens=True
|
| 30 |
+
)
|
| 31 |
+
return output
|
| 32 |
+
|
| 33 |
+
demo = gr.Interface(
|
| 34 |
+
fn=generate_code,
|
| 35 |
+
inputs=[
|
| 36 |
+
gr.Image(type="filepath", label="Upload UI Image"),
|
| 37 |
+
gr.Textbox(label="Prompt", value="Generate HTML for this UI")
|
| 38 |
+
],
|
| 39 |
+
outputs=gr.Code(label="Generated Code", language="html"),
|
| 40 |
+
title="UI2Codeⁿ Demo",
|
| 41 |
+
description="A Visual Language Model for Interactive UI-to-Code Generation."
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 46 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
|