Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,63 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from PIL import Image
|
3 |
-
import numpy as np
|
4 |
-
from transformers import SamModel, SamProcessor
|
5 |
-
from diffusers import AutoPipelineForInpainting
|
6 |
-
import torch
|
7 |
-
|
8 |
-
#
|
9 |
-
device = "
|
10 |
-
|
11 |
-
model
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
outputs
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
)
|
61 |
-
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
from transformers import SamModel, SamProcessor
|
5 |
+
from diffusers import AutoPipelineForInpainting
|
6 |
+
import torch
|
7 |
+
|
8 |
+
# Force the model to use CPU regardless of the availability of CUDA
|
9 |
+
device = "cpu"
|
10 |
+
|
11 |
+
# Update model loading and processing to work on CPU
|
12 |
+
model_name = "facebook/sam-vit-huge"
|
13 |
+
model = SamModel.from_pretrained(model_name).to(device)
|
14 |
+
processor = SamProcessor.from_pretrained(model_name)
|
15 |
+
|
16 |
+
def mask_to_rgb(mask):
|
17 |
+
bg_transparent = np.zeros(mask.shape + (4,), dtype=np.uint8)
|
18 |
+
bg_transparent[mask == 1] = [0, 255, 0, 127]
|
19 |
+
return bg_transparent
|
20 |
+
|
21 |
+
def get_processed_inputs(image, points):
|
22 |
+
input_points = [[list(map(int, point.split(',')))] for point in points.split('|') if point]
|
23 |
+
inputs = processor(image, input_points, return_tensors="pt").to(device)
|
24 |
+
with torch.no_grad():
|
25 |
+
outputs = model(**inputs)
|
26 |
+
masks = processor.image_processor.post_process_masks(
|
27 |
+
outputs.pred_masks.cpu(),
|
28 |
+
inputs["original_sizes"].cpu(),
|
29 |
+
inputs["reshaped_input_sizes"].cpu()
|
30 |
+
)
|
31 |
+
best_mask = masks[0][0][outputs.iou_scores.argmax()]
|
32 |
+
return ~best_mask.cpu().numpy()
|
33 |
+
|
34 |
+
def inpaint(raw_image, input_mask, prompt, negative_prompt=None, seed=74294536, cfgs=7):
|
35 |
+
mask_image = Image.fromarray(input_mask)
|
36 |
+
rand_gen = torch.manual_seed(seed)
|
37 |
+
pipeline = AutoPipelineForInpainting.from_pretrained(
|
38 |
+
"diffusers/stable-diffusion-xl-1.0-inpainting-0.1", torch_dtype=torch.float16
|
39 |
+
)
|
40 |
+
pipeline.enable_model_cpu_offload()
|
41 |
+
image = pipeline(
|
42 |
+
prompt=prompt,
|
43 |
+
image=raw_image,
|
44 |
+
mask_image=mask_image,
|
45 |
+
guidance_scale=cfgs,
|
46 |
+
negative_prompt=negative_prompt,
|
47 |
+
generator=rand_gen
|
48 |
+
).images[0]
|
49 |
+
return image
|
50 |
+
|
51 |
+
# Gradio Interface with Click Events
|
52 |
+
def gradio_interface(image, points):
|
53 |
+
raw_image = Image.fromarray(image).convert("RGB").resize((512, 512))
|
54 |
+
mask = get_processed_inputs(raw_image, points)
|
55 |
+
processed_image = inpaint(raw_image, mask, "a car driving on Mars. Studio lights, 1970s", "artifacts, low quality, distortion")
|
56 |
+
return processed_image, mask_to_rgb(mask)
|
57 |
+
|
58 |
+
iface = gr.Interface(
|
59 |
+
fn=gradio_interface,
|
60 |
+
inputs=["image", gr.Image(shape=(512, 512), image_mode='RGB', source="canvas", tool="sketch")],
|
61 |
+
outputs=["image", "image"]
|
62 |
+
)
|
63 |
+
iface.launch(share=True)
|