Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,21 @@
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def generate_mockup(user_prompt):
|
| 16 |
system_prompt = (
|
|
@@ -18,43 +23,29 @@ def generate_mockup(user_prompt):
|
|
| 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 |
-
#
|
| 27 |
-
with open("mockup.html", "w") as f:
|
| 28 |
-
f.write(html_code)
|
| 29 |
-
|
| 30 |
-
# Generate PNG image with explicit config
|
| 31 |
output_img = "mockup.png"
|
| 32 |
-
|
| 33 |
|
| 34 |
return html_code, output_img
|
| 35 |
-
|
| 36 |
except Exception as e:
|
| 37 |
return f"❌ Error: {e}", None
|
| 38 |
|
| 39 |
-
|
| 40 |
-
# -----------------------
|
| 41 |
-
# Gradio Interface
|
| 42 |
-
# -----------------------
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
gr.Markdown("## ⚡ Power Apps Mockup Generator (Gemini + HTML + PNG)")
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
lines=3
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
generate_btn = gr.Button("Generate Mockup")
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
html_output = gr.Code(label="Generated HTML + CSS", language="html")
|
| 57 |
-
image_output = gr.Image(label="Mockup Preview")
|
| 58 |
|
| 59 |
generate_btn.click(
|
| 60 |
fn=generate_mockup,
|
|
|
|
| 1 |
import os
|
| 2 |
import google.generativeai as genai
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
+
import asyncio
|
| 5 |
+
from pyppeteer import launch
|
| 6 |
|
| 7 |
+
# Configure Gemini
|
|
|
|
|
|
|
| 8 |
genai.configure(api_key=os.environ["GENAI_API_KEY"])
|
| 9 |
model = genai.GenerativeModel("gemini-2.0-flash")
|
| 10 |
|
| 11 |
+
# Function to render HTML → PNG using pyppeteer
|
| 12 |
+
async def render_html_to_png(html_code, output_img="mockup.png"):
|
| 13 |
+
browser = await launch(args=['--no-sandbox'])
|
| 14 |
+
page = await browser.newPage()
|
| 15 |
+
await page.setContent(html_code)
|
| 16 |
+
await page.screenshot({'path': output_img, 'fullPage': True})
|
| 17 |
+
await browser.close()
|
| 18 |
+
return output_img
|
| 19 |
|
| 20 |
def generate_mockup(user_prompt):
|
| 21 |
system_prompt = (
|
|
|
|
| 23 |
"Convert user descriptions into complete HTML + CSS code that looks like a Power Apps mobile screen. "
|
| 24 |
"Do NOT include JSON or explanations. Only return valid HTML + CSS."
|
| 25 |
)
|
|
|
|
| 26 |
try:
|
| 27 |
response = model.generate_content(system_prompt + "\n" + user_prompt)
|
| 28 |
html_code = response.text.strip()
|
| 29 |
|
| 30 |
+
# Render PNG
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
output_img = "mockup.png"
|
| 32 |
+
asyncio.get_event_loop().run_until_complete(render_html_to_png(html_code, output_img))
|
| 33 |
|
| 34 |
return html_code, output_img
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
return f"❌ Error: {e}", None
|
| 37 |
|
| 38 |
+
# Gradio UI
|
|
|
|
|
|
|
|
|
|
| 39 |
with gr.Blocks() as demo:
|
| 40 |
gr.Markdown("## ⚡ Power Apps Mockup Generator (Gemini + HTML + PNG)")
|
| 41 |
+
user_input = gr.Textbox(
|
| 42 |
+
label="Describe your Power Apps mockup screen",
|
| 43 |
+
placeholder="Example: A login screen with username, password, and a login button",
|
| 44 |
+
lines=3
|
| 45 |
+
)
|
|
|
|
|
|
|
|
|
|
| 46 |
generate_btn = gr.Button("Generate Mockup")
|
| 47 |
+
html_output = gr.Code(label="Generated HTML + CSS", language="html")
|
| 48 |
+
image_output = gr.Image(label="Mockup Preview")
|
|
|
|
|
|
|
| 49 |
|
| 50 |
generate_btn.click(
|
| 51 |
fn=generate_mockup,
|