Spaces:
Running
on
Zero
Running
on
Zero
LuJingyi-John
commited on
Commit
·
4b2ec41
1
Parent(s):
722e880
Add title and project link to interface
Browse files- app.py +4 -0
- utils/drag.py +3 -0
- utils/ui_utils.py +3 -0
app.py
CHANGED
|
@@ -8,6 +8,10 @@ DEFAULT_GEN_SIZE = 512
|
|
| 8 |
|
| 9 |
def create_interface():
|
| 10 |
with gr.Blocks() as app:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# State variables
|
| 12 |
state = {
|
| 13 |
'canvas_size': gr.Number(value=CANVAS_SIZE, visible=False, precision=0),
|
|
|
|
| 8 |
|
| 9 |
def create_interface():
|
| 10 |
with gr.Blocks() as app:
|
| 11 |
+
# Add main title and project link
|
| 12 |
+
gr.Markdown("# A simplified implementation of Inpaint4Drag")
|
| 13 |
+
gr.Markdown("## [Visit our project page for more examples](https://visual-ai.github.io/inpaint4drag)")
|
| 14 |
+
|
| 15 |
# State variables
|
| 16 |
state = {
|
| 17 |
'canvas_size': gr.Number(value=CANVAS_SIZE, visible=False, precision=0),
|
utils/drag.py
CHANGED
|
@@ -3,6 +3,8 @@ import cv2
|
|
| 3 |
import torch
|
| 4 |
from typing import Union
|
| 5 |
|
|
|
|
|
|
|
| 6 |
def contour_to_points_and_mask(contour: np.ndarray, image_shape: tuple) -> tuple[np.ndarray, np.ndarray]:
|
| 7 |
"""Convert a contour to a set of points and binary mask.
|
| 8 |
|
|
@@ -187,6 +189,7 @@ def get_points_within_image_bounds(
|
|
| 187 |
|
| 188 |
return valid_points_mask
|
| 189 |
|
|
|
|
| 190 |
def bi_warp(
|
| 191 |
region_mask: np.ndarray,
|
| 192 |
control_points: Union[np.ndarray, torch.Tensor],
|
|
|
|
| 3 |
import torch
|
| 4 |
from typing import Union
|
| 5 |
|
| 6 |
+
import spaces
|
| 7 |
+
|
| 8 |
def contour_to_points_and_mask(contour: np.ndarray, image_shape: tuple) -> tuple[np.ndarray, np.ndarray]:
|
| 9 |
"""Convert a contour to a set of points and binary mask.
|
| 10 |
|
|
|
|
| 189 |
|
| 190 |
return valid_points_mask
|
| 191 |
|
| 192 |
+
@spaces.GPU
|
| 193 |
def bi_warp(
|
| 194 |
region_mask: np.ndarray,
|
| 195 |
control_points: Union[np.ndarray, torch.Tensor],
|
utils/ui_utils.py
CHANGED
|
@@ -12,6 +12,7 @@ from diffusers import AutoPipelineForInpainting, AutoencoderTiny, LCMScheduler
|
|
| 12 |
|
| 13 |
from utils.drag import bi_warp
|
| 14 |
|
|
|
|
| 15 |
|
| 16 |
__all__ = [
|
| 17 |
'clear_all', 'resize',
|
|
@@ -219,6 +220,7 @@ def preview_out_image(canvas, points, inpaint_ks):
|
|
| 219 |
return image, None
|
| 220 |
|
| 221 |
# Inpaint tools
|
|
|
|
| 222 |
def setup_pipeline(device='cuda', model_version='v1-5'):
|
| 223 |
"""Initialize optimized inpainting pipeline with specified model configuration."""
|
| 224 |
MODEL_CONFIGS = {
|
|
@@ -273,6 +275,7 @@ def get_pipeline():
|
|
| 273 |
pipe.cached_prompt_embeds = pipe.encode_prompt('', 'cpu', 1, False)[0]
|
| 274 |
return pipe
|
| 275 |
|
|
|
|
| 276 |
def inpaint(image, inpaint_mask):
|
| 277 |
"""Perform efficient inpainting on masked regions using Stable Diffusion."""
|
| 278 |
if image is None:
|
|
|
|
| 12 |
|
| 13 |
from utils.drag import bi_warp
|
| 14 |
|
| 15 |
+
import spaces
|
| 16 |
|
| 17 |
__all__ = [
|
| 18 |
'clear_all', 'resize',
|
|
|
|
| 220 |
return image, None
|
| 221 |
|
| 222 |
# Inpaint tools
|
| 223 |
+
@spaces.GPU
|
| 224 |
def setup_pipeline(device='cuda', model_version='v1-5'):
|
| 225 |
"""Initialize optimized inpainting pipeline with specified model configuration."""
|
| 226 |
MODEL_CONFIGS = {
|
|
|
|
| 275 |
pipe.cached_prompt_embeds = pipe.encode_prompt('', 'cpu', 1, False)[0]
|
| 276 |
return pipe
|
| 277 |
|
| 278 |
+
@spaces.GPU
|
| 279 |
def inpaint(image, inpaint_mask):
|
| 280 |
"""Perform efficient inpainting on masked regions using Stable Diffusion."""
|
| 281 |
if image is None:
|