Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -52,11 +52,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
|
| 56 |
|
| 57 |
api_key = api_key or GEMINI_KEY
|
| 58 |
if not api_key:
|
| 59 |
-
return
|
| 60 |
|
| 61 |
# Step 2: Gemini Setup
|
| 62 |
try:
|
|
@@ -64,12 +64,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
|
| 68 |
except Exception as e:
|
| 69 |
if "authentication" in str(e).lower():
|
| 70 |
-
return
|
| 71 |
else:
|
| 72 |
-
return
|
| 73 |
|
| 74 |
# Step 3: Preprocess Image
|
| 75 |
try:
|
|
@@ -78,14 +78,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
|
| 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
|
| 89 |
|
| 90 |
# Step 5: Call Gemini API
|
| 91 |
try:
|
|
@@ -95,22 +95,20 @@ 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
|
| 99 |
|
| 100 |
-
# Step 6:
|
| 101 |
-
validation = {"score": 8, "issues": [], "suggestions": []}
|
| 102 |
input_tokens = len(img_b64) // 4
|
| 103 |
output_tokens = len(raw_prompt.split())
|
| 104 |
|
| 105 |
-
return {
|
| 106 |
-
"
|
| 107 |
-
"
|
| 108 |
-
|
| 109 |
-
}, validation, {"tokens": f"Input: {input_tokens}, Output: {output_tokens}"}
|
| 110 |
|
| 111 |
except Exception as e:
|
| 112 |
traceback.print_exc()
|
| 113 |
-
return
|
| 114 |
|
| 115 |
|
| 116 |
# --- Modern Copy Function ---
|
|
@@ -182,9 +180,12 @@ def build_interface():
|
|
| 182 |
],
|
| 183 |
outputs=[prompt_output, quality_report, token_stats],
|
| 184 |
api_name="generate"
|
| 185 |
-
).
|
| 186 |
fn=lambda: "β
Prompt generated successfully!",
|
| 187 |
outputs=status_msg
|
|
|
|
|
|
|
|
|
|
| 188 |
)
|
| 189 |
|
| 190 |
copy_btn.click(
|
|
|
|
| 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 |
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", None, None
|
| 68 |
except Exception as e:
|
| 69 |
if "authentication" in str(e).lower():
|
| 70 |
+
return "π Invalid API key or authentication error", None, None
|
| 71 |
else:
|
| 72 |
+
return f"βοΈ API initialization error: {str(e)}", None, None
|
| 73 |
|
| 74 |
# Step 3: Preprocess Image
|
| 75 |
try:
|
|
|
|
| 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)}", None, None
|
| 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.", None, None
|
| 89 |
|
| 90 |
# Step 5: Call Gemini API
|
| 91 |
try:
|
|
|
|
| 95 |
)
|
| 96 |
raw_prompt = response.text
|
| 97 |
except Exception as e:
|
| 98 |
+
return f"π€ Prompt generation failed: {str(e)}", None, None
|
| 99 |
|
| 100 |
+
# Step 6: Token Stats
|
|
|
|
| 101 |
input_tokens = len(img_b64) // 4
|
| 102 |
output_tokens = len(raw_prompt.split())
|
| 103 |
|
| 104 |
+
return raw_prompt, {"score": 8, "issues": [], "suggestions": []}, {
|
| 105 |
+
"Input Tokens": input_tokens,
|
| 106 |
+
"Output Tokens": output_tokens
|
| 107 |
+
}
|
|
|
|
| 108 |
|
| 109 |
except Exception as e:
|
| 110 |
traceback.print_exc()
|
| 111 |
+
return f"π₯ Unexpected error: {str(e)}", None, None
|
| 112 |
|
| 113 |
|
| 114 |
# --- Modern Copy Function ---
|
|
|
|
| 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(
|