dina1 commited on
Commit
020ec68
·
verified ·
1 Parent(s): 79cffcd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -79
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
- # Step 1: Gemini Setup
9
- # -------------------------------
10
- API_KEY = os.environ.get("GENAI_API_KEY")
11
- genai.configure(api_key=API_KEY)
12
- model = genai.GenerativeModel("gemini-2.5-pro")
13
 
14
- # -------------------------------
15
- # Step 2: Convert requirement JSON schema
16
- # -------------------------------
17
- def generate_schema(requirement: str):
18
- prompt = f"""
19
- Convert the following requirement into a valid JSON schema for a PowerApps form.
20
- Only output JSON.
21
- Requirement: {requirement}
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
- # Step 3: Draw Mockup (PIL)
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
- y = 50
49
- x_start = 50
50
- box_width = 400
51
- box_height = 35
52
 
53
- for field in schema.get("fields", []):
54
- if field["type"] == "text":
55
- draw.text((x_start, y), field["label"], fill="black", font=font)
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
- return img
65
 
66
- # -------------------------------
67
- # Step 4: Full pipeline
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
- # Step 5: Gradio UI
76
- # -------------------------------
77
  with gr.Blocks() as demo:
78
- gr.Markdown("## 🔵 PowerApps Mockup Generator (Gemini 2.5 Pro + PIL)")
79
- gr.Markdown("""
80
- - Enter a natural language requirement.
81
- - Gemini 2.5 Pro → generates JSON schema.
82
- - PIL renders a crisp PowerApps-style layout.
83
- - Fallback schema is used if parsing fails.
84
- """)
 
 
 
85
 
86
- req_text = gr.Textbox(label="Requirements", placeholder="e.g. Create a registration form with Name, Email, Phone, and Submit button")
87
- json_out = gr.Code(label="Generated Schema (JSON)", language="json")
88
- pil_out = gr.Image(label="Generated PowerApps Mockup", type="pil")
89
- raw_out = gr.Textbox(label="Raw LLM Output")
90
 
91
- gen_btn = gr.Button("Generate Mockup")
92
- gen_btn.click(generate_all, inputs=req_text, outputs=[json_out, pil_out, raw_out])
 
 
 
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()