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