Spaces:
Runtime error
Runtime error
Update share_btn.py
Browse files- share_btn.py +77 -1
share_btn.py
CHANGED
@@ -9,7 +9,83 @@ loading_icon_html = """<svg id="share-btn-loading-icon" style="display:none;" cl
|
|
9 |
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>"""
|
10 |
|
11 |
share_js = """
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
alert("hello")
|
14 |
|
15 |
console.log("hello")
|
|
|
9 |
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>"""
|
10 |
|
11 |
share_js = """
|
12 |
+
async () => {
|
13 |
+
async function uploadFile(file){
|
14 |
+
const UPLOAD_URL = 'https://huggingface.co/uploads';
|
15 |
+
const response = await fetch(UPLOAD_URL, {
|
16 |
+
method: 'POST',
|
17 |
+
headers: {
|
18 |
+
'Content-Type': file.type,
|
19 |
+
'X-Requested-With': 'XMLHttpRequest',
|
20 |
+
},
|
21 |
+
body: file, /// <- File inherits from Blob
|
22 |
+
});
|
23 |
+
const url = await response.text();
|
24 |
+
return url;
|
25 |
+
}
|
26 |
+
async function getInputImgFile(imgEl){
|
27 |
+
const res = await fetch(imgEl.src);
|
28 |
+
const blob = await res.blob();
|
29 |
+
const imgId = Date.now() % 200;
|
30 |
+
const isPng = imgEl.src.startsWith(`data:image/png`);
|
31 |
+
if(isPng){
|
32 |
+
const fileName = `magic-prompt-${{imgId}}.png`;
|
33 |
+
return new File([blob], fileName, { type: 'image/png' });
|
34 |
+
}else{
|
35 |
+
const fileName = `magic-prompt-${{imgId}}.jpg`;
|
36 |
+
return new File([blob], fileName, { type: 'image/jpeg' });
|
37 |
+
}
|
38 |
+
}
|
39 |
+
const gradioEl = document.querySelector('body > gradio-app');
|
40 |
+
// const gradioEl = document.querySelector("gradio-app").shadowRoot;
|
41 |
+
const inputImgEl = gradioEl.querySelector('#input-img img');
|
42 |
+
const imgEls = gradioEl.querySelectorAll('#generated-gallery img');
|
43 |
+
const promptTxt = gradioEl.querySelector('#translated textarea').value;
|
44 |
+
let titleTxt = promptTxt;
|
45 |
+
if(titleTxt.length > 100){
|
46 |
+
titleTxt = titleTxt.slice(0, 100) + ' ...';
|
47 |
+
}
|
48 |
+
const shareBtnEl = gradioEl.querySelector('#share-btn');
|
49 |
+
const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
|
50 |
+
const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
|
51 |
+
if(!imgEls.length){
|
52 |
+
return;
|
53 |
+
};
|
54 |
+
shareBtnEl.style.pointerEvents = 'none';
|
55 |
+
shareIconEl.style.display = 'none';
|
56 |
+
loadingIconEl.style.removeProperty('display');
|
57 |
+
const files = await Promise.all(
|
58 |
+
[...imgEls].map(async (imgEl) => {
|
59 |
+
const res = await fetch(imgEl.src);
|
60 |
+
const blob = await res.blob();
|
61 |
+
const imgId = Date.now() % 200;
|
62 |
+
const fileName = `sd-perception-${{imgId}}.jpg`;
|
63 |
+
return new File([blob], fileName, { type: 'image/jpeg' });
|
64 |
+
})
|
65 |
+
);
|
66 |
+
const inputFile = await getInputImgFile(inputImgEl);
|
67 |
+
files.push(inputFile);
|
68 |
+
const urls = await Promise.all(files.map((f) => uploadFile(f)));
|
69 |
+
const urlInputImg = urls.pop();
|
70 |
+
const htmlImgs = urls.map(url => `<img src='${url}' width='400' height='400'>`);
|
71 |
+
const htmlImgsMd = htmlImgs.join(`\n`);
|
72 |
+
const descriptionMd = `#### Input img:
|
73 |
+
<img src='${urlInputImg}' style='max-height: 350px;'>
|
74 |
+
#### Caption:
|
75 |
+
${promptTxt}
|
76 |
+
#### Generations:
|
77 |
+
<div style='display: flex; flex-wrap: wrap; column-gap: 0.75rem;'>
|
78 |
+
${htmlImgsMd}
|
79 |
+
</div>`;
|
80 |
+
const params = new URLSearchParams({
|
81 |
+
title: titleTxt,
|
82 |
+
description: descriptionMd,
|
83 |
+
});
|
84 |
+
const paramsStr = params.toString();
|
85 |
+
window.open(`https://huggingface.co/spaces/huggingface-projects/magic-diffusion/new?${paramsStr}`, '_blank');
|
86 |
+
shareBtnEl.style.removeProperty('pointer-events');
|
87 |
+
shareIconEl.style.removeProperty('display');
|
88 |
+
loadingIconEl.style.display = 'none'; }
|
89 |
alert("hello")
|
90 |
|
91 |
console.log("hello")
|