comdoleger commited on
Commit
c8789f0
1 Parent(s): dc396dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -11
app.py CHANGED
@@ -13,7 +13,29 @@ from enum import Enum
13
 
14
  api_key = os.getenv("FAI_API_KEY")
15
  api = os.getenv("FAI_API")
 
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  def image_to_base64(image):
19
  # Open the image file
@@ -113,8 +135,17 @@ def resize_to_fit(max_size, original_size):
113
  return new_width, new_height
114
 
115
 
116
- def process_generate(fore, prompt, intensity, mode, refprompt, bg):
117
-
 
 
 
 
 
 
 
 
 
118
  fore = create_square_image(fore)
119
  size = fore.size
120
  image_width = size[0]
@@ -127,7 +158,8 @@ def process_generate(fore, prompt, intensity, mode, refprompt, bg):
127
  gr.Warning("ℹ️ The input image size is too big, I will lower it!")
128
  image_width, image_height = resize_to_fit((1500,1500), (image_width, image_height))
129
 
130
-
 
131
 
132
  forestr = image_to_base64(fore.convert("RGBA"))
133
  data = {
@@ -156,6 +188,8 @@ def process_generate(fore, prompt, intensity, mode, refprompt, bg):
156
 
157
  return image
158
 
 
 
159
  def update_value(val):
160
  return val
161
 
@@ -210,10 +244,7 @@ with block:
210
  value=Stage.FULL.value,
211
  label="Generation Mode", type='value')
212
  mode.change(fn=update_value, inputs=mode, outputs=mode)
213
- with gr.Column():
214
- bg = gr.Checkbox(info="Remove Backgroung")
215
- bg.change(fn=update_value, inputs=bg, outputs=bg)
216
-
217
  with gr.Column():
218
  gr.HTML('''
219
  <div>
@@ -226,12 +257,13 @@ with block:
226
  with gr.Row():
227
  intensity = gr.Slider(label="Refiner Strength", minimum=1.0, maximum=7.0, value=3.0, step=0.5)
228
  intensity.change(fn=update_value, inputs=intensity, outputs=intensity)
 
 
229
  generate_button = gr.Button(value="Generate")
230
 
231
-
232
-
233
-
234
- ips = [fore, prompt, intensity, mode, refprompt, bg]
235
  generate_button.click(fn=process_generate, inputs=ips, outputs=[result_gallery])
236
 
237
 
 
13
 
14
  api_key = os.getenv("FAI_API_KEY")
15
  api = os.getenv("FAI_API")
16
+ rmbgkey = os.getenv("RMBGKEY")
17
 
18
+ def rmbg(pil_image):
19
+
20
+ # Convert PIL image to bytes
21
+ image_bytes = BytesIO()
22
+ pil_image.save(image_bytes, format='PNG')
23
+ image_bytes.seek(0)
24
+
25
+ # Send the image to the remove.bg API
26
+ response = requests.post(
27
+ 'https://api.remove.bg/v1.0/removebg',
28
+ files={'image_file': ('filename.png', image_bytes, 'image/png')},
29
+ data={'size': 'auto'},
30
+ headers={'X-Api-Key': rmbgkey}
31
+ )
32
+
33
+ if response.status_code == 200:
34
+ # Convert the bytes response to a PIL image
35
+ result_image = Image.open(BytesIO(response.content))
36
+ return result_image
37
+ else:
38
+ return None
39
 
40
  def image_to_base64(image):
41
  # Open the image file
 
135
  return new_width, new_height
136
 
137
 
138
+ def process_generate(fore, prompt, intensity, mode, refprompt, isrmbg):
139
+
140
+ if isrmbg:
141
+ try:
142
+ rmbgfore = rmbg(fore)
143
+ print(f"Background removed!")
144
+ if rmbgfore:
145
+ fore = rmbgfore
146
+ except:
147
+ pass
148
+
149
  fore = create_square_image(fore)
150
  size = fore.size
151
  image_width = size[0]
 
158
  gr.Warning("ℹ️ The input image size is too big, I will lower it!")
159
  image_width, image_height = resize_to_fit((1500,1500), (image_width, image_height))
160
 
161
+
162
+
163
 
164
  forestr = image_to_base64(fore.convert("RGBA"))
165
  data = {
 
188
 
189
  return image
190
 
191
+
192
+
193
  def update_value(val):
194
  return val
195
 
 
244
  value=Stage.FULL.value,
245
  label="Generation Mode", type='value')
246
  mode.change(fn=update_value, inputs=mode, outputs=mode)
247
+
 
 
 
248
  with gr.Column():
249
  gr.HTML('''
250
  <div>
 
257
  with gr.Row():
258
  intensity = gr.Slider(label="Refiner Strength", minimum=1.0, maximum=7.0, value=3.0, step=0.5)
259
  intensity.change(fn=update_value, inputs=intensity, outputs=intensity)
260
+ isrmbg = gr.Checkbox(label="Remove Background")
261
+ isrmbg.change(fn=update_value, inputs=isrmbg, outputs=isrmbg)
262
  generate_button = gr.Button(value="Generate")
263
 
264
+
265
+
266
+ ips = [fore, prompt, intensity, mode, refprompt, isrmbg]
 
267
  generate_button.click(fn=process_generate, inputs=ips, outputs=[result_gallery])
268
 
269