Fia/StableDiffusionCPU
11
1import gradio as gr2 3import torch4from torch import autocast5from diffusers import StableDiffusionPipeline6from datasets import load_dataset7from PIL import Image 8import re9import streamlit as st10 11model_id = "CompVis/stable-diffusion-v1-4"12device = "cpu"13 14#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. 15pipe = StableDiffusionPipeline.from_pretrained(model_id, use_auth_token=st.secrets["AUTH_KEY"], torch_dtype=torch.float32)16def dummy(images, **kwargs): return images, False 17pipe.safety_checker = dummy18 19def infer(prompt, width, height, steps, scale, seed): 20 if seed == -1: 21 images_list = pipe(22 [prompt],23 height=height, 24 width=width,25 num_inference_steps=steps,26 guidance_scale=scale,27 generator=torch.Generator(device=device).manual_seed(seed))28 else:29 images_list = pipe(30 [prompt],31 height=height, 32 width=width,33 num_inference_steps=steps,34 guidance_scale=scale)35 36 return images_list["sample"]37 38css = """39 .gradio-container {40 font-family: 'IBM Plex Sans', sans-serif;41 }42 .gr-button {43 color: white;44 border-color: black;45 background: black;46 }47 input[type='range'] {48 accent-color: black;49 }50 .dark input[type='range'] {51 accent-color: #dfdfdf;52 }53 .container {54 max-width: 730px;55 margin: auto;56 padding-top: 1.5rem;57 }58 #gallery {59 min-height: 22rem;60 margin-bottom: 15px;61 margin-left: auto;62 margin-right: auto;63 border-bottom-right-radius: .5rem !important;64 border-bottom-left-radius: .5rem !important;65 }66 #gallery>div>.h-full {67 min-height: 20rem;68 }69 .details:hover {70 text-decoration: underline;71 }72 .gr-button {73 white-space: nowrap;74 }75 .gr-button:focus {76 border-color: rgb(147 197 253 / var(--tw-border-opacity));77 outline: none;78 box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);79 --tw-border-opacity: 1;80 --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);81 --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);82 --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));83 --tw-ring-opacity: .5;84 }85 .footer {86 margin-bottom: 45px;87 margin-top: 35px;88 text-align: center;89 border-bottom: 1px solid #e5e5e5;90 }91 .footer>p {92 font-size: .8rem;93 display: inline-block;94 padding: 0 10px;95 transform: translateY(10px);96 background: white;97 }98 .dark .footer {99 border-color: #303030;100 }101 .dark .footer>p {102 background: #0b0f19;103 }104 .acknowledgments h4{105 margin: 1.25em 0 .25em 0;106 font-weight: bold;107 font-size: 115%;108 }109"""110 111block = gr.Blocks(css=css)112 113with block:114 gr.HTML(115 """116 <div style="text-align: center; max-width: 650px; margin: 0 auto;">117 <div118 style="119 display: inline-flex;120 align-items: center;121 gap: 0.8rem;122 font-size: 1.75rem;123 "124 >125 <h1 style="font-weight: 900; margin-bottom: 7px;">126 Stable Diffusion CPU127 </h1>128 </div>129 </div>130 """131 )132 with gr.Group():133 with gr.Box():134 with gr.Row().style(mobile_collapse=False, equal_height=True):135 text = gr.Textbox(136 label="Enter your prompt",137 show_label=False,138 max_lines=1,139 placeholder="Enter your prompt",140 ).style(141 border=(True, False, True, True),142 rounded=(True, False, False, True),143 container=False,144 )145 btn = gr.Button("Generate image").style(146 margin=False,147 rounded=(False, True, True, False),148 )149 150 gallery = gr.Gallery(151 label="Generated images", show_label=False, elem_id="gallery"152 ).style(grid=[2], height="auto")153 154 with gr.Row().style(mobile_collapse=False, equal_height=True):155 width = gr.Slider(label="Width", minimum=32, maximum=1024, value=512, step=8)156 height = gr.Slider(label="Height", minimum=32, maximum=1024, value=512, step=8)157 158 with gr.Row():159 steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=30, step=1)160 scale = gr.Slider(161 label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1162 )163 seed = gr.Slider(164 label="Seed",165 minimum=-1,166 maximum=2147483647,167 step=1,168 value=-1,169 )170 171 text.submit(infer, inputs=[text, width, height, steps, scale, seed], outputs=gallery)172 btn.click(infer, inputs=[text, width, height, steps, scale, seed], outputs=gallery)173 174block.queue(max_size=10).launch()