Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,154 +1,174 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
|
5 |
-
|
6 |
-
|
7 |
import torch
|
|
|
8 |
|
|
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
margin: 0 auto;
|
63 |
max-width: 640px;
|
64 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
"""
|
66 |
|
67 |
-
with gr.Blocks(css=css) as demo:
|
68 |
-
with gr.Column(
|
69 |
-
gr.
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
step=32,
|
108 |
-
value=1024, # Replace with defaults that work for your model
|
109 |
-
)
|
110 |
-
|
111 |
-
height = gr.Slider(
|
112 |
-
label="Height",
|
113 |
-
minimum=256,
|
114 |
-
maximum=MAX_IMAGE_SIZE,
|
115 |
-
step=32,
|
116 |
-
value=1024, # Replace with defaults that work for your model
|
117 |
-
)
|
118 |
-
|
119 |
-
with gr.Row():
|
120 |
-
guidance_scale = gr.Slider(
|
121 |
-
label="Guidance scale",
|
122 |
-
minimum=0.0,
|
123 |
-
maximum=10.0,
|
124 |
-
step=0.1,
|
125 |
-
value=0.0, # Replace with defaults that work for your model
|
126 |
-
)
|
127 |
-
|
128 |
-
num_inference_steps = gr.Slider(
|
129 |
-
label="Number of inference steps",
|
130 |
-
minimum=1,
|
131 |
-
maximum=50,
|
132 |
-
step=1,
|
133 |
-
value=2, # Replace with defaults that work for your model
|
134 |
-
)
|
135 |
-
|
136 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
137 |
gr.on(
|
138 |
-
triggers=[
|
139 |
-
fn=
|
140 |
-
inputs=[
|
141 |
-
|
142 |
-
negative_prompt,
|
143 |
-
seed,
|
144 |
-
randomize_seed,
|
145 |
-
width,
|
146 |
-
height,
|
147 |
-
guidance_scale,
|
148 |
-
num_inference_steps,
|
149 |
-
],
|
150 |
-
outputs=[result, seed],
|
151 |
)
|
152 |
|
153 |
if __name__ == "__main__":
|
154 |
demo.launch()
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
import gradio as gr
|
3 |
+
import re
|
4 |
+
from PIL import Image
|
5 |
|
6 |
+
import os
|
7 |
+
import numpy as np
|
8 |
import torch
|
9 |
+
from diffusers import FluxImg2ImgPipeline
|
10 |
|
11 |
+
dtype = torch.bfloat16
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
|
14 |
+
pipe = FluxImg2ImgPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16).to(device)
|
15 |
+
|
16 |
+
|
17 |
+
|
18 |
+
def sanitize_prompt(prompt):
|
19 |
+
# Allow only alphanumeric characters, spaces, and basic punctuation
|
20 |
+
allowed_chars = re.compile(r"[^a-zA-Z0-9\s.,!?-]")
|
21 |
+
sanitized_prompt = allowed_chars.sub("", prompt)
|
22 |
+
return sanitized_prompt
|
23 |
+
|
24 |
+
def convert_to_fit_size(original_width_and_height, maximum_size = 2048):
|
25 |
+
width, height =original_width_and_height
|
26 |
+
if width <= maximum_size and height <= maximum_size:
|
27 |
+
return width,height
|
28 |
+
|
29 |
+
if width > height:
|
30 |
+
scaling_factor = maximum_size / width
|
31 |
+
else:
|
32 |
+
scaling_factor = maximum_size / height
|
33 |
+
|
34 |
+
new_width = int(width * scaling_factor)
|
35 |
+
new_height = int(height * scaling_factor)
|
36 |
+
return new_width, new_height
|
37 |
+
|
38 |
+
def adjust_to_multiple_of_32(width: int, height: int):
|
39 |
+
width = width - (width % 32)
|
40 |
+
height = height - (height % 32)
|
41 |
+
return width, height
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
@spaces.GPU(duration=120)
|
47 |
+
def process_images(image,prompt="a girl",strength=0.75,seed=0,inference_step=4,progress=gr.Progress(track_tqdm=True)):
|
48 |
+
#print("start process_images")
|
49 |
+
progress(0, desc="Starting")
|
50 |
+
|
51 |
+
|
52 |
+
def process_img2img(image,prompt="a person",strength=0.75,seed=0,num_inference_steps=4):
|
53 |
+
#print("start process_img2img")
|
54 |
+
if image == None:
|
55 |
+
print("empty input image returned")
|
56 |
+
return None
|
57 |
+
|
58 |
+
generators = []
|
59 |
+
generator = torch.Generator(device).manual_seed(seed)
|
60 |
+
generators.append(generator)
|
61 |
+
fit_width,fit_height = convert_to_fit_size(image.size)
|
62 |
+
#print(f"fit {width}x{height}")
|
63 |
+
width,height = adjust_to_multiple_of_32(fit_width,fit_height)
|
64 |
+
#print(f"multiple {width}x{height}")
|
65 |
+
image = image.resize((width, height), Image.LANCZOS)
|
66 |
+
#mask_image = mask_image.resize((width, height), Image.NEAREST)
|
67 |
+
|
68 |
+
# more parameter see https://huggingface.co/docs/diffusers/api/pipelines/flux#diffusers.FluxInpaintPipeline
|
69 |
+
#print(prompt)
|
70 |
+
output = pipe(prompt=prompt, image=image,generator=generator,strength=strength,width=width,height=height
|
71 |
+
,guidance_scale=0,num_inference_steps=num_inference_steps,max_sequence_length=256)
|
72 |
+
|
73 |
+
pil_image = output.images[0]#Image.fromarray()
|
74 |
+
new_width,new_height = pil_image.size
|
75 |
+
|
76 |
+
# resize back multiple of 32
|
77 |
+
if (new_width!=fit_width) or (new_height!=fit_height):
|
78 |
+
resized_image= pil_image.resize((fit_width,fit_height),Image.LANCZOS)
|
79 |
+
return resized_image
|
80 |
+
|
81 |
+
return pil_image
|
82 |
+
|
83 |
+
output = process_img2img(image,prompt,strength,seed,inference_step)
|
84 |
+
|
85 |
+
#print("end process_images")
|
86 |
+
return output
|
87 |
+
|
88 |
+
|
89 |
+
def read_file(path: str) -> str:
|
90 |
+
with open(path, 'r', encoding='utf-8') as f:
|
91 |
+
content = f.read()
|
92 |
+
|
93 |
+
return content
|
94 |
+
|
95 |
+
|
96 |
+
css="""
|
97 |
+
#col-left {
|
98 |
+
margin: 0 auto;
|
99 |
+
max-width: 640px;
|
100 |
+
}
|
101 |
+
#col-right {
|
102 |
margin: 0 auto;
|
103 |
max-width: 640px;
|
104 |
}
|
105 |
+
.grid-container {
|
106 |
+
display: flex;
|
107 |
+
align-items: center;
|
108 |
+
justify-content: center;
|
109 |
+
gap:10px
|
110 |
+
}
|
111 |
+
|
112 |
+
.image {
|
113 |
+
width: 128px;
|
114 |
+
height: 128px;
|
115 |
+
object-fit: cover;
|
116 |
+
}
|
117 |
+
|
118 |
+
.text {
|
119 |
+
font-size: 16px;
|
120 |
+
}
|
121 |
+
|
122 |
"""
|
123 |
|
124 |
+
with gr.Blocks(css=css, elem_id="demo-container") as demo:
|
125 |
+
with gr.Column():
|
126 |
+
gr.HTML(read_file("demo_header.html"))
|
127 |
+
gr.HTML(read_file("demo_tools.html"))
|
128 |
+
with gr.Row():
|
129 |
+
with gr.Column():
|
130 |
+
image = gr.Image(height=800,sources=['upload','clipboard'],image_mode='RGB', elem_id="image_upload", type="pil", label="Upload")
|
131 |
+
with gr.Row(elem_id="prompt-container", equal_height=False):
|
132 |
+
with gr.Row():
|
133 |
+
prompt = gr.Textbox(label="Prompt",value="a women",placeholder="Your prompt (what you want in place of what is erased)", elem_id="prompt")
|
134 |
+
|
135 |
+
btn = gr.Button("Img2Img", elem_id="run_button",variant="primary")
|
136 |
+
|
137 |
+
with gr.Accordion(label="Advanced Settings", open=False):
|
138 |
+
with gr.Row( equal_height=True):
|
139 |
+
strength = gr.Number(value=0.75, minimum=0, maximum=0.75, step=0.01, label="strength")
|
140 |
+
seed = gr.Number(value=100, minimum=0, step=1, label="seed")
|
141 |
+
inference_step = gr.Number(value=4, minimum=1, step=4, label="inference_step")
|
142 |
+
id_input=gr.Text(label="Name", visible=False)
|
143 |
+
|
144 |
+
with gr.Column():
|
145 |
+
image_out = gr.Image(height=800,sources=[],label="Output", elem_id="output-img",format="jpg")
|
146 |
+
|
147 |
+
|
148 |
+
|
149 |
+
|
150 |
+
|
151 |
+
gr.Examples(
|
152 |
+
examples=[
|
153 |
+
["examples/draw_input.jpg", "examples/draw_output.jpg","a women ,eyes closed,mouth opened"],
|
154 |
+
["examples/draw-gimp_input.jpg", "examples/draw-gimp_output.jpg","a women ,eyes closed,mouth opened"],
|
155 |
+
["examples/gimp_input.jpg", "examples/gimp_output.jpg","a women ,hand on neck"],
|
156 |
+
["examples/inpaint_input.jpg", "examples/inpaint_output.jpg","a women ,hand on neck"]
|
157 |
+
]
|
158 |
+
,
|
159 |
+
inputs=[image,image_out,prompt],
|
160 |
+
)
|
161 |
+
gr.HTML(
|
162 |
+
gr.HTML(read_file("demo_footer.html"))
|
163 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
gr.on(
|
165 |
+
triggers=[btn.click, prompt.submit],
|
166 |
+
fn = process_images,
|
167 |
+
inputs = [image,prompt,strength,seed,inference_step],
|
168 |
+
outputs = [image_out]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
)
|
170 |
|
171 |
if __name__ == "__main__":
|
172 |
demo.launch()
|
173 |
+
|
174 |
+
|