laminar/stable_diffusion
0
1import gradio as gr2 3import torch4from torch import autocast5from diffusers import StableDiffusionPipeline6from datasets import load_dataset7from PIL import Image 8import re9import os10 11model_id = "CompVis/stable-diffusion-v1-4"12device = "cpu"13 14auth_token = os.environ.get("READ_TOKEN")15#If you are running this code locally, you need to either do a 'huggingface-cli login` or paste your User Access Token from here https://huggingface.co/settings/tokens into the use_auth_token field below. 16pipe = StableDiffusionPipeline.from_pretrained(model_id, use_auth_token=auth_token , revision="fp16", torch_dtype=torch.float16)17pipe = pipe.to(device)18#When running locally, you won`t have access to this, so you can remove this part19#word_list_dataset = load_dataset("stabilityai/word-list", data_files="list.txt", use_auth_token=True)20#word_list = word_list_dataset["train"]['text']21 22def infer(prompt, samples, steps, scale, seed):23 #When running locally you can also remove this filter24 #for filter in word_list:25 # if re.search(rf"\b{filter}\b", prompt):26 # raise gr.Error("Unsafe content found. Please try again with different prompts.")27 28 generator = torch.Generator(device=device).manual_seed(seed)29 30 #If you are running locally with CPU, you can remove the `with autocast("cuda")`31 with autocast("cpu"):32 images_list = pipe(33 [prompt] * samples,34 num_inference_steps=steps,35 guidance_scale=scale,36 generator=generator,37 )38 images = []39 safe_image = Image.open(r"unsafe.png")40 for i, image in enumerate(images_list["sample"]):41 if(images_list["nsfw_content_detected"][i]):42 images.append(safe_image)43 else:44 images.append(image)45 return images46 47css = """48 .gradio-container {49 font-family: 'IBM Plex Sans', sans-serif;50 }51 .gr-button {52 color: white;53 border-color: black;54 background: black;55 }56 input[type='range'] {57 accent-color: black;58 }59 .dark input[type='range'] {60 accent-color: #dfdfdf;61 }62 .container {63 max-width: 730px;64 margin: auto;65 padding-top: 1.5rem;66 }67 #gallery {68 min-height: 22rem;69 margin-bottom: 15px;70 margin-left: auto;71 margin-right: auto;72 border-bottom-right-radius: .5rem !important;73 border-bottom-left-radius: .5rem !important;74 }75 #gallery>div>.h-full {76 min-height: 20rem;77 }78 .details:hover {79 text-decoration: underline;80 }81 .gr-button {82 white-space: nowrap;83 }84 .gr-button:focus {85 border-color: rgb(147 197 253 / var(--tw-border-opacity));86 outline: none;87 box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);88 --tw-border-opacity: 1;89 --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);90 --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);91 --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));92 --tw-ring-opacity: .5;93 }94 #advanced-btn {95 font-size: .7rem !important;96 line-height: 19px;97 margin-top: 12px;98 margin-bottom: 12px;99 padding: 2px 8px;100 border-radius: 14px !important;101 }102 #advanced-options {103 display: none;104 margin-bottom: 20px;105 }106 .footer {107 margin-bottom: 45px;108 margin-top: 35px;109 text-align: center;110 border-bottom: 1px solid #e5e5e5;111 }112 .footer>p {113 font-size: .8rem;114 display: inline-block;115 padding: 0 10px;116 transform: translateY(10px);117 background: white;118 }119 .dark .footer {120 border-color: #303030;121 }122 .dark .footer>p {123 background: #0b0f19;124 }125 .acknowledgments h4{126 margin: 1.25em 0 .25em 0;127 font-weight: bold;128 font-size: 115%;129 }130"""131 132block = gr.Blocks(css=css)133 134examples = [135 [136 'A high tech solarpunk utopia in the Amazon rainforest',137 4,138 45,139 7.5,140 1024,141 ],142 [143 'A pikachu fine dining with a view to the Eiffel Tower',144 4,145 45,146 7,147 1024,148 ],149 [150 'A mecha robot in a favela in expressionist style',151 4,152 45,153 7,154 1024,155 ],156 [157 'an insect robot preparing a delicious meal',158 4,159 45,160 7,161 1024,162 ],163 [164 "A small cabin on top of a snowy mountain in the style of Disney, artstation",165 4,166 45,167 7,168 1024,169 ],170]171 172with block:173 gr.HTML(174 """175 <div style="text-align: center; max-width: 650px; margin: 0 auto;">176 <div177 style="178 display: inline-flex;179 align-items: center;180 gap: 0.8rem;181 font-size: 1.75rem;182 "183 >184 <svg185 width="0.65em"186 height="0.65em"187 viewBox="0 0 115 115"188 fill="none"189 xmlns="http://www.w3.org/2000/svg"190 >191 <rect width="23" height="23" fill="white"></rect>192 <rect y="69" width="23" height="23" fill="white"></rect>193 <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>194 <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>195 <rect x="46" width="23" height="23" fill="white"></rect>196 <rect x="46" y="69" width="23" height="23" fill="white"></rect>197 <rect x="69" width="23" height="23" fill="black"></rect>198 <rect x="69" y="69" width="23" height="23" fill="black"></rect>199 <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>200 <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>201 <rect x="115" y="46" width="23" height="23" fill="white"></rect>202 <rect x="115" y="115" width="23" height="23" fill="white"></rect>203 <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>204 <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>205 <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>206 <rect x="92" y="69" width="23" height="23" fill="white"></rect>207 <rect x="69" y="46" width="23" height="23" fill="white"></rect>208 <rect x="69" y="115" width="23" height="23" fill="white"></rect>209 <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>210 <rect x="46" y="46" width="23" height="23" fill="black"></rect>211 <rect x="46" y="115" width="23" height="23" fill="black"></rect>212 <rect x="46" y="69" width="23" height="23" fill="black"></rect>213 <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>214 <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>215 <rect x="23" y="69" width="23" height="23" fill="black"></rect>216 </svg>217 <h1 style="font-weight: 900; margin-bottom: 7px;">218 Stable Diffusion Demo219 </h1>220 </div>221 <p style="margin-bottom: 10px; font-size: 94%">222 Stable Diffusion is a state of the art text-to-image model that generates223 images from text.<br>For faster generation and forthcoming API224 access you can try225 <a226 href="http://beta.dreamstudio.ai/"227 style="text-decoration: underline;"228 target="_blank"229 >DreamStudio Beta</a230 >231 </p>232 </div>233 """234 )235 with gr.Group():236 with gr.Box():237 with gr.Row().style(mobile_collapse=False, equal_height=True):238 text = gr.Textbox(239 label="Enter your prompt",240 show_label=False,241 max_lines=1,242 placeholder="Enter your prompt",243 ).style(244 border=(True, False, True, True),245 rounded=(True, False, False, True),246 container=False,247 )248 btn = gr.Button("Generate image").style(249 margin=False,250 rounded=(False, True, True, False),251 )252 253 gallery = gr.Gallery(254 label="Generated images", show_label=False, elem_id="gallery"255 ).style(grid=[2], height="auto")256 257 advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")258 259 with gr.Row(elem_id="advanced-options"):260 samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)261 steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)262 scale = gr.Slider(263 label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1264 )265 seed = gr.Slider(266 label="Seed",267 minimum=0,268 maximum=2147483647,269 step=1,270 randomize=True,271 )272 273 ex = gr.Examples(examples=examples, fn=infer, inputs=[text, samples, steps, scale, seed], outputs=gallery, cache_examples=True)274 ex.dataset.headers = [""]275 276 277 text.submit(infer, inputs=[text, samples, steps, scale, seed], outputs=gallery)278 btn.click(infer, inputs=[text, samples, steps, scale, seed], outputs=gallery)279 advanced_button.click(280 None,281 [],282 text,283 _js="""284 () => {285 const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");286 options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";287 }""",288 )289 gr.HTML(290 """291 <div class="footer">292 <p>Model by <a href="https://huggingface.co/CompVis" style="text-decoration: underline;" target="_blank">CompVis</a> and <a href="https://huggingface.co/stabilityai" style="text-decoration: underline;" target="_blank">Stability AI</a> - Gradio Demo by 🤗 Hugging Face293 </p>294 </div>295 <div class="acknowledgments">296 <p><h4>LICENSE</h4>297The model is licensed with an <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>298 <p><h4>Biases and content acknowledgment</h4>299Despite 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 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>300 </div>301 """302 )303 304block.queue(max_size=10).launch()