Spaces:
Running
Running
image editor
Browse files- app.py +63 -27
- requirements.txt +1 -0
app.py
CHANGED
@@ -1,15 +1,15 @@
|
|
1 |
-
import
|
|
|
2 |
import torch
|
3 |
import spaces
|
4 |
import gradio as gr
|
5 |
import os
|
6 |
-
from diffusers.pipelines.flux.pipeline_flux_controlnet_inpaint import (
|
7 |
-
|
8 |
-
)
|
9 |
from diffusers.pipelines.flux.pipeline_flux_inpaint import FluxInpaintPipeline
|
10 |
-
from diffusers.models.controlnet_flux import FluxControlNetModel
|
11 |
from controlnet_aux import CannyDetector
|
12 |
-
import psutil
|
13 |
|
14 |
# login hf token
|
15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
@@ -18,6 +18,7 @@ HF_TOKEN = os.getenv("HF_TOKEN")
|
|
18 |
#
|
19 |
# login()
|
20 |
|
|
|
21 |
dtype = torch.bfloat16
|
22 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
23 |
|
@@ -34,37 +35,65 @@ pipe.enable_model_cpu_offload()
|
|
34 |
|
35 |
canny = CannyDetector()
|
36 |
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
memory_total = memory.total / (1024.0**3)
|
43 |
-
return {
|
44 |
-
"percent": f"{memory_percent}%",
|
45 |
-
"used": f"{memory_used:.3f}GB",
|
46 |
-
"total": f"{memory_total:.3f}GB",
|
47 |
-
}
|
48 |
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
def inpaint(
|
52 |
image,
|
53 |
-
mask,
|
54 |
prompt,
|
55 |
strength,
|
56 |
num_inference_steps,
|
57 |
guidance_scale,
|
58 |
controlnet_conditioning_scale,
|
59 |
):
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
image_res = pipe(
|
63 |
prompt,
|
64 |
-
image=
|
65 |
# control_image=canny_image,
|
66 |
# controlnet_conditioning_scale=controlnet_conditioning_scale,
|
67 |
-
mask_image=
|
68 |
strength=strength,
|
69 |
num_inference_steps=num_inference_steps,
|
70 |
guidance_scale=guidance_scale,
|
@@ -75,16 +104,23 @@ def inpaint(
|
|
75 |
|
76 |
with gr.Blocks() as demo:
|
77 |
# gr.LoginButton()
|
78 |
-
with gr.Row():
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
gr.Interface(
|
84 |
fn=inpaint,
|
85 |
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
gr.Image(type="pil", label="Input Image"),
|
87 |
-
gr.Image(type="pil", label="Mask Image"),
|
88 |
gr.Textbox(label="Prompt"),
|
89 |
gr.Slider(0, 1, value=0.95, label="Strength"),
|
90 |
gr.Slider(1, 100, value=50, step=1, label="Number of Inference Steps"),
|
|
|
1 |
+
from typing import Tuple
|
2 |
+
from PIL import Image
|
3 |
import torch
|
4 |
import spaces
|
5 |
import gradio as gr
|
6 |
import os
|
7 |
+
# from diffusers.pipelines.flux.pipeline_flux_controlnet_inpaint import (
|
8 |
+
# FluxControlNetInpaintPipeline,
|
9 |
+
# )
|
10 |
from diffusers.pipelines.flux.pipeline_flux_inpaint import FluxInpaintPipeline
|
11 |
+
# from diffusers.models.controlnet_flux import FluxControlNetModel
|
12 |
from controlnet_aux import CannyDetector
|
|
|
13 |
|
14 |
# login hf token
|
15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
18 |
#
|
19 |
# login()
|
20 |
|
21 |
+
IMAGE_SIZE = 1024
|
22 |
dtype = torch.bfloat16
|
23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
24 |
|
|
|
35 |
|
36 |
canny = CannyDetector()
|
37 |
|
38 |
+
def resize_image_dimensions(
|
39 |
+
original_resolution_wh: Tuple[int, int],
|
40 |
+
maximum_dimension: int = IMAGE_SIZE
|
41 |
+
) -> Tuple[int, int]:
|
42 |
+
width, height = original_resolution_wh
|
43 |
|
44 |
+
# if width <= maximum_dimension and height <= maximum_dimension:
|
45 |
+
# width = width - (width % 32)
|
46 |
+
# height = height - (height % 32)
|
47 |
+
# return width, height
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
if width > height:
|
50 |
+
scaling_factor = maximum_dimension / width
|
51 |
+
else:
|
52 |
+
scaling_factor = maximum_dimension / height
|
53 |
|
54 |
+
new_width = int(width * scaling_factor)
|
55 |
+
new_height = int(height * scaling_factor)
|
56 |
+
|
57 |
+
new_width = new_width - (new_width % 32)
|
58 |
+
new_height = new_height - (new_height % 32)
|
59 |
+
|
60 |
+
return new_width, new_height
|
61 |
+
|
62 |
+
# def get_system_memory():
|
63 |
+
# memory = psutil.virtual_memory()
|
64 |
+
# memory_percent = memory.percent
|
65 |
+
# memory_used = memory.used / (1024.0**3)
|
66 |
+
# memory_total = memory.total / (1024.0**3)
|
67 |
+
# return {
|
68 |
+
# "percent": f"{memory_percent}%",
|
69 |
+
# "used": f"{memory_used:.3f}GB",
|
70 |
+
# "total": f"{memory_total:.3f}GB",
|
71 |
+
# }
|
72 |
+
#
|
73 |
+
|
74 |
+
@spaces.GPU(duration=100)
|
75 |
def inpaint(
|
76 |
image,
|
77 |
+
# mask,
|
78 |
prompt,
|
79 |
strength,
|
80 |
num_inference_steps,
|
81 |
guidance_scale,
|
82 |
controlnet_conditioning_scale,
|
83 |
):
|
84 |
+
|
85 |
+
image = image['background']
|
86 |
+
mask = image['layers'][0]
|
87 |
+
width, height = resize_image_dimensions(original_resolution_wh=image.size)
|
88 |
+
resized_image = image.resize((width, height), Image.LANCZOS)
|
89 |
+
resized_mask = mask.resize((width, height), Image.LANCZOS)
|
90 |
|
91 |
image_res = pipe(
|
92 |
prompt,
|
93 |
+
image=resized_image,
|
94 |
# control_image=canny_image,
|
95 |
# controlnet_conditioning_scale=controlnet_conditioning_scale,
|
96 |
+
mask_image=resized_mask,
|
97 |
strength=strength,
|
98 |
num_inference_steps=num_inference_steps,
|
99 |
guidance_scale=guidance_scale,
|
|
|
104 |
|
105 |
with gr.Blocks() as demo:
|
106 |
# gr.LoginButton()
|
107 |
+
# with gr.Row():
|
108 |
+
# with gr.Column():
|
109 |
+
# gr.Textbox(value="Hello Memory")
|
110 |
+
# with gr.Column():
|
111 |
+
# gr.JSON(get_system_memory, every=1)
|
112 |
gr.Interface(
|
113 |
fn=inpaint,
|
114 |
inputs=[
|
115 |
+
gr.ImageEditor(
|
116 |
+
label='Image',
|
117 |
+
type='pil',
|
118 |
+
sources=["upload", "webcam"],
|
119 |
+
image_mode='RGB',
|
120 |
+
layers=False,
|
121 |
+
brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed")),
|
122 |
gr.Image(type="pil", label="Input Image"),
|
123 |
+
# gr.Image(type="pil", label="Mask Image"),
|
124 |
gr.Textbox(label="Prompt"),
|
125 |
gr.Slider(0, 1, value=0.95, label="Strength"),
|
126 |
gr.Slider(1, 100, value=50, step=1, label="Number of Inference Steps"),
|
requirements.txt
CHANGED
@@ -9,3 +9,4 @@ tokenizers
|
|
9 |
spaces
|
10 |
huggingface_hub
|
11 |
psutil
|
|
|
|
9 |
spaces
|
10 |
huggingface_hub
|
11 |
psutil
|
12 |
+
mediapipe
|