-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
112 lines (91 loc) · 5.34 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import streamlit as st
import pathlib
from PIL import Image
import google.generativeai as genai
# Configure the API key directly in the script
API_KEY = 'AIzaSyBA8tc28tq92GjX3rqxCi_w4yHxoUCuc2I'
genai.configure(api_key=API_KEY)
# Generation configuration
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 40,
"max_output_tokens": 8192,
"response_mime_type": "text/plain",
}
# Safety settings
safety_settings = [
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
]
# Model name
MODEL_NAME = "gemini-2.0-flash-exp"
# Create the model
model = genai.GenerativeModel(
model_name=MODEL_NAME,
safety_settings=safety_settings,
generation_config=generation_config,
)
# Start a chat session
chat_session = model.start_chat(history=[])
# Function to send a message to the model
def send_message_to_model(message, image_path):
image_input = {
'mime_type': 'image/jpeg',
'data': pathlib.Path(image_path).read_bytes()
}
response = chat_session.send_message([message, image_input])
return response.text
# Streamlit app
def main():
st.set_page_config(page_title="Modern UI to Code Tool", layout="centered")
st.title("Modern UI From Your Image")
st.subheader('Made with ❤️ by [BimaDev](https://instagram.com/biimaa_jo)')
# Framework selection (e.g., Tailwind, Bootstrap, etc.)
frameworks = ["Tailwind", "Bootstrap", "Materialize", "Bulma", "Vanilla CSS"]
selected_framework = st.radio("Pilih Framework yang ingin kamu gunakan: ",frameworks)
uploaded_file = st.file_uploader("Pilih Gambar Kamu...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
try:
# Load and display the image
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image.', use_container_width=True)
# Convert image to RGB mode if it has an alpha channel
if image.mode == 'RGBA':
image = image.convert('RGB')
# Save the uploaded image temporarily
temp_image_path = pathlib.Path("temp_image.jpg")
image.save(temp_image_path, format="JPEG")
# Generate UI description
if st.button("Code UI"):
st.write("🧑💻 Melihat gambar kamu")
prompt = "Describe this UI in accurate details. When you reference a UI element put its name and bounding box in the format: [object name (y_min, x_min, y_max, x_max)]. Also Describe the color of the elements."
description = send_message_to_model(prompt, temp_image_path)
# st.write(description)
# Refine the description
st.write("🔍 Identifikasi gambar kamu")
refine_prompt = f"Compare the described UI elements with the provided image and identify any missing elements or inaccuracies. Also Describe the color of the elements. Provide a refined and accurate description of the UI elements based on this comparison. Here is the initial description: {description}"
refined_description = send_message_to_model(refine_prompt, temp_image_path)
# st.write(refined_description)
# Generate HTML
st.write("🛠️ Membuat rencana...")
html_prompt = f"Create an HTML file based on the following UI description, using the UI elements described in the previous response. Include {selected_framework} CSS within the HTML file to style the elements. Make sure the colors used are the same as the original UI. The UI needs to be responsive and mobile-first, matching the original UI as closely as possible. Do not include any explanations or comments. Avoid using html. and at the end. ONLY return the HTML code with inline CSS. Here is the refined description: {refined_description}"
initial_html = send_message_to_model(html_prompt, temp_image_path)
#st.code(initial_html, language='html')
# Refine HTML
st.write("🔧 Membuat Code...")
refine_html_prompt = f"Validate the following HTML code based on the UI description and image and provide a refined version of the HTML code with {selected_framework} CSS that improves accuracy, responsiveness, and adherence to the original design. ONLY return the refined HTML code with inline CSS.DONT DECLARE NAME HTML IN THE FIRST LINE, JUST CODE AVOID USING html. and at the end. if there is an image used in the code, use a dummy image. Here is the initial HTML: {initial_html}"
refined_html = send_message_to_model(refine_html_prompt, temp_image_path)
st.code(refined_html, language='html')
# Save the refined HTML to a file
with open("index.html", "w") as file:
file.write(refined_html)
st.success("file HTML telah dibuat, Silahkan Download")
# Provide download link for HTML
st.download_button(label="Download HTML", data=refined_html, file_name="index.html", mime="text/html")
except Exception as e:
st.error(f"An error occurred: {e}")
if __name__ == "__main__":
main()