Spaces:
Running
Running
make a/b tester
Browse files
app.py
CHANGED
@@ -4,8 +4,8 @@
|
|
4 |
# in the Software without restriction, including without limitation the rights
|
5 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
6 |
# copies of the Software, and to permit persons to whom the Software is
|
7 |
-
|
8 |
import spaces
|
|
|
9 |
import os
|
10 |
import random
|
11 |
import uuid
|
@@ -13,17 +13,37 @@ import gradio as gr
|
|
13 |
import numpy as np
|
14 |
from PIL import Image
|
15 |
import torch
|
16 |
-
from diffusers import StableDiffusionXLPipeline,
|
|
|
|
|
|
|
17 |
from typing import Tuple
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
DESCRIPTIONXX = """
|
29 |
## REALVISXL V5.0 BF16 ⚡⚡⚡⚡
|
@@ -41,10 +61,10 @@ MODEL_OPTIONS = {
|
|
41 |
|
42 |
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
43 |
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
44 |
-
ENABLE_CPU_OFFLOAD =
|
45 |
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1"))
|
46 |
|
47 |
-
device = "cpu"
|
48 |
|
49 |
style_list = [
|
50 |
{
|
@@ -72,34 +92,92 @@ style_list = [
|
|
72 |
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
73 |
DEFAULT_STYLE_NAME = "Style Zero"
|
74 |
STYLE_NAMES = list(styles.keys())
|
|
|
|
|
|
|
75 |
|
76 |
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
77 |
if style_name in styles:
|
78 |
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
79 |
else:
|
80 |
p, n = styles[DEFAULT_STYLE_NAME]
|
81 |
-
|
82 |
if not negative:
|
83 |
negative = ""
|
84 |
return p.replace("{prompt}", positive), n + negative
|
85 |
|
86 |
def load_and_prepare_model(model_id):
|
87 |
-
model_dtypes = {
|
88 |
-
"ford442/RealVisXL_V5.0_BF16": torch.bfloat16,
|
89 |
-
}
|
90 |
-
|
91 |
-
# Get the dtype based on the model_id
|
92 |
dtype = model_dtypes.get(model_id, torch.bfloat16) # Default to float32 if not found
|
93 |
-
|
94 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
99 |
add_watermarker=False,
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
return pipe
|
104 |
|
105 |
# Preload and compile both models
|
@@ -107,9 +185,24 @@ models = {key: load_and_prepare_model(value) for key, value in MODEL_OPTIONS.ite
|
|
107 |
|
108 |
MAX_SEED = np.iinfo(np.int32).max
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
def save_image(img):
|
111 |
unique_name = str(uuid.uuid4()) + ".png"
|
112 |
-
img.save(unique_name)
|
113 |
return unique_name
|
114 |
|
115 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
@@ -117,54 +210,211 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
|
117 |
seed = random.randint(0, MAX_SEED)
|
118 |
return seed
|
119 |
|
120 |
-
@spaces.GPU(duration=
|
121 |
-
def
|
122 |
model_choice: str,
|
123 |
prompt: str,
|
124 |
negative_prompt: str = "",
|
125 |
use_negative_prompt: bool = False,
|
126 |
-
style_selection: str =
|
127 |
seed: int = 1,
|
128 |
width: int = 768,
|
129 |
height: int = 768,
|
130 |
guidance_scale: float = 4,
|
131 |
-
num_inference_steps: int =
|
132 |
randomize_seed: bool = False,
|
133 |
use_resolution_binning: bool = True,
|
134 |
num_images: int = 1,
|
135 |
-
progress=gr.Progress(track_tqdm=True)
|
136 |
):
|
|
|
|
|
137 |
global models
|
138 |
pipe = models[model_choice]
|
139 |
-
|
140 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
141 |
-
generator = torch.Generator(device=
|
142 |
-
|
143 |
-
prompt, negative_prompt = apply_style(style_selection, prompt, negative_prompt)
|
144 |
-
|
145 |
options = {
|
146 |
"prompt": [prompt] * num_images,
|
147 |
-
"negative_prompt": [negative_prompt]
|
|
|
148 |
"width": width,
|
149 |
"height": height,
|
150 |
"guidance_scale": guidance_scale,
|
151 |
"num_inference_steps": num_inference_steps,
|
152 |
"generator": generator,
|
|
|
153 |
"output_type": "pil",
|
154 |
}
|
155 |
-
|
156 |
if use_resolution_binning:
|
157 |
options["use_resolution_binning"] = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
images = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
for i in range(0, num_images, BATCH_SIZE):
|
161 |
batch_options = options.copy()
|
162 |
batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
|
163 |
if "negative_prompt" in batch_options:
|
164 |
batch_options["negative_prompt"] = options["negative_prompt"][i:i+BATCH_SIZE]
|
165 |
images.extend(pipe(**batch_options).images)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
image_paths = [save_image(img) for img in images]
|
|
|
|
|
168 |
return image_paths, seed
|
169 |
|
170 |
def load_predefined_images1():
|
@@ -181,23 +431,21 @@ def load_predefined_images1():
|
|
181 |
]
|
182 |
return predefined_images1
|
183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
|
185 |
-
|
186 |
-
# predefined_images = [
|
187 |
-
# "assets2/11.png",
|
188 |
-
# "assets2/22.png",
|
189 |
-
# "assets2/33.png",
|
190 |
-
# "assets2/44.png",
|
191 |
-
# "assets2/55.png",
|
192 |
-
# "assets2/66.png",
|
193 |
-
# "assets2/77.png",
|
194 |
-
# "assets2/88.png",
|
195 |
-
# "assets2/99.png",
|
196 |
-
# ]
|
197 |
-
# return predefined_image
|
198 |
-
|
199 |
-
|
200 |
-
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
201 |
gr.Markdown(DESCRIPTIONXX)
|
202 |
with gr.Row():
|
203 |
prompt = gr.Text(
|
@@ -207,7 +455,9 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
207 |
placeholder="Enter your prompt",
|
208 |
container=False,
|
209 |
)
|
210 |
-
|
|
|
|
|
211 |
result = gr.Gallery(label="Result", columns=1, show_label=False)
|
212 |
|
213 |
with gr.Row():
|
@@ -217,7 +467,6 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
217 |
value="REALVISXL V5.0 BF16"
|
218 |
)
|
219 |
|
220 |
-
with gr.Accordion("Advanced options", open=False, visible=True):
|
221 |
style_selection = gr.Radio(
|
222 |
show_label=True,
|
223 |
container=True,
|
@@ -241,7 +490,7 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
241 |
max_lines=5,
|
242 |
lines=4,
|
243 |
placeholder="Enter a negative prompt",
|
244 |
-
value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs,
|
245 |
visible=True,
|
246 |
)
|
247 |
seed = gr.Slider(
|
@@ -271,9 +520,9 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
271 |
guidance_scale = gr.Slider(
|
272 |
label="Guidance Scale",
|
273 |
minimum=0.1,
|
274 |
-
maximum=
|
275 |
step=0.1,
|
276 |
-
value=4
|
277 |
)
|
278 |
num_inference_steps = gr.Slider(
|
279 |
label="Number of inference steps",
|
@@ -298,12 +547,56 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
298 |
|
299 |
gr.on(
|
300 |
triggers=[
|
301 |
-
|
302 |
-
|
303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
],
|
305 |
-
|
306 |
-
fn=
|
307 |
inputs=[
|
308 |
model_choice,
|
309 |
prompt,
|
@@ -368,7 +661,6 @@ if __name__ == "__main__":
|
|
368 |
outputs=gr.Textbox(label="Text Generated"),
|
369 |
title=title,
|
370 |
description=description,
|
371 |
-
theme="huggingface"
|
372 |
)
|
373 |
|
374 |
combined_interface = gr.TabbedInterface([demo_interface, text_gen_interface], ["Image Generation", "Text Generation"])
|
|
|
4 |
# in the Software without restriction, including without limitation the rights
|
5 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
6 |
# copies of the Software, and to permit persons to whom the Software is
|
|
|
7 |
import spaces
|
8 |
+
|
9 |
import os
|
10 |
import random
|
11 |
import uuid
|
|
|
13 |
import numpy as np
|
14 |
from PIL import Image
|
15 |
import torch
|
16 |
+
from diffusers import AutoencoderKL, StableDiffusionXLPipeline, UNet2DConditionModel
|
17 |
+
from diffusers import EulerAncestralDiscreteScheduler
|
18 |
+
from diffusers import DPMSolverMultistepScheduler
|
19 |
+
|
20 |
from typing import Tuple
|
21 |
+
import paramiko
|
22 |
+
import gc
|
23 |
+
import time
|
24 |
+
import datetime
|
25 |
+
#from diffusers.schedulers import AysSchedules
|
26 |
|
27 |
+
from gradio import themes
|
28 |
+
from hidiffusion import apply_hidiffusion, remove_hidiffusion
|
29 |
+
|
30 |
+
import gc
|
31 |
+
|
32 |
+
torch.backends.cuda.matmul.allow_tf32 = False
|
33 |
+
torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = False
|
34 |
+
torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction = False
|
35 |
+
torch.backends.cudnn.allow_tf32 = False
|
36 |
+
torch.backends.cudnn.deterministic = False
|
37 |
+
#torch.backends.cudnn.benchmark = False
|
38 |
+
torch.backends.cuda.preferred_blas_library="cublas"
|
39 |
+
# torch.backends.cuda.preferred_linalg_library="cusolver"
|
40 |
+
|
41 |
+
torch.set_float32_matmul_precision("highest")
|
42 |
+
|
43 |
+
FTP_HOST = "1ink.us"
|
44 |
+
FTP_USER = "ford442"
|
45 |
+
FTP_PASS = "GoogleBez12!"
|
46 |
+
FTP_DIR = "1ink.us/stable_diff/" # Remote directory on FTP server
|
47 |
|
48 |
DESCRIPTIONXX = """
|
49 |
## REALVISXL V5.0 BF16 ⚡⚡⚡⚡
|
|
|
61 |
|
62 |
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
63 |
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
64 |
+
ENABLE_CPU_OFFLOAD = 0
|
65 |
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1"))
|
66 |
|
67 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
68 |
|
69 |
style_list = [
|
70 |
{
|
|
|
92 |
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
93 |
DEFAULT_STYLE_NAME = "Style Zero"
|
94 |
STYLE_NAMES = list(styles.keys())
|
95 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
96 |
+
|
97 |
+
#sampling_schedule = AysSchedules["StableDiffusionXLTimesteps"]
|
98 |
|
99 |
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
100 |
if style_name in styles:
|
101 |
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
102 |
else:
|
103 |
p, n = styles[DEFAULT_STYLE_NAME]
|
|
|
104 |
if not negative:
|
105 |
negative = ""
|
106 |
return p.replace("{prompt}", positive), n + negative
|
107 |
|
108 |
def load_and_prepare_model(model_id):
|
109 |
+
model_dtypes = {"ford442/RealVisXL_V5.0_BF16": torch.bfloat16,}
|
|
|
|
|
|
|
|
|
110 |
dtype = model_dtypes.get(model_id, torch.bfloat16) # Default to float32 if not found
|
111 |
+
#vae = AutoencoderKL.from_pretrained("ford442/sdxl-vae-bf16", safety_checker=None)
|
112 |
+
#vae = AutoencoderKL.from_pretrained("stabilityai/sdxl-vae")
|
113 |
+
#vae = AutoencoderKL.from_pretrained('cross-attention/asymmetric-autoencoder-kl-x-2',use_safetensors=False)
|
114 |
+
#vae = AutoencoderKL.from_single_file('https://huggingface.co/ford442/sdxl-vae-bf16/mySLR/myslrVAE_v10.safetensors')
|
115 |
+
#vaeX = AutoencoderKL.from_pretrained("stabilityai/sd-vae-ft-mse",use_safetensors=True)
|
116 |
+
#vaeX = AutoencoderKL.from_pretrained('ford442/Juggernaut-XI-v11-fp32',subfolder='vae').to(torch.bfloat16) # ,use_safetensors=True FAILS
|
117 |
+
#vaeX = AutoencoderKL.from_pretrained('ford442/RealVisXL_V5.0_FP64',subfolder='vae').to(torch.bfloat16) # ,use_safetensors=True FAILS
|
118 |
+
#unetX = UNet2DConditionModel.from_pretrained('ford442/RealVisXL_V5.0_BF16',subfolder='unet').to(torch.bfloat16) # ,use_safetensors=True FAILS
|
119 |
+
# vae = AutoencoderKL.from_pretrained("BeastHF/MyBack_SDXL_Juggernaut_XL_VAE/MyBack_SDXL_Juggernaut_XL_VAE_V10(version_X).safetensors",safety_checker=None).to(torch.bfloat16)
|
120 |
+
#sched = EulerAncestralDiscreteScheduler.from_pretrained("SG161222/RealVisXL_V5.0", subfolder='scheduler',beta_schedule="scaled_linear", steps_offset=1,timestep_spacing="trailing"))
|
121 |
+
#sched = EulerAncestralDiscreteScheduler.from_pretrained("SG161222/RealVisXL_V5.0", subfolder='scheduler', steps_offset=1,timestep_spacing="trailing")
|
122 |
+
sched = EulerAncestralDiscreteScheduler.from_pretrained('SG161222/RealVisXL_V5.0', subfolder='scheduler',beta_schedule="scaled_linear", beta_start=0.00085, beta_end=0.012, steps_offset=1,use_karras_sigmas=True)
|
123 |
+
#sched = EulerAncestralDiscreteScheduler.from_pretrained('SG161222/RealVisXL_V5.0', subfolder='scheduler')
|
124 |
+
#pipeX = StableDiffusionXLPipeline.from_pretrained("SG161222/RealVisXL_V5.0").to(torch.bfloat16)
|
125 |
+
#pipeX = StableDiffusionXLPipeline.from_pretrained("ford442/Juggernaut-XI-v11-fp32",use_safetensors=True)
|
126 |
+
|
127 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
128 |
+
#'ford442/RealVisXL_V5.0_BF16',
|
129 |
+
# 'ford442/Juggernaut-XI-v11-fp32',
|
130 |
+
'SG161222/RealVisXL_V5.0',
|
131 |
+
#torch_dtype=torch.bfloat16,
|
132 |
add_watermarker=False,
|
133 |
+
# custom_pipeline="lpw_stable_diffusion_xl",
|
134 |
+
#use_safetensors=True,
|
135 |
+
# use_auth_token=HF_TOKEN,
|
136 |
+
# vae=AutoencoderKL.from_pretrained("BeastHF/MyBack_SDXL_Juggernaut_XL_VAE/MyBack_SDXL_Juggernaut_XL_VAE_V10(version_X).safetensors",repo_type='model',safety_checker=None),
|
137 |
+
# vae=AutoencoderKL.from_pretrained("stabilityai/sdxl-vae",repo_type='model',safety_checker=None, torch_dtype=torch.float32),
|
138 |
+
# vae=AutoencoderKL.from_pretrained("ford442/sdxl-vae-bf16",repo_type='model',safety_checker=None),
|
139 |
+
#vae=vae,
|
140 |
+
#unet=pipeX.unet,
|
141 |
+
#scheduler = sched,
|
142 |
+
# scheduler = EulerAncestralDiscreteScheduler.from_config(pipeX.scheduler.config, beta_schedule="scaled_linear", beta_start=0.00085, beta_end=0.012, steps_offset=1)
|
143 |
+
#scheduler=EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config, beta_schedule="scaled_linear", beta_start=0.00085, beta_end=0.012, steps_offset =1)
|
144 |
+
)
|
145 |
+
|
146 |
+
#sched = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config, beta_schedule="scaled_linear",use_karras_sigmas=True, algorithm_type="dpmsolver++")
|
147 |
+
#pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config, beta_schedule="scaled_linear", beta_start=0.00085, beta_end=0.012, steps_offset=1)
|
148 |
+
#pipe.scheduler = DPMSolverMultistepScheduler.from_pretrained('SG161222/RealVisXL_V5.0', subfolder='scheduler', algorithm_type='sde-dpmsolver++')
|
149 |
+
#pipe.vae = vaeX
|
150 |
+
#pipe.unet = unetX
|
151 |
+
#pipe.vae.do_resize=False
|
152 |
+
pipe.scheduler = sched
|
153 |
+
#pipe.vae=vae.to(torch.bfloat16)
|
154 |
+
#pipe.unet=pipeX.unet
|
155 |
+
#pipe.scheduler=EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config, beta_schedule="scaled_linear", beta_start=0.00085, beta_end=0.012, steps_offset=1)
|
156 |
+
|
157 |
+
pipe.to(device)
|
158 |
+
pipe.to(torch.bfloat16)
|
159 |
+
|
160 |
+
apply_hidiffusion(pipe)
|
161 |
|
162 |
+
#pipe.unet.set_default_attn_processor()
|
163 |
+
#pipe.vae.set_default_attn_processor()
|
164 |
+
|
165 |
+
print(f'Pipeline: ')
|
166 |
+
print(f'_optional_components: {pipe._optional_components}')
|
167 |
+
print(f'watermark: {pipe.watermark}')
|
168 |
+
print(f'image_processor: {pipe.image_processor}')
|
169 |
+
print(f'feature_extractor: {pipe.feature_extractor}')
|
170 |
+
print(f'init noise scale: {pipe.scheduler.init_noise_sigma}')
|
171 |
+
#print(f'UNET: {pipe.unet}')
|
172 |
+
pipe.watermark=None
|
173 |
+
pipe.safety_checker=None
|
174 |
+
#pipe.to(torch.device("cuda:0"))
|
175 |
+
#pipe.vae.to(torch.bfloat16)
|
176 |
+
#pipe.to(device, torch.bfloat16)
|
177 |
+
#del pipeX
|
178 |
+
#sched = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config, beta_schedule="scaled_linear", algorithm_type="dpmsolver++")
|
179 |
+
#sched = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config, beta_schedule="linear", algorithm_type="dpmsolver++")
|
180 |
+
#sched = DDIMScheduler.from_config(pipe.scheduler.config)
|
181 |
return pipe
|
182 |
|
183 |
# Preload and compile both models
|
|
|
185 |
|
186 |
MAX_SEED = np.iinfo(np.int32).max
|
187 |
|
188 |
+
neg_prompt_2 = " 'non-photorealistic':1.5, 'unrealistic skin','unattractive face':1.3, 'low quality':1.1, ('dull color scheme', 'dull colors', 'digital noise':1.2),'amateurish', 'poorly drawn face':1.3, 'poorly drawn', 'distorted face', 'low resolution', 'simplistic' "
|
189 |
+
|
190 |
+
def upload_to_ftp(filename):
|
191 |
+
try:
|
192 |
+
transport = paramiko.Transport((FTP_HOST, 22))
|
193 |
+
destination_path=FTP_DIR+filename
|
194 |
+
transport.connect(username = FTP_USER, password = FTP_PASS)
|
195 |
+
sftp = paramiko.SFTPClient.from_transport(transport)
|
196 |
+
sftp.put(filename, destination_path)
|
197 |
+
sftp.close()
|
198 |
+
transport.close()
|
199 |
+
print(f"Uploaded {filename} to FTP server")
|
200 |
+
except Exception as e:
|
201 |
+
print(f"FTP upload error: {e}")
|
202 |
+
|
203 |
def save_image(img):
|
204 |
unique_name = str(uuid.uuid4()) + ".png"
|
205 |
+
img.save(unique_name,optimize=False,compress_level=0)
|
206 |
return unique_name
|
207 |
|
208 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
|
|
210 |
seed = random.randint(0, MAX_SEED)
|
211 |
return seed
|
212 |
|
213 |
+
@spaces.GPU(duration=30)
|
214 |
+
def generate_30(
|
215 |
model_choice: str,
|
216 |
prompt: str,
|
217 |
negative_prompt: str = "",
|
218 |
use_negative_prompt: bool = False,
|
219 |
+
style_selection: str = "",
|
220 |
seed: int = 1,
|
221 |
width: int = 768,
|
222 |
height: int = 768,
|
223 |
guidance_scale: float = 4,
|
224 |
+
num_inference_steps: int = 125,
|
225 |
randomize_seed: bool = False,
|
226 |
use_resolution_binning: bool = True,
|
227 |
num_images: int = 1,
|
228 |
+
progress=gr.Progress(track_tqdm=True) # Add progress as a keyword argument
|
229 |
):
|
230 |
+
torch.cuda.empty_cache()
|
231 |
+
gc.collect()
|
232 |
global models
|
233 |
pipe = models[model_choice]
|
|
|
234 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
235 |
+
generator = torch.Generator(device='cuda').manual_seed(seed)
|
236 |
+
#prompt, negative_prompt = apply_style(style_selection, prompt, negative_prompt)
|
|
|
|
|
237 |
options = {
|
238 |
"prompt": [prompt] * num_images,
|
239 |
+
"negative_prompt": [negative_prompt],
|
240 |
+
"negative_prompt_2": [neg_prompt_2],
|
241 |
"width": width,
|
242 |
"height": height,
|
243 |
"guidance_scale": guidance_scale,
|
244 |
"num_inference_steps": num_inference_steps,
|
245 |
"generator": generator,
|
246 |
+
# "timesteps": sampling_schedule,
|
247 |
"output_type": "pil",
|
248 |
}
|
|
|
249 |
if use_resolution_binning:
|
250 |
options["use_resolution_binning"] = True
|
251 |
+
images = []
|
252 |
+
pipe.scheduler.set_timesteps(num_inference_steps,device)
|
253 |
+
# write note txt
|
254 |
+
filename= f'tst_{seed}.txt'
|
255 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
256 |
+
with open(filename, "w") as f:
|
257 |
+
f.write(f"Realvis 5.0: {seed} png\n")
|
258 |
+
f.write(f"Date/time: {timestamp} \n")
|
259 |
+
f.write(f"Prompt: {prompt} \n")
|
260 |
+
f.write(f"Steps: {num_inference_steps} \n")
|
261 |
+
f.write(f"Guidance Scale: {guidance_scale} \n")
|
262 |
+
f.write(f"SPACE SETUP: \n")
|
263 |
+
f.write(f"Use Model Dtype: no \n")
|
264 |
+
f.write(f"Model Scheduler: Euler_a custom before cuda \n")
|
265 |
+
f.write(f"Model VAE: FP64 \n")
|
266 |
+
f.write(f"Model UNET: FP64 \n")
|
267 |
+
upload_to_ftp(filename)
|
268 |
+
for i in range(0, num_images, BATCH_SIZE):
|
269 |
+
batch_options = options.copy()
|
270 |
+
batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
|
271 |
+
if "negative_prompt" in batch_options:
|
272 |
+
batch_options["negative_prompt"] = options["negative_prompt"][i:i+BATCH_SIZE]
|
273 |
+
images.extend(pipe(**batch_options).images)
|
274 |
+
sd_image_path = f"rv50_{seed}.png"
|
275 |
+
images[0].save(sd_image_path,optimize=False,compress_level=0)
|
276 |
+
upload_to_ftp(sd_image_path)
|
277 |
+
image_paths = [save_image(img) for img in images]
|
278 |
+
torch.cuda.empty_cache()
|
279 |
+
gc.collect()
|
280 |
+
return image_paths, seed
|
281 |
|
282 |
+
@spaces.GPU(duration=60)
|
283 |
+
def generate_60(
|
284 |
+
model_choice: str,
|
285 |
+
prompt: str,
|
286 |
+
negative_prompt: str = "",
|
287 |
+
use_negative_prompt: bool = False,
|
288 |
+
style_selection: str = "",
|
289 |
+
seed: int = 1,
|
290 |
+
width: int = 768,
|
291 |
+
height: int = 768,
|
292 |
+
guidance_scale: float = 4,
|
293 |
+
num_inference_steps: int = 250,
|
294 |
+
randomize_seed: bool = False,
|
295 |
+
use_resolution_binning: bool = True,
|
296 |
+
num_images: int = 1,
|
297 |
+
progress=gr.Progress(track_tqdm=True) # Add progress as a keyword argument
|
298 |
+
):
|
299 |
+
torch.cuda.empty_cache()
|
300 |
+
gc.collect()
|
301 |
+
global models
|
302 |
+
pipe = models[model_choice]
|
303 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
304 |
+
generator = torch.Generator(device='cuda').manual_seed(seed)
|
305 |
+
#prompt, negative_prompt = apply_style(style_selection, prompt, negative_prompt)
|
306 |
+
options = {
|
307 |
+
"prompt": [prompt] * num_images,
|
308 |
+
"negative_prompt": [negative_prompt],
|
309 |
+
"negative_prompt_2": [neg_prompt_2],
|
310 |
+
"width": width,
|
311 |
+
"height": height,
|
312 |
+
"guidance_scale": guidance_scale,
|
313 |
+
"num_inference_steps": num_inference_steps,
|
314 |
+
"generator": generator,
|
315 |
+
# "timesteps": sampling_schedule,
|
316 |
+
"output_type": "pil",
|
317 |
+
}
|
318 |
+
if use_resolution_binning:
|
319 |
+
options["use_resolution_binning"] = True
|
320 |
images = []
|
321 |
+
pipe.scheduler.set_timesteps(num_inference_steps,device)
|
322 |
+
# write note txt
|
323 |
+
filename= f'tst_{seed}.txt'
|
324 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
325 |
+
with open(filename, "w") as f:
|
326 |
+
f.write(f"Realvis 5.0: {seed} png\n")
|
327 |
+
f.write(f"Date/time: {timestamp} \n")
|
328 |
+
f.write(f"Prompt: {prompt} \n")
|
329 |
+
f.write(f"Steps: {num_inference_steps} \n")
|
330 |
+
f.write(f"Guidance Scale: {guidance_scale} \n")
|
331 |
+
f.write(f"SPACE SETUP: \n")
|
332 |
+
f.write(f"Use Model Dtype: no \n")
|
333 |
+
f.write(f"Model Scheduler: Euler_a custom before cuda \n")
|
334 |
+
f.write(f"Model VAE: FP64 \n")
|
335 |
+
f.write(f"Model UNET: FP64 \n")
|
336 |
+
upload_to_ftp(filename)
|
337 |
for i in range(0, num_images, BATCH_SIZE):
|
338 |
batch_options = options.copy()
|
339 |
batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
|
340 |
if "negative_prompt" in batch_options:
|
341 |
batch_options["negative_prompt"] = options["negative_prompt"][i:i+BATCH_SIZE]
|
342 |
images.extend(pipe(**batch_options).images)
|
343 |
+
sd_image_path = f"rv50_{seed}.png"
|
344 |
+
images[0].save(sd_image_path,optimize=False,compress_level=0)
|
345 |
+
upload_to_ftp(sd_image_path)
|
346 |
+
image_paths = [save_image(img) for img in images]
|
347 |
+
torch.cuda.empty_cache()
|
348 |
+
gc.collect()
|
349 |
+
return image_paths, seed
|
350 |
|
351 |
+
@spaces.GPU(duration=90)
|
352 |
+
def generate_90(
|
353 |
+
model_choice: str,
|
354 |
+
prompt: str,
|
355 |
+
negative_prompt: str = "",
|
356 |
+
use_negative_prompt: bool = False,
|
357 |
+
style_selection: str = "",
|
358 |
+
seed: int = 1,
|
359 |
+
width: int = 768,
|
360 |
+
height: int = 768,
|
361 |
+
guidance_scale: float = 4,
|
362 |
+
num_inference_steps: int = 250,
|
363 |
+
randomize_seed: bool = False,
|
364 |
+
use_resolution_binning: bool = True,
|
365 |
+
num_images: int = 1,
|
366 |
+
progress=gr.Progress(track_tqdm=True) # Add progress as a keyword argument
|
367 |
+
):
|
368 |
+
torch.cuda.empty_cache()
|
369 |
+
gc.collect()
|
370 |
+
global models
|
371 |
+
pipe = models[model_choice]
|
372 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
373 |
+
generator = torch.Generator(device='cuda').manual_seed(seed)
|
374 |
+
#prompt, negative_prompt = apply_style(style_selection, prompt, negative_prompt)
|
375 |
+
options = {
|
376 |
+
"prompt": [prompt] * num_images,
|
377 |
+
"negative_prompt": [negative_prompt],
|
378 |
+
"negative_prompt_2": [neg_prompt_2],
|
379 |
+
"width": width,
|
380 |
+
"height": height,
|
381 |
+
"guidance_scale": guidance_scale,
|
382 |
+
"num_inference_steps": num_inference_steps,
|
383 |
+
"generator": generator,
|
384 |
+
# "timesteps": sampling_schedule,
|
385 |
+
"output_type": "pil",
|
386 |
+
}
|
387 |
+
if use_resolution_binning:
|
388 |
+
options["use_resolution_binning"] = True
|
389 |
+
images = []
|
390 |
+
pipe.scheduler.set_timesteps(num_inference_steps,device)
|
391 |
+
# write note txt
|
392 |
+
filename= f'tst_{seed}.txt'
|
393 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
394 |
+
with open(filename, "w") as f:
|
395 |
+
f.write(f"Realvis 5.0: {seed} png\n")
|
396 |
+
f.write(f"Date/time: {timestamp} \n")
|
397 |
+
f.write(f"Prompt: {prompt} \n")
|
398 |
+
f.write(f"Steps: {num_inference_steps} \n")
|
399 |
+
f.write(f"Guidance Scale: {guidance_scale} \n")
|
400 |
+
f.write(f"SPACE SETUP: \n")
|
401 |
+
f.write(f"Use Model Dtype: no \n")
|
402 |
+
f.write(f"Model Scheduler: Euler_a custom before cuda \n")
|
403 |
+
f.write(f"Model VAE: FP64 \n")
|
404 |
+
f.write(f"Model UNET: FP64 \n")
|
405 |
+
upload_to_ftp(filename)
|
406 |
+
for i in range(0, num_images, BATCH_SIZE):
|
407 |
+
batch_options = options.copy()
|
408 |
+
batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
|
409 |
+
if "negative_prompt" in batch_options:
|
410 |
+
batch_options["negative_prompt"] = options["negative_prompt"][i:i+BATCH_SIZE]
|
411 |
+
images.extend(pipe(**batch_options).images)
|
412 |
+
sd_image_path = f"rv50_{seed}.png"
|
413 |
+
images[0].save(sd_image_path,optimize=False,compress_level=0)
|
414 |
+
upload_to_ftp(sd_image_path)
|
415 |
image_paths = [save_image(img) for img in images]
|
416 |
+
torch.cuda.empty_cache()
|
417 |
+
gc.collect()
|
418 |
return image_paths, seed
|
419 |
|
420 |
def load_predefined_images1():
|
|
|
431 |
]
|
432 |
return predefined_images1
|
433 |
|
434 |
+
css = '''
|
435 |
+
#col-container {
|
436 |
+
margin: 0 auto;
|
437 |
+
max-width: 640px;
|
438 |
+
}
|
439 |
+
h1{text-align:center}
|
440 |
+
footer {
|
441 |
+
visibility: hidden
|
442 |
+
}
|
443 |
+
body {
|
444 |
+
background-color: green;
|
445 |
+
}
|
446 |
+
'''
|
447 |
|
448 |
+
with gr.Blocks(theme=gr.themes.Origin(),css=css) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
449 |
gr.Markdown(DESCRIPTIONXX)
|
450 |
with gr.Row():
|
451 |
prompt = gr.Text(
|
|
|
455 |
placeholder="Enter your prompt",
|
456 |
container=False,
|
457 |
)
|
458 |
+
run_button_30 = gr.Button("Run 30 Seconds", scale=0)
|
459 |
+
run_button_60 = gr.Button("Run 60 Seconds", scale=0)
|
460 |
+
run_button_90 = gr.Button("Run 90 Seconds", scale=0)
|
461 |
result = gr.Gallery(label="Result", columns=1, show_label=False)
|
462 |
|
463 |
with gr.Row():
|
|
|
467 |
value="REALVISXL V5.0 BF16"
|
468 |
)
|
469 |
|
|
|
470 |
style_selection = gr.Radio(
|
471 |
show_label=True,
|
472 |
container=True,
|
|
|
490 |
max_lines=5,
|
491 |
lines=4,
|
492 |
placeholder="Enter a negative prompt",
|
493 |
+
value="('deformed', 'distorted', 'disfigured':1.3),'not photorealistic':1.5, 'poorly drawn', 'bad anatomy', 'wrong anatomy', 'extra limb', 'missing limb', 'floating limbs', 'poorly drawn hands', 'poorly drawn feet', 'poorly drawn face':1.3, 'out of frame', 'extra limbs', 'bad anatomy', 'bad art', 'beginner', 'distorted face','amateur'",
|
494 |
visible=True,
|
495 |
)
|
496 |
seed = gr.Slider(
|
|
|
520 |
guidance_scale = gr.Slider(
|
521 |
label="Guidance Scale",
|
522 |
minimum=0.1,
|
523 |
+
maximum=30,
|
524 |
step=0.1,
|
525 |
+
value=4,
|
526 |
)
|
527 |
num_inference_steps = gr.Slider(
|
528 |
label="Number of inference steps",
|
|
|
547 |
|
548 |
gr.on(
|
549 |
triggers=[
|
550 |
+
run_button_30.click,
|
551 |
+
],
|
552 |
+
# api_name="generate", # Add this line
|
553 |
+
fn=generate_30,
|
554 |
+
inputs=[
|
555 |
+
model_choice,
|
556 |
+
prompt,
|
557 |
+
negative_prompt,
|
558 |
+
use_negative_prompt,
|
559 |
+
style_selection,
|
560 |
+
seed,
|
561 |
+
width,
|
562 |
+
height,
|
563 |
+
guidance_scale,
|
564 |
+
num_inference_steps,
|
565 |
+
randomize_seed,
|
566 |
+
num_images,
|
567 |
+
],
|
568 |
+
outputs=[result, seed],
|
569 |
+
)
|
570 |
+
|
571 |
+
gr.on(
|
572 |
+
triggers=[
|
573 |
+
run_button_60.click,
|
574 |
+
],
|
575 |
+
# api_name="generate", # Add this line
|
576 |
+
fn=generate_60,
|
577 |
+
inputs=[
|
578 |
+
model_choice,
|
579 |
+
prompt,
|
580 |
+
negative_prompt,
|
581 |
+
use_negative_prompt,
|
582 |
+
style_selection,
|
583 |
+
seed,
|
584 |
+
width,
|
585 |
+
height,
|
586 |
+
guidance_scale,
|
587 |
+
num_inference_steps,
|
588 |
+
randomize_seed,
|
589 |
+
num_images,
|
590 |
+
],
|
591 |
+
outputs=[result, seed],
|
592 |
+
)
|
593 |
+
|
594 |
+
gr.on(
|
595 |
+
triggers=[
|
596 |
+
run_button_90.click,
|
597 |
],
|
598 |
+
# api_name="generate", # Add this line
|
599 |
+
fn=generate_90,
|
600 |
inputs=[
|
601 |
model_choice,
|
602 |
prompt,
|
|
|
661 |
outputs=gr.Textbox(label="Text Generated"),
|
662 |
title=title,
|
663 |
description=description,
|
|
|
664 |
)
|
665 |
|
666 |
combined_interface = gr.TabbedInterface([demo_interface, text_gen_interface], ["Image Generation", "Text Generation"])
|