Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import spaces | |
| from diffusers import StableDiffusionInstructPix2PixPipeline | |
| from loguru import logger | |
| from PIL import Image | |
| pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained( | |
| "timbrooks/instruct-pix2pix" | |
| ).to("cuda") | |
| def generate( | |
| prompt: str, | |
| init_image: Image.Image, | |
| progress=gr.Progress(), | |
| ): | |
| logger.info(f"Starting image generation: {dict(prompt=prompt, image=init_image)}") | |
| # Downscale the image | |
| init_image.thumbnail((1024, 1024)) | |
| logger.debug(f"Generating image: {dict(prompt=prompt)}") | |
| def progress_callback(pipe, step_index, timestep, callback_kwargs): | |
| logger.trace( | |
| f"Callback: {dict(num_timesteps=pipe.num_timesteps, step_index=step_index, timestep=timestep)}" | |
| ) | |
| progress((step_index + 1, pipe.num_timesteps)) | |
| return callback_kwargs | |
| images = pipe( | |
| prompt=prompt, | |
| image=init_image, | |
| callback_on_step_end=progress_callback, | |
| ).images | |
| return images[0] | |
| demo = gr.Interface( | |
| fn=generate, | |
| inputs=[ | |
| gr.Text(label="Prompt"), | |
| gr.Image(label="Init image", type="pil"), | |
| ], | |
| outputs=[gr.Image(label="Output")], | |
| ) | |
| demo.launch() | |