Sanjeetk5 commited on
Commit
f8eac15
·
1 Parent(s): afcb531
Files changed (1) hide show
  1. share_btn.py +13 -0
share_btn.py CHANGED
@@ -22,12 +22,14 @@ share_js = """async () => {
22
  const url = await response.text();
23
  return url;
24
  }
 
25
  async function getInputImgFile(imgCanvas){
26
  const blob = await new Promise(resolve => imgCanvas.toBlob(resolve));
27
  const imgId = Date.now() % 200;
28
  const fileName = `sd-inpainting-${{imgId}}.png`;
29
  return new File([blob], fileName, { type: 'image/png' });
30
  }
 
31
  async function getOutoutImgFile(imgEl){
32
  const res = await fetch(imgEl.src);
33
  const blob = await res.blob();
@@ -35,6 +37,7 @@ share_js = """async () => {
35
  const fileName = `sd-inpainting-${{imgId}}.png`;
36
  return new File([blob], fileName, { type: 'image/png' });
37
  }
 
38
  const gradioEl = document.querySelector('body > gradio-app');
39
  // const gradioEl = document.querySelector("gradio-app").shadowRoot;
40
  const inputImgCanvas = gradioEl.querySelector('canvas[key="drawing"]');
@@ -47,34 +50,44 @@ share_js = """async () => {
47
  const shareBtnEl = gradioEl.querySelector('#share-btn');
48
  const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
49
  const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
 
50
  if(!outputImgEl){
51
  return;
52
  };
 
53
  shareBtnEl.style.pointerEvents = 'none';
54
  shareIconEl.style.display = 'none';
55
  loadingIconEl.style.removeProperty('display');
 
56
  const inputImgFile = await getInputImgFile(inputImgCanvas);
57
  const outputImgFile = await getOutoutImgFile(outputImgEl);
58
  const files = [inputImgFile, outputImgFile];
 
59
  const urls = await Promise.all(files.map((f) => uploadFile(f)));
 
60
  const htmlImgs = urls.map(url => `<img src='${url}' style='max-width: 450px;'>`);
61
  const [inputImgUrl, outputImgUrl] = htmlImgs;
 
62
  const descriptionMd = `<div style='display: flex; flex-wrap: wrap; column-gap: 0.75rem;'>
63
  <div>
64
  ${inputImgUrl}
 
65
  ${promptTxt}
66
  </div>
67
  <div>
68
  ${outputImgUrl}
69
  </div>
70
  </div>`;
 
71
  const params = new URLSearchParams({
72
  title: titleTxt,
73
  description: descriptionMd,
74
  });
 
75
  const paramsStr = params.toString();
76
 
77
  window.open(`https://huggingface.co/spaces/diffusers/stable-diffusion-xl-inpainting/discussions/new?${paramsStr}&preview=true`, '_blank');
 
78
  shareBtnEl.style.removeProperty('pointer-events');
79
  shareIconEl.style.removeProperty('display');
80
  loadingIconEl.style.display = 'none';
 
22
  const url = await response.text();
23
  return url;
24
  }
25
+
26
  async function getInputImgFile(imgCanvas){
27
  const blob = await new Promise(resolve => imgCanvas.toBlob(resolve));
28
  const imgId = Date.now() % 200;
29
  const fileName = `sd-inpainting-${{imgId}}.png`;
30
  return new File([blob], fileName, { type: 'image/png' });
31
  }
32
+
33
  async function getOutoutImgFile(imgEl){
34
  const res = await fetch(imgEl.src);
35
  const blob = await res.blob();
 
37
  const fileName = `sd-inpainting-${{imgId}}.png`;
38
  return new File([blob], fileName, { type: 'image/png' });
39
  }
40
+
41
  const gradioEl = document.querySelector('body > gradio-app');
42
  // const gradioEl = document.querySelector("gradio-app").shadowRoot;
43
  const inputImgCanvas = gradioEl.querySelector('canvas[key="drawing"]');
 
50
  const shareBtnEl = gradioEl.querySelector('#share-btn');
51
  const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
52
  const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
53
+
54
  if(!outputImgEl){
55
  return;
56
  };
57
+
58
  shareBtnEl.style.pointerEvents = 'none';
59
  shareIconEl.style.display = 'none';
60
  loadingIconEl.style.removeProperty('display');
61
+
62
  const inputImgFile = await getInputImgFile(inputImgCanvas);
63
  const outputImgFile = await getOutoutImgFile(outputImgEl);
64
  const files = [inputImgFile, outputImgFile];
65
+
66
  const urls = await Promise.all(files.map((f) => uploadFile(f)));
67
+
68
  const htmlImgs = urls.map(url => `<img src='${url}' style='max-width: 450px;'>`);
69
  const [inputImgUrl, outputImgUrl] = htmlImgs;
70
+
71
  const descriptionMd = `<div style='display: flex; flex-wrap: wrap; column-gap: 0.75rem;'>
72
  <div>
73
  ${inputImgUrl}
74
+
75
  ${promptTxt}
76
  </div>
77
  <div>
78
  ${outputImgUrl}
79
  </div>
80
  </div>`;
81
+
82
  const params = new URLSearchParams({
83
  title: titleTxt,
84
  description: descriptionMd,
85
  });
86
+
87
  const paramsStr = params.toString();
88
 
89
  window.open(`https://huggingface.co/spaces/diffusers/stable-diffusion-xl-inpainting/discussions/new?${paramsStr}&preview=true`, '_blank');
90
+
91
  shareBtnEl.style.removeProperty('pointer-events');
92
  shareIconEl.style.removeProperty('display');
93
  loadingIconEl.style.display = 'none';