yizhangliu commited on
Commit
23e2e2a
β€’
1 Parent(s): 12416bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -16
app.py CHANGED
@@ -13,7 +13,7 @@ import time
13
  import imghdr
14
  from loguru import logger
15
 
16
- from share_btn import community_icon_html, loading_icon_html, share_js
17
 
18
  from lama_cleaner.model_manager import ModelManager
19
  from lama_cleaner.schema import Config
@@ -192,19 +192,26 @@ def predict(input, img_enhancer):
192
  image = np.array(image_pil)
193
  mask = np.array(mask_pil.convert("L"))
194
  output = model_process(image, mask, img_enhancer)
195
- return output, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
196
 
197
-
198
- css = '''
199
- .container {max-width: 100%;margin: auto;padding-top: 1.5rem}
200
  #begin-btn {color: blue; font-size:20px;}
201
  #work-container {min-width: min(160px, 100%) !important;flex-grow: 0 !important}
202
- #image_output{margin: 0 auto; text-align: center;width:640px}
203
- #erase-container{margin: 0 auto; text-align: center;width:150px;border-width:5px;border-color:#2c9748}
 
 
204
  #enhancer-checkbox{width:520px}
205
  #enhancer-tip{width:450px}
206
  #enhancer-tip-div{text-align: left}
207
- #prompt-container{margin: 0 auto; text-align: center;width:fit-content;min-width: min(150px, 100%);flex-grow: 0; flex-wrap: nowrap;}
 
 
 
 
 
 
208
  #image_upload .touch-none{display: flex}
209
  @keyframes spin {
210
  from {
@@ -239,6 +246,7 @@ set_page_elements = """async() => {
239
  return false;
240
  }
241
  }
 
242
  var gradioEl = document.querySelector('body > gradio-app').shadowRoot;
243
  if (!gradioEl) {
244
  gradioEl = document.querySelector('body > gradio-app');
@@ -249,16 +257,21 @@ set_page_elements = """async() => {
249
  const image_output = gradioEl.querySelectorAll('#image_output')[0];
250
  const data_image = gradioEl.querySelectorAll('#image_upload [data-testid="image"]')[0];
251
  const data_image_div = gradioEl.querySelectorAll('#image_upload [data-testid="image"] > div')[0];
 
252
  if (isMobile()) {
253
  const group1_width = group1.offsetWidth;
 
254
  image_upload.setAttribute('style', 'width:' + (group1_width - 13*2) + 'px; min-height:none;');
255
  data_image.setAttribute('style', 'width: ' + (group1_width - 14*2) + 'px;min-height:none;');
256
  data_image_div.setAttribute('style', 'width: ' + (group1_width - 14*2) + 'px;min-height:none;');
257
  image_output.setAttribute('style', 'width: ' + (group1_width - 13*2) + 'px;min-height:none;');
 
258
  const enhancer = gradioEl.querySelectorAll('#enhancer-checkbox')[0];
259
  enhancer.style.display = "none";
 
260
  const share_btn_container = gradioEl.querySelectorAll('#share-btn-container')[0];
261
  share_btn_container.setAttribute('style', 'width: 0px; height:0px;');
 
262
  } else {
263
  max_height = 800;
264
  image_upload.setAttribute('style', 'min-height: ' + max_height + 'px; overflow-x: overlay');
@@ -267,6 +280,45 @@ set_page_elements = """async() => {
267
  }
268
  group1.style.display = "none";
269
  group2.style.display = "block";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
  }"""
271
 
272
  image_blocks = gr.Blocks(css=css, title='Image Cleaner')
@@ -300,9 +352,9 @@ with image_blocks as demo:
300
  with gr.Column():
301
  image_input = gr.Image(source='upload', elem_id="image_upload",tool='sketch', type=f'{image_type}',
302
  label="Upload", show_label=False).style(mobile_collapse=False)
303
- with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
304
- with gr.Column(elem_id="erase-container"):
305
- btn_erase = gr.Button(value = "Erase(↓)",elem_id="erase_btn").style(
306
  margin=True,
307
  rounded=(True, True, True, True),
308
  full_width=True,
@@ -313,12 +365,16 @@ with image_blocks as demo:
313
  with gr.Row(elem_id="output-container"):
314
  with gr.Column():
315
  image_out = gr.Image(elem_id="image_output",label="Result", show_label=False)
316
- with gr.Group(elem_id="share-btn-container"):
317
- community_icon = gr.HTML(community_icon_html, visible=False)
318
- loading_icon = gr.HTML(loading_icon_html, visible=False)
319
- share_button = gr.Button("Share to community", elem_id="share-btn", visible=False)
 
 
 
320
 
321
- btn_erase.click(fn=predict, inputs=[image_input, img_enhancer], outputs=[image_out, community_icon, loading_icon, share_button])
 
322
  share_button.click(None, [], [], _js=share_js)
323
 
324
  begin_button.click(fn=None, inputs=[], outputs=[group_1, group_2], _js=set_page_elements)
 
13
  import imghdr
14
  from loguru import logger
15
 
16
+ from share_btn import community_icon_html, loading_icon_html, download_icon_html, share_js
17
 
18
  from lama_cleaner.model_manager import ModelManager
19
  from lama_cleaner.schema import Config
 
192
  image = np.array(image_pil)
193
  mask = np.array(mask_pil.convert("L"))
194
  output = model_process(image, mask, img_enhancer)
195
+ return output, gr.update(visible=True), gr.update(visible=True)
196
 
197
+ css = '''
198
+ .container {max-width: 1150px; margin: auto;padding-top: 1.5rem}
 
