Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
|
4 |
+
import torch
|
5 |
+
from diffusers import AutoPipelineForInpainting
|
6 |
+
from diffusers.utils import load_image
|
7 |
+
|
8 |
+
def draw_on_image(image, prompt):
|
9 |
+
print(image, prompt)
|
10 |
+
|
11 |
+
if not prompt:
|
12 |
+
return
|
13 |
+
|
14 |
+
init_image = load_image(
|
15 |
+
image["image"]
|
16 |
+
)
|
17 |
+
mask_image = load_image(
|
18 |
+
image["mask"]
|
19 |
+
)
|
20 |
+
|
21 |
+
res_image = pipeline(prompt=prompt, image=init_image, mask_image=mask_image).images[0]
|
22 |
+
|
23 |
+
return res_image
|
24 |
+
|
25 |
+
inputs = [
|
26 |
+
gr.Image(tool="sketch", label="Image", type="pil"),
|
27 |
+
gr.Text(max_lines=1)
|
28 |
+
]
|
29 |
+
|
30 |
+
if torch.cuda.is_available():
|
31 |
+
torch_dtype = torch.float32
|
32 |
+
device = "cuda"
|
33 |
+
else:
|
34 |
+
torch_dtype = torch.float16
|
35 |
+
device = "cpu"
|
36 |
+
|
37 |
+
pipeline = AutoPipelineForInpainting.from_pretrained(
|
38 |
+
"kandinsky-community/kandinsky-2-2-decoder-inpaint", torch_dtype=torch_dtype
|
39 |
+
)
|
40 |
+
|
41 |
+
pipeline.to(device)
|
42 |
+
|
43 |
+
app = gr.Interface(draw_on_image, inputs=inputs, outputs="image")
|
44 |
+
app.queue()
|
45 |
+
app.launch(share=True)
|