Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
from transformers import CLIPSegProcessor, CLIPSegForImageSegmentation
|
| 5 |
+
from diffusers import DiffusionPipeline
|
| 6 |
+
from torch import autocast
|
| 7 |
+
|
| 8 |
+
url = "https://github.com/timojl/clipseg/blob/master/example_image.jpg?raw=true"
|
| 9 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
| 10 |
+
image
|
| 11 |
+
|
| 12 |
+
processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
|
| 13 |
+
model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined")
|
| 14 |
+
|
| 15 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 16 |
+
"runwayml/stable-diffusion-inpainting",
|
| 17 |
+
custom_pipeline="text_inpainting",
|
| 18 |
+
segmentation_model=model,
|
| 19 |
+
segmentation_processor=processor
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 23 |
+
pipe = pipe.to(device)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def process_image(image, text, prompt):
|
| 27 |
+
image = image.resize((512, 512))
|
| 28 |
+
with autocast("cuda"):
|
| 29 |
+
inpainted_image = pipe(image=image, text=text, prompt=prompt).images[0]
|
| 30 |
+
return inpainted_image
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
title = "Interactive demo: Text-based inpainting with CLIPSeg x Stable Diffusion"
|
| 34 |
+
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."
|
| 35 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2112.10003'>CLIPSeg: Image Segmentation Using Text and Image Prompts</a> | <a href='https://huggingface.co/docs/transformers/main/en/model_doc/clipseg'>HuggingFace docs</a></p>"
|
| 36 |
+
|
| 37 |
+
examples = [["example_image.png", "a glass", "a cup"]]
|
| 38 |
+
|
| 39 |
+
interface = gr.Interface(fn=process_image,
|
| 40 |
+
inputs=[gr.Image(type="pil"), gr.Textbox(label="text"), gr.Textbox(label="prompt")],
|
| 41 |
+
outputs=gr.Image(type="pil"),
|
| 42 |
+
title=title,
|
| 43 |
+
description=description,
|
| 44 |
+
article=article,
|
| 45 |
+
examples=examples)
|
| 46 |
+
|
| 47 |
+
interface.launch(debug=True)
|