Spaces:
Running
Running
ehristoforu
commited on
Commit
•
261228d
1
Parent(s):
03a0cea
Create generateColab.py
Browse files- engine/generateColab.py +120 -0
engine/generateColab.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import requests
|
3 |
+
import torch
|
4 |
+
import time
|
5 |
+
import gradio as gr
|
6 |
+
from io import BytesIO
|
7 |
+
from PIL import Image
|
8 |
+
import imageio
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
import os
|
11 |
+
|
12 |
+
load_dotenv("config.txt")
|
13 |
+
|
14 |
+
path_to_base_model = "models/checkpoint/gpu-model/base/dreamdrop-v1.safetensors"
|
15 |
+
path_to_inpaint_model = "models/checkpoint/gpu-model/inpaint/dreamdrop-inpainting.safetensors"
|
16 |
+
|
17 |
+
xl = os.getenv("xl")
|
18 |
+
|
19 |
+
if xl == "True":
|
20 |
+
from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline, StableDiffusionXLInpaintPipeline
|
21 |
+
pipe_t2i = StableDiffusionXLPipeline.from_single_file(path_to_base_model, torch_dtype=torch.float16, use_safetensors=True)
|
22 |
+
pipe_t2i = pipe_t2i.to("cuda")
|
23 |
+
|
24 |
+
pipe_i2i = StableDiffusionXLImg2ImgPipeline.from_single_file(path_to_base_model, torch_dtype=torch.float16, use_safetensors=True)
|
25 |
+
pipe_i2i = pipe_i2i.to("cuda")
|
26 |
+
|
27 |
+
pipe_inpaint = StableDiffusionXLInpaintPipeline.from_single_file(path_to_inpaint_model, torch_dtype=torch.float16, use_safetensors=True)
|
28 |
+
pipe_inpaint = pipe_inpaint.to("cuda")
|
29 |
+
else:
|
30 |
+
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, StableDiffusionInpaintPipeline
|
31 |
+
pipe_t2i = StableDiffusionPipeline.from_single_file(path_to_base_model, torch_dtype=torch.float16, use_safetensors=True)
|
32 |
+
pipe_t2i = pipe_t2i.to("cuda")
|
33 |
+
|
34 |
+
pipe_i2i = StableDiffusionImg2ImgPipeline.from_single_file(path_to_base_model, torch_dtype=torch.float16, use_safetensors=True)
|
35 |
+
pipe_i2i = pipe_i2i.to("cuda")
|
36 |
+
|
37 |
+
pipe_inpaint = StableDiffusionInpaintPipeline.from_single_file(path_to_inpaint_model, torch_dtype=torch.float16, use_safetensors=True)
|
38 |
+
pipe_inpaint = pipe_inpaint.to("cuda")
|
39 |
+
|
40 |
+
|
41 |
+
pipe_t2i.load_lora_weights(pretrained_model_name_or_path_or_dict="models/lora", weight_name="epic_noiseoffset.safetensors")
|
42 |
+
pipe_t2i.fuse_lora(lora_scale=0.1)
|
43 |
+
|
44 |
+
pipe_i2i.load_lora_weights(pretrained_model_name_or_path_or_dict="models/lora", weight_name="epic_noiseoffset.safetensors")
|
45 |
+
pipe_i2i.fuse_lora(lora_scale=0.1)
|
46 |
+
|
47 |
+
pipe_inpaint.load_lora_weights(pretrained_model_name_or_path_or_dict="models/lora", weight_name="epic_noiseoffset.safetensors")
|
48 |
+
pipe_inpaint.fuse_lora(lora_scale=0.1)
|
49 |
+
|
50 |
+
|
51 |
+
def gpugen(prompt, mode, guidance, width, height, num_images, i2i_strength, inpaint_strength, i2i_change, inpaint_change, init=None, inpaint_image=None, progress = gr.Progress(track_tqdm=True)):
|
52 |
+
if mode == "Fast":
|
53 |
+
steps = 30
|
54 |
+
elif mode == "High Quality":
|
55 |
+
steps = 45
|
56 |
+
else:
|
57 |
+
steps = 20
|
58 |
+
results = []
|
59 |
+
seed = random.randint(1, 9999999)
|
60 |
+
if not i2i_change and not inpaint_change:
|
61 |
+
num = random.randint(100, 99999)
|
62 |
+
start_time = time.time()
|
63 |
+
for _ in range(num_images):
|
64 |
+
image = pipe_t2i(
|
65 |
+
prompt=prompt,
|
66 |
+
negative_prompt="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
|
67 |
+
num_inference_steps=steps,
|
68 |
+
guidance_scale=guidance,
|
69 |
+
width=width, height=height,
|
70 |
+
seed=seed,
|
71 |
+
).images
|
72 |
+
image[0].save(f"outputs/{num}_txt2img_gpu{_}.jpg")
|
73 |
+
results.append(image[0])
|
74 |
+
end_time = time.time()
|
75 |
+
execution_time = end_time - start_time
|
76 |
+
return results, f"Time taken: {execution_time} sec."
|
77 |
+
elif inpaint_change and not i2i_change:
|
78 |
+
imageio.imwrite("output_image.png", inpaint_image["mask"])
|
79 |
+
|
80 |
+
num = random.randint(100, 99999)
|
81 |
+
start_time = time.time()
|
82 |
+
for _ in range(num_images):
|
83 |
+
image = pipe_inpaint(
|
84 |
+
prompt=prompt,
|
85 |
+
image=inpaint_image["image"],
|
86 |
+
mask_image=inpaint_image["mask"],
|
87 |
+
negative_prompt="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
|
88 |
+
num_inference_steps=steps,
|
89 |
+
guidance_scale=guidance,
|
90 |
+
strength=inpaint_strength,
|
91 |
+
width=width, height=height,
|
92 |
+
seed=seed,
|
93 |
+
).images
|
94 |
+
image[0].save(f"outputs/{num}_inpaint_gpu{_}.jpg")
|
95 |
+
results.append(image[0])
|
96 |
+
end_time = time.time()
|
97 |
+
execution_time = end_time - start_time
|
98 |
+
return results, f"Time taken: {execution_time} sec."
|
99 |
+
|
100 |
+
else:
|
101 |
+
num = random.randint(100, 99999)
|
102 |
+
start_time = time.time()
|
103 |
+
for _ in range(num_images):
|
104 |
+
image = pipe_i2i(
|
105 |
+
prompt=prompt,
|
106 |
+
negative_prompt="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
|
107 |
+
image=init,
|
108 |
+
num_inference_steps=steps,
|
109 |
+
guidance_scale=guidance,
|
110 |
+
width=width, height=height,
|
111 |
+
strength=i2i_strength,
|
112 |
+
seed=seed,
|
113 |
+
).images
|
114 |
+
image[0].save(f"outputs/{num}_img2img_gpu{_}.jpg")
|
115 |
+
results.append(image[0])
|
116 |
+
end_time = time.time()
|
117 |
+
execution_time = end_time - start_time
|
118 |
+
return results, f"Time taken: {execution_time} sec."
|
119 |
+
|
120 |
+
|