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

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()