yizhangliu commited on
Commit
e1972ca
1 Parent(s): a370c7b

Update share_btn.py

Browse files
Files changed (1) hide show
  1. share_btn.py +11 -16
share_btn.py CHANGED
@@ -22,21 +22,12 @@ share_js = """async () => {
22
  return url;
23
  }
24
 
25
- function find_els(parent, selectors, min_size){
26
- var elAry = Array.prototype.filter.call(parent.querySelectorAll(selectors),function(el){
27
- return (el.offsetWidth>min_size && el.offsetHeight>min_size);
28
- });
29
- return elAry;
30
- }
31
-
32
  var gradioEl = document.querySelector('body > gradio-app').shadowRoot;
33
  if (!gradioEl) {
34
  gradioEl = document.querySelector('body > gradio-app');
35
  }
36
 
37
- // const imgEls = gradioEl.querySelectorAll('#gallery img');
38
  const imgEls = find_els(gradioEl, '#gallery .overflow-hidden', 50);
39
- console.log(imgEls);
40
  const promptTxt = 'my perfect work';
41
 
42
  const shareBtnEl = gradioEl.querySelector('#share-btn');
@@ -49,17 +40,21 @@ share_js = """async () => {
49
  shareIconEl.style.display = 'none';
50
  loadingIconEl.style.removeProperty('display');
51
 
52
- const files = await Promise.all(
 
53
  [...imgEls].map(async (imgEl) => {
54
- const res = await fetch(imgEl.src);
55
- const blob = await res.blob();
56
- const imgId = Date.now() % 200;
57
- const fileName = `img-cleaner-${{imgId}}.jpg`;
58
- return new File([blob], fileName, { type: 'image/jpeg' });
 
 
59
  })
60
  );
 
61
  const urls = await Promise.all(files.map((f) => uploadFile(f)));
62
- const htmlImgs = urls.map(url => `<img src='${url}' width='400' height1='400'>`);
63
  const descriptionMd = `<div style='display: flex; flex-wrap: wrap; column-gap: 0.75rem;'>
64
  ${htmlImgs.join(`\n`)}
65
  </div>`;
 
22
  return url;
23
  }
24
 
 
 
 
 
 
 
 
25
  var gradioEl = document.querySelector('body > gradio-app').shadowRoot;
26
  if (!gradioEl) {
27
  gradioEl = document.querySelector('body > gradio-app');
28
  }
29
 
 
30
  const imgEls = find_els(gradioEl, '#gallery .overflow-hidden', 50);
 
31
  const promptTxt = 'my perfect work';
32
 
33
  const shareBtnEl = gradioEl.querySelector('#share-btn');
 
40
  shareIconEl.style.display = 'none';
41
  loadingIconEl.style.removeProperty('display');
42
 
43
+ var files = [];
44
+ await Promise.all(
45
  [...imgEls].map(async (imgEl) => {
46
+ if (imgEl.offsetWidth > 50 && imgEl.offsetHeight > 50) {
47
+ const res = await fetch(imgEl.src);
48
+ const blob = await res.blob();
49
+ const imgId = Date.now() % 200;
50
+ const fileName = `img-cleaner-${{imgId}}.jpg`;
51
+ files.push(new File([blob], fileName, { type: 'image/jpeg'}));
52
+ }
53
  })
54
  );
55
+
56
  const urls = await Promise.all(files.map((f) => uploadFile(f)));
57
+ const htmlImgs = urls.map(url => `<img src='${url}' width='400' height='400'>`);
58
  const descriptionMd = `<div style='display: flex; flex-wrap: wrap; column-gap: 0.75rem;'>
59
  ${htmlImgs.join(`\n`)}
60
  </div>`;