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_size=(400, 400)): img = Image.open(image_file) img.thumbnail(max_size) buffered = io.BytesIO() img.save(buffered, format="JPEG") image_bytes = buffered.getvalue() base64_str = base64.b64encode(image_bytes).decode("utf-8") return f"data:image/jpeg;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.,
,