ssboost commited on
Commit
2e87213
Β·
verified Β·
1 Parent(s): a0c634e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -11
app.py CHANGED
@@ -13,7 +13,30 @@ REPLICATE_API_TOKEN = os.getenv("REPLICATE_API_TOKEN")
13
  if REPLICATE_API_TOKEN:
14
  os.environ["REPLICATE_API_TOKEN"] = REPLICATE_API_TOKEN
15
 
16
- def upload_image_to_temp_url(image):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  """이미지λ₯Ό μž„μ‹œ URL둜 μ—…λ‘œλ“œν•˜λŠ” ν•¨μˆ˜ (μ‹€μ œλ‘œλŠ” base64 인코딩)"""
18
  if image is None:
19
  return None
@@ -169,7 +192,7 @@ def process_with_ai_enhancement(image_path, prompt_option):
169
  except Exception as e:
170
  return None, f"AI 처리 쀑 였λ₯˜: {str(e)}"
171
 
172
- def create_coupang_thumbnail(image, apply_shadow, apply_angle_change, angle_option, ai_enhancement, prompt_option):
173
  """쿠팑 썸넀일 생성 메인 ν•¨μˆ˜"""
174
  if image is None:
175
  return None, "이미지λ₯Ό μ—…λ‘œλ“œν•΄μ£Όμ„Έμš”."
