Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,13 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import json
|
| 4 |
import traceback
|
|
|
|
| 5 |
import io
|
| 6 |
import base64
|
| 7 |
-
|
| 8 |
|
| 9 |
# --- Environment Configuration ---
|
| 10 |
GEMINI_KEY = os.environ.get("GEMINI_KEY", "")
|
| 11 |
-
DEFAULT_PORT = int(os.environ.get("PORT", 7860))
|
| 12 |
|
| 13 |
# --- Style Template Optimization ---
|
| 14 |
BASE_TEMPLATE = """Describe this design as a concise Flux 1.1 Pro prompt focusing on:
|
|
@@ -28,7 +27,6 @@ STYLE_INSTRUCTIONS = {
|
|
| 28 |
# --- Flux Configuration ---
|
| 29 |
FLUX_SPECS = {
|
| 30 |
"aspect_ratios": ["1:1", "16:9", "4:3", "9:16"],
|
| 31 |
-
"formats": ["SVG", "PNG", "PDF"],
|
| 32 |
"color_modes": ["B&W", "CMYK", "RGB"],
|
| 33 |
"dpi_options": [72, 150, 300, 600]
|
| 34 |
}
|
|
@@ -50,24 +48,23 @@ def generate_prompt(image, api_key, style, creativity, neg_prompt, aspect, color
|
|
| 50 |
try:
|
| 51 |
# Step 1: Input Validation
|
| 52 |
if not image:
|
| 53 |
-
return "โ ๏ธ Please upload an image."
|
| 54 |
|
| 55 |
api_key = api_key or GEMINI_KEY
|
| 56 |
if not api_key:
|
| 57 |
-
return "๐ API key required - set in env (GEMINI_KEY) or input field."
|
| 58 |
|
| 59 |
# Step 2: Gemini Setup
|
| 60 |
try:
|
| 61 |
-
import google.generativeai as genai
|
| 62 |
genai.configure(api_key=api_key)
|
| 63 |
model = genai.GenerativeModel("gemini-1.5-pro")
|
| 64 |
except ImportError:
|
| 65 |
-
return "",
|
| 66 |
except Exception as e:
|
| 67 |
if "authentication" in str(e).lower():
|
| 68 |
-
return "",
|
| 69 |
else:
|
| 70 |
-
return "",
|
| 71 |
|
| 72 |
# Step 3: Preprocess Image
|
| 73 |
try:
|
|
@@ -76,14 +73,14 @@ def generate_prompt(image, api_key, style, creativity, neg_prompt, aspect, color
|
|
| 76 |
img.save(img_bytes, format="PNG")
|
| 77 |
img_b64 = base64.b64encode(img_bytes.getvalue()).decode()
|
| 78 |
except Exception as e:
|
| 79 |
-
return "",
|
| 80 |
|
| 81 |
# Step 4: Build Instruction Prompt
|
| 82 |
try:
|
| 83 |
instruction = f"{STYLE_INSTRUCTIONS[style]}\nAVOID: {neg_prompt}\n"
|
| 84 |
instruction += f"ASPECT: {aspect}, COLORS: {color_mode}, DPI: {dpi}\n"
|
| 85 |
except KeyError:
|
| 86 |
-
return "",
|
| 87 |
|
| 88 |
# Step 5: Call Gemini API
|
| 89 |
try:
|
|
@@ -93,34 +90,20 @@ def generate_prompt(image, api_key, style, creativity, neg_prompt, aspect, color
|
|
| 93 |
)
|
| 94 |
raw_prompt = response.text
|
| 95 |
except Exception as e:
|
| 96 |
-
return "",
|
| 97 |
-
|
| 98 |
-
# Step 6: Token Stats
|
| 99 |
-
input_tokens = len(img_b64) // 4
|
| 100 |
-
output_tokens = len(raw_prompt.split())
|
| 101 |
|
| 102 |
-
return
|
| 103 |
-
raw_prompt,
|
| 104 |
-
{"score": 8, "issues": [], "suggestions": []},
|
| 105 |
-
{"Input Tokens": input_tokens, "Output Tokens": output_tokens},
|
| 106 |
-
"โ
Prompt generated successfully!"
|
| 107 |
-
)
|
| 108 |
|
| 109 |
except Exception as e:
|
| 110 |
traceback.print_exc()
|
| 111 |
-
return "",
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
# --- Modern Copy Function ---
|
| 115 |
-
def copy_text(text):
|
| 116 |
-
return text, "โ Copied to clipboard!", gr.Button.update(variant="secondary")
|
| 117 |
|
| 118 |
|
| 119 |
# --- Main Interface ---
|
| 120 |
def build_interface():
|
| 121 |
global STYLE_INSTRUCTIONS # Ensure access to global dict
|
| 122 |
|
| 123 |
-
with gr.Blocks(title="Flux Pro Generator"
|
| 124 |
gr.Markdown("# ๐จ Flux Pro Prompt Generator")
|
| 125 |
gr.Markdown("Generate optimized design prompts from images using Google's Gemini.")
|
| 126 |
|
|
@@ -157,20 +140,12 @@ def build_interface():
|
|
| 157 |
|
| 158 |
with gr.Column(scale=2):
|
| 159 |
status_msg = gr.Textbox(label="๐ข Status", interactive=False)
|
| 160 |
-
|
| 161 |
prompt_output = gr.Textbox(
|
| 162 |
label="๐ Optimized Prompt",
|
| 163 |
-
lines=
|
| 164 |
-
interactive=True
|
| 165 |
-
show_copy_button=True
|
| 166 |
)
|
| 167 |
|
| 168 |
-
with gr.Row():
|
| 169 |
-
copy_btn = gr.Button()#"๐ Copy to Clipboard", variant="secondary")
|
| 170 |
-
|
| 171 |
-
quality_report = gr.JSON()#label="๐ Quality Report")
|
| 172 |
-
token_stats = gr.JSON()#label="๐งฎ Token Usage")
|
| 173 |
-
|
| 174 |
# Event Handling
|
| 175 |
gen_btn.click(
|
| 176 |
fn=generate_prompt,
|
|
@@ -178,22 +153,10 @@ def build_interface():
|
|
| 178 |
img_input, api_key, style, creativity,
|
| 179 |
neg_prompt, aspect, color_mode, dpi
|
| 180 |
],
|
| 181 |
-
outputs=[prompt_output,
|
| 182 |
api_name="generate"
|
| 183 |
)
|
| 184 |
|
| 185 |
-
copy_btn.click(
|
| 186 |
-
fn=copy_text,
|
| 187 |
-
inputs=prompt_output,
|
| 188 |
-
outputs=[prompt_output, status_msg, copy_btn],
|
| 189 |
-
js="""
|
| 190 |
-
(text) => {
|
| 191 |
-
if(text) navigator.clipboard.writeText(text);
|
| 192 |
-
return [text, 'โ Copied!', { variant: 'secondary' }];
|
| 193 |
-
}
|
| 194 |
-
"""
|
| 195 |
-
)
|
| 196 |
-
|
| 197 |
return app
|
| 198 |
|
| 199 |
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import traceback
|
| 4 |
+
from PIL import Image, ImageEnhance, ImageFilter
|
| 5 |
import io
|
| 6 |
import base64
|
| 7 |
+
import google.generativeai as genai
|
| 8 |
|
| 9 |
# --- Environment Configuration ---
|
| 10 |
GEMINI_KEY = os.environ.get("GEMINI_KEY", "")
|
|
|
|
| 11 |
|
| 12 |
# --- Style Template Optimization ---
|
| 13 |
BASE_TEMPLATE = """Describe this design as a concise Flux 1.1 Pro prompt focusing on:
|
|
|
|
| 27 |
# --- Flux Configuration ---
|
| 28 |
FLUX_SPECS = {
|
| 29 |
"aspect_ratios": ["1:1", "16:9", "4:3", "9:16"],
|
|
|
|
| 30 |
"color_modes": ["B&W", "CMYK", "RGB"],
|
| 31 |
"dpi_options": [72, 150, 300, 600]
|
| 32 |
}
|
|
|
|
| 48 |
try:
|
| 49 |
# Step 1: Input Validation
|
| 50 |
if not image:
|
| 51 |
+
return "", "โ ๏ธ Please upload an image."
|
| 52 |
|
| 53 |
api_key = api_key or GEMINI_KEY
|
| 54 |
if not api_key:
|
| 55 |
+
return "", "๐ API key required - set in env (GEMINI_KEY) or input field."
|
| 56 |
|
| 57 |
# Step 2: Gemini Setup
|
| 58 |
try:
|
|
|
|
| 59 |
genai.configure(api_key=api_key)
|
| 60 |
model = genai.GenerativeModel("gemini-1.5-pro")
|
| 61 |
except ImportError:
|
| 62 |
+
return "", "๐ซ Failed to import google.generativeai. Install with: pip install google-generativeai"
|
| 63 |
except Exception as e:
|
| 64 |
if "authentication" in str(e).lower():
|
| 65 |
+
return "", "๐ Invalid API key or authentication error."
|
| 66 |
else:
|
| 67 |
+
return "", f"โ๏ธ API initialization error: {str(e)}"
|
| 68 |
|
| 69 |
# Step 3: Preprocess Image
|
| 70 |
try:
|
|
|
|
| 73 |
img.save(img_bytes, format="PNG")
|
| 74 |
img_b64 = base64.b64encode(img_bytes.getvalue()).decode()
|
| 75 |
except Exception as e:
|
| 76 |
+
return "", f"๐ผ๏ธ Image preparation failed: {str(e)}"
|
| 77 |
|
| 78 |
# Step 4: Build Instruction Prompt
|
| 79 |
try:
|
| 80 |
instruction = f"{STYLE_INSTRUCTIONS[style]}\nAVOID: {neg_prompt}\n"
|
| 81 |
instruction += f"ASPECT: {aspect}, COLORS: {color_mode}, DPI: {dpi}\n"
|
| 82 |
except KeyError:
|
| 83 |
+
return "", "๐ ๏ธ Invalid style selected. Please choose from available options."
|
| 84 |
|
| 85 |
# Step 5: Call Gemini API
|
| 86 |
try:
|
|
|
|
| 90 |
)
|
| 91 |
raw_prompt = response.text
|
| 92 |
except Exception as e:
|
| 93 |
+
return "", f"๐ค Prompt generation failed: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
return raw_prompt, "โ
Prompt generated successfully!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
except Exception as e:
|
| 98 |
traceback.print_exc()
|
| 99 |
+
return "", f"๐ฅ Unexpected error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
|
| 102 |
# --- Main Interface ---
|
| 103 |
def build_interface():
|
| 104 |
global STYLE_INSTRUCTIONS # Ensure access to global dict
|
| 105 |
|
| 106 |
+
with gr.Blocks(title="Flux Pro Generator") as app:
|
| 107 |
gr.Markdown("# ๐จ Flux Pro Prompt Generator")
|
| 108 |
gr.Markdown("Generate optimized design prompts from images using Google's Gemini.")
|
| 109 |
|
|
|
|
| 140 |
|
| 141 |
with gr.Column(scale=2):
|
| 142 |
status_msg = gr.Textbox(label="๐ข Status", interactive=False)
|
|
|
|
| 143 |
prompt_output = gr.Textbox(
|
| 144 |
label="๐ Optimized Prompt",
|
| 145 |
+
lines=10,
|
| 146 |
+
interactive=True
|
|
|
|
| 147 |
)
|
| 148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
# Event Handling
|
| 150 |
gen_btn.click(
|
| 151 |
fn=generate_prompt,
|
|
|
|
| 153 |
img_input, api_key, style, creativity,
|
| 154 |
neg_prompt, aspect, color_mode, dpi
|
| 155 |
],
|
| 156 |
+
outputs=[prompt_output, status_msg],
|
| 157 |
api_name="generate"
|
| 158 |
)
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
return app
|
| 161 |
|
| 162 |
|