hansoneze commited on
Commit
1214215
·
1 Parent(s): 52c0085

Switch to Gemini + update requirements

Browse files
Files changed (1) hide show
  1. app.py +76 -11
app.py CHANGED
@@ -221,7 +221,7 @@ with gr.Blocks(css="""
221
  # ---------------------------
222
  # Theme Toggle
223
  # ---------------------------
224
- theme_state = gr.State("light") # default theme
225
 
226
  def toggle_theme(current):
227
  return "dark" if current == "light" else "light"
@@ -229,7 +229,7 @@ with gr.Blocks(css="""
229
  with gr.Row():
230
  with gr.Column():
231
  gr.Markdown("<h1 style='text-align:center; font-size:2.2em;'>✨ SnapLift – AI Social Media Booster</h1>")
232
- gr.Markdown("<p style='text-align:center; font-size:1.1em; color:#555;'>Upload your product photo, replace background, and auto-generate <b>marketing captions</b> + <b>hashtags</b>!</p>")
233
  with gr.Column(scale=0.2):
234
  theme_btn = gr.Button("🌙 Toggle Theme")
235
 
@@ -240,10 +240,14 @@ with gr.Blocks(css="""
240
  # ---------------------------
241
  with gr.Tab("📸 Image Editor"):
242
  with gr.Row(equal_height=True):
243
- # ---- Left Column: Inputs & Controls ----
244
  with gr.Column(scale=1):
245
  with gr.Group(elem_classes="box"):
246
- input_img = gr.Image(type="pil", label="📤 Upload Main Photo")
 
 
 
 
 
247
 
248
  with gr.Accordion("🎨 Background Options", open=True):
249
  bg_choices = gr.Dropdown(
@@ -251,7 +255,12 @@ with gr.Blocks(css="""
251
  value=(os.listdir(BG_DIR)[0] if os.listdir(BG_DIR) else None),
252
  label="Choose Background"
253
  )
254
- bg_upload = gr.Image(type="pil", label="Or Upload Your Background")
 
 
 
 
 
255
  bg_preview = gr.Image(type="pil", label="Background Preview", interactive=False)
256
 
257
  def load_bg(choice):
@@ -277,7 +286,26 @@ with gr.Blocks(css="""
277
  blend_strength = gr.Slider(0, 1, value=0.5, step=0.1, label="Blending Strength")
278
  blur_background = gr.Checkbox(label="Blur Background", value=False)
279
 
280
- btn = gr.Button("🚀 Generate New Photo", elem_classes="box")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281
 
282
  # ---- Right Column: Output ----
283
  with gr.Column(scale=1):
@@ -288,12 +316,49 @@ with gr.Blocks(css="""
288
  elem_id="output-img",
289
  interactive=False
290
  )
291
- download_btn = gr.File(label="⬇️ Download Final Image")
292
-
293
- # Bind button to function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
  btn.click(
295
- fn=process_image,
296
- inputs=[input_img, bg_choices, bg_upload, logo_upload, logo_transparency, logo_position, blur_background, blend_strength],
297
  outputs=[output_img, download_btn]
298
  )
299
 
 
221
  # ---------------------------
222
  # Theme Toggle
223
  # ---------------------------
224
+ theme_state = gr.State("light")
225
 
226
  def toggle_theme(current):
227
  return "dark" if current == "light" else "light"
 
229
  with gr.Row():
230
  with gr.Column():
231
  gr.Markdown("<h1 style='text-align:center; font-size:2.2em;'>✨ SnapLift – AI Social Media Booster</h1>")
232
+ gr.Markdown("<p style='text-align:center; font-size:1.1em; color:#555;'>Upload or capture your product photo, replace background, and auto-generate <b>marketing captions</b> + <b>hashtags</b>!</p>")
233
  with gr.Column(scale=0.2):
234
  theme_btn = gr.Button("🌙 Toggle Theme")
235
 
 
240
  # ---------------------------
241
  with gr.Tab("📸 Image Editor"):
242
  with gr.Row(equal_height=True):
 
243
  with gr.Column(scale=1):
244
  with gr.Group(elem_classes="box"):
245
+ input_img = gr.Image(
246
+ type="pil",
247
+ label="📤 Upload or Capture Main Photo",
248
+ sources=["upload", "camera"],
249
+ tool="editor"
250
+ )
251
 
252
  with gr.Accordion("🎨 Background Options", open=True):
253
  bg_choices = gr.Dropdown(
 
255
  value=(os.listdir(BG_DIR)[0] if os.listdir(BG_DIR) else None),
256
  label="Choose Background"
257
  )
258
+ bg_upload = gr.Image(
259
+ type="pil",
260
+ label="📤 Upload or Capture Background",
261
+ sources=["upload", "camera"],
262
+ tool="editor"
263
+ )
264
  bg_preview = gr.Image(type="pil", label="Background Preview", interactive=False)
265
 
266
  def load_bg(choice):
 
286
  blend_strength = gr.Slider(0, 1, value=0.5, step=0.1, label="Blending Strength")
287
  blur_background = gr.Checkbox(label="Blur Background", value=False)
288
 
289
+ with gr.Accordion("💾 Export Options", open=True):
290
+ export_format = gr.Dropdown(
291
+ ["PNG", "JPG", "PDF"],
292
+ value="PNG",
293
+ label="Export Format"
294
+ )
295
+ export_size = gr.Dropdown(
296
+ [
297
+ "Original",
298
+ "Instagram (1080x1080)",
299
+ "Facebook (1200x628)",
300
+ "TikTok (1080x1920)",
301
+ "LinkedIn (1200x1200)",
302
+ "Twitter (1600x900)"
303
+ ],
304
+ value="Original",
305
+ label="Social Media Size"
306
+ )
307
+
308
+ btn = gr.Button("🚀 Generate & Export", elem_classes="box")
309
 
310
  # ---- Right Column: Output ----
311
  with gr.Column(scale=1):
 
316
  elem_id="output-img",
317
  interactive=False
318
  )
