import time import base64 import io from PIL import Image from bs4 import BeautifulSoup from langchain_google_genai import ChatGoogleGenerativeAI from langchain_core.messages import HumanMessage def resize_and_encode_image(image_file, max_long_side=1024): img = Image.open(image_file) width, height = img.size if max(width, height) > max_long_side: if width > height: new_width = max_long_side new_height = int(max_long_side * (height / width)) else: new_height = max_long_side new_width = int(max_long_side * (width / height)) img = img.resize((new_width, new_height), Image.Resampling.LANCZOS) buffered = io.BytesIO() img.save(buffered, format="PNG") image_bytes = buffered.getvalue() base64_str = base64.b64encode(image_bytes).decode("utf-8") return f"data:image/png;base64,{base64_str}" def beautify_html(html_code): soup = BeautifulSoup(html_code, "html.parser") return soup.prettify() def generate_html_css_from_image(image_file): image_data_url = resize_and_encode_image(image_file) prompt_text = """ You are an expert front-end developer. The input is a screenshot of a website UI. Carefully analyze its layout and generate accurate, semantic, and maintainable HTML and CSS. Follow these professional guidelines: 1) Structure & Semantics: - Use HTML5 semantic tags that match the visual hierarchy (e.g.,
,