dina1 commited on
Commit
cffca37
·
verified ·
1 Parent(s): 2b0ffa7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ # -----------------------
3
+ # Imports
4
+ # -----------------------
5
+ import google.generativeai as genai
6
+ import imgkit
7
+ import gradio as gr
8
+ import uuid
9
+ import os
10
+
11
+ # -----------------------
12
+ # Configure Gemini API ONCE at startup
13
+ # -----------------------
14
+ genai.configure(api_key=os.environ.get("GEMINI_API_KEY")) # use HF secret for API key
15
+ model = genai.GenerativeModel("gemini-2.0-flash") # initialized once
16
+
17
+ # -----------------------
18
+ # Function: Generate Mockup Image
19
+ # -----------------------
20
+ def generate_mockup(user_prompt):
21
+ """
22
+ Generate PNG mockup directly from user description
23
+ """
24
+ system_prompt = (
25
+ "You are a Power Apps mockup generator. "
26
+ "Convert user descriptions into complete HTML + CSS code that looks like a Power Apps mobile screen. "
27
+ "Do NOT include JSON or explanations. Only return valid HTML + CSS."
28
+ )
29
+
30
+ try:
31
+ # Generate HTML using the already configured model
32
+ response = model.generate_content(system_prompt + "\n" + user_prompt)
33
+ html_code = response.text.strip()
34
+
35
+ # Save HTML to a unique file
36
+ unique_id = str(uuid.uuid4())
37
+ html_file = f"mockup_{unique_id}.html"
38
+ img_file = f"mockup_{unique_id}.png"
39
+
40
+ with open(html_file, "w", encoding="utf-8") as f:
41
+ f.write(html_code)
42
+
43
+ # Convert HTML to PNG
44
+ imgkit.from_file(html_file, img_file)
45
+
46
+ return img_file
47
+ except Exception as e:
48
+ return None
49
+
50
+ # -----------------------
51
+ # Gradio UI
52
+ # -----------------------
53
+ with gr.Blocks() as demo:
54
+ gr.Markdown("## ⚡ Power Apps Mockup Generator (Gemini + Gradio)")
55
+
56
+ user_input = gr.Textbox(
57
+ label="Describe your Power Apps mockup screen",
58
+ lines=4,
59
+ placeholder="Example: A login screen with username, password, and login button"
60
+ )
61
+ generate_btn = gr.Button("Generate Mockup")
62
+ image_output = gr.Image(label="Mockup Preview")
63
+
64
+ generate_btn.click(fn=generate_mockup, inputs=user_input, outputs=image_output)
65
+
66
+ # Run app
67
+ demo.launch(server_name="0.0.0.0", server_port=7860)