@@ -178,6 +201,20 @@ def create_coupang_thumbnail(image, apply_shadow, apply_angle_change, angle_opti
178
  result_image = image.copy()
179
  status_messages = []
180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  # 1단계: μƒν’ˆ 각도 λ³€κ²½ (μ•„μ΄ν…œ μœ„λ„ˆ νšŒν”Ό)
182
  if apply_angle_change and REPLICATE_API_TOKEN:
183
  temp_path = upload_image_to_temp_url(result_image)
@@ -239,11 +276,12 @@ def create_coupang_thumbnail(image, apply_shadow, apply_angle_change, angle_opti
239
  def create_interface():
240
  with gr.Blocks(title="πŸ›’ 쿠팑 썸넀일 생성기", theme=gr.themes.Soft()) as iface:
241
  gr.Markdown("""
242
- # πŸ›’ 쿠팑 썸넀일 생성기
243
 
244
- 배경이 제거된 μƒν’ˆ 이미지λ₯Ό μ—…λ‘œλ“œν•˜λ©΄ 쿠팑 κ·œμ •μ— λ§žλŠ” μΈλ„€μΌλ‘œ μžλ™ λ³€ν™˜ν•΄λ“œλ¦½λ‹ˆλ‹€!
245
 
246
- ## πŸ’‘ μ μš©λ˜λŠ” 쿠팑 κ·œμ • λŒ€μ‘ μ „λž΅:
 
247
  - βœ… **흰 λ°°κ²½ + 85% κ·œμΉ™** μ™„λ²½ μ€€μˆ˜
248
  - βœ… **AI λ§ˆλ²• ν”„λ‘¬ν”„νŠΈ**둜 ν™”μ§ˆ ν–₯상
249
  - βœ… **μƒν’ˆ 각도 λ³€κ²½**으둜 μ•„μ΄ν…œ μœ„λ„ˆ(쀑볡) νšŒν”Ό
@@ -254,13 +292,21 @@ def create_interface():
254
  with gr.Column(scale=1):
255
  gr.Markdown("### πŸ“€ 이미지 μ—…λ‘œλ“œ")
256
  input_image = gr.Image(
257
- label="λ°°κ²½ 제거된 μƒν’ˆ 이미지",
258
  type="pil",
259
  height=300
260
  )
261
 
262
  gr.Markdown("### βš™οΈ μ˜΅μ…˜ μ„€μ •")
263
 
 
 
 
 
 
 
 
 
264
  with gr.Group():
265
  gr.Markdown("**🎨 효과 μ˜΅μ…˜**")
266
  apply_shadow = gr.Checkbox(
@@ -312,11 +358,15 @@ def create_interface():
312
  gr.Markdown("""
313
  ---
314
  ## πŸ“‹ μ‚¬μš© 방법
315
- 1. **배경이 제거된 μƒν’ˆ 이미지**λ₯Ό μ—…λ‘œλ“œν•˜μ„Έμš”
316
- 2. **μƒν’ˆ 각도 λ³€κ²½ μ˜΅μ…˜**을 μ„ νƒν•˜μ—¬ μ•„μ΄ν…œ μœ„λ„ˆ(쀑볡)λ₯Ό νšŒν”Όν•˜μ„Έμš”
317
- 3. μ›ν•˜λŠ” **효과 μ˜΅μ…˜**을 μ„ νƒν•˜μ„Έμš”
318
- 4. **AI ν–₯상**을 μ‚¬μš©ν•˜λ €λ©΄ Replicate API 토큰을 ν™˜κ²½λ³€μˆ˜λ‘œ μ„€μ •ν•˜μ„Έμš”
319
- 5. **'쿠팑 썸넀일 생성'** λ²„νŠΌμ„ ν΄λ¦­ν•˜μ„Έμš”
 
 
 
 
320
 
321
  ## 🎯 μƒν’ˆ 각도 λ³€κ²½ μ˜΅μ…˜ μ„€λͺ…
322
  - **μ™Όμͺ½ 45도**: μƒν’ˆμ„ μ™Όμͺ½μ—μ„œ 45도 κ°λ„λ‘œ μ΄¬μ˜ν•œ λŠλ‚Œ
@@ -343,6 +393,7 @@ def create_interface():
343
  create_coupang_thumbnail,
344
  inputs=[
345
  input_image,
 
346
  apply_shadow,
347
  apply_angle_change,
348
  angle_option,
 
13
  if REPLICATE_API_TOKEN:
14
  os.environ["REPLICATE_API_TOKEN"] = REPLICATE_API_TOKEN
15
 
16
+ def remove_background_with_ai(image_path):
17
+ """AIλ₯Ό μ΄μš©ν•œ λ°°κ²½ 제거"""
18
+ if not REPLICATE_API_TOKEN:
19
+ return None, "Replicate API 토큰이 μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€."
20
+
21
+ try:
22
+ # Replicate Background Remover API 호좜
23
+ output = replicate.run(
24
+ "851-labs/background-remover:a029dff38972b5fda4ec5d75d7d1cd25aeff621d2cf4946a41055d7db66b80bc",
25
+ input={
26
+ "image": open(image_path, "rb")
27
+ }
28
+ )
29
+
30
+ # κ²°κ³Ό 이미지 λ‹€μš΄λ‘œλ“œ
31
+ response = requests.get(output)
32
+ if response.status_code == 200:
33
+ bg_removed_image = Image.open(io.BytesIO(response.content))
34
+ return bg_removed_image, "λ°°κ²½ 제거 μ™„λ£Œ"
35
+ else:
36
+ return None, "λ°°κ²½ 제거 μ‹€νŒ¨"
37
+
38
+ except Exception as e:
39
+ return None, f"λ°°κ²½ 제거 쀑 였λ₯˜: {str(e)}"
40
  """이미지λ₯Ό μž„μ‹œ URL둜 μ—…λ‘œλ“œν•˜λŠ” ν•¨μˆ˜ (μ‹€μ œλ‘œλŠ” base64 인코딩)"""
41
  if image is None:
42
  return None
 
192
  except Exception as e:
193
  return None, f"AI 처리 쀑 였λ₯˜: {str(e)}"
194
 
195
+ def create_coupang_thumbnail(image, auto_remove_bg, apply_shadow, apply_angle_change, angle_option, ai_enhancement, prompt_option):
196
  """쿠팑 썸넀일 생성 메인 ν•¨μˆ˜"""
197
  if image is None:
198
  return None, "이미지λ₯Ό μ—…λ‘œλ“œν•΄μ£Όμ„Έμš”."
 
201
  result_image = image.copy()
202
  status_messages = []
203
 
204
+ # 0단계: μžλ™ λ°°κ²½ 제거 (선택사항)
205
+ if auto_remove_bg and REPLICATE_API_TOKEN:
206
+ temp_path = upload_image_to_temp_url(result_image)
207
+ if temp_path:
208
+ bg_removed_img, bg_msg = remove_background_with_ai(temp_path)
209
+ if bg_removed_img:
210
+ result_image = bg_removed_img
211
+ status_messages.append(f"βœ… μžλ™ λ°°κ²½ 제거: {bg_msg}")
212
+ else:
213
+ status_messages.append(f"❌ 배경 제거: {bg_msg}")
214
+ os.unlink(temp_path) # μž„μ‹œ 파일 μ‚­μ œ
215
+ elif auto_remove_bg and not REPLICATE_API_TOKEN:
216
+ status_messages.append("⚠️ λ°°κ²½ μ œκ±°μ—λŠ” Replicate API 토큰이 ν•„μš”ν•©λ‹ˆλ‹€")
217
+
218
  # 1단계: μƒν’ˆ 각도 λ³€κ²½ (μ•„μ΄ν…œ μœ„λ„ˆ νšŒν”Ό)
219
  if apply_angle_change and REPLICATE_API_TOKEN:
220
  temp_path = upload_image_to_temp_url(result_image)
 
276
  def create_interface():
277
  with gr.Blocks(title="πŸ›’ 쿠팑 썸넀일 생성기", theme=gr.themes.Soft()) as iface:
278
  gr.Markdown("""
279
+ # 쿠팑 썸넀일 생성기 - Gradio 5.0+
280
 
281
+ 일반 μƒν’ˆ 이미지λ₯Ό μ—…λ‘œλ“œν•˜λ©΄ **μžλ™ λ°°κ²½ 제거 β†’ 쿠팑 κ·œμ • μ€€μˆ˜ 썸넀일**둜 ν•œ λ²ˆμ— λ³€ν™˜ν•΄λ“œλ¦½λ‹ˆλ‹€!
282
 
283
+ ## πŸ’‘ μ™„μ „ μžλ™ν™”λœ 쿠팑 κ·œμ • λŒ€μ‘:
284
+ - 🎯 **μžλ™ λ°°κ²½ 제거** (AI 기반)
285
  - βœ… **흰 λ°°κ²½ + 85% κ·œμΉ™** μ™„λ²½ μ€€μˆ˜
286
  - βœ… **AI λ§ˆλ²• ν”„λ‘¬ν”„νŠΈ**둜 ν™”μ§ˆ ν–₯상
287
  - βœ… **μƒν’ˆ 각도 λ³€κ²½**으둜 μ•„μ΄ν…œ μœ„λ„ˆ(쀑볡) νšŒν”Ό
 
292
  with gr.Column(scale=1):
293
  gr.Markdown("### πŸ“€ 이미지 μ—…λ‘œλ“œ")
294
  input_image = gr.Image(
295
+ label="μƒν’ˆ 이미지 (λ°°κ²½ μžˆμ–΄λ„ OK!)",
296
  type="pil",
297
  height=300
298
  )
299
 
300
  gr.Markdown("### βš™οΈ μ˜΅μ…˜ μ„€μ •")
301
 
302
+ with gr.Group():
303
+ gr.Markdown("**πŸ”§ μ „μ²˜λ¦¬ μ˜΅μ…˜**")
304
+ auto_remove_bg = gr.Checkbox(
305
+ label="μžλ™ λ°°κ²½ 제거",
306
+ value=True,
307
+ info="AI둜 배경을 μžλ™ 제거 (Replicate API ν•„μš”)"
308
+ )
309
+
310
  with gr.Group():
311
  gr.Markdown("**🎨 효과 μ˜΅μ…˜**")
312
  apply_shadow = gr.Checkbox(
 
358
  gr.Markdown("""
359
  ---
360
  ## πŸ“‹ μ‚¬μš© 방법
361
+ 1. **일반 μƒν’ˆ 이미지**λ₯Ό μ—…λ‘œλ“œν•˜μ„Έμš” (λ°°κ²½ μžˆμ–΄λ„ OK!)
362
+ 2. **μžλ™ λ°°κ²½ 제거** μ˜΅μ…˜μ„ μΌœλ‘μ„Έμš” (κΈ°λ³Έκ°’)
363
+ 3. **μƒν’ˆ 각도 λ³€κ²½ μ˜΅μ…˜**을 μ„ νƒν•˜μ—¬ μ•„μ΄ν…œ μœ„λ„ˆ(쀑볡)λ₯Ό νšŒν”Όν•˜μ„Έμš”
364
+ 4. μ›ν•˜λŠ” **효과 μ˜΅μ…˜**을 μ„ νƒν•˜μ„Έμš”
365
+ 5. **Replicate API 토큰**을 ν™˜κ²½λ³€μˆ˜λ‘œ μ„€μ •ν•˜μ„Έμš” (ν•„μˆ˜)
366
+ 6. **'쿠팑 썸넀일 생성'** λ²„νŠΌμ„ ν΄λ¦­ν•˜μ„Έμš”
367
+
368
+ ## πŸ”„ μ™„μ „ μžλ™ν™” μ›Œν¬ν”Œλ‘œμš°
369
+ **원본 이미지** β†’ **λ°°κ²½ 제거** β†’ **각도 λ³€κ²½** β†’ **ν’ˆμ§ˆ ν–₯상** β†’ **쿠팑 κ·œμ • 썸넀일**
370
 
371
  ## 🎯 μƒν’ˆ 각도 λ³€κ²½ μ˜΅μ…˜ μ„€λͺ…
372
  - **μ™Όμͺ½ 45도**: μƒν’ˆμ„ μ™Όμͺ½μ—μ„œ 45도 κ°λ„λ‘œ μ΄¬μ˜ν•œ λŠλ‚Œ
 
393
  create_coupang_thumbnail,
394
  inputs=[
395
  input_image,
396
+ auto_remove_bg,
397
  apply_shadow,
398
  apply_angle_change,
399
  angle_option,