199
  #begin-btn {color: blue; font-size:20px;}
200
  #work-container {min-width: min(160px, 100%) !important;flex-grow: 0 !important}
201
+
202
+ #op-container{margin: 0 auto; text-align: center;width:fit-content;min-width: min(150px, 100%);flex-grow: 0; flex-wrap: nowrap;}
203
+ #erase-btn-container{margin: 0 auto; text-align: center;width:150px;border-width:5px;border-color:#2c9748}
204
+ #erase-btn {padding:0;}
205
  #enhancer-checkbox{width:520px}
206
  #enhancer-tip{width:450px}
207
  #enhancer-tip-div{text-align: left}
208
+
209
+ #image_output{margin: 0 auto; text-align: center;width:640px}
210
+
211
+ #download-container{margin: 0 auto; text-align: center;width:fit-content; min-width: min(150px, 100%);flex-grow: 0; flex-wrap: nowrap;}
212
+ #download-btn-container{margin: 0 auto; text-align: center;width:100px;border-width:1px;border-color:#2c9748}
213
+ #download-btn {padding:0;}
214
+
215
  #image_upload .touch-none{display: flex}
216
  @keyframes spin {
217
  from {
 
246
  return false;
247
  }
248
  }
249
+
250
  var gradioEl = document.querySelector('body > gradio-app').shadowRoot;
