mroccuper commited on
Commit
8d1bd48
Β·
verified Β·
1 Parent(s): f3402c2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import google.generativeai as genai
3
+ from PIL import Image
4
+ import io
5
+ import base64
6
+
7
+ # Mapping styles to instructions
8
+ STYLE_INSTRUCTIONS = {
9
+ "General": (
10
+ "Describe this design as a single, cohesive, and concise prompt suitable for Flux 1.1 Pro. "
11
+ "Focus on key elements such as text, symbols, layout, and overall style."
12
+ ),
13
+ "Realistic": (
14
+ "Describe this design with a focus on photorealistic details, including textures, lighting, and depth, "
15
+ "ensuring accuracy in representation. Avoid illustrations or cartoon-like interpretations."
16
+ ),
17
+ "Kawaii/Cartoon": (
18
+ "Describe this design with an emphasis on cute, rounded shapes, playful expressions, and vibrant, fun aesthetics "
19
+ "typical of the kawaii/cartoon style. Focus on light-hearted and visually charming elements."
20
+ ),
21
+ "Vector": (
22
+ "Describe this design as a single, cohesive, and concise prompt suitable for Flux 1.1 Pro. "
23
+ "Focus on key elements such as text, symbols, layout, and overall style. "
24
+ "Specify that the design should be created in a clean, professional vector and illustration style, "
25
+ "emphasizing sharp lines, geometric shapes, and smooth gradients where applicable. "
26
+ "The entire design must be in black and white, using only shades of gray for depth and contrast. "
27
+ "Avoid any use of color unless it is essential to the design's purpose. "
28
+ "Avoid cartoonish or overly stylized elements unless they align with the design's purpose. "
29
+ "Use clear and direct language to convey the design's theme, humor, or purpose, if applicable. "
30
+ "Ensure the description is compact, well-structured, and optimized for creating a graphic. "
31
+ "Include only essential details and avoid unnecessary elaboration."
32
+ ),
33
+ "Silhouette": (
34
+ "Describe this design using high-contrast black and white silhouettes, focusing on bold shapes and clear outlines "
35
+ "to convey the subject effectively. Eliminate interior details unless crucial to recognition."
36
+ ),
37
+ }
38
+
39
+ # Function to check API key validity
40
+ def check_api(api_key):
41
+ try:
42
+ genai.configure(api_key=api_key)
43
+ model = genai.GenerativeModel("gemini-1.5-pro")
44
+ response = model.generate_content("Hello, can you respond?")
45
+ return "βœ… API Key is valid!" if response.text else "⚠️ API responded but empty."
46
+ except Exception as e:
47
+ return f"❌ Error: {str(e)}"
48
+
49
+ # Function to generate a prompt using Gemini
50
+ def generate_prompt(image, api_key, style):
51
+ if not api_key:
52
+ return "❌ Error: Please enter your Google Gemini API key."
53
+
54
+ try:
55
+ genai.configure(api_key=api_key)
56
+ model = genai.GenerativeModel("gemini-1.5-pro")
57
+
58
+ # Convert image to base64
59
+ image_bytes = io.BytesIO()
60
+ image.save(image_bytes, format=image.format if image.format else "PNG")
61
+ image_base64 = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
62
+
63
+ instruction = STYLE_INSTRUCTIONS.get(style, STYLE_INSTRUCTIONS["General"])
64
+
65
+ response = model.generate_content([
66
+ instruction,
67
+ {"mime_type": "image/png", "data": image_base64}
68
+ ])
69
+
70
+ return response.text if response.text else "⚠️ No response generated."
71
+ except Exception as e:
72
+ return f"❌ Error generating prompt: {str(e)}"
73
+
74
+ # Gradio Interface
75
+ def main():
76
+ with gr.Blocks() as app:
77
+ gr.Markdown("## 🎨 Flux Prompt Generator with Gemini 1.5 Pro")
78
+
79
+ api_key = gr.Textbox(label="πŸ”‘ Google Gemini API Key", type="password", placeholder="Paste your Gemini API key here")
80
+ check_button = gr.Button("βœ… Check API Key")
81
+ check_output = gr.Textbox(label="API Key Status")
82
+
83
+ with gr.Row():
84
+ with gr.Column():
85
+ image_input = gr.Image(label="πŸ“· Upload Design Image", type="pil", width=300)
86
+ style_dropdown = gr.Dropdown(label="🎨 Style", choices=list(STYLE_INSTRUCTIONS.keys()), value="General")
87
+
88
+ generate_button = gr.Button("🧠 Generate Prompt")
89
+ prompt_output = gr.Textbox(label="πŸ“ Generated Flux Prompt", lines=6)
90
+ copy_button = gr.Button("πŸ“‹ Copy to Clipboard")
91
+
92
+ def copy_prompt(prompt):
93
+ return gr.update(value=prompt, interactive=True)
94
+
95
+ check_button.click(check_api, inputs=api_key, outputs=check_output)
96
+ generate_button.click(generate_prompt, inputs=[image_input, api_key, style_dropdown], outputs=prompt_output)
97
+ copy_button.click(copy_prompt, inputs=prompt_output, outputs=prompt_output)
98
+
99
+ return app
100
+
101
+ # Launch the app
102
+ app = main()
103
+ app.launch()