Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import torch
|
|
| 3 |
|
| 4 |
from diffusers import AutoPipelineForInpainting, UNet2DConditionModel
|
| 5 |
import diffusers
|
| 6 |
-
from share_btn import community_icon_html, loading_icon_html
|
| 7 |
|
| 8 |
pipe = AutoPipelineForInpainting.from_pretrained("SG161222/Realistic_Vision_V5.0_noVAE")
|
| 9 |
|
|
@@ -74,6 +74,79 @@ div#share-btn-container > div {flex-direction: row;background: black;align-items
|
|
| 74 |
#image_upload{border-bottom-left-radius: 0px;border-bottom-right-radius: 0px}
|
| 75 |
'''
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
image_blocks = gr.Blocks(css=css, elem_id="total-container")
|
| 78 |
with image_blocks as demo:
|
| 79 |
gr.HTML(read_content("header.html"))
|
|
@@ -105,7 +178,7 @@ with image_blocks as demo:
|
|
| 105 |
|
| 106 |
btn.click(fn=predict, inputs=[image, prompt, negative_prompt, guidance_scale, steps, strength, scheduler], outputs=[image_out, share_btn_container], api_name='run')
|
| 107 |
prompt.submit(fn=predict, inputs=[image, prompt, negative_prompt, guidance_scale, steps, strength, scheduler], outputs=[image_out, share_btn_container])
|
| 108 |
-
share_button.click(None, [], [],
|
| 109 |
|
| 110 |
gr.Examples(
|
| 111 |
examples=[
|
|
|
|
| 3 |
|
| 4 |
from diffusers import AutoPipelineForInpainting, UNet2DConditionModel
|
| 5 |
import diffusers
|
| 6 |
+
from share_btn import community_icon_html, loading_icon_html
|
| 7 |
|
| 8 |
pipe = AutoPipelineForInpainting.from_pretrained("SG161222/Realistic_Vision_V5.0_noVAE")
|
| 9 |
|
|
|
|
| 74 |
#image_upload{border-bottom-left-radius: 0px;border-bottom-right-radius: 0px}
|
| 75 |
'''
|
| 76 |
|
| 77 |
+
share_js = """async () => {
|
| 78 |
+
async function uploadFile(file){
|
| 79 |
+
const UPLOAD_URL = 'https://huggingface.co/uploads';
|
| 80 |
+
const response = await fetch(UPLOAD_URL, {
|
| 81 |
+
method: 'POST',
|
| 82 |
+
headers: {
|
| 83 |
+
'Content-Type': file.type,
|
| 84 |
+
'X-Requested-With': 'XMLHttpRequest',
|
| 85 |
+
},
|
| 86 |
+
body: file, /// <- File inherits from Blob
|
| 87 |
+
});
|
| 88 |
+
const url = await response.text();
|
| 89 |
+
return url;
|
| 90 |
+
}
|
| 91 |
+
async function getInputImgFile(imgCanvas){
|
| 92 |
+
const blob = await new Promise(resolve => imgCanvas.toBlob(resolve));
|
| 93 |
+
const imgId = Date.now() % 200;
|
| 94 |
+
const fileName = `sd-inpainting-${{imgId}}.png`;
|
| 95 |
+
return new File([blob], fileName, { type: 'image/png' });
|
| 96 |
+
}
|
| 97 |
+
async function getOutoutImgFile(imgEl){
|
| 98 |
+
const res = await fetch(imgEl.src);
|
| 99 |
+
const blob = await res.blob();
|
| 100 |
+
const imgId = Date.now() % 200;
|
| 101 |
+
const fileName = `sd-inpainting-${{imgId}}.png`;
|
| 102 |
+
return new File([blob], fileName, { type: 'image/png' });
|
| 103 |
+
}
|
| 104 |
+
const gradioEl = document.querySelector('body > gradio-app');
|
| 105 |
+
// const gradioEl = document.querySelector("gradio-app").shadowRoot;
|
| 106 |
+
const inputImgCanvas = gradioEl.querySelector('canvas[key="drawing"]');
|
| 107 |
+
const outputImgEl = gradioEl.querySelector('#output-img img');
|
| 108 |
+
const promptTxt = gradioEl.querySelector('#prompt textarea').value;
|
| 109 |
+
let titleTxt = promptTxt;
|
| 110 |
+
if(titleTxt.length > 100){
|
| 111 |
+
titleTxt = titleTxt.slice(0, 100) + ' ...';
|
| 112 |
+
}
|
| 113 |
+
const shareBtnEl = gradioEl.querySelector('#share-btn');
|
| 114 |
+
const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
|
| 115 |
+
const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
|
| 116 |
+
if(!outputImgEl){
|
| 117 |
+
return;
|
| 118 |
+
};
|
| 119 |
+
shareBtnEl.style.pointerEvents = 'none';
|
| 120 |
+
shareIconEl.style.display = 'none';
|
| 121 |
+
loadingIconEl.style.removeProperty('display');
|
| 122 |
+
const inputImgFile = await getInputImgFile(inputImgCanvas);
|
| 123 |
+
const outputImgFile = await getOutoutImgFile(outputImgEl);
|
| 124 |
+
const files = [inputImgFile, outputImgFile];
|
| 125 |
+
const urls = await Promise.all(files.map((f) => uploadFile(f)));
|
| 126 |
+
const htmlImgs = urls.map(url => `<img src='${url}' style='max-width: 450px;'>`);
|
| 127 |
+
const [inputImgUrl, outputImgUrl] = htmlImgs;
|
| 128 |
+
const descriptionMd = `<div style='display: flex; flex-wrap: wrap; column-gap: 0.75rem;'>
|
| 129 |
+
<div>
|
| 130 |
+
${inputImgUrl}
|
| 131 |
+
${promptTxt}
|
| 132 |
+
</div>
|
| 133 |
+
<div>
|
| 134 |
+
${outputImgUrl}
|
| 135 |
+
</div>
|
| 136 |
+
</div>`;
|
| 137 |
+
const params = new URLSearchParams({
|
| 138 |
+
title: titleTxt,
|
| 139 |
+
description: descriptionMd,
|
| 140 |
+
});
|
| 141 |
+
const paramsStr = params.toString();
|
| 142 |
+
|
| 143 |
+
window.open(`https://huggingface.co/spaces/diffusers/stable-diffusion-xl-inpainting/discussions/new?${paramsStr}&preview=true`, '_blank');
|
| 144 |
+
shareBtnEl.style.removeProperty('pointer-events');
|
| 145 |
+
shareIconEl.style.removeProperty('display');
|
| 146 |
+
loadingIconEl.style.display = 'none';
|
| 147 |
+
}"""
|
| 148 |
+
|
| 149 |
+
|
| 150 |
image_blocks = gr.Blocks(css=css, elem_id="total-container")
|
| 151 |
with image_blocks as demo:
|
| 152 |
gr.HTML(read_content("header.html"))
|
|
|
|
| 178 |
|
| 179 |
btn.click(fn=predict, inputs=[image, prompt, negative_prompt, guidance_scale, steps, strength, scheduler], outputs=[image_out, share_btn_container], api_name='run')
|
| 180 |
prompt.submit(fn=predict, inputs=[image, prompt, negative_prompt, guidance_scale, steps, strength, scheduler], outputs=[image_out, share_btn_container])
|
| 181 |
+
share_button.click(None, [], [], share_js)
|
| 182 |
|
| 183 |
gr.Examples(
|
| 184 |
examples=[
|