hansoneze commited on
Commit
0a551d8
·
1 Parent(s): 17f2aa0

Fix Gallery component for Gradio compatibility

Browse files
Files changed (1) hide show
  1. app.py +15 -16
app.py CHANGED
@@ -3,7 +3,7 @@ from datetime import datetime
3
  from PIL import Image
4
  import gradio as gr
5
  from rembg import remove
6
- import openai
7
 
8
  # ---------------------------
9
  # CONFIG
@@ -17,8 +17,8 @@ LIFETIME = 24 * 60 * 60 # 24 hours
17
  os.makedirs(UPLOAD_DIR, exist_ok=True)
18
  os.makedirs(RESULTS_DIR, exist_ok=True)
19
 
20
- # Set OpenAI API key (must be added in Hugging Face Secrets as OPENAI_API_KEY)
21
- openai.api_key = os.getenv("OPENAI_API_KEY")
22
 
23
  # ---------------------------
24
  # HELPERS
@@ -40,8 +40,10 @@ def replace_background(input_path, bg_choice):
40
  check_size(input_path)
41
  input_img = Image.open(input_path).convert("RGBA")
42
  fg = remove(input_img)
 
43
  bg_path = os.path.join(BG_DIR, bg_choice)
44
  bg = Image.open(bg_path).convert("RGBA").resize(fg.size)
 
45
  result = Image.alpha_composite(bg, fg)
46
  timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
47
  result_path = os.path.join(RESULTS_DIR, f"result_{timestamp}.png")
@@ -52,18 +54,15 @@ def replace_background(input_path, bg_choice):
52
  # ---------------------------
53
  # IMAGE PROCESSOR
54
  # ---------------------------
55
- def process_image(input_img, bg_choices):
56
  if input_img is None:
57
  return []
 
58
  temp_path = os.path.join(UPLOAD_DIR, f"upload_{int(time.time())}.png")
59
  input_img.save(temp_path)
60
 
61
- results = []
62
- for bg_choice in bg_choices: # Loop through all selected backgrounds
63
- result_path = replace_background(temp_path, bg_choice)
64
- results.append(result_path)
65
-
66
- return results
67
 
68
  # ---------------------------
69
  # CAPTION GENERATOR
@@ -76,7 +75,7 @@ def generate_caption(prompt="Promote my product"):
76
  "Format output clearly as:\nInstagram:\nFacebook:\nTikTok:\n"
77
  )
78
 
79
- response = openai.chat.completions.create(
80
  model="gpt-3.5-turbo",
81
  messages=[{"role": "user", "content": full_prompt}],
82
  temperature=0.7,
@@ -100,13 +99,13 @@ with gr.Blocks(css="footer {display:none !important}") as demo:
100
  with gr.Tab("📸 Image Editor"):
101
  with gr.Row():
102
  input_img = gr.Image(type="pil", label="Upload Photo")
103
- bg_choices = gr.CheckboxGroup(
104
  choices=os.listdir(BG_DIR),
105
- value=[os.listdir(BG_DIR)[0]],
106
- label="Choose Background(s)"
107
  )
108
- btn = gr.Button("✨ Generate New Photos")
109
- output_imgs = gr.Gallery(label="Generated Images", elem_id="gallery", columns=3, rows=2)
110
  btn.click(fn=process_image, inputs=[input_img, bg_choices], outputs=output_imgs)
111
 
112
  # Caption Generator Tab
 
3
  from PIL import Image
4
  import gradio as gr
5
  from rembg import remove
6
+ from openai import OpenAI
7
 
8
  # ---------------------------
9
  # CONFIG
 
17
  os.makedirs(UPLOAD_DIR, exist_ok=True)
18
  os.makedirs(RESULTS_DIR, exist_ok=True)
19
 
20
+ # OpenAI API key (must be added in Hugging Face Secrets as OPENAI_API_KEY)
21
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
22
 
23
  # ---------------------------
24
  # HELPERS
 
40
  check_size(input_path)
41
  input_img = Image.open(input_path).convert("RGBA")
42
  fg = remove(input_img)
43
+
44
  bg_path = os.path.join(BG_DIR, bg_choice)
45
  bg = Image.open(bg_path).convert("RGBA").resize(fg.size)
46
+
47
  result = Image.alpha_composite(bg, fg)
48
  timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
49
  result_path = os.path.join(RESULTS_DIR, f"result_{timestamp}.png")
 
54
  # ---------------------------
55
  # IMAGE PROCESSOR
56
  # ---------------------------
57
+ def process_image(input_img, bg_choice):
58
  if input_img is None:
59
  return []
60
+
61
  temp_path = os.path.join(UPLOAD_DIR, f"upload_{int(time.time())}.png")
62
  input_img.save(temp_path)
63
 
64
+ result_path = replace_background(temp_path, bg_choice)
65
+ return [result_path]
 
 
 
 
66
 
67
  # ---------------------------
68
  # CAPTION GENERATOR
 
75
  "Format output clearly as:\nInstagram:\nFacebook:\nTikTok:\n"
76
  )
77
 
78
+ response = client.chat.completions.create(
79
  model="gpt-3.5-turbo",
80
  messages=[{"role": "user", "content": full_prompt}],
81
  temperature=0.7,
 
99
  with gr.Tab("📸 Image Editor"):
100
  with gr.Row():
101
  input_img = gr.Image(type="pil", label="Upload Photo")
102
+ bg_choices = gr.Dropdown(
103
  choices=os.listdir(BG_DIR),
104
+ value=os.listdir(BG_DIR)[0] if os.listdir(BG_DIR) else None,
105
+ label="Choose Background"
106
  )
107
+ btn = gr.Button("✨ Generate New Photo")
108
+ output_imgs = gr.Gallery(label="Generated Image", elem_id="gallery", columns=1, rows=1)
109
  btn.click(fn=process_image, inputs=[input_img, bg_choices], outputs=output_imgs)
110
 
111
  # Caption Generator Tab