251
  if (!gradioEl) {
252
  gradioEl = document.querySelector('body > gradio-app');
 
257
  const image_output = gradioEl.querySelectorAll('#image_output')[0];
258
  const data_image = gradioEl.querySelectorAll('#image_upload [data-testid="image"]')[0];
259
  const data_image_div = gradioEl.querySelectorAll('#image_upload [data-testid="image"] > div')[0];
260
+
261
  if (isMobile()) {
262
  const group1_width = group1.offsetWidth;
263
+
264
  image_upload.setAttribute('style', 'width:' + (group1_width - 13*2) + 'px; min-height:none;');
265
  data_image.setAttribute('style', 'width: ' + (group1_width - 14*2) + 'px;min-height:none;');
266
  data_image_div.setAttribute('style', 'width: ' + (group1_width - 14*2) + 'px;min-height:none;');
267
  image_output.setAttribute('style', 'width: ' + (group1_width - 13*2) + 'px;min-height:none;');
268
+
269
  const enhancer = gradioEl.querySelectorAll('#enhancer-checkbox')[0];
270
  enhancer.style.display = "none";
271
+
272
  const share_btn_container = gradioEl.querySelectorAll('#share-btn-container')[0];
273
  share_btn_container.setAttribute('style', 'width: 0px; height:0px;');
274
+
275
  } else {
276
  max_height = 800;
277
  image_upload.setAttribute('style', 'min-height: ' + max_height + 'px; overflow-x: overlay');
 
280
  }
281
  group1.style.display = "none";
282
  group2.style.display = "block";
283
+
284
+ }"""
285
+
286
+ download_img = """async() => {
287
+ Date.prototype.Format = function (fmt) {
288
+ var o = {
289
+ "M+": this.getMonth() + 1,
290
+ "d+": this.getDate(),
291
+ "h+": this.getHours(),
292
+ "m+": this.getMinutes(),
293
+ "s+": this.getSeconds(),
294
+ "q+": Math.floor((this.getMonth() + 3) / 3),
295
+ "S": this.getMilliseconds()
296
+ };
297
+ if (/(y+)/.test(fmt))
298
+ fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
299
+ for (var k in o)
300
+ if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
301
+ return fmt;
302
+ }
303
+
304
+ var gradioEl = document.querySelector('body > gradio-app').shadowRoot;
305
+ if (!gradioEl) {
306
+ gradioEl = document.querySelector('body > gradio-app');
307
+ }
308
+ const out_image = gradioEl.querySelectorAll('#image_output img')[0];
309
+ if (out_image) {
310
+ var x=new XMLHttpRequest();
311
+ x.open("GET", out_image.src, true);
312
+ x.responseType = 'blob';
313
+ x.onload = function(e){
314
+ var url = window.URL.createObjectURL(x.response)
315
+ var a = document.createElement('a');
316
+ a.href = url;
317
+ a.download = (new Date()).Format("yyyyMMdd_hhmmss");
318
+ a.click();
319
+ }
320
+ x.send();
321
+ }
322
  }"""
323
 
324
  image_blocks = gr.Blocks(css=css, title='Image Cleaner')
 
352
  with gr.Column():
353
  image_input = gr.Image(source='upload', elem_id="image_upload",tool='sketch', type=f'{image_type}',
354
  label="Upload", show_label=False).style(mobile_collapse=False)
355
+ with gr.Row(elem_id="op-container").style(mobile_collapse=False, equal_height=True):
356
+ with gr.Column(elem_id="erase-btn-container"):
357
+ erase_btn = gr.Button(value = "Erase(↓)",elem_id="erase-btn").style(
358
  margin=True,
359
  rounded=(True, True, True, True),
360
  full_width=True,
 
365
  with gr.Row(elem_id="output-container"):
366
  with gr.Column():
367
  image_out = gr.Image(elem_id="image_output",label="Result", show_label=False)
368
+ with gr.Group(elem_id="share-btn-container", visible=False) as share_btn_container:
369
+ community_icon = gr.HTML(community_icon_html)
370
+ loading_icon = gr.HTML(loading_icon_html)
371
+ share_button = gr.Button("Share to community", elem_id="share-btn")
372
+ with gr.Row(elem_id="download-container", visible=False) as download_container:
373
+ with gr.Column(elem_id="download-btn-container"):
374
+ download_button = gr.Button(elem_id="download-btn", value="Save(β†’)")
375
 
376
+ erase_btn.click(fn=predict, inputs=[image_input, img_enhancer], outputs=[image_out, download_container, share_btn_container])
377
+ download_button.click(None, [], [], _js=download_img)
378
  share_button.click(None, [], [], _js=share_js)
379
 
380
  begin_button.click(fn=None, inputs=[], outputs=[group_1, group_2], _js=set_page_elements)