Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import json
|
4 |
-
import time
|
5 |
import traceback
|
6 |
import io
|
7 |
import base64
|
@@ -10,7 +9,6 @@ from PIL import Image, ImageEnhance, ImageFilter
|
|
10 |
# --- Environment Configuration ---
|
11 |
GEMINI_KEY = os.environ.get("GEMINI_KEY", "")
|
12 |
DEFAULT_PORT = int(os.environ.get("PORT", 7860))
|
13 |
-
API_TIMEOUT = 30 # seconds
|
14 |
|
15 |
# --- Style Template Optimization ---
|
16 |
BASE_TEMPLATE = """Describe this design as a concise Flux 1.1 Pro prompt focusing on:
|
@@ -52,11 +50,11 @@ def generate_prompt(image, api_key, style, creativity, neg_prompt, aspect, color
|
|
52 |
try:
|
53 |
# Step 1: Input Validation
|
54 |
if not image:
|
55 |
-
return "β οΈ Please upload an image.", None, None
|
56 |
|
57 |
api_key = api_key or GEMINI_KEY
|
58 |
if not api_key:
|
59 |
-
return "π API key required - set in env (GEMINI_KEY) or input field.", None, None
|
60 |
|
61 |
# Step 2: Gemini Setup
|
62 |
try:
|
@@ -64,12 +62,12 @@ def generate_prompt(image, api_key, style, creativity, neg_prompt, aspect, color
|
|
64 |
genai.configure(api_key=api_key)
|
65 |
model = genai.GenerativeModel("gemini-1.5-pro")
|
66 |
except ImportError:
|
67 |
-
return "π« Failed to import google.generativeai. Install with: pip install google-generativeai"
|
68 |
except Exception as e:
|
69 |
if "authentication" in str(e).lower():
|
70 |
-
return "π Invalid API key or authentication error"
|
71 |
else:
|
72 |
-
return f"βοΈ API initialization error: {str(e)}"
|
73 |
|
74 |
# Step 3: Preprocess Image
|
75 |
try:
|
@@ -78,14 +76,14 @@ def generate_prompt(image, api_key, style, creativity, neg_prompt, aspect, color
|
|
78 |
img.save(img_bytes, format="PNG")
|
79 |
img_b64 = base64.b64encode(img_bytes.getvalue()).decode()
|
80 |
except Exception as e:
|
81 |
-
return f"πΌοΈ Image preparation failed: {str(e)}"
|
82 |
|
83 |
# Step 4: Build Instruction Prompt
|
84 |
try:
|
85 |
instruction = f"{STYLE_INSTRUCTIONS[style]}\nAVOID: {neg_prompt}\n"
|
86 |
instruction += f"ASPECT: {aspect}, COLORS: {color_mode}, DPI: {dpi}\n"
|
87 |
except KeyError:
|
88 |
-
return "π οΈ Invalid style selected. Please choose from available options."
|
89 |
|
90 |
# Step 5: Call Gemini API
|
91 |
try:
|
@@ -95,20 +93,22 @@ def generate_prompt(image, api_key, style, creativity, neg_prompt, aspect, color
|
|
95 |
)
|
96 |
raw_prompt = response.text
|
97 |
except Exception as e:
|
98 |
-
return f"π€ Prompt generation failed: {str(e)}"
|
99 |
|
100 |
# Step 6: Token Stats
|
101 |
input_tokens = len(img_b64) // 4
|
102 |
output_tokens = len(raw_prompt.split())
|
103 |
|
104 |
-
return
|
105 |
-
|
106 |
-
"
|
107 |
-
|
|
|
|
|
108 |
|
109 |
except Exception as e:
|
110 |
traceback.print_exc()
|
111 |
-
return f"π₯ Unexpected error: {str(e)}"
|
112 |
|
113 |
|
114 |
# --- Modern Copy Function ---
|
@@ -178,14 +178,8 @@ def build_interface():
|
|
178 |
img_input, api_key, style, creativity,
|
179 |
neg_prompt, aspect, color_mode, dpi
|
180 |
],
|
181 |
-
outputs=[prompt_output, quality_report, token_stats],
|
182 |
api_name="generate"
|
183 |
-
).success(
|
184 |
-
fn=lambda: "β
Prompt generated successfully!",
|
185 |
-
outputs=status_msg
|
186 |
-
).error(
|
187 |
-
fn=lambda err: f"β Error: {err}",
|
188 |
-
outputs=status_msg
|
189 |
)
|
190 |
|
191 |
copy_btn.click(
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import json
|
|
|
4 |
import traceback
|
5 |
import io
|
6 |
import base64
|
|
|
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:
|
|
|
50 |
try:
|
51 |
# Step 1: Input Validation
|
52 |
if not image:
|
53 |
+
return "β οΈ Please upload an image.", None, None, "β οΈ No image uploaded."
|
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.", None, None, "π Missing Gemini API key."
|
58 |
|
59 |
# Step 2: Gemini Setup
|
60 |
try:
|
|
|
62 |
genai.configure(api_key=api_key)
|
63 |
model = genai.GenerativeModel("gemini-1.5-pro")
|
64 |
except ImportError:
|
65 |
+
return "", None, None, "π« Failed to import google.generativeai. Install with: pip install google-generativeai"
|
66 |
except Exception as e:
|
67 |
if "authentication" in str(e).lower():
|
68 |
+
return "", None, None, "π Invalid API key or authentication error."
|
69 |
else:
|
70 |
+
return "", None, None, f"βοΈ API initialization error: {str(e)}"
|
71 |
|
72 |
# Step 3: Preprocess Image
|
73 |
try:
|
|
|
76 |
img.save(img_bytes, format="PNG")
|
77 |
img_b64 = base64.b64encode(img_bytes.getvalue()).decode()
|
78 |
except Exception as e:
|
79 |
+
return "", None, None, f"πΌοΈ Image preparation failed: {str(e)}"
|
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 "", None, None, "π οΈ Invalid style selected. Please choose from available options."
|
87 |
|
88 |
# Step 5: Call Gemini API
|
89 |
try:
|
|
|
93 |
)
|
94 |
raw_prompt = response.text
|
95 |
except Exception as e:
|
96 |
+
return "", None, None, f"π€ Prompt generation failed: {str(e)}"
|
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 "", None, None, f"π₯ Unexpected error: {str(e)}"
|
112 |
|
113 |
|
114 |
# --- Modern Copy Function ---
|
|
|
178 |
img_input, api_key, style, creativity,
|
179 |
neg_prompt, aspect, color_mode, dpi
|
180 |
],
|
181 |
+
outputs=[prompt_output, quality_report, token_stats, status_msg],
|
182 |
api_name="generate"
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
)
|
184 |
|
185 |
copy_btn.click(
|