Spaces:
Build error
Build error
| import gradio as gr | |
| from optimum.intel.openvino import OVStableDiffusionPipeline | |
| from diffusers.training_utils import set_seed | |
| from diffusers import LMSDiscreteScheduler, StableDiffusionPipeline | |
| import time | |
| # scheduler = DDPMScheduler(beta_start=0.00085, beta_end=0.012, | |
| # beta_schedule="scaled_linear", num_train_timesteps=1000) | |
| pipe_torch_fp32 = StableDiffusionPipeline.from_pretrained("svjack/Stable-Diffusion-Pokemon-en") | |
| pipe_fp32 = OVStableDiffusionPipeline.from_pretrained("OpenVINO/stable-diffusion-pokemons-fp32", compile=False)# scheduler=scheduler) | |
| pipe_fp32.reshape(batch_size=1, height=512, width=512, num_images_per_prompt=1) | |
| pipe_fp32.compile() | |
| pipe_int8 = OVStableDiffusionPipeline.from_pretrained("OpenVINO/stable-diffusion-pokemons-quantized-aggressive", compile=False) | |
| pipe_int8.reshape(batch_size=1, height=512, width=512, num_images_per_prompt=1) | |
| pipe_int8.compile() | |
| pipe_tome_int8 = OVStableDiffusionPipeline.from_pretrained("OpenVINO/stable-diffusion-pokemons-tome-quantized-aggressive", compile=False) | |
| pipe_tome_int8.reshape(batch_size=1, height=512, width=512, num_images_per_prompt=1) | |
| pipe_tome_int8.compile() | |
| pipes = { | |
| "Torch fp32": pipe_torch_fp32, | |
| "OpenVINO fp32": pipe_fp32, | |
| "OpenVINO 8-bit quantized": pipe_int8, | |
| "OpenVINO merged and quantized": pipe_tome_int8 | |
| } | |
| def generate(prompt, option, seed): | |
| pipe = pipes[option] | |
| set_seed(int(seed)) | |
| start_time = time.time() | |
| output = pipe(prompt, num_inference_steps=50, output_type="pil", height=512, width=512) | |
| elapsed_time = time.time() - start_time | |
| return (output.images[0], "{:10.4f}".format(elapsed_time)) | |
| examples = ["cartoon bird", | |
| "a drawing of a green pokemon with red eyes", | |
| "plant pokemon in jungle"] | |
| model_options = [option for option in pipes.keys()] | |
| gr.Interface( | |
| fn=generate, | |
| inputs=[gr.inputs.Textbox(placeholder="cartoon bird", label="Prompt", lines=1), | |
| gr.inputs.Dropdown(choices=model_options, default=model_options[-1], label="Model version"), | |
| gr.inputs.Textbox(placeholder="42", label="Seed", lines=1) | |
| ], | |
| outputs=[gr.outputs.Image(type="pil", label="Generated Image"), gr.outputs.Textbox(label="Inference time")], | |
| title="OpenVINO-optimized Stable Diffusion", | |
| description="This is the Optimum-based demo for NNCF-optimized Stable Diffusion pipeline trained on 'lambdalabs/pokemon-blip-captions' dataset and running with OpenVINO.\n" | |
| "The pipeline is run using 8 vCPUs (4 cores) only.", | |
| theme="huggingface", | |
| ).launch() |