from utils import encode_image, Doubao, Qwen_2_5_VL
from PIL import Image
import bs4
from threading import Thread
import time
import argparse
import json
import os
# This dictionary holds the user's instructions for the current run.
user_instruction = {"sidebar": "", "header": "", "navigation": "", "main content": ""}
def get_args():
parser = argparse.ArgumentParser(description="Generates an HTML layout from bounding box data.")
parser.add_argument('--run_id', type=str, required=True, help='A unique identifier for the processing run.')
parser.add_argument('--instructions', type=str, help='A JSON string of instructions for different components.')
return parser.parse_args()
def get_prompt_dict(instructions):
"""Dynamically creates the prompt dictionary with the user's instructions."""
# return {
# "sidebar": f"""This is a screenshot of a container. Please fill in a complete HTML and tail-wind CSS code to accurately reproduce the given container. Please note that the layout, icon style, size, and text information of all blocks need to be basically consistent with the original screenshot based on the user's additional conditions. User instruction: {instructions["sidebar"]}. The following is the code for filling in:
#
# your code here
#
,
# only return the code within the
and
tags""",
# "header": f"""This is a screenshot of a container. Please fill in a complete HTML and tail-wind CSS code to accurately reproduce the given container. Please note that the relative position, layout, text information, and color of all blocks in the boundary box need to be basically consistent with the original screenshot based on the user's additional conditions. User instruction: {instructions["header"]}. The following is the code for filling in:
#
# your code here
#
,
# only return the code within the
and
tags""",
# "navigation": f"""This is a screenshot of a container. Please fill in a complete HTML and tail-wind CSS code to accurately reproduce the given container. Please note that the relative position, layout, text information, and color of all blocks in the boundary box need to be basically consistent with the original screenshot based on the user's additional conditions. Please use the same icons as in the original screenshot. User instruction: {instructions["navigation"]}. The following is the code for filling in:
#
# your code here
#
,
# only return the code within the
and
tags""",
# "main content": f"""This is a screenshot of a container. Please fill in a complete HTML and tail-wind CSS code to accurately reproduce the given container. Please note that all images displayed in the screenshot must be replaced with pure gray-400 image blocks of the same size as the corresponding images in the original screenshot, and the text information in the images does not need to be recognized. The relative position, layout, text information, and color of all blocks in the boundary box need to be basically consistent with the original screenshot based on the user's additional conditions. User instruction: {instructions["main content"]}. The following is the code for filling in:
#
标签内的代码"""
}
def generate_code(bbox_tree, img_path, bot, instructions):
"""Generates code for each leaf node in the bounding box tree."""
img = Image.open(img_path)
code_dict = {}
prompt_dict = get_prompt_dict(instructions)
def _generate_code(node):
if not node.get("children"): # It's a leaf node
bbox = node["bbox"]
cropped_img = img.crop(bbox)
node_type = node.get("type")
if node_type and node_type in prompt_dict:
prompt = prompt_dict[node_type]
try:
code = bot.ask(prompt, encode_image(cropped_img))
code_dict[node["id"]] = code
except Exception as e:
print(f"Error generating code for {node_type}: {e}")
else:
print(f"Node type '{node_type}' not found or invalid.")
else:
for child in node["children"]:
_generate_code(child)
_generate_code(bbox_tree)
return code_dict
def generate_html(bbox_tree, output_file):
"""Generates an HTML file with nested containers based on the bounding box tree."""
html_template_start = """
Bounding Boxes Layout