Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,53 @@
|
|
|
|
|
|
|
|
| 1 |
from html2image import Html2Image
|
| 2 |
import uuid
|
| 3 |
import os
|
| 4 |
-
import google.generativeai as genai
|
| 5 |
-
import gradio as gr
|
| 6 |
|
|
|
|
| 7 |
# Configure Gemini API
|
|
|
|
| 8 |
genai.configure(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 9 |
model = genai.GenerativeModel("gemini-2.0-flash")
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
def generate_mockup(user_prompt):
|
| 14 |
system_prompt = (
|
| 15 |
"You are a Power Apps mockup generator. "
|
| 16 |
"Convert user descriptions into complete HTML + CSS code that looks like a Power Apps mobile screen. "
|
| 17 |
"Do NOT include JSON or explanations. Only return valid HTML + CSS."
|
| 18 |
)
|
| 19 |
-
response = model.generate_content(system_prompt + "\n" + user_prompt)
|
| 20 |
-
return response.text.strip()
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
with gr.Blocks() as demo:
|
| 23 |
gr.Markdown("## ⚡ Power Apps Mockup Generator (Gemini + Gradio)")
|
| 24 |
user_input = gr.Textbox(
|
|
@@ -27,8 +56,8 @@ with gr.Blocks() as demo:
|
|
| 27 |
placeholder="Example: A login screen with username, password, and login button"
|
| 28 |
)
|
| 29 |
generate_btn = gr.Button("Generate Mockup")
|
| 30 |
-
|
| 31 |
|
| 32 |
-
generate_btn.click(fn=generate_mockup, inputs=user_input, outputs=
|
| 33 |
|
| 34 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
import google.generativeai as genai
|
| 2 |
+
import gradio as gr
|
| 3 |
from html2image import Html2Image
|
| 4 |
import uuid
|
| 5 |
import os
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# -----------------------
|
| 8 |
# Configure Gemini API
|
| 9 |
+
# -----------------------
|
| 10 |
genai.configure(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 11 |
model = genai.GenerativeModel("gemini-2.0-flash")
|
| 12 |
|
| 13 |
+
# -----------------------
|
| 14 |
+
# Html2Image Setup
|
| 15 |
+
# -----------------------
|
| 16 |
+
hti = Html2Image(executable_path="/usr/bin/chromium")
|
| 17 |
|
| 18 |
+
# -----------------------
|
| 19 |
+
# Function: Generate Mockup Image
|
| 20 |
+
# -----------------------
|
| 21 |
def generate_mockup(user_prompt):
|
| 22 |
system_prompt = (
|
| 23 |
"You are a Power Apps mockup generator. "
|
| 24 |
"Convert user descriptions into complete HTML + CSS code that looks like a Power Apps mobile screen. "
|
| 25 |
"Do NOT include JSON or explanations. Only return valid HTML + CSS."
|
| 26 |
)
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
try:
|
| 29 |
+
# Generate HTML
|
| 30 |
+
response = model.generate_content(system_prompt + "\n" + user_prompt)
|
| 31 |
+
html_code = response.text.strip()
|
| 32 |
+
|
| 33 |
+
# Unique file names
|
| 34 |
+
unique_id = str(uuid.uuid4())
|
| 35 |
+
html_file = f"mockup_{unique_id}.html"
|
| 36 |
+
img_file = f"mockup_{unique_id}.png"
|
| 37 |
+
|
| 38 |
+
with open(html_file, "w", encoding="utf-8") as f:
|
| 39 |
+
f.write(html_code)
|
| 40 |
+
|
| 41 |
+
# Convert HTML -> PNG
|
| 42 |
+
hti.screenshot(html_file=html_file, save_as=img_file)
|
| 43 |
+
|
| 44 |
+
return img_file
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Error: {e}"
|
| 47 |
+
|
| 48 |
+
# -----------------------
|
| 49 |
+
# Gradio UI
|
| 50 |
+
# -----------------------
|
| 51 |
with gr.Blocks() as demo:
|
| 52 |
gr.Markdown("## ⚡ Power Apps Mockup Generator (Gemini + Gradio)")
|
| 53 |
user_input = gr.Textbox(
|
|
|
|
| 56 |
placeholder="Example: A login screen with username, password, and login button"
|
| 57 |
)
|
| 58 |
generate_btn = gr.Button("Generate Mockup")
|
| 59 |
+
image_output = gr.Image(label="Mockup Preview")
|
| 60 |
|
| 61 |
+
generate_btn.click(fn=generate_mockup, inputs=user_input, outputs=image_output)
|
| 62 |
|
| 63 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|