Spaces:
Runtime error
Runtime error
| from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, DPMSolverMultistepScheduler | |
| import gradio as gr | |
| import cpuinfo | |
| import torch | |
| from PIL import Image | |
| from diffusers import OnnxStableDiffusionPipeline | |
| import pipeline_openvino_stable_diffusion | |
| model_id = 'OFA-Sys/small-stable-diffusion-v0' | |
| prefix = '' | |
| scheduler = DPMSolverMultistepScheduler.from_pretrained(model_id, subfolder="scheduler") | |
| onnx_pipe = OnnxStableDiffusionPipeline.from_pretrained( | |
| "OFA-Sys/small-stable-diffusion-v0", | |
| revision="onnx", | |
| provider="CPUExecutionProvider", | |
| ) | |
| pipe = pipeline_openvino_stable_diffusion.OpenVINOStableDiffusionPipeline.from_onnx_pipeline(onnx_pipe) | |
| def error_str(error, title="Error"): | |
| return f"""#### {title} | |
| {error}""" if error else "" | |
| def inference(prompt, guidance, steps, width=512, height=512, seed=0, neg_prompt=""): | |
| generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None | |
| try: | |
| return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator), None | |
| except Exception as e: | |
| return None, error_str(e) | |
| def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator): | |
| result = pipe( | |
| prompt, | |
| negative_prompt = neg_prompt, | |
| num_inference_steps = int(steps), | |
| guidance_scale = guidance, | |
| width = width, | |
| height = height, | |
| generator = generator) | |
| return result.images[0] | |
| css = """.main-div div{display:inline-flex;align-items:center;gap:.8rem;font-size:1.75rem}.main-div div h1{font-weight:900;margin-bottom:7px}.main-div p{margin-bottom:10px;font-size:94%}a{text-decoration:underline}.tabs{margin-top:0;margin-bottom:0}#gallery{min-height:20rem} | |
| """ | |
| with gr.Blocks(css=css) as demo: | |
| gr.HTML( | |
| f""" | |
| <div class="main-div"> | |
| <div> | |
| <h1>Small Stable Diffusion V0</h1> | |
| </div> | |
| <p> | |
| Demo for <a href="https://huggingface.co/OFA-Sys/small-stable-diffusion-v0">Small Stable Diffusion V0</a> Stable Diffusion model.<br> | |
| </p> | |
| Running on CPUs with <a href="https://github.com/OFA-Sys/diffusion-deploy">diffusion-deploy</a> to speedup the inference. | |
| </div> | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=55): | |
| with gr.Group(): | |
| with gr.Row(): | |
| prompt = gr.Textbox(label="Prompt", show_label=False, max_lines=2,placeholder=f"{prefix} [your prompt]").style(container=False) | |
| generate = gr.Button(value="Generate").style(rounded=(False, True, True, False)) | |
| image_out = gr.Image(height=512) | |
| error_output = gr.Markdown() | |
| with gr.Column(scale=45): | |
| with gr.Tab("Options"): | |
| with gr.Group(): | |
| neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image") | |
| with gr.Row(): | |
| guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15) | |
| steps = gr.Slider(label="Steps", value=15, minimum=2, maximum=75, step=1) | |
| with gr.Row(): | |
| width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8) | |
| height = gr.Slider(label="Height", value=512, minimum=64, maximum=1024, step=8) | |
| seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1) | |
| inputs = [prompt, guidance, steps, width, height, seed, neg_prompt] | |
| outputs = [image_out, error_output] | |
| prompt.submit(inference, inputs=inputs, outputs=outputs) | |
| generate.click(inference, inputs=inputs, outputs=outputs) | |
| gr.HTML(""" | |
| <div style="border-top: 1px solid #303030;"> | |
| <br> | |
| <p>This space was created using <a href="https://huggingface.co/spaces/anzorq/sd-space-creator">SD Space Creator</a>.</p> | |
| </div> | |
| """) | |
| print(cpuinfo.get_cpu_info()) | |
| demo.queue(concurrency_count=1) | |
| demo.launch() | |