Fabrice-TIERCELIN
commited on
Commit
•
b0ce11b
1
Parent(s):
399d79a
Upload the space
Browse files- README.md +17 -6
- app.py +322 -0
- requirements.txt +8 -0
README.md
CHANGED
@@ -1,12 +1,23 @@
|
|
1 |
---
|
2 |
-
title: Image
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
|
|
10 |
---
|
11 |
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Image-to-Image SDXL Turbo (any size)
|
3 |
+
emoji: ↕️
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: green
|
6 |
+
tags:
|
7 |
+
- Image-to-Image
|
8 |
+
- Image-2-Image
|
9 |
+
- Img-to-Img
|
10 |
+
- Img-2-Img
|
11 |
+
- SDXL
|
12 |
+
- Stable Diffusion
|
13 |
+
- language models
|
14 |
+
- LLMs
|
15 |
sdk: gradio
|
16 |
+
sdk_version: 4.22.0
|
17 |
app_file: app.py
|
18 |
pinned: false
|
19 |
+
license: mit
|
20 |
+
short_description: Modifies the render of your image, at any resolution, freely
|
21 |
---
|
22 |
|
23 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,322 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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1 |
+
from diffusers import AutoPipelineForImage2Image
|
2 |
+
from PIL import Image, ImageFilter
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
import numpy as np
|
6 |
+
import time
|
7 |
+
import math
|
8 |
+
import random
|
9 |
+
import imageio
|
10 |
+
import torch
|
11 |
+
|
12 |
+
max_64_bit_int = 2**63 - 1
|
13 |
+
|
14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
15 |
+
floatType = torch.float16 if torch.cuda.is_available() else torch.float32
|
16 |
+
variant = "fp16" if torch.cuda.is_available() else None
|
17 |
+
pipe = AutoPipelineForImage2Image.from_pretrained("stabilityai/sdxl-turbo", torch_dtype = floatType, variant = variant)
|
18 |
+
pipe = pipe.to(device)
|
19 |
+
|
20 |
+
def update_seed(is_randomize_seed, seed):
|
21 |
+
if is_randomize_seed:
|
22 |
+
return random.randint(0, max_64_bit_int)
|
23 |
+
return seed
|
24 |
+
|
25 |
+
def toggle_debug(is_debug_mode):
|
26 |
+
if is_debug_mode:
|
27 |
+
return [gr.update(visible = True)]
|
28 |
+
return [gr.update(visible = False)]
|
29 |
+
|
30 |
+
def check(
|
31 |
+
source_img,
|
32 |
+
prompt,
|
33 |
+
negative_prompt,
|
34 |
+
num_inference_steps,
|
35 |
+
guidance_scale,
|
36 |
+
image_guidance_scale,
|
37 |
+
strength,
|
38 |
+
denoising_steps,
|
39 |
+
seed,
|
40 |
+
debug_mode,
|
41 |
+
progress = gr.Progress()
|
42 |
+
):
|
43 |
+
if source_img is None:
|
44 |
+
raise gr.Error("Please provide an image.")
|
45 |
+
|
46 |
+
if prompt is None or prompt == "":
|
47 |
+
raise gr.Error("Please provide a prompt input.")
|
48 |
+
|
49 |
+
def inpaint(
|
50 |
+
source_img,
|
51 |
+
prompt,
|
52 |
+
negative_prompt,
|
53 |
+
num_inference_steps,
|
54 |
+
guidance_scale,
|
55 |
+
image_guidance_scale,
|
56 |
+
strength,
|
57 |
+
denoising_steps,
|
58 |
+
seed,
|
59 |
+
debug_mode,
|
60 |
+
progress = gr.Progress()
|
61 |
+
):
|
62 |
+
check(
|
63 |
+
source_img,
|
64 |
+
prompt,
|
65 |
+
negative_prompt,
|
66 |
+
num_inference_steps,
|
67 |
+
guidance_scale,
|
68 |
+
image_guidance_scale,
|
69 |
+
strength,
|
70 |
+
denoising_steps,
|
71 |
+
seed,
|
72 |
+
debug_mode
|
73 |
+
)
|
74 |
+
start = time.time()
|
75 |
+
progress(0, desc = "Preparing data...")
|
76 |
+
|
77 |
+
if negative_prompt is None:
|
78 |
+
negative_prompt = ""
|
79 |
+
|
80 |
+
if num_inference_steps is None:
|
81 |
+
num_inference_steps = 25
|
82 |
+
|
83 |
+
if guidance_scale is None:
|
84 |
+
guidance_scale = 7
|
85 |
+
|
86 |
+
if image_guidance_scale is None:
|
87 |
+
image_guidance_scale = 1.1
|
88 |
+
|
89 |
+
if strength is None:
|
90 |
+
strength = 0.5
|
91 |
+
|
92 |
+
if denoising_steps is None:
|
93 |
+
denoising_steps = 1000
|
94 |
+
|
95 |
+
if seed is None:
|
96 |
+
seed = random.randint(0, max_64_bit_int)
|
97 |
+
|
98 |
+
random.seed(seed)
|
99 |
+
torch.manual_seed(seed)
|
100 |
+
|
101 |
+
input_image = source_img.convert("RGB")
|
102 |
+
|
103 |
+
original_height, original_width, original_channel = np.array(input_image).shape
|
104 |
+
output_width = original_width
|
105 |
+
output_height = original_height
|
106 |
+
|
107 |
+
# Limited to 1 million pixels
|
108 |
+
if 1024 * 1024 < output_width * output_height:
|
109 |
+
factor = ((1024 * 1024) / (output_width * output_height))**0.5
|
110 |
+
process_width = math.floor(output_width * factor)
|
111 |
+
process_height = math.floor(output_height * factor)
|
112 |
+
|
113 |
+
limitation = " Due to technical limitation, the image have been downscaled and then upscaled.";
|
114 |
+
else:
|
115 |
+
process_width = output_width
|
116 |
+
process_height = output_height
|
117 |
+
|
118 |
+
limitation = "";
|
119 |
+
|
120 |
+
# Width and height must be multiple of 8
|
121 |
+
if (process_width % 8) != 0 or (process_height % 8) != 0:
|
122 |
+
if ((process_width - (process_width % 8) + 8) * (process_height - (process_height % 8) + 8)) <= (1024 * 1024):
|
123 |
+
process_width = process_width - (process_width % 8) + 8
|
124 |
+
process_height = process_height - (process_height % 8) + 8
|
125 |
+
elif (process_height % 8) <= (process_width % 8) and ((process_width - (process_width % 8) + 8) * process_height) <= (1024 * 1024):
|
126 |
+
process_width = process_width - (process_width % 8) + 8
|
127 |
+
process_height = process_height - (process_height % 8)
|
128 |
+
elif (process_width % 8) <= (process_height % 8) and (process_width * (process_height - (process_height % 8) + 8)) <= (1024 * 1024):
|
129 |
+
process_width = process_width - (process_width % 8)
|
130 |
+
process_height = process_height - (process_height % 8) + 8
|
131 |
+
else:
|
132 |
+
process_width = process_width - (process_width % 8)
|
133 |
+
process_height = process_height - (process_height % 8)
|
134 |
+
|
135 |
+
progress(None, desc = "Processing...")
|
136 |
+
output_image = pipe(
|
137 |
+
seeds = [seed],
|
138 |
+
width = process_width,
|
139 |
+
height = process_height,
|
140 |
+
prompt = prompt,
|
141 |
+
negative_prompt = negative_prompt,
|
142 |
+
image = input_image,
|
143 |
+
num_inference_steps = num_inference_steps,
|
144 |
+
guidance_scale = guidance_scale,
|
145 |
+
image_guidance_scale = image_guidance_scale,
|
146 |
+
strength = strength,
|
147 |
+
denoising_steps = denoising_steps,
|
148 |
+
show_progress_bar = True
|
149 |
+
).images[0]
|
150 |
+
|
151 |
+
if limitation != "":
|
152 |
+
output_image = output_image.resize((output_width, output_height))
|
153 |
+
|
154 |
+
if debug_mode == False:
|
155 |
+
input_image = None
|
156 |
+
|
157 |
+
end = time.time()
|
158 |
+
secondes = int(end - start)
|
159 |
+
minutes = secondes // 60
|
160 |
+
secondes = secondes - (minutes * 60)
|
161 |
+
hours = minutes // 60
|
162 |
+
minutes = minutes - (hours * 60)
|
163 |
+
return [
|
164 |
+
output_image,
|
165 |
+
"Start again to get a different result. The new image is " + str(output_width) + " pixels large and " + str(output_height) + " pixels high, so an image of " + f'{output_width * output_height:,}' + " pixels. The image have been generated in " + str(hours) + " h, " + str(minutes) + " min, " + str(secondes) + " sec." + limitation,
|
166 |
+
input_image
|
167 |
+
]
|
168 |
+
|
169 |
+
with gr.Blocks() as interface:
|
170 |
+
gr.Markdown(
|
171 |
+
"""
|
172 |
+
<p style="text-align: center;"><b><big><big><big>Image-to-Image</big></big></big></b></p>
|
173 |
+
<p style="text-align: center;">Modifies the global render of your image, at any resolution, freely, without account, without watermark, without installation, which can be downloaded</p>
|
174 |
+
<br/>
|
175 |
+
<br/>
|
176 |
+
🚀 Powered by <i>SDXL Turbo</i> artificial intellingence. For illustration purpose, not information purpose. The new content is not based on real information but imagination.
|
177 |
+
<br/>
|
178 |
+
<ul>
|
179 |
+
<li>To change the <b>view angle</b> of your image, I recommend to use <i>Zero123</i>,</li>
|
180 |
+
<li>To <b>upscale</b> your image, I recommend to use <i>Ilaria Upscaler</i>,</li>
|
181 |
+
<li>To change one <b>detail</b> on your image, I recommend to use <i>Inpaint SDXL</i>,</li>
|
182 |
+
<li>If you need to enlarge the <b>viewpoint</b> of your image, I recommend you to use <i>Uncrop</i>,</li>
|
183 |
+
<li>To remove the <b>background</b> of your image, I recommend to use <i>BRIA</i>,</li>
|
184 |
+
<li>To make a <b>tile</b> of your image, I recommend to use <i>Make My Image Tile</i>,</li>
|
185 |
+
<li>To modify <b>anything else</b> on your image, I recommend to use <i>Instruct Pix2Pix</i>.</li>
|
186 |
+
</ul>
|
187 |
+
<br/>
|
188 |
+
🐌 Slow process... ~1 hour.<br>You can duplicate this space on a free account, it works on CPU and should also run on CUDA.<br/>
|
189 |
+
<a href='https://huggingface.co/spaces/Fabrice-TIERCELIN/Image-to-Image?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14'></a>
|
190 |
+
<br/>
|
191 |
+
⚖️ You can use, modify and share the generated images but not for commercial uses.
|
192 |
+
|
193 |
+
"""
|
194 |
+
)
|
195 |
+
with gr.Column():
|
196 |
+
source_img = gr.Image(label = "Your image", sources = ["upload", "webcam", "clipboard"], type = "pil")
|
197 |
+
prompt = gr.Textbox(label = "Prompt", info = "Describe the subject, the background and the style of image; 77 token limit", placeholder = "Describe what you want to see in the entire image")
|
198 |
+
strength = gr.Slider(value = 0.5, minimum = 0.01, maximum = 1.0, step = 0.01, label = "Strength", info = "lower=follow the original image, higher=follow the prompt")
|
199 |
+
with gr.Accordion("Advanced options", open = False):
|
200 |
+
negative_prompt = gr.Textbox(label = "Negative prompt", placeholder = "Describe what you do NOT want to see in the entire image", value = "Ugly, malformed, noise, blur, watermark")
|
201 |
+
num_inference_steps = gr.Slider(minimum = 10, maximum = 100, value = 25, step = 1, label = "Number of inference steps", info = "lower=faster, higher=image quality")
|
202 |
+
guidance_scale = gr.Slider(minimum = 1, maximum = 13, value = 7, step = 0.1, label = "Classifier-Free Guidance Scale", info = "lower=image quality, higher=follow the prompt")
|
203 |
+
image_guidance_scale = gr.Slider(minimum = 1, value = 1.1, step = 0.1, label = "Image Guidance Scale", info = "lower=image quality, higher=follow the image")
|
204 |
+
denoising_steps = gr.Slider(minimum = 0, maximum = 1000, value = 1000, step = 1, label = "Denoising", info = "lower=irrelevant result, higher=relevant result")
|
205 |
+
randomize_seed = gr.Checkbox(label = "\U0001F3B2 Randomize seed", value = True, info = "If checked, result is always different")
|
206 |
+
seed = gr.Slider(minimum = 0, maximum = max_64_bit_int, step = 1, randomize = True, label = "Seed")
|
207 |
+
debug_mode = gr.Checkbox(label = "Debug mode", value = False, info = "Show intermediate results")
|
208 |
+
|
209 |
+
submit = gr.Button("Redraw", variant = "primary")
|
210 |
+
|
211 |
+
redrawn_image = gr.Image(label = "Redrawn image")
|
212 |
+
information = gr.Label(label = "Information")
|
213 |
+
original_image = gr.Image(label = "Original image", visible = False)
|
214 |
+
|
215 |
+
submit.click(update_seed, inputs = [
|
216 |
+
randomize_seed, seed
|
217 |
+
], outputs = [
|
218 |
+
seed
|
219 |
+
], queue = False, show_progress = False).then(toggle_debug, debug_mode, [
|
220 |
+
original_image
|
221 |
+
], queue = False, show_progress = False).then(check, inputs = [
|
222 |
+
source_img,
|
223 |
+
prompt,
|
224 |
+
negative_prompt,
|
225 |
+
num_inference_steps,
|
226 |
+
guidance_scale,
|
227 |
+
image_guidance_scale,
|
228 |
+
strength,
|
229 |
+
denoising_steps,
|
230 |
+
seed,
|
231 |
+
debug_mode
|
232 |
+
], outputs = [], queue = False, show_progress = False).success(inpaint, inputs = [
|
233 |
+
source_img,
|
234 |
+
prompt,
|
235 |
+
negative_prompt,
|
236 |
+
num_inference_steps,
|
237 |
+
guidance_scale,
|
238 |
+
image_guidance_scale,
|
239 |
+
strength,
|
240 |
+
denoising_steps,
|
241 |
+
seed,
|
242 |
+
debug_mode
|
243 |
+
], outputs = [
|
244 |
+
redrawn_image,
|
245 |
+
information,
|
246 |
+
original_image
|
247 |
+
], scroll_to_output = True)
|
248 |
+
|
249 |
+
gr.Examples(
|
250 |
+
inputs = [
|
251 |
+
source_img,
|
252 |
+
prompt,
|
253 |
+
negative_prompt,
|
254 |
+
num_inference_steps,
|
255 |
+
guidance_scale,
|
256 |
+
image_guidance_scale,
|
257 |
+
strength,
|
258 |
+
denoising_steps,
|
259 |
+
randomize_seed,
|
260 |
+
seed,
|
261 |
+
debug_mode
|
262 |
+
],
|
263 |
+
outputs = [
|
264 |
+
redrawn_image,
|
265 |
+
information,
|
266 |
+
original_image
|
267 |
+
],
|
268 |
+
examples = [
|
269 |
+
[
|
270 |
+
"./Examples/Example1.png",
|
271 |
+
"Drawn image, line art, illustration",
|
272 |
+
"3d, photo, realistic, noise, blur, watermark",
|
273 |
+
25,
|
274 |
+
7,
|
275 |
+
1.1,
|
276 |
+
0.8,
|
277 |
+
1000,
|
278 |
+
True,
|
279 |
+
42,
|
280 |
+
False
|
281 |
+
],
|
282 |
+
],
|
283 |
+
cache_examples = False,
|
284 |
+
)
|
285 |
+
|
286 |
+
gr.Markdown(
|
287 |
+
"""
|
288 |
+
## How to prompt your image
|
289 |
+
|
290 |
+
To easily read your prompt, start with the subject, then describ the pose or action, then secondary elements, then the background, then the graphical style, then the image quality:
|
291 |
+
```
|
292 |
+
A Vietnamese woman, red clothes, walking, smilling, in the street, a car on the left, in a modern city, photorealistic, 8k
|
293 |
+
```
|
294 |
+
|
295 |
+
You can use round brackets to increase the importance of a part:
|
296 |
+
```
|
297 |
+
A Vietnamese woman, (red clothes), walking, smilling, in the street, a car on the left, in a modern city, photorealistic, 8k
|
298 |
+
```
|
299 |
+
|
300 |
+
You can use several levels of round brackets to even more increase the importance of a part:
|
301 |
+
```
|
302 |
+
A Vietnamese woman, ((red clothes)), (walking), smilling, in the street, a car on the left, in a modern city, photorealistic, 8k
|
303 |
+
```
|
304 |
+
|
305 |
+
You can use number instead of several round brackets:
|
306 |
+
```
|
307 |
+
A Vietnamese woman, (red clothes:1.5), (walking), smilling, in the street, a car on the left, in a modern city, photorealistic, 8k
|
308 |
+
```
|
309 |
+
|
310 |
+
You can do the same thing with square brackets to decrease the importance of a part:
|
311 |
+
```
|
312 |
+
A [Vietnamese] woman, (red clothes:1.5), (walking), smilling, in the street, a car on the left, in a modern city, photorealistic, 8k
|
313 |
+
```
|
314 |
+
|
315 |
+
To easily read your negative prompt, organize it the same way as your prompt (not important for the AI):
|
316 |
+
```
|
317 |
+
man, boy, hat, running, tree, bicycle, forest, drawing, painting, cartoon, 3d, monochrome, blurry, noisy, bokeh
|
318 |
+
```
|
319 |
+
"""
|
320 |
+
)
|
321 |
+
|
322 |
+
interface.queue().launch()
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torchvision
|
2 |
+
diffusers
|
3 |
+
transformers
|
4 |
+
accelerate
|
5 |
+
ftfy
|
6 |
+
scipy
|
7 |
+
imageio
|
8 |
+
invisible_watermark
|