Spaces:
				
			
			
	
			
			
		Paused
		
	
	
	
			
			
	
	
	
	
		
		
		Paused
		
	| import gradio as gr | |
| import numpy as np | |
| import random | |
| import spaces | |
| import torch | |
| from diffusers import StableDiffusionInstructPix2PixPipeline, EulerAncestralDiscreteScheduler | |
| from diffusers.utils import load_image | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| model_repo_id = "timbrooks/instruct-pix2pix" | |
| if torch.cuda.is_available(): | |
| torch_dtype = torch.float16 | |
| else: | |
| torch_dtype = torch.float32 | |
| pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained( | |
| model_repo_id, | |
| torch_dtype=torch_dtype, | |
| safety_checker=None | |
| ) | |
| pipe = pipe.to(device) | |
| pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config) | |
| MAX_SEED = np.iinfo(np.int32).max | |
| MAX_IMAGE_SIZE = 1024 | |
| def infer( | |
| prompt, | |
| input_image, | |
| negative_prompt, | |
| seed, | |
| randomize_seed, | |
| image_guidance_scale, | |
| guidance_scale, | |
| num_inference_steps, | |
| progress=gr.Progress(track_tqdm=True), | |
| ): | |
| if input_image is None: | |
| return None, seed | |
| if randomize_seed: | |
| seed = random.randint(0, MAX_SEED) | |
| generator = torch.Generator().manual_seed(seed) | |
| # Process the image | |
| if input_image is not None: | |
| width, height = input_image.size | |
| # Ensure width and height are valid for the model | |
| if width > MAX_IMAGE_SIZE: | |
| width = MAX_IMAGE_SIZE | |
| if height > MAX_IMAGE_SIZE: | |
| height = MAX_IMAGE_SIZE | |
| image = pipe( | |
| prompt=prompt, | |
| image=input_image, | |
| negative_prompt=negative_prompt, | |
| guidance_scale=guidance_scale, | |
| image_guidance_scale=image_guidance_scale, | |
| num_inference_steps=num_inference_steps, | |
| generator=generator, | |
| ).images[0] | |
| return image, seed | |
| examples = [ | |
| ["Turn the sky into a sunset", "https://huggingface.co/datasets/patrickvonplaten/images/resolve/main/aa_xl/000000009.png"], | |
| ["Turn him into a cyborg", "https://raw.githubusercontent.com/timothybrooks/instruct-pix2pix/main/imgs/example.jpg"], | |
| ["Make it look like winter", "https://huggingface.co/datasets/patrickvonplaten/images/resolve/main/aa_xl/000000009.png"], | |
| ] | |
| css = """ | |
| #col-container { | |
| margin: 0 auto; | |
| max-width: 840px; | |
| } | |
| """ | |
| with gr.Blocks(css=css) as demo: | |
| with gr.Column(elem_id="col-container"): | |
| gr.Markdown(" # InstructPix2Pix - Image Editing") | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| input_image = gr.Image( | |
| label="Input Image", | |
| type="pil", | |
| height=400 | |
| ) | |
| with gr.Column(scale=1): | |
| result = gr.Image(label="Result", height=400) | |
| prompt = gr.Text( | |
| label="Instruction", | |
| placeholder="Enter your instruction (e.g., 'turn the sky into a sunset')", | |
| ) | |
| run_button = gr.Button("Run", variant="primary") | |
| with gr.Accordion("Advanced Settings", open=False): | |
| negative_prompt = gr.Text( | |
| label="Negative prompt", | |
| max_lines=1, | |
| placeholder="Enter a negative prompt", | |
| ) | |
| with gr.Row(): | |
| image_guidance_scale = gr.Slider( | |
| label="Image guidance scale", | |
| minimum=0.0, | |
| maximum=5.0, | |
| step=0.1, | |
| value=1.0, | |
| ) | |
| guidance_scale = gr.Slider( | |
| label="Guidance scale", | |
| minimum=1.0, | |
| maximum=20.0, | |
| step=0.1, | |
| value=7.5, | |
| ) | |
| seed = gr.Slider( | |
| label="Seed", | |
| minimum=0, | |
| maximum=MAX_SEED, | |
| step=1, | |
| value=0, | |
| ) | |
| randomize_seed = gr.Checkbox(label="Randomize seed", value=True) | |
| num_inference_steps = gr.Slider( | |
| label="Number of inference steps", | |
| minimum=1, | |
| maximum=100, | |
| step=1, | |
| value=20, | |
| ) | |
| gr.Examples( | |
| examples=examples, | |
| inputs=[prompt, input_image], | |
| outputs=[result, seed], | |
| fn=infer, | |
| cache_examples=True, | |
| ) | |
| gr.on( | |
| triggers=[run_button.click], | |
| fn=infer, | |
| inputs=[ | |
| prompt, | |
| input_image, | |
| negative_prompt, | |
| seed, | |
| randomize_seed, | |
| image_guidance_scale, | |
| guidance_scale, | |
| num_inference_steps, | |
| ], | |
| outputs=[result, seed], | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |
