from PIL import Image import requests from transformers import CLIPSegProcessor, CLIPSegForImageSegmentation from diffusers import DiffusionPipeline from torch import autocast url = "https://github.com/timojl/clipseg/blob/master/example_image.jpg?raw=true" image = Image.open(requests.get(url, stream=True).raw) image processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined") model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined") pipe = DiffusionPipeline.from_pretrained( "runwayml/stable-diffusion-inpainting", custom_pipeline="text_inpainting", segmentation_model=model, segmentation_processor=processor ) device = "cuda" if torch.cuda.is_available() else "cpu" pipe = pipe.to(device) def process_image(image, text, prompt): image = image.resize((512, 512)) with autocast("cuda"): inpainted_image = pipe(image=image, text=text, prompt=prompt).images[0] return inpainted_image title = "Interactive demo: Text-based inpainting with CLIPSeg x Stable Diffusion" description = "Demo for using CLIPSeg, a CLIP-based model for zero- and one-shot image segmentation. This model can be used to segment things in an image based on text. This way, one can use it to provide a binary mask for Stable Diffusion, which the latter needs to inpaint. To use it, simply upload an image and add a text to mask as well as a text which indicates what to replace, or use one of the examples below and click 'submit'. Results will show up in a few seconds." article = "

CLIPSeg: Image Segmentation Using Text and Image Prompts | HuggingFace docs

" examples = [["example_image.png", "a glass", "a cup"]] interface = gr.Interface(fn=process_image, inputs=[gr.Image(type="pil"), gr.Textbox(label="text"), gr.Textbox(label="prompt")], outputs=gr.Image(type="pil"), title=title, description=description, article=article, examples=examples) interface.launch(debug=True)