File size: 6,315 Bytes
b511d75
8d1bd48
19338e6
abca33d
9abec8f
 
abca33d
9abec8f
19338e6
 
 
 
 
 
 
 
 
8d1bd48
83a7945
19338e6
 
 
 
 
 
 
 
 
 
 
 
83a7945
b511d75
19338e6
 
2dea97c
19338e6
 
 
 
 
 
 
bc1dcdf
19338e6
 
 
 
bc1dcdf
19338e6
abca33d
bc1dcdf
19338e6
 
abca33d
19338e6
bc1dcdf
9abec8f
cb93ca5
 
 
abca33d
cb93ca5
 
abca33d
70ebe1c
abca33d
19338e6
bc1dcdf
 
 
 
 
 
 
abca33d
19338e6
bc1dcdf
 
 
 
 
abca33d
19338e6
bc1dcdf
9abec8f
 
 
cb93ca5
9abec8f
19338e6
9abec8f
abca33d
bc1dcdf
abca33d
19338e6
2dea97c
19338e6
abca33d
bc1dcdf
cb93ca5
19338e6
 
bc1dcdf
 
abca33d
9abec8f
bc1dcdf
19338e6
cb93ca5
0fc1ac7
19338e6
bc1dcdf
 
 
 
 
 
 
70ebe1c
 
 
 
 
 
bc1dcdf
0fc1ac7
 
 
 
 
 
 
 
 
 
70ebe1c
 
 
 
 
bc1dcdf
cb93ca5
bc1dcdf
cb93ca5
 
 
 
bc1dcdf
19338e6
 
70ebe1c
831308c
cb93ca5
70ebe1c
bc1dcdf
70ebe1c
 
abca33d
70ebe1c
 
bc1dcdf
19338e6
 
bc1dcdf
70ebe1c
19338e6
 
70ebe1c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
import gradio as gr
import traceback
from PIL import Image, ImageEnhance, ImageFilter
import io
import base64
import google.generativeai as genai

# --- Environment Configuration ---
GEMINI_KEY = os.environ.get("GEMINI_KEY", "")

# --- Style Template Optimization ---
BASE_TEMPLATE = """Describe this design as a concise Flux 1.1 Pro prompt focusing on:
- Key visual elements
- Technical specifications
- Style consistency
- Functional requirements"""

STYLE_INSTRUCTIONS = {
    "General": BASE_TEMPLATE,
    "Realistic": f"{BASE_TEMPLATE}\nPHOTOREALISTIC RULES: Use photography terms, texture details, accurate lighting",
    "Kawaii": f"{BASE_TEMPLATE}\nKAWAII RULES: Rounded shapes, pastel colors, cute expressions",
    "Vector": f"{BASE_TEMPLATE}\nVECTOR RULES: Clean lines, geometric shapes, B&W gradients",
    "Silhouette": f"{BASE_TEMPLATE}\nSILHOUETTE RULES: High contrast, minimal details, strong outlines"
}

# --- Flux Configuration ---
FLUX_SPECS = {
    "aspect_ratios": ["1:1", "16:9", "4:3", "9:16"],
    "color_modes": ["B&W", "CMYK", "RGB"],
    "dpi_options": [72, 150, 300, 600]
}

# --- Image Processing Pipeline ---
def preprocess_image(img):
    try:
        if isinstance(img, str):  # Handle file paths
            img = Image.open(img)
        img = img.convert("RGB")
        img = ImageEnhance.Contrast(img).enhance(1.2)
        img = img.filter(ImageFilter.SHARPEN)
        return img
    except Exception as e:
        raise ValueError(f"πŸ”΄ Image processing failed: {str(e)}")

# --- Core Generation Engine ---
def generate_prompt(image, api_key, style, creativity, neg_prompt, aspect, color_mode, dpi):
    try:
        # Step 1: Input Validation
        if not image:
            return "", "⚠️ Please upload an image."

        api_key = api_key or GEMINI_KEY
        if not api_key:
            return "", "πŸ”‘ API key required - set in env (GEMINI_KEY) or input field."

        # Step 2: Gemini Setup
        try:
            genai.configure(api_key=api_key)
            model = genai.GenerativeModel("gemini-1.5-pro")
        except ImportError:
            return "", "🚫 Failed to import google.generativeai. Install with: pip install google-generativeai"
        except Exception as e:
            if "authentication" in str(e).lower():
                return "", "πŸ” Invalid API key or authentication error."
            else:
                return "", f"βš™οΈ API initialization error: {str(e)}"

        # Step 3: Preprocess Image
        try:
            img = preprocess_image(image)
            img_bytes = io.BytesIO()
            img.save(img_bytes, format="PNG")
            img_b64 = base64.b64encode(img_bytes.getvalue()).decode()
        except Exception as e:
            return "", f"πŸ–ΌοΈ Image preparation failed: {str(e)}"

        # Step 4: Build Instruction Prompt
        try:
            instruction = f"{STYLE_INSTRUCTIONS[style]}\nAVOID: {neg_prompt}\n"
            instruction += f"ASPECT: {aspect}, COLORS: {color_mode}, DPI: {dpi}\n"
        except KeyError:
            return "", "πŸ› οΈ Invalid style selected. Please choose from available options."

        # Step 5: Call Gemini API
        try:
            response = model.generate_content(
                contents=[instruction, {"mime_type": "image/png", "data": img_b64}],
                generation_config={"temperature": creativity}
            )
            raw_prompt = response.text
        except Exception as e:
            return "", f"πŸ€– Prompt generation failed: {str(e)}"

        return raw_prompt, "βœ… Prompt generated successfully!"

    except Exception as e:
        traceback.print_exc()
        return "", f"πŸ’₯ Unexpected error: {str(e)}"


# --- Main Interface ---
def build_interface():
    global STYLE_INSTRUCTIONS  # Ensure access to global dict

    with gr.Blocks(title="Flux Pro Generator") as app:
        gr.Markdown("# 🎨 Flux Pro Prompt Generator")
        gr.Markdown("Generate optimized design prompts from images using Google's Gemini.")

        with gr.Row():
            # Left Panel: Image Upload & API Key
            with gr.Column(scale=1):
                api_key = gr.Textbox(
                    label="πŸ”‘ Gemini API Key",
                    value=GEMINI_KEY,
                    type="password",
                    info="Set GEMINI_KEY environment variable for production."
                )

                img_input = gr.Image(
                    label="πŸ–ΌοΈ Upload Design",
                    type="pil",
                    sources=["upload"],
                    interactive=True
                )

            # Right Panel: Prompt Output + Controls Below
            with gr.Column(scale=2):
                status_msg = gr.Textbox(label="πŸ“’ Status", interactive=False)
                prompt_output = gr.Textbox(
                    label="πŸ“ Optimized Prompt",
                    lines=8,
                    interactive=True
                )

                # Controls under prompt box
                style = gr.Dropdown(
                    list(STYLE_INSTRUCTIONS.keys()),
                    value="General",
                    label="🎨 Target Style"
                )

                with gr.Accordion("βš™οΈ Advanced Settings", open=False):
                    creativity = gr.Slider(0.0, 1.0, 0.7, label="🧠 Creativity Level")
                    neg_prompt = gr.Textbox(label="🚫 Negative Prompts", placeholder="What to avoid")
                    aspect = gr.Dropdown(FLUX_SPECS["aspect_ratios"], value="1:1", label="Aspect Ratio")
                    color_mode = gr.Dropdown(FLUX_SPECS["color_modes"], value="RGB", label="Color Mode")
                    dpi = gr.Dropdown([str(d) for d in FLUX_SPECS["dpi_options"]], value="300", label="Output DPI")

                gen_btn = gr.Button("✨ Generate Prompt", variant="primary")

        # Event Handling
        gen_btn.click(
            fn=generate_prompt,
            inputs=[
                img_input, api_key, style, creativity,
                neg_prompt, aspect, color_mode, dpi
            ],
            outputs=[prompt_output, status_msg],
            api_name="generate"
        )

    return app


# --- Production Launch ---
if __name__ == "__main__":
    app = build_interface()
    app.launch()