Update app.py
Browse files
app.py
CHANGED
|
@@ -1,94 +1,64 @@
|
|
| 1 |
import os
|
| 2 |
-
import json
|
| 3 |
-
import gradio as gr
|
| 4 |
-
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
import google.generativeai as genai
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
#
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
genai.
|
| 12 |
-
model = genai.GenerativeModel("gemini-2.5-pro")
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
#
|
| 16 |
-
#
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
response = model.generate_content(prompt)
|
| 24 |
-
raw_text = response.text.strip()
|
| 25 |
-
|
| 26 |
-
try:
|
| 27 |
-
schema = json.loads(raw_text)
|
| 28 |
-
except Exception:
|
| 29 |
-
schema = {
|
| 30 |
-
"layout": "form",
|
| 31 |
-
"fields": [
|
| 32 |
-
{"type": "text", "label": "Name"},
|
| 33 |
-
{"type": "text", "label": "Email"},
|
| 34 |
-
{"type": "text", "label": "Phone"},
|
| 35 |
-
{"type": "button", "label": "Submit"}
|
| 36 |
-
]
|
| 37 |
-
}
|
| 38 |
-
return schema, raw_text
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
def draw_mockup(schema, canvas_size=(720, 900)):
|
| 44 |
-
img = Image.new("RGB", canvas_size, "white")
|
| 45 |
-
draw = ImageDraw.Draw(img)
|
| 46 |
-
font = ImageFont.load_default()
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
box_height = 35
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
y += 20
|
| 57 |
-
draw.rectangle([x_start, y, x_start + box_width, y + box_height], outline="black", width=2)
|
| 58 |
-
y += 60
|
| 59 |
-
elif field["type"] == "button":
|
| 60 |
-
draw.rectangle([x_start, y, x_start + 200, y + box_height], fill="blue", outline="black")
|
| 61 |
-
draw.text((x_start + 70, y + 8), field["label"], fill="white", font=font)
|
| 62 |
-
y += 60
|
| 63 |
|
| 64 |
-
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# -------------------------------
|
| 69 |
-
def generate_all(requirement: str):
|
| 70 |
-
schema, raw_text = generate_schema(requirement)
|
| 71 |
-
pil_img = draw_mockup(schema)
|
| 72 |
-
return json.dumps(schema, indent=2), pil_img, raw_text
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
#
|
| 76 |
-
#
|
| 77 |
with gr.Blocks() as demo:
|
| 78 |
-
gr.Markdown("##
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
raw_out = gr.Textbox(label="Raw LLM Output")
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
demo.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
+
import imgkit
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
+
# -----------------------
|
| 7 |
+
# Configure Gemini API
|
| 8 |
+
# -----------------------
|
| 9 |
+
genai.configure(api_key=os.environ["GENAI_API_KEY"])
|
| 10 |
+
model = genai.GenerativeModel("gemini-2.0-flash")
|
|
|
|
| 11 |
|
| 12 |
+
# -----------------------
|
| 13 |
+
# Function: Generate HTML from user description
|
| 14 |
+
# -----------------------
|
| 15 |
+
def generate_mockup(user_prompt):
|
| 16 |
+
system_prompt = (
|
| 17 |
+
"You are a Power Apps mockup generator. "
|
| 18 |
+
"Convert user descriptions into complete HTML + CSS code that looks like a Power Apps mobile screen. "
|
| 19 |
+
"Do NOT include JSON or explanations. Only return valid HTML + CSS."
|
| 20 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
try:
|
| 23 |
+
response = model.generate_content(system_prompt + "\n" + user_prompt)
|
| 24 |
+
html_code = response.text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Save HTML
|
| 27 |
+
with open("mockup.html", "w") as f:
|
| 28 |
+
f.write(html_code)
|
|
|
|
| 29 |
|
| 30 |
+
# Generate PNG image
|
| 31 |
+
output_img = "mockup.png"
|
| 32 |
+
imgkit.from_file("mockup.html", output_img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
return html_code, output_img
|
| 35 |
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"❌ Error: {e}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# -----------------------
|
| 40 |
+
# Gradio Interface
|
| 41 |
+
# -----------------------
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
+
gr.Markdown("## ⚡ Power Apps Mockup Generator (Gemini + HTML + PNG)")
|
| 44 |
+
|
| 45 |
+
with gr.Row():
|
| 46 |
+
user_input = gr.Textbox(
|
| 47 |
+
label="Describe your Power Apps mockup screen",
|
| 48 |
+
placeholder="Example: A login screen with username, password, and a login button",
|
| 49 |
+
lines=3
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
generate_btn = gr.Button("Generate Mockup")
|
| 53 |
|
| 54 |
+
with gr.Row():
|
| 55 |
+
html_output = gr.Code(label="Generated HTML + CSS", language="html")
|
| 56 |
+
image_output = gr.Image(label="Mockup Preview")
|
|
|
|
| 57 |
|
| 58 |
+
generate_btn.click(
|
| 59 |
+
fn=generate_mockup,
|
| 60 |
+
inputs=user_input,
|
| 61 |
+
outputs=[html_output, image_output]
|
| 62 |
+
)
|
| 63 |
|
| 64 |
demo.launch()
|