community_icon_html = """""" loading_icon_html = """""" share_js = """async () => { async function uploadFile(file){ const UPLOAD_URL = 'https://huggingface.co/uploads'; const response = await fetch(UPLOAD_URL, { method: 'POST', headers: { 'Content-Type': file.type, 'X-Requested-With': 'XMLHttpRequest', }, body: file, /// <- File inherits from Blob }); const url = await response.text(); return url; } const gradioEl = document.querySelector('body > gradio-app'); const imgEls = gradioEl.querySelectorAll('#generated-gallery img'); const promptTxt = gradioEl.querySelector('#prompt_input input').value; const shareBtnEl = gradioEl.querySelector('#share-btn'); const shareIconEl = gradioEl.querySelector('#share-btn-share-icon'); const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon'); if(!imgEls.length){ return; }; shareBtnEl.style.pointerEvents = 'none'; shareIconEl.style.display = 'none'; loadingIconEl.style.removeProperty('display'); const files = await Promise.all( [...imgEls].map(async (imgEl) => { const res = await fetch(imgEl.src); const blob = await res.blob(); const imgId = Date.now() % 200; const fileName = `diffuse-the-rest-${{imgId}}.png`; return new File([blob], fileName, { type: 'image/png' }); }) ); const REGEX_CONCEPT = /<(((?!<.+>).)+)>/gm; const matches = [...promptTxt.matchAll(REGEX_CONCEPT)]; const concepts = matches.map(m => m[1]); const conceptLibraryMd = concepts.map(c => `${c}`).join(`, `); const urls = await Promise.all(files.map((f) => uploadFile(f))); const htmlImgs = urls.map(url => ``); const htmlImgsMd = htmlImgs.join(`\n`); const descriptionMd = `#### Prompt: ${promptTxt.replace(//g, '\\\>')} #### Concepts Used: ${conceptLibraryMd} #### Generations:
${htmlImgsMd}
`; const params = new URLSearchParams({ title: promptTxt, description: descriptionMd, }); const paramsStr = params.toString(); window.open(`https://huggingface.co/spaces/sd-concepts-library/stable-diffusion-conceptualizer/discussions/new?${paramsStr}`, '_blank'); shareBtnEl.style.removeProperty('pointer-events'); shareIconEl.style.removeProperty('display'); loadingIconEl.style.display = 'none'; }"""