319
+ download_btn = gr.File(label="⬇️ Download HD Export")
320
+
321
+ # ---------------------------
322
+ # Wrapper to handle export formats + resizing
323
+ # ---------------------------
324
+ def process_and_export(input_img, bg_choice, bg_upload, logo_upload, logo_transparency, logo_position, blur_background, blend_strength, export_format, export_size):
325
+ result = process_image(input_img, bg_choice, bg_upload, logo_upload, logo_transparency, logo_position, blur_background, blend_strength)
326
+ if isinstance(result, list):
327
+ result = result[0]
328
+
329
+ img = Image.open(result)
330
+
331
+ # Resize if needed
332
+ size_map = {
333
+ "Instagram (1080x1080)": (1080, 1080),
334
+ "Facebook (1200x628)": (1200, 628),
335
+ "TikTok (1080x1920)": (1080, 1920),
336
+ "LinkedIn (1200x1200)": (1200, 1200),
337
+ "Twitter (1600x900)": (1600, 900)
338
+ }
339
+ if export_size in size_map:
340
+ img = img.resize(size_map[export_size], Image.LANCZOS)
341
+
342
+ # Prepare export path
343
+ timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
344
+ if export_format == "PNG":
345
+ out_path = os.path.join(RESULTS_DIR, f"export_{timestamp}.png")
346
+ img.save(out_path, "PNG", quality=95)
347
+ elif export_format == "JPG":
348
+ out_path = os.path.join(RESULTS_DIR, f"export_{timestamp}.jpg")
349
+ img.convert("RGB").save(out_path, "JPEG", quality=95)
350
+ elif export_format == "PDF":
351
+ out_path = os.path.join(RESULTS_DIR, f"export_{timestamp}.pdf")
352
+ img.convert("RGB").save(out_path, "PDF", resolution=300.0)
353
+ else:
354
+ out_path = result
355
+
356
+ return result, out_path
357
+
358
+ # Bind button
359
  btn.click(
360
+ fn=process_and_export,
361
+ inputs=[input_img, bg_choices, bg_upload, logo_upload, logo_transparency, logo_position, blur_background, blend_strength, export_format, export_size],
362
  outputs=[output_img, download_btn]
363
  )
364