Spaces:
Running
Running
yizhangliu
commited on
Commit
•
6c85792
1
Parent(s):
9e9e6aa
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from io import BytesIO
|
4 |
+
import requests
|
5 |
+
import PIL
|
6 |
+
from PIL import Image
|
7 |
+
import numpy as np
|
8 |
+
import os
|
9 |
+
import uuid
|
10 |
+
import torch
|
11 |
+
from torch import autocast
|
12 |
+
import cv2
|
13 |
+
from matplotlib import pyplot as plt
|
14 |
+
from torchvision import transforms
|
15 |
+
from diffusers import DiffusionPipeline
|
16 |
+
|
17 |
+
from share_btn import community_icon_html, loading_icon_html, share_js
|
18 |
+
|
19 |
+
auth_token = os.environ.get("API_TOKEN") or True
|
20 |
+
|
21 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
22 |
+
|
23 |
+
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-inpainting", dtype=torch.float16, revision="fp16", use_auth_token=auth_token).to(device)
|
24 |
+
|
25 |
+
transform = transforms.Compose([
|
26 |
+
transforms.ToTensor(),
|
27 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
28 |
+
transforms.Resize((512, 512)),
|
29 |
+
])
|
30 |
+
|
31 |
+
def read_content(file_path: str) -> str:
|
32 |
+
"""read the content of target file
|
33 |
+
"""
|
34 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
35 |
+
content = f.read()
|
36 |
+
|
37 |
+
return content
|
38 |
+
|
39 |
+
def predict(dict, prompt=""):
|
40 |
+
init_image = dict["image"].convert("RGB").resize((512, 512))
|
41 |
+
mask = dict["mask"].convert("RGB").resize((512, 512))
|
42 |
+
output = pipe(prompt = prompt, image=init_image, mask_image=mask,guidance_scale=7.5)
|
43 |
+
return output.images[0], gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
44 |
+
|
45 |
+
|
46 |
+
css = '''
|
47 |
+
.container {max-width: 1150px;margin: auto;padding-top: 1.5rem}
|
48 |
+
#image_upload{min-height:400px}
|
49 |
+
#image_upload [data-testid="image"], #image_upload [data-testid="image"] > div{min-height: 400px}
|
50 |
+
#mask_radio .gr-form{background:transparent; border: none}
|
51 |
+
#word_mask{margin-top: .75em !important}
|
52 |
+
#word_mask textarea:disabled{opacity: 0.3}
|
53 |
+
.footer {margin-bottom: 45px;margin-top: 35px;text-align: center;border-bottom: 1px solid #e5e5e5}
|
54 |
+
.footer>p {font-size: .8rem; display: inline-block; padding: 0 10px;transform: translateY(10px);background: white}
|
55 |
+
.dark .footer {border-color: #303030}
|
56 |
+
.dark .footer>p {background: #0b0f19}
|
57 |
+
.acknowledgments h4{margin: 1.25em 0 .25em 0;font-weight: bold;font-size: 115%}
|
58 |
+
#image_upload .touch-none{display: flex}
|
59 |
+
@keyframes spin {
|
60 |
+
from {
|
61 |
+
transform: rotate(0deg);
|
62 |
+
}
|
63 |
+
to {
|
64 |
+
transform: rotate(360deg);
|
65 |
+
}
|
66 |
+
}
|
67 |
+
#share-btn-container {
|
68 |
+
display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
|
69 |
+
}
|
70 |
+
#share-btn {
|
71 |
+
all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;
|
72 |
+
}
|
73 |
+
#share-btn * {
|
74 |
+
all: unset;
|
75 |
+
}
|
76 |
+
#share-btn-container div:nth-child(-n+2){
|
77 |
+
width: auto !important;
|
78 |
+
min-height: 0px !important;
|
79 |
+
}
|
80 |
+
#share-btn-container .wrap {
|
81 |
+
display: none !important;
|
82 |
+
}
|
83 |
+
'''
|
84 |
+
|
85 |
+
image_blocks = gr.Blocks(css=css)
|
86 |
+
with image_blocks as demo:
|
87 |
+
gr.HTML(read_content("header.html"))
|
88 |
+
with gr.Group():
|
89 |
+
with gr.Box():
|
90 |
+
with gr.Row():
|
91 |
+
with gr.Column():
|
92 |
+
image = gr.Image(source='upload', tool='sketch', elem_id="image_upload", type="pil", label="Upload").style(height=400)
|
93 |
+
with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
|
94 |
+
prompt = gr.Textbox(placeholder = 'Your prompt (what you want in place of what is erased)', show_label=False, elem_id="input-text")
|
95 |
+
btn = gr.Button("Inpaint!").style(
|
96 |
+
margin=False,
|
97 |
+
rounded=(False, True, True, False),
|
98 |
+
full_width=False,
|
99 |
+
)
|
100 |
+
with gr.Column():
|
101 |
+
image_out = gr.Image(label="Output", elem_id="output-img").style(height=400)
|
102 |
+
with gr.Group(elem_id="share-btn-container"):
|
103 |
+
community_icon = gr.HTML(community_icon_html, visible=False)
|
104 |
+
loading_icon = gr.HTML(loading_icon_html, visible=False)
|
105 |
+
share_button = gr.Button("Share to community", elem_id="share-btn", visible=False)
|
106 |
+
|
107 |
+
|
108 |
+
btn.click(fn=predict, inputs=[image, prompt], outputs=[image_out, community_icon, loading_icon, share_button])
|
109 |
+
share_button.click(None, [], [], _js=share_js)
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
gr.HTML(
|
114 |
+
"""
|
115 |
+
<div class="footer">
|
116 |
+
<p>Model by <a href="https://huggingface.co/runwayml" style="text-decoration: underline;" target="_blank">RunwayML</a> - Gradio Demo by 🤗 Hugging Face
|
117 |
+
</p>
|
118 |
+
</div>
|
119 |
+
<div class="acknowledgments">
|
120 |
+
<p><h4>LICENSE</h4>
|
121 |
+
The model is licensed with a <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" style="text-decoration: underline;" target="_blank">CreativeML Open RAIL-M</a> license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a></p>
|
122 |
+
<p><h4>Biases and content acknowledgment</h4>
|
123 |
+
Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence. The model was trained on the <a href="https://laion.ai/blog/laion-5b/" style="text-decoration: underline;" target="_blank">LAION-5B dataset</a>, which scraped non-curated image-text-pairs from the internet (the exception being the removal of illegal content) and is meant for research purposes. You can read more in the <a href="https://huggingface.co/CompVis/stable-diffusion-v1-4" style="text-decoration: underline;" target="_blank">model card</a></p>
|
124 |
+
</div>
|
125 |
+
"""
|
126 |
+
)
|
127 |
+
|
128 |
+
image_blocks.launch()
|