Spaces:
Running
Running
yizhangliu
commited on
Commit
•
2d8e201
1
Parent(s):
92808ba
Update share_btn.py
Browse files- share_btn.py +64 -93
share_btn.py
CHANGED
@@ -7,98 +7,69 @@ loading_icon_html = """<svg id="share-btn-loading-icon" style="display:none;" cl
|
|
7 |
style="color: #ffffff; "
|
8 |
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" fill="none" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><circle style="opacity: 0.25;" cx="12" cy="12" r="10" stroke="white" stroke-width="4"></circle><path style="opacity: 0.75;" fill="white" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg>"""
|
9 |
|
10 |
-
download_icon_html = """<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="24px" height="24px" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve"> <image id="image0" width="24" height="24" x="0" y="0"
|
11 |
-
href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAAAAADFHGIkAAAABGdBTUEAALGPC/xhBQAAACBjSFJN
|
12 |
-
AAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QA/4ePzL8AAAAJcEhZ
|
13 |
-
cwAADsMAAA7DAcdvqGQAAAAHdElNRQfmCxAEABlIYjStAAAAAW9yTlQBz6J3mgAAAThJREFUKM9N
|
14 |
-
kT1uVHEMxH/+eClQkJB2lVPkBBENiUSNcgUK7rE3gFwACQ5AnYKKmk16kBAnAIVq928PRfblrQvb
|
15 |
-
47Eta2ziYJLTZjP0ORkmkI0Z29ME9NJ2TMhmB0AudX3hjT0xx6tec7uAXFLtjjYdEzax1HGaggK1
|
16 |
-
5u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|
-
}
|
65 |
-
|
66 |
-
const shareBtnEl = gradioEl.querySelector('#share-btn');
|
67 |
-
const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
|
68 |
-
const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
|
69 |
-
if(!outputImgEl){
|
70 |
-
return;
|
71 |
-
};
|
72 |
-
shareBtnEl.style.pointerEvents = 'none';
|
73 |
-
shareIconEl.style.display = 'none';
|
74 |
-
|
75 |
-
loadingIconEl.style.removeProperty('display');
|
76 |
-
const inputImgFile = await getInputImgFile(inputImgCanvas);
|
77 |
-
const outputImgFile = await getOutoutImgFile(outputImgEl);
|
78 |
-
|
79 |
-
const files = [inputImgFile, outputImgFile];
|
80 |
-
const urls = await Promise.all(files.map((f) => uploadFile(f)));
|
81 |
-
const htmlImgs = urls.map(url => `<img src='${url}' style='max-width: 450px;'>`);
|
82 |
-
const [inputImgUrl, outputImgUrl] = htmlImgs;
|
83 |
-
|
84 |
-
var descriptionMd = `<div style='display: flex; flex-wrap: wrap; column-gap: 0.75rem;'>
|
85 |
-
<div>
|
86 |
-
${inputImgUrl}
|
87 |
-
${promptTxt}
|
88 |
-
</div>
|
89 |
-
<div>
|
90 |
-
${outputImgUrl}
|
91 |
-
</div>
|
92 |
-
</div>`;
|
93 |
-
const params = new URLSearchParams({
|
94 |
-
title: titleTxt,
|
95 |
-
description: descriptionMd,
|
96 |
-
});
|
97 |
-
|
98 |
-
const paramsStr = params.toString();
|
99 |
-
window.open(`${window.location.href}/discussions/new?${paramsStr}`, '_blank');
|
100 |
-
shareBtnEl.style.removeProperty('pointer-events');
|
101 |
-
shareIconEl.style.removeProperty('display');
|
102 |
-
loadingIconEl.style.display = 'none';
|
103 |
-
|
104 |
-
}"""
|
|
|
7 |
style="color: #ffffff; "
|
8 |
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" fill="none" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><circle style="opacity: 0.25;" cx="12" cy="12" r="10" stroke="white" stroke-width="4"></circle><path style="opacity: 0.75;" fill="white" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg>"""
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
share_js = """async () => {
|
11 |
+
async function uploadFile(file){
|
12 |
+
const UPLOAD_URL = 'https://huggingface.co/uploads';
|
13 |
+
const response = await fetch(UPLOAD_URL, {
|
14 |
+
method: 'POST',
|
15 |
+
headers: {
|
16 |
+
'Content-Type': file.type,
|
17 |
+
'X-Requested-With': 'XMLHttpRequest',
|
18 |
+
},
|
19 |
+
body: file, /// <- File inherits from Blob
|
20 |
+
});
|
21 |
+
const url = await response.text();
|
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');
|
43 |
+
const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
|
44 |
+
const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
|
45 |
+
if(!imgEls.length){
|
46 |
+
return;
|
47 |
+
};
|
48 |
+
shareBtnEl.style.pointerEvents = 'none';
|
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>`;
|
66 |
+
const params = new URLSearchParams({
|
67 |
+
title: promptTxt,
|
68 |
+
description: descriptionMd,
|
69 |
+
});
|
70 |
+
const paramsStr = params.toString();
|
71 |
+
window.open(`https://huggingface.co/spaces/yizhangliu/ImgCleaner/discussions/new?${paramsStr}`, '_blank');
|
72 |
+
shareBtnEl.style.removeProperty('pointer-events');
|
73 |
+
shareIconEl.style.removeProperty('display');
|
74 |
+
loadingIconEl.style.display = 'none';
|
75 |
+
}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|