mroccuper commited on
Commit
3bed1b9
Β·
verified Β·
1 Parent(s): bc1dcdf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
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 {"error": "⚠️ Please upload an image."}, None, None
56
 
57
  api_key = api_key or GEMINI_KEY
58
  if not api_key:
59
- return {"error": "πŸ”‘ API key required - set in env (GEMINI_KEY) or input field."}, None, None
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 {"error": "🚫 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 {"error": "πŸ” Invalid API key or authentication error"}, None, None
71
  else:
72
- return {"error": f"βš™οΈ API initialization error: {str(e)}"}, None, None
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 {"error": 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 {"error": "πŸ› οΈ Invalid style selected. Please choose from available options."}, None, None
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 {"error": f"πŸ€– Prompt generation failed: {str(e)}"}, None, None
99
 
100
- # Step 6: Quality & Token Stats
101
- validation = {"score": 8, "issues": [], "suggestions": []}
102
  input_tokens = len(img_b64) // 4
103
  output_tokens = len(raw_prompt.split())
104
 
105
- return {
106
- "prompt": raw_prompt,
107
- "validation": validation,
108
- "stats": {"input": input_tokens, "output": output_tokens}
109
- }, validation, {"tokens": f"Input: {input_tokens}, Output: {output_tokens}"}
110
 
111
  except Exception as e:
112
  traceback.print_exc()
113
- return {"error": f"πŸ’₯ Unexpected error: {str(e)}"}, None, None
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
- ).then(
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(