import gradio as gr
import torch
import modin.pandas as pd
from PIL import Image
from diffusers import DiffusionPipeline
import os
import random
import torchsde
from math import floor, copysign
##########
#
# Spaghetti AI by MagicFixesEverything
# https://huggingface.co/magicfixeseverything
#
# This app is based on scripts by:
# https://huggingface.co/Manjushri
# This app has been adapted from that person's versions.
#
##########
#
# For instructions on how to install this app offline, see the instructions
# here:
# https://huggingface.co/spaces/magicfixeseverything/ai_image_creation/blob/main/Instructions.txt
#
# The display was tested with gradio version 4.11.0.
#
# To launch this script, use the following in the command prompt, taking off
# the # at the start. (You will need to adjust the start of the path if you
# have changed the location)
#
#cd C:\Diffusers && .venv\Scripts\activate.bat && py .venv\ai_image_creation\app.py
#
# You must have a NVIDIA graphics card in your computer with Cuda installed
# to use this script. It will not work on just a CPU on Windows.
#
# If not using "enable_model_cpu_offload" or "enable_sequential_cpu_offload",
# memory usage will remain high until command prompt is closed. (whether
# image is being created or not)
#
###############################################################################
###############################################################################
#
#
#
# Begin Configurations
#
#
#
###############################################################################
###############################################################################
#
# Main Directory
#
# This is where everything goes. Your Python virtual environment should
# be here. Model data will be stored here. (unless you change the next
# configuration) If configured, imagery will also be automatically be
# saved here.
#
main_dir = "C:/Spaghetti_AI"
####################
#
# Only Use Local Files (IMPORTANT)
#
# Please read the section below the configuration. The app will not work
# unless this is set to "0" in order for it to download model data.
#
# 0 False (Model data will be downloaded)
# 1 True (Model data will not be downloaded, even if missing)
#
only_use_local_files = 0
#
# This is an important value. HuggingFace doesn't just download a model
# once and never try again. If something is updated, it will download it
# again. It will not delete the older version. This could eventually
# allow it to use all the space on your drive as that could potentially
# add 5 to 15 gigabytes of data each time a model updates. This variable
# forces the script not to check for model data. However, when set to
# "1", the app will not download any data for the models, including for
# refining and upscaling. That would mean the app will not work. You need
# to set this to "0" when you need to download data. While there are ways
# to not download as much data, it is not as simple. I prefer this
# method. You could check occasionally and manually delete model data,
# but if you're like me, you would forget.
#
# To download data you can use each model in the app when this is set to
# "0" and it will download the needed data. That does require you to use
# each model. Then you could set this back to "1". However, if you want
# to download all the data, without having to use each model, including
# using refining and upscaling at some point at least once, there are
# two options.
#
# To download all default model data, meaning the default model selected
# for each base model (set in
# "base_model_model_configuration_defaults_object"), including data for
# the refiner and upscaler, add this to the end of the url for option 1:
#
# ?download_data=1
#
# Like this for option 1:
# http://127.0.0.1:7860/?download_data=1
#
# To download all model data, for everything in the
# "model_configuration_links_object", including data for the refiner and
# upscaler, add this to the end of the url for option 2:
#
# ?download_data=2
#
# Both options will download dozens of gigabytes of data, most especially
# the second option, so you may not want to do that. Before you do that,
# make sure you have removed from the configurations the models you do
# not want. For option 1, remove the base models you do not want to use
# in "base_model_array". For option 2, remove the model configurations
# you do not want to use in
# "base_model_object_of_model_configuration_arrays".
#
# To have model data download, this variable must be set to 0. You must
# also set "HF_HUB_OFFLINE" to "0" in "spaghetti_ai_launcher.bat" if you
# use that script. If you use that script, and either is not that, model
# data will not download.
#
####################
#
# Use Custom Hugging Face Cache Directory
#
# The folder where model data is stored can get huge. I choose to add it
# to a place where I am more likely to notice it more often. If you use
# other Hugging Face things however, and will use these models in those
# other things, then you might want to consider not having this here as
# it would duplicate the model data.
#
# If set to 1, the data would be here:
# C:\Diffusers\model_data
#
# If set to 0, the data would be here:
# %USERPROFILE%/.cache/huggingface/hub
# Which would look like this, where {Username} is the username of
# your Windows account:
# C:\Users\{Username}\.cache\huggingface\hub
#
# You need to clean out the folder occasionally as this folder will get
# extremely large. Eventually, it would take up all the space on your
# computer.
#
use_custom_hugging_face_cache_dir = 1
#####
#
# Name of Model Data Folder
#
# This is where all the model data will go. (unless you changed it in the
# previous configuration) This folder will get very large. You need to
# clean it out manually occasionally.
#
cache_directory_folder_name = "model_data"
####################
#
# Default Base Model
#
# This will automatically be SDXL Turbo if you are running this on a CPU.
#
default_base_model = "sdxl"
####################
#
# Use Safety Checker
#
# This can block some NSFW images for the models that allow it.
#
# 0 No
# 1 Yes
#
use_safety_checker = 0
#####
#
# Auto Save Imagery
#
# You can automatically save the image file, and a text file with the
# prompt details.
#
auto_save_imagery = 1
#####
#
# Name of Saved Images Folder
#
# You can change the name of this folder if you want. Imagery will be
# saved in a folder called "saved_images" in the directory configured
# in "main_dir". (the saved images folder will be created
# automatically) A directory for each day will be created in this
# folder. Imagery will then be placed in each folder.
#
saved_images_folder_name = "saved_images"
####################
#
# Auto Open Browser From Command Prompt
#
auto_open_browser = 0
####################
#
# Include Close Command Prompt / Cancel Button
#
# This doesn't work well at all. It just closes the command prompt. I
# might remove this eventually unless someone knows of a way it can work
# better, such as stopping the generation without closing the command
# prompt.
#
enable_close_command_prompt_button = 0
####################
#
# Use Denoising Start In Base Model When Using Refiner
#
# If set to "1", refining will end at the percent (expressed as decimal)
# defined in the denoising start for the refiner. If the steps set are
# 100, and the denoising start value is 0.75, the base model will run for
# 75 steps. The refiner will then run for 25 steps.
#
default_use_denoising_start_in_base_model_when_using_refiner = 1
####################
#
# Base Model Output To Refiner Is In Latent Space
#
# If set to "1", base model output is in latent space instead of PIL
# image when sent to refiner.
#
default_base_model_output_to_refiner_is_in_latent_space = 1
####################
#
# Log Generation Times
#
# Log generation times to saved text output. The initial time it takes to
# load a model is not included in the generation time.
#
log_generation_times = 1
####################
#
# Use Image Gallery
#
use_image_gallery = 1
####################
#
# Show Image Creation Progress Log
#
# This adds the current step that image generation is on.
#
show_image_creation_progress_log = 1
####################
#
# Show Messages In Command Prompt
#
# Messages will be printed in command prompt.
#
show_messages_in_command_prompt = 1
####################
#
# Show Messages In Modal On Page
#
# A popup appears in the top right corner on the page.
#
show_messages_in_modal_on_page = 0
####################
#
# Suppress Hugging Face Hub Offline Status
#
# By default, we add messages about the current setting of
# "HF_HUB_OFFLINE".
#
suppress_hugging_face_hub_offline_status = 0
####################
#
# Add Seed Into Pipe
#
# To make generation deterministic. I add the option because the online
# configuration for the PhotoReal site doesn't do that and it changes
# things.
#
default_add_seed_into_pipe = 1
####################
#
# Max Queue Size
#
max_queue_size_if_cpu = 1
max_queue_size_if_torch = 20
####################
#
# Allow Online Configurations
#
# This allows matching what was created on these sites:
# https://huggingface.co/spaces/Manjushri/SDXL-1.0
# https://huggingface.co/spaces/Manjushri/PhotoReal-V3.7.5
#
allow_online_configurations = 1
####################
#
# Up Next Is Various Configuration Arrays and Objects
#
####################
base_model_array = [
"sdxl",
"photoreal",
"sdxl_turbo",
"sd_1_5_runwayml"
]
base_model_names_object = {
"sdxl": "Stable Diffusion XL",
"photoreal": "PhotoReal",
"sdxl_turbo": "Stable Diffusion XL Turbo",
"sd_1_5_runwayml": "Stable Diffusion 1.5"
}
####################
#
# "sdxl_default"
#
# - My customized configurations. (subject to change)
#
# "sdxl_2023-11-12"
#
# - Valid from November 12th to present.
# Number of steps in upscaler changed from 5 to 15.
#
# "sdxl_2023-09-05"
#
# - Valid from September 5th to November 12th.
# There were changes on this date.
#
# "photoreal_default"
#
# - My customized configurations. (subject to change)
# "circulus/canvers-real-v3.7.5"
#
# Seeds do not match the online PhotoReal version.
#
# "photoreal_3-8-1"
#
# - My customized configurations. (subject to change)
# "circulus/canvers-real-v3.8.1"
#
# "photoreal_3-8"
#
# - My customized configurations. (subject to change)
# "circulus/canvers-real-v3.8"
#
# "photoreal_3-7-5"
#
# - My customized configurations. (subject to change)
# "circulus/canvers-real-v3.7.5"
#
# "photoreal_3-6"
#
# - My customized configurations. (subject to change)
# "circulus/canvers-realistic-v3.6"
#
# "photoreal_2023-11-12"
#
# - Valid from November 12th to present.
# New base model: "circulus/canvers-real-v3.7.5"
#
# "photoreal_2023-09-01"
#
# - Valid from September 1st to November 12th.
# "circulus/canvers-realistic-v3.6" was already in effect.
# But there were changes on this date.
#
# "sdxl_turbo_default"
#
# - My customized configurations. (subject to change)
#
# "sd_1_5_runwayml_default"
#
# - My customized configurations. (subject to change)
#
base_model_object_of_model_configuration_arrays = {
"sdxl": [
"sdxl_default",
"sdxl_2023-11-12",
"sdxl_2023-09-05"
],
"photoreal": [
"photoreal_default",
"photoreal_3-8-1",
"photoreal_3-8",
"photoreal_3-7-5",
"photoreal_3-6",
"photoreal_2023-11-12",
"photoreal_2023-09-01"
],
"sdxl_turbo": [
"sdxl_turbo_default"
],
"sd_1_5_runwayml": [
"sd_1_5_runwayml_default"
]
}
####################
model_configuration_names_object = {
"sdxl_default": "1.0 - Default",
"sdxl_2023-11-12": "1.0 (2023-11-12 online config)",
"sdxl_2023-09-05": "1.0 (2023-09-05 online config)",
"photoreal_default": "3.6 - Default",
"photoreal_3-8-1": "3.8.1 - Default",
"photoreal_3-8": "3.8 - Default",
"photoreal_3-7-5": "3.7.5 - Default",
"photoreal_3-6": "3.6 - Default",
"photoreal_2023-11-12": "3.7.5 (2023-11-12 online config)",
"photoreal_2023-09-01": "3.6 (2023-09-01 online config)",
"sdxl_turbo_default": "Default",
"sd_1_5_runwayml_default": "1.5 - Default"
}
model_configuration_links_object = {
"sdxl_default": "stabilityai/stable-diffusion-xl-base-1.0",
"sdxl_2023-11-12": "stabilityai/stable-diffusion-xl-base-1.0",
"sdxl_2023-09-05": "stabilityai/stable-diffusion-xl-base-1.0",
"photoreal_default": "circulus/canvers-realistic-v3.6",
"photoreal_3-8-1": "circulus/canvers-real-v3.8.1",
"photoreal_3-8": "circulus/canvers-real-v3.8",
"photoreal_3-7-5": "circulus/canvers-real-v3.7.5",
"photoreal_3-6": "circulus/canvers-realistic-v3.6",
"photoreal_2023-11-12": "circulus/canvers-real-v3.7.5",
"photoreal_2023-09-01": "circulus/canvers-realistic-v3.6",
"sdxl_turbo_default": "stabilityai/sdxl-turbo",
"sd_1_5_runwayml_default": "runwayml/stable-diffusion-v1-5"
}
model_configuration_force_refiner_object = {
"sdxl_2023-11-12": 1,
"sdxl_2023-09-05": 1
}
# For now, the ones that force the refiner also have the "Refiner Number of
# Iterations" available.
model_configuration_include_refiner_number_of_steps_object = model_configuration_force_refiner_object
#model_configuration_include_refiner_number_of_steps_object = {
# "sdxl_2023-11-12": 1,
# "sdxl_2023-09-05": 1
#}
# For now, the ones that force the refiner also need upscaling steps hidden.
model_configuration_hide_upscaler_steps_object = model_configuration_force_refiner_object
#model_configuration_hide_upscaler_steps_object = {
# "sdxl_2023-11-12": 1,
# "sdxl_2023-09-05": 1
#}
####################
sdxl_online_configurations_object = {
"sdxl_2023-11-12": 1,
"sdxl_2023-09-05": 1
}
photoreal_online_configurations_object = {
"photoreal_2023-11-12": 1,
"photoreal_2023-09-01": 1
}
####################
base_models_not_supporting_denoising_end_for_base_model_object = {
"photoreal": 1,
"sd_1_5_runwayml": 1
}
####################
hugging_face_refiner_partial_path = "stabilityai/stable-diffusion-xl-refiner-1.0"
hugging_face_upscaler_partial_path = "stabilityai/sd-x2-latent-upscaler"
####################
base_model_model_configuration_defaults_object = {
"sdxl": "sdxl_default",
"photoreal": "photoreal_default",
"sdxl_turbo": "sdxl_turbo_default",
"sd_1_5_runwayml": "sd_1_5_runwayml_default"
}
####################
#
# Links:
#
# SD-XL 1.0-base Model Card
# https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0
#
# SD-XL 1.0-refiner Model Card
# https://huggingface.co/stabilityai/stable-diffusion-xl-refiner-1.0
#
# Stable Diffusion x2 latent upscaler model card
# https://huggingface.co/stabilityai/sd-x2-latent-upscaler
#
# PhotoReal
# 3.7.5: https://huggingface.co/circulus/canvers-real-v3.7.5
# 3.6: https://huggingface.co/circulus/canvers-realistic-v3.6
#
# SDXL Turbo
# https://huggingface.co/stabilityai/sdxl-turbo
#
# Stable Diffusion v1-5 (runwayml)
# https://huggingface.co/runwayml/stable-diffusion-v1-5
#
####################
default_scheduler = "model_default"
schedulers_array = [
"model_default",
"ddim",
"ddpm",
"dpm_solver_multistep",
"dpm_solver_multistep_karras_sigmas_true",
"dpm_solver_multistep_algorithm_type_sde-dpmsolver_pp",
"dpm_solver_multistep_karras_sigmas_true_algorithm_type_sde-dpmsolver_pp",
"dpm_solver_singlestep",
"dpm_solver_singlestep_karras_sigmas_true",
"kdpm2_discrete",
"kdpm2_discrete_karras_sigmas_true",
"kdpm2_ancestral_discrete",
"kdpm2_ancestral_discrete_karras_sigmas_true",
"euler_discrete",
"euler_ancestral_discrete",
"heun_discrete",
"lms_discrete",
"lms_discrete_karras_sigmas_true",
"pndm",
"pndm_skip_prk_steps_true",
"deis_multistep",
"dpm_solver_sde",
"uni_pc_multistep"
]
scheduler_long_names_object = {
"model_default": "Model Default",
"ddim": "DDIM",
"ddpm": "DDPM",
"dpm_solver_multistep": "DPM++ 2M (DPMSolverMultistep)",
"dpm_solver_multistep_karras_sigmas_true": "DPM++ 2M Karras (DPMSolverMultistep with use_karras_sigmas=True)",
"dpm_solver_multistep_algorithm_type_sde-dpmsolver_pp": "DPM++ 2M SDE (DPMSolverMultistep with algorithm_type=\"sde-dpmsolver++\")",
"dpm_solver_multistep_karras_sigmas_true_algorithm_type_sde-dpmsolver_pp": "DPM++ 2M SDE Karras (DPMSolverMultistep with use_karras_sigmas=True & algorithm_type=\"sde-dpmsolver++\")",
"dpm_solver_singlestep": "DPM++ SDE (DPMSolverSinglestep)",
"dpm_solver_singlestep_karras_sigmas_true": "DPM++ SDE Karras (DPMSolverSinglestep with use_karras_sigmas=True)",
"kdpm2_discrete": "DPM2 (KDPM2Discrete)",
"kdpm2_discrete_karras_sigmas_true": "DPM2 Karras (KDPM2Discrete with use_karras_sigmas=True)",
"kdpm2_ancestral_discrete": "DPM2 a (KDPM2AncestralDiscrete)",
"kdpm2_ancestral_discrete_karras_sigmas_true": "DPM2 a Karras (KDPM2AncestralDiscrete with use_karras_sigmas=True)",
"euler_discrete": "Euler (EulerDiscrete)",
"euler_ancestral_discrete": "Euler a (EulerAncestralDiscrete)",
"heun_discrete": "Heun (HeunDiscrete)",
"lms_discrete": "LMS (LMSDiscrete)",
"lms_discrete_karras_sigmas_true": "LMS Karras (LMSDiscrete with use_karras_sigmas=True)",
"pndm": "PNDM",
"pndm_skip_prk_steps_true": "PNDM (with skip_prk_steps=True) - Close to PLMS",
"deis_multistep": "DEISMultistep",
"dpm_solver_sde": "DPMSolverSDE",
"uni_pc_multistep": "UniPCMultistep"
}
scheduler_short_names_object = {
"ddim": "DDIM",
"ddpm": "DDPM",
"dpm_solver_multistep": "DPM++ 2M",
"dpm_solver_multistep_karras_sigmas_true": "DPM++ 2M Karras",
"dpm_solver_multistep_algorithm_type_sde-dpmsolver_pp": "DPM++ 2M SDE",
"dpm_solver_multistep_karras_sigmas_true_algorithm_type_sde-dpmsolver_pp": "DPM++ 2M SDE Karras",
"dpm_solver_singlestep": "DPM++ SDE",
"dpm_solver_singlestep_karras_sigmas_true": "DPM++ SDE Karras",
"kdpm2_discrete": "DPM2",
"kdpm2_discrete_karras_sigmas_true": "DPM2 Karras",
"kdpm2_ancestral_discrete": "DPM2 a",
"kdpm2_ancestral_discrete_karras_sigmas_true": "DPM2 a Karras",
"euler_discrete": "Euler",
"euler_ancestral_discrete": "Euler a",
"heun_discrete": "Heun",
"lms_discrete": "LMS",
"lms_discrete_karras_sigmas_true": "LMS Karras",
"pndm": "PNDM",
"pndm_skip_prk_steps_true": "PNDM (with skip_prk_steps=True) - Close to PLMS",
"deis_multistep": "DEISMultistep",
"dpm_solver_sde": "DPMSolverSDE",
"uni_pc_multistep": "UniPCMultistep"
}
scheduler_name_to_identifier_in_app_object = {
"DDIMScheduler": "ddim",
"DDPMScheduler": "ddpm",
"DPMSolverMultistepScheduler": "dpm_solver_multistep",
"DPMSolverSinglestepScheduler": "dpm_solver_singlestep",
"KDPM2DiscreteScheduler": "kdpm2_discrete",
"KDPM2AncestralDiscreteScheduler": "kdpm2_ancestral_discrete",
"EulerDiscreteScheduler": "euler_discrete",
"EulerAncestralDiscreteScheduler": "euler_ancestral_discrete",
"HeunDiscreteScheduler": "heun_discrete",
"LMSDiscreteScheduler": "lms_discrete",
"PNDMScheduler": "pndm",
"DEISMultistepScheduler": "deis_multistep",
"DPMSolverSDEScheduler": "dpm_solver_sde",
"UniPCMultistepScheduler": "uni_pc_multistep"
}
####################
#
# Determine automatically if on CPU or GPU
#
# CPU will not work on Windows.
#
device = "cpu"
if torch.cuda.is_available():
device = "cuda"
PYTORCH_CUDA_ALLOC_CONF = {
"max_split_size_mb": 8000
}
torch.cuda.max_memory_allocated(
device = device
)
torch.cuda.empty_cache()
if device == "cpu":
default_base_model = "sdxl_turbo"
####################
#
# Determine if running on HuggingFace
#
try:
if (str(os.uname()).find("magicfixeseverything") >= 0):
script_being_run_on_hugging_face = 1
except:
script_being_run_on_hugging_face = 0
if script_being_run_on_hugging_face == 1:
allow_online_configurations = 0
####################
default_prompt = ""
default_negative_prompt = ""
default_width = 768
default_height = 768
minimum_width = 64
minimum_height = 64
maximum_width = 2048 # 1024
maximum_height = 2048 # 1024
default_base_model_steps = 50
default_base_model_steps_for_sdxl_turbo = 2
maximum_base_model_steps = 150 # 100
maximum_base_model_steps_for_sdxl_turbo = 25
default_guidance_scale = 7.5
minimum_guidance_scale = 0
maximum_guidance_scale = 30
# Must be greater than 0
guidance_scale_input_slider_steps = 0.25
#
# To have the seed be random, set this to:
#
# "random"
#
default_seed_value = "random"
maximum_seed = 1000000000000000000
add_generation_information_to_image = 1
# If you turn off the refiner it will not be available in the display unless
# you select an online configuration option that requires it.
enable_refiner = 1
enable_upscaler = 1
# Selected on form as a default?
default_refiner_selected = 0
default_upscaler_selected = 0
# Accordion visible on load?
#
# 0 If selected as default, will be open. Otherwise, closed.
# 1 Always starts open
default_refiner_accordion_open = 1
default_upscaler_accordion_open = 1
default_refiner_denoise_start = 0.95
minimum_refiner_denoise_start = 0.01
maximum_refiner_denoise_start = 0.99
# Must be greater than 0
refiner_denoise_start_input_slider_steps = 0.01
# Only for SDXL online configuration. We just use base model steps and denoise start normally
default_refining_steps_for_online_config_field = 100
maximum_refining_steps_for_online_config_field = 100
# Upscaler Options
maximum_upscaler_steps = 150
default_upscaler_steps = 50
# xFormers:
#
# https://huggingface.co/docs/diffusers/optimization/xformers
use_xformers = 1
# Scaled dot product attention (SDPA) is used by default for PyTorch 2.0. To
# use default instead, set this to 1.
#
# https://huggingface.co/docs/diffusers/optimization/torch2.0#scaled-dot-product-attention
use_default_attn_processor = 0
display_xformers_usage_in_prompt_info = 1
include_transformers_version_in_prompt_info = 1
display_default_attn_processor_usage_in_prompt_info = 1
# You can't select both sequential and model cpu offloading. If you select
# both, model cpu offloading will be used.
use_sequential_cpu_offload_for_base_model = 1
use_sequential_cpu_offload_for_refiner = 1
use_sequential_cpu_offload_for_upscaler = 1
use_model_cpu_offload_for_base_model = 0
use_model_cpu_offload_for_refiner = 0
use_model_cpu_offload_for_upscaler = 0
if default_base_model == "sdxl":
# SDXL
default_width = 1024
default_height = 1024
#elif default_base_model == "photoreal":
# PhotoReal
#elif default_base_model == "sdxl_turbo":
# SDXL Turbo
#elif default_base_model == "sd_1_5_runwayml":
# SD 1.5
# Must be multiple of 8
width_and_height_input_slider_steps = 8
opening_html = ""
ending_html = ""
max_queue_size = max_queue_size_if_torch
if device == "cpu":
if script_being_run_on_hugging_face == 1:
opening_html = "This app is extremely slow. This app is not running on a GPU. The first time it loads after the space is rebuilt it might take 10 minutes to generate a SDXL Turbo image. It may take around 3 minutes after that point to do two steps. (with no refining or upscaling) For other models, it may take an hour to create a single image. Want apps that work fast? Use these which this app is based on: Stable Diffusion XL, PhotoReal with SDXL 1.0 Refiner and SDXL Turbo Unofficial Demo. This app is designed to give more options, but it's too slow to operate and test on a CPU. There are some features that are either not available, or more limited, in the online version of this app. (such as smaller allowed image dimensions and less steps allowed) This app is still in development. Next will be the ability to create a link like this."
else:
opening_html = "This app is currently running on a CPU. If you have a NVIDIA graphics card, make sure you have torch installed so that you can use your GPU to create imagery. If you don't, it will work extremely slowly."
max_queue_size = max_queue_size_if_cpu
if allow_online_configurations == 1:
ending_html = """This app allows you to try to match images that can be generated using several tools online. (Stable Diffusion XL, PhotoReal with SDXL 1.0 Refiner and SDXL Turbo Unofficial Demo) You can select the base model you want to use in the first dropdown option. The second configuration option involves choosing which version and/or configuration to choose. Certain configurations try to match the version online, taking into account changes that were made over time. Another configuration involves a default configuration I choose and is subject to change while I am still designing this app.
"""
ending_html += """
Tokens are not individual characters. If the prompt length is too long, the display will notify you what part of the prompt wasn't used. Changing just the image dimensions alone will change the image generated. For some models, trying to make a large image, such as 1024x1024, may add extra people and come out worse than using smaller dimensions.
The original script for this app was written by Manjushri."""
refiner_on_text = "Refiner is on. "
refiner_off_text = "Refiner is off. "
upscaler_on_text = "Upscaler is on. "
upscaler_off_text = "Upscaler is off. "
number_of_reserved_tokens = 2
###############################################################################
###############################################################################
#
#
#
# End Configurations
#
#
#
###############################################################################
###############################################################################
if script_being_run_on_hugging_face == 1:
# If on HuggingFace, I change some things.
use_custom_hugging_face_cache_dir = 0
auto_save_imagery = 0
show_messages_in_modal_on_page = 0
show_messages_in_command_prompt = 1
only_use_local_files = 0
if device == "cpu":
# If on CPU at HuggingFace, I reduce what is available.
show_image_creation_progress_log = 1
minimum_width = 256
minimum_height = 256
maximum_width = 768
maximum_height = 768
minimum_guidance_scale = 1
maximum_guidance_scale = 15
maximum_base_model_steps = 30
maximum_base_model_steps_for_sdxl_turbo = 5
minimum_refiner_denoise_start = 0.70
maximum_upscaler_steps = 15
default_upscaler_steps = 10
ending_html = """
If you would like to download this app to run offline on a Windows computer that has a NVIDIA graphics card, click here to download it.
""" + ending_html
if default_width < minimum_width:
default_width = minimum_width
if default_height < minimum_height:
default_height = minimum_height
if default_width > maximum_width:
default_width = maximum_width
if default_height > maximum_height:
default_height = maximum_height
if default_base_model_steps > maximum_base_model_steps:
default_base_model_steps = maximum_base_model_steps
if default_base_model_steps_for_sdxl_turbo > maximum_base_model_steps_for_sdxl_turbo:
default_base_model_steps_for_sdxl_turbo = maximum_base_model_steps_for_sdxl_turbo
if default_guidance_scale < minimum_guidance_scale:
default_guidance_scale = minimum_guidance_scale
if default_guidance_scale > maximum_guidance_scale:
default_guidance_scale = maximum_guidance_scale
if default_upscaler_steps > maximum_upscaler_steps:
default_upscaler_steps = maximum_upscaler_steps
only_use_local_files_bool = False
if only_use_local_files == 1:
only_use_local_files_bool = True
if allow_online_configurations == 0:
base_model_array = [
"sdxl",
"photoreal",
"sdxl_turbo"
]
base_model_object_of_model_configuration_arrays = {
"sdxl": [
"sdxl_default"
],
"photoreal": [
"photoreal_default"
],
"sdxl_turbo": [
"sdxl_turbo_default"
]
}
base_model_model_configuration_defaults_object = {
"sdxl": "sdxl_default",
"photoreal": "photoreal_default",
"sdxl_turbo": "sdxl_turbo_default"
}
hugging_face_hub_is_offline = 0
if script_being_run_on_hugging_face == 0:
if (
("HF_HUB_OFFLINE" in os.environ) and
(int(os.environ["HF_HUB_OFFLINE"]) == 1)
):
hugging_face_hub_is_offline = 1
only_use_local_files = 1
if suppress_hugging_face_hub_offline_status == 1:
if hugging_face_hub_is_offline == 0:
print ("Note: The Hugging Face cache directory does not automatically delete older data. Over time, it could eventually grow to use all the space on the drive it is on. You either need to manually clean out the folder occasionally or see Instructons.txt on how to not automatically update data once you have downloaded everything you need.")
else:
print ("You are working offline. Data will not be downloaded. See \"ai_image_creation.bat\" or \"Instructions.txt\" for more info.")
if device == "cuda":
PYTORCH_CUDA_ALLOC_CONF = {
"max_split_size_mb": 8000
}
torch.cuda.max_memory_allocated(
device = device
)
torch.cuda.empty_cache()
saved_images_dir = main_dir + "/" + saved_images_folder_name
hugging_face_cache_dir = main_dir + "/" + cache_directory_folder_name
if not os.path.exists(hugging_face_cache_dir):
os.makedirs(hugging_face_cache_dir)
if auto_save_imagery == 1:
from datetime import datetime
import time
if (
(log_generation_times == 1) or
(show_image_creation_progress_log == 1)
):
import time
if device == "cpu":
use_sequential_cpu_offload_for_base_model = 0
use_sequential_cpu_offload_for_refiner = 0
use_sequential_cpu_offload_for_upscaler = 0
use_model_cpu_offload_for_base_model = 0
use_model_cpu_offload_for_refiner = 0
use_model_cpu_offload_for_upscaler = 0
use_xformers = 0
if (
(use_sequential_cpu_offload_for_base_model == 1) and
(use_model_cpu_offload_for_base_model == 1)
):
use_sequential_cpu_offload_for_base_model = 0
if (
(use_sequential_cpu_offload_for_refiner == 1) and
(use_model_cpu_offload_for_refiner == 1)
):
use_sequential_cpu_offload_for_refiner = 0
if (
(use_sequential_cpu_offload_for_upscaler == 1) and
(use_model_cpu_offload_for_upscaler == 1)
):
use_sequential_cpu_offload_for_upscaler = 0
def error_function(
text_message
):
print (text_message)
raise Exception(text_message)
additional_prompt_info_html = ""
if auto_save_imagery == 1:
additional_prompt_info_html = " The image, and a text file with generation information, will be saved automatically."
if use_xformers == 1:
from xformers.ops import MemoryEfficientAttentionFlashAttentionOp
if use_default_attn_processor == 1:
from diffusers.models.attention_processor import AttnProcessor
if (
default_base_model and
(default_base_model in base_model_object_of_model_configuration_arrays) and
(default_base_model in base_model_model_configuration_defaults_object)
):
default_model_configuration = base_model_model_configuration_defaults_object[default_base_model]
if default_model_configuration in model_configuration_names_object:
default_model_configuration_choices_array = []
for this_model_configuration in base_model_object_of_model_configuration_arrays[default_base_model]:
if this_model_configuration in model_configuration_names_object:
default_model_configuration_choices_array.append(
model_configuration_names_object[this_model_configuration]
)
else:
error_function("A default configuration must be properly named in the code.")
else:
error_function("A default configuration must be properly configured in the code.")
else:
error_function("A default base model must be properly configured in the code.")
default_base_model_nicely_named_value = base_model_names_object[default_base_model]
default_model_configuration_nicely_named_value = model_configuration_names_object[default_model_configuration]
if not (
default_scheduler and
default_scheduler in scheduler_long_names_object
):
error_function("A default scheduler must be properly configured in the code.")
default_scheduler_nicely_named_value = scheduler_long_names_object[default_scheduler]
if enable_refiner != 1:
default_refiner_selected = 0
if enable_upscaler != 1:
default_upscaler_selected = 0
model_configuration_requires_refiner = 0
if default_model_configuration in model_configuration_force_refiner_object:
model_configuration_requires_refiner = model_configuration_force_refiner_object[default_model_configuration]
if model_configuration_requires_refiner == 1:
enable_refiner = 1
default_refiner_selected = 1
default_refine_option = "No"
if default_refiner_selected == 1:
default_refine_option = "Yes"
default_upscale_option = "No"
if default_upscaler_selected == 1:
default_upscale_option = "Yes"
online_configurations_object = {}
online_configurations_object.update(sdxl_online_configurations_object)
online_configurations_object.update(photoreal_online_configurations_object)
is_default_config = 1
if default_model_configuration in online_configurations_object:
is_default_config = 0
default_refiner_and_upscaler_status_text = ""
default_use_denoising_start_in_base_model_when_using_refiner_is_selected = False
if default_use_denoising_start_in_base_model_when_using_refiner == 1:
default_use_denoising_start_in_base_model_when_using_refiner_is_selected = True
default_base_model_output_to_refiner_is_in_latent_space_is_selected = False
if default_base_model_output_to_refiner_is_in_latent_space == 1:
default_base_model_output_to_refiner_is_in_latent_space_is_selected = True
refiner_default_config_accordion_visible = True
if (
(enable_refiner != 1) or
(is_default_config != 1)
):
refiner_default_config_accordion_visible = False
refiner_default_config_accordion_open = False
if (
(default_refiner_accordion_open == 1) or
(
(is_default_config == 1) and
(default_refiner_selected == 1)
)
):
refiner_default_config_accordion_open = True
refiner_online_config_accordion_visible = True
if (
(enable_refiner != 1) or
(is_default_config == 1)
):
refiner_online_config_accordion_visible = False
refiner_online_config_accordion_open = False
if (
(default_refiner_accordion_open == 1) or
(
(is_default_config != 1) and
(default_refiner_selected == 1)
)
):
refiner_online_config_accordion_open = True
refiner_group_visible = False
if enable_refiner == 1:
refiner_group_visible = True
if default_refiner_selected == 1:
default_refiner_and_upscaler_status_text += refiner_on_text
else:
default_refiner_and_upscaler_status_text += refiner_off_text
upscaler_accordion_open = False
if (
(default_upscaler_selected == 1) or
(default_upscaler_accordion_open == 1)
):
upscaler_accordion_open = True
upscaler_group_visible = False
if enable_upscaler == 1:
upscaler_group_visible = True
if default_upscaler_selected == 1:
default_refiner_and_upscaler_status_text += upscaler_on_text
else:
default_refiner_and_upscaler_status_text += upscaler_off_text
default_negative_prompt_field_row_visibility = True
default_negative_prompt_for_sdxl_turbo_field_row_visibility = False
default_base_model_steps_field_row_visibility = True
default_base_model_steps_field_for_sdxl_turbo_field_row_visibility = False
default_guidance_scale_field_row_visibility = True
default_guidance_scale_for_sdxl_turbo_field_row_visibility = False
if default_base_model == "sdxl_turbo":
default_negative_prompt_field_row_visibility = False
default_negative_prompt_for_sdxl_turbo_field_row_visibility = True
default_base_model_steps_field_row_visibility = False
default_base_model_steps_field_for_sdxl_turbo_field_row_visibility = True
default_guidance_scale_field_row_visibility = False
default_guidance_scale_for_sdxl_turbo_field_row_visibility = True
default_refining_use_denoising_start_in_base_model_when_using_refiner_field_row_visibility = True
if default_base_model in base_models_not_supporting_denoising_end_for_base_model_object:
default_refining_use_denoising_start_in_base_model_when_using_refiner_field_row_visibility = False
default_add_seed_into_pipe_field_row_visibility = False
if is_default_config == 1:
default_add_seed_into_pipe_field_row_visibility = True
default_add_seed_into_pipe_is_selected = False
if default_add_seed_into_pipe == 1:
default_add_seed_into_pipe_is_selected = True
default_base_model_choices_array = []
stored_model_configuration_names_object = {}
for this_base_model in base_model_array:
default_base_model_choices_array.append(
base_model_names_object[this_base_model]
)
stored_model_configuration = base_model_model_configuration_defaults_object[this_base_model]
stored_model_configuration_names_object[this_base_model] = model_configuration_names_object[stored_model_configuration]
default_scheduler_choices_array = []
for this_scheduler in schedulers_array:
default_scheduler_choices_array.append(
scheduler_long_names_object[this_scheduler]
)
make_seed_selection_a_textbox = 1
if maximum_seed <= 9007199254740992:
make_seed_selection_a_textbox = 0
###############################################################################
###############################################################################
#
#
#
#
#
#
# Functions
#
#
#
#
#
#
###############################################################################
###############################################################################
#####################
#
# Rounded Number
#
# A better, and seemingly more accurate, way to round.
#
# https://realpython.com/python-rounding/
#####################
def rounded_number(n, decimals=0):
n = float(n)
multiplier = 10**decimals
rounded_abs = (floor(abs(n) * multiplier + 0.5) / multiplier)
rounded_value = round(copysign(rounded_abs, n), decimals)
return rounded_value
#####################
#
# Rounded Number
#
# Format number to a certain number of decimal places and output it to a string.
#
# https://stackoverflow.com/questions/1995615/how-can-i-format-a-decimal-to-always-show-2-decimal-places
#####################
def formatted_number(n, decimals=0):
rounded_value = rounded_number(n, decimals)
formatted_value = '{:.{prec}f}'.format(rounded_value, prec=decimals)
return formatted_value
#####################
#
# Show Message
#
# Display message to user in model on web form and/or command prompt.
#
#####################
def generate_random_seed():
maximum_seed_for_random = maximum_seed
if maximum_seed_for_random > 9007199254740992:
# If above this number, seeds may not be able to be entered into
# slider properly.
maximum_seed_for_random = 9007199254740992
actual_seed = int(random.randrange(0, 10**len(str(maximum_seed_for_random))))
return actual_seed
#####################
#
# Show Message
#
# Display message to user in model on web form and/or command prompt.
#
#####################
def show_message(
message_to_display
):
if show_messages_in_command_prompt == 1:
print (message_to_display)
if show_messages_in_modal_on_page == 1:
gr.Info(message_to_display)
#####################
#
# Convert Seconds
#
# Convert raw seconds to the numer of hours, minutes and seconds.
#
#####################
def convert_seconds(
seconds
):
# Google AI Code
hours = seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60
return hours, minutes, seconds
#####################
#
# Base Model Valid
#
# Return True if valid.
#
#####################
def base_model_valid(base_model_name_value):
try:
base_model_name_value_str = str(base_model_name_value).lower()
if (
(base_model_name_value_str in base_model_object_of_model_configuration_arrays) and
(base_model_name_value_str in base_model_model_configuration_defaults_object)
):
return True
else:
return False
except ValueError:
return False
#####################
#
# Model Configuration Valid
#
# Return True if valid.
#
#####################
def model_configuration_valid(
base_model_name_value,
model_configuration_name_value
):
try:
base_model_name_value_str = str(base_model_name_value).lower()
model_configuration_name_value_str = str(model_configuration_name_value).lower()
for this_base_model in base_model_array:
for this_model_configuration in base_model_object_of_model_configuration_arrays[this_base_model]:
if (
(base_model_name_value_str == this_base_model) and
(model_configuration_name_value_str == this_model_configuration)
):
return True
return False
except ValueError:
return False
#####################
#
# Prompt Valid
#
# Return True if valid.
#
#####################
def prompt_valid(prompt_field):
try:
prompt_field_str = str(prompt_field)
if len(prompt_field_str) <= 500:
return True
else:
return False
except ValueError:
return False
#####################
#
# Negative Prompt Valid
#
# Return True if valid.
#
#####################
def negative_prompt_valid(negative_prompt_field):
try:
negative_prompt_field_str = str(negative_prompt_field)
if len(negative_prompt_field_str) <= 500:
return True
else:
return False
except ValueError:
return False
#####################
#
# Scheduler/Sampler Valid
#
# Return True if valid.
#
#####################
def scheduler_valid(scheduler_field):
try:
scheduler_str = str(scheduler_field).lower()
if scheduler_str in scheduler_long_names_object:
return True
else:
return False
except ValueError:
return False
#####################
#
# Width Valid
#
# Return True if valid.
#
#####################
def width_valid(width_num_str):
try:
width_num = int(width_num_str)
if (
(width_num >= int(minimum_width)) and
(width_num <= int(maximum_width)) and
(width_num % int(width_and_height_input_slider_steps)) == 0
):
return True
else:
return False
except ValueError:
return False
#####################
#
# Height Valid
#
# Return True if valid.
#
#####################
def height_valid(height_num_str):
try:
height_num = int(height_num_str)
if (
(height_num >= int(minimum_height)) and
(height_num <= int(maximum_height)) and
(height_num % int(width_and_height_input_slider_steps)) == 0
):
return True
else:
return False
except ValueError:
return False
#####################
#
# Guidance Scale Valid
#
# Return True if valid.
#
#####################
def guidance_scale_valid(guidance_scale_str):
try:
guidance_scale_num = float(guidance_scale_str)
guidance_scale_num_times_100 = (guidance_scale_num * 100)
guidance_scale_num_times_100_with_int = int(guidance_scale_num_times_100)
guidance_scale_input_slider_steps_times_100 = (float(guidance_scale_input_slider_steps) * 100)
if (
(guidance_scale_num >= float(minimum_guidance_scale)) and
(guidance_scale_num <= float(maximum_guidance_scale)) and
(guidance_scale_num_times_100 == guidance_scale_num_times_100_with_int) and
((guidance_scale_num_times_100 % guidance_scale_input_slider_steps_times_100) == 0)
):
return True
else:
return False
except ValueError:
return False
#####################
#
# Steps Valid
#
# Return True if valid.
#
#####################
def steps_valid(
steps_num_str,
base_model_name_value
):
try:
steps_num = int(steps_num_str)
base_model_name_value_str = str(base_model_name_value).lower()
if steps_num > 0:
if (base_model_name_value_str == "sdxl_turbo"):
if steps_num <= int(maximum_base_model_steps_for_sdxl_turbo):
return True
else:
if steps_num <= int(maximum_base_model_steps):
return True
return False
except ValueError:
return False
#####################
#
# Seed Valid
#
# Return True if valid.
#
#####################
def seed_valid(
seed_num_str
):
try:
seed_num = int(seed_num_str)
if (
(seed_num >= 0) and
(seed_num <= int(maximum_seed))
):
return True
else:
return False
except ValueError:
return False
#####################
#
# Refiner Denoise Start
#
# Return True if valid.
#
#####################
def refiner_denoise_start_valid(
refiner_denoise_start_str
):
try:
refiner_denoise_start_num = float(refiner_denoise_start_str)
refiner_denoise_start_num_times_100 = (refiner_denoise_start_num * 100)
refiner_denoise_start_num_times_100_with_int = int(refiner_denoise_start_num_times_100)
refiner_denoise_start_input_slider_steps_times_100 = (float(refiner_denoise_start_input_slider_steps) * 100)
if (
(refiner_denoise_start_num >= float(minimum_refiner_denoise_start)) and
(refiner_denoise_start_num <= float(maximum_refiner_denoise_start)) and
(refiner_denoise_start_num_times_100 == refiner_denoise_start_num_times_100_with_int) and
((refiner_denoise_start_num_times_100 % refiner_denoise_start_input_slider_steps_times_100) == 0)
):
return True
else:
return False
except ValueError:
return False
#####################
#
# Refiner Steps
#
# Return True if valid.
#
#####################
def refining_steps_valid(
refining_steps_num_str
):
try:
refining_steps_num = int(refining_steps_num_str)
if (
(refining_steps_num > 0) and
(refining_steps_num <= int(maximum_refining_steps_for_online_config_field))
):
return True
else:
return False
except ValueError:
return False
#####################
#
# Upscaler Steps
#
# Return True if valid.
#
#####################
def upscaling_steps_valid(
upscaling_steps_num_str
):
try:
upscaling_steps_num = int(upscaling_steps_num_str)
if (
(upscaling_steps_num > 0) and
(upscaling_steps_num <= int(maximum_upscaler_steps))
):
return True
else:
return False
except ValueError:
return False
#####################
#
# Numerical Bool
#
# Return 1 for anything that is True/Yes/1. Everything else is False.
#
#####################
def numerical_bool(
original_value
):
new_value = 0
if (
(original_value == 1) or
(original_value == "Yes") or
(original_value == "True") or
(original_value == True)
):
new_value = 1
return new_value
#####################
#
# Truncate Prompt
#
# Truncate a prompt. Get the actual prompt that will be used and save the
# part of the prompt that will not be used.
#
#####################
def truncate_prompt (
pipe,
existing_prompt_text
):
# Only 77 tokens are allowed in the prompt. 2 are reserved, meaning it is
# truncated to 75. This happens automatically, but we want to tell people
# that
tokenizer = pipe.tokenizer
max_token_length_of_model = pipe.tokenizer.model_max_length - number_of_reserved_tokens
prompt_text_words_array = existing_prompt_text.split(" ")
prompt_text_words_array_length = len(prompt_text_words_array)
prompt_text_words_index = 0
prompt_text_substring = ""
prompt_text_not_used_substring = ""
for prompt_text_word in prompt_text_words_array:
prompt_text_words_index += 1
substring_to_test = prompt_text_substring
if prompt_text_words_index > 1:
substring_to_test += " "
substring_to_test += prompt_text_word
token_length_of_substring_to_test = len(tokenizer.tokenize(substring_to_test))
if token_length_of_substring_to_test > max_token_length_of_model:
prompt_text_not_used_substring += prompt_text_word + " "
else:
prompt_text_substring = substring_to_test
return (
prompt_text_substring,
prompt_text_not_used_substring
)
#####################
#
# Construct Pipe
#
# Prepare the base model.
#
#####################
def construct_pipe (
base_model_name_value,
model_configuration_name_value
):
if device == "cuda":
torch.cuda.empty_cache()
base_model_kwargs = {}
if (
(base_model_name_value == "sdxl") or
(base_model_name_value == "photoreal") or
(base_model_name_value == "sdxl_turbo") or
(base_model_name_value == "sd_1_5_runwayml")
):
base_model_kwargs["use_safetensors"] = True
if use_safety_checker == 0:
if (
(base_model_name_value == "photoreal") or
(base_model_name_value == "sd_1_5_runwayml")
):
base_model_kwargs = {
"safety_checker": None,
"requires_safety_checker": False
}
if device == "cuda":
if (
(base_model_name_value == "sdxl") or
(base_model_name_value == "sdxl_turbo") or
(base_model_name_value == "sd_1_5_runwayml")
):
base_model_kwargs["variant"] = "fp16"
base_model_kwargs["torch_dtype"] = torch.float16
if use_custom_hugging_face_cache_dir == 1:
base_model_kwargs["cache_dir"] = hugging_face_cache_dir
pipe = DiffusionPipeline.from_pretrained(
pretrained_model_name_or_path = model_configuration_links_object[model_configuration_name_value],
local_files_only = only_use_local_files_bool,
**base_model_kwargs
)
if use_model_cpu_offload_for_base_model == 1:
pipe.enable_model_cpu_offload()
if use_xformers == 1:
pipe.enable_xformers_memory_efficient_attention()
pipe = pipe.to(device)
if use_sequential_cpu_offload_for_base_model == 1:
pipe.enable_sequential_cpu_offload()
if use_default_attn_processor == 1:
pipe.unet.set_default_attn_processor()
if device == "cuda":
torch.cuda.empty_cache()
# else:
# pipe.unet = torch.compile(
# pipe.unet,
# mode = "reduce-overhead",
# fullgraph = True
# )
return (
pipe
)
#####################
#
# Configure Scheduler
#
#####################
def configure_scheduler (
pipe,
scheduler_value
):
scheduler_config = pipe.scheduler.config
scheduler = scheduler_value
if scheduler_value == "model_default":
scheduler_name = pipe.scheduler.config._class_name
if scheduler_name in scheduler_name_to_identifier_in_app_object:
scheduler = scheduler_name_to_identifier_in_app_object[scheduler_name]
scheduler_used = scheduler
if scheduler == "ddim":
from diffusers import DDIMScheduler
pipe.scheduler = DDIMScheduler.from_config(scheduler_config)
elif scheduler == "ddpm":
from diffusers import DDPMScheduler
pipe.scheduler = DDPMScheduler.from_config(scheduler_config)
elif scheduler == "dpm_solver_multistep":
from diffusers import DPMSolverMultistepScheduler
pipe.scheduler = DPMSolverMultistepScheduler.from_config(scheduler_config)
elif scheduler == "dpm_solver_multistep_karras_sigmas_true":
new_scheduler_config = dict(pipe.scheduler.config)
new_scheduler_config.update({"use_karras_sigmas": True})
from diffusers import DPMSolverMultistepScheduler
pipe.scheduler = DPMSolverMultistepScheduler.from_config(new_scheduler_config)
elif scheduler == "dpm_solver_multistep_algorithm_type_sde-dpmsolver_pp":
new_scheduler_config = dict(pipe.scheduler.config)
new_scheduler_config.update({"algorithm_type": "sde-dpmsolver++"})
from diffusers import DPMSolverMultistepScheduler
pipe.scheduler = DPMSolverMultistepScheduler.from_config(new_scheduler_config)
elif scheduler == "dpm_solver_multistep_karras_sigmas_true_algorithm_type_sde-dpmsolver_pp":
new_scheduler_config = dict(pipe.scheduler.config)
new_scheduler_config.update({"use_karras_sigmas": True})
new_scheduler_config.update({"algorithm_type": "sde-dpmsolver++"})
from diffusers import DPMSolverMultistepScheduler
pipe.scheduler = DPMSolverMultistepScheduler.from_config(new_scheduler_config)
elif scheduler == "dpm_solver_singlestep":
from diffusers import DPMSolverSinglestepScheduler
pipe.scheduler = DPMSolverSinglestepScheduler.from_config(scheduler_config)
elif scheduler == "dpm_solver_singlestep_karras_sigmas_true":
new_scheduler_config = dict(pipe.scheduler.config)
new_scheduler_config.update({"use_karras_sigmas": True})
from diffusers import DPMSolverSinglestepScheduler
pipe.scheduler = DPMSolverSinglestepScheduler.from_config(new_scheduler_config)
elif scheduler == "kdpm2_discrete":
from diffusers import KDPM2DiscreteScheduler
pipe.scheduler = KDPM2DiscreteScheduler.from_config(scheduler_config)
elif scheduler == "kdpm2_discrete_karras_sigmas_true":
new_scheduler_config = dict(pipe.scheduler.config)
new_scheduler_config.update({"use_karras_sigmas": True})
from diffusers import KDPM2DiscreteScheduler
pipe.scheduler = KDPM2DiscreteScheduler.from_config(new_scheduler_config)
elif scheduler == "kdpm2_ancestral_discrete":
from diffusers import KDPM2AncestralDiscreteScheduler
pipe.scheduler = KDPM2AncestralDiscreteScheduler.from_config(scheduler_config)
elif scheduler == "kdpm2_ancestral_discrete_karras_sigmas_true":
new_scheduler_config = dict(pipe.scheduler.config)
new_scheduler_config.update({"use_karras_sigmas": True})
from diffusers import KDPM2AncestralDiscreteScheduler
pipe.scheduler = KDPM2AncestralDiscreteScheduler.from_config(new_scheduler_config)
elif scheduler == "euler_discrete":
from diffusers import EulerDiscreteScheduler
pipe.scheduler = EulerDiscreteScheduler.from_config(scheduler_config)
elif scheduler == "euler_ancestral_discrete":
from diffusers import EulerAncestralDiscreteScheduler
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(scheduler_config)
elif scheduler == "heun_discrete":
from diffusers import HeunDiscreteScheduler
pipe.scheduler = HeunDiscreteScheduler.from_config(scheduler_config)
elif scheduler == "lms_discrete":
from diffusers import LMSDiscreteScheduler
pipe.scheduler = LMSDiscreteScheduler.from_config(scheduler_config)
elif scheduler == "lms_discrete_karras_sigmas_true":
new_scheduler_config = dict(pipe.scheduler.config)
new_scheduler_config.update({"use_karras_sigmas": True})
from diffusers import LMSDiscreteScheduler
pipe.scheduler = LMSDiscreteScheduler.from_config(new_scheduler_config)
elif scheduler == "pndm":
from diffusers import PNDMScheduler
pipe.scheduler = PNDMScheduler.from_config(scheduler_config)
elif scheduler == "pndm_skip_prk_steps_true":
new_scheduler_config = dict(pipe.scheduler.config)
new_scheduler_config.update({"skip_prk_steps": True})
from diffusers import PNDMScheduler
pipe.scheduler = PNDMScheduler.from_config(new_scheduler_config)
elif scheduler == "deis_multistep":
from diffusers import DEISMultistepScheduler
pipe.scheduler = DEISMultistepScheduler.from_config(scheduler_config)
elif scheduler == "dpm_solver_sde":
from diffusers import DPMSolverSDEScheduler
pipe.scheduler = DPMSolverSDEScheduler.from_config(scheduler_config)
elif scheduler == "uni_pc_multistep":
from diffusers import UniPCMultistepScheduler
pipe.scheduler = UniPCMultistepScheduler.from_config(scheduler_config)
else:
from diffusers import PNDMScheduler
pipe.scheduler = PNDMScheduler.from_config(scheduler_config)
scheduler_used = "pndm"
return (
scheduler_used
)
#####################
#
# Construct Refiner
#
# Prepare the refiner.
#
#####################
def construct_refiner ():
refiner_kwargs = {
"use_safetensors": True
}
if device == "cuda":
refiner_kwargs["variant"] = "fp16"
refiner_kwargs["torch_dtype"] = torch.float16
if use_custom_hugging_face_cache_dir == 1:
refiner_kwargs["cache_dir"] = hugging_face_cache_dir
refiner = DiffusionPipeline.from_pretrained(
pretrained_model_name_or_path = hugging_face_refiner_partial_path,
local_files_only = only_use_local_files_bool,
**refiner_kwargs
)
if use_model_cpu_offload_for_refiner == 1:
refiner.enable_model_cpu_offload()
if use_xformers == 1:
refiner.enable_xformers_memory_efficient_attention()
refiner = refiner.to(device)
if use_sequential_cpu_offload_for_refiner == 1:
refiner.enable_sequential_cpu_offload()
if use_default_attn_processor == 1:
refiner.unet.set_default_attn_processor()
if device == "cuda":
torch.cuda.empty_cache()
# else:
# refiner.unet = torch.compile(
# refiner.unet,
# mode = "reduce-overhead",
# fullgraph = True
# )
return (
refiner
)
#####################
#
# Construct Upscaler
#
# Prepare the upscaler.
#
#####################
def construct_upscaler ():
upscaler_kwargs = {
"use_safetensors": True
}
if device == "cuda":
upscaler_kwargs["torch_dtype"] = torch.float16
if use_custom_hugging_face_cache_dir == 1:
upscaler_kwargs["cache_dir"] = hugging_face_cache_dir
upscaler = DiffusionPipeline.from_pretrained(
pretrained_model_name_or_path = hugging_face_upscaler_partial_path,
local_files_only = only_use_local_files_bool,
**upscaler_kwargs
)
if use_model_cpu_offload_for_upscaler == 1:
upscaler.enable_model_cpu_offload()
if use_xformers == 1:
upscaler.enable_xformers_memory_efficient_attention()
upscaler = upscaler.to(device)
if use_sequential_cpu_offload_for_upscaler == 1:
upscaler.enable_sequential_cpu_offload()
if use_default_attn_processor == 1:
upscaler.unet.set_default_attn_processor()
if device == "cuda":
torch.cuda.empty_cache()
# else:
# upscaler.unet = torch.compile(
# upscaler.unet,
# mode = "reduce-overhead",
# fullgraph = True
# )
return (
upscaler
)
#####################
#
# Update Prompt Info From Gallery
#
# If you select an image in the image gallery, display the prompt
# information for that image.
#
#####################
def update_prompt_info_from_gallery (
gallery_data: gr.SelectData,
image_gallery_array_state_value
):
gallery_data_index = gallery_data.index
output_image_gallery_field_update = gr.Gallery(
selected_index = gallery_data_index
)
output_text_field_update = image_gallery_array_state_value[gallery_data_index]
return {
output_image_gallery_field: output_image_gallery_field_update,
output_text_field: output_text_field_update
}
#####################
#
# Before Create Image Function
#
# This is loaded before the image creation begins.
#
#####################
def before_create_image_function ():
generate_image_btn_update = gr.Button(
value = "Generating...",
variant = "secondary",
interactive = False
)
output_text_field_update = gr.Textbox(
visible = False
)
return {
generate_image_btn: generate_image_btn_update,
output_text_field: output_text_field_update
}
#####################
#
# After Create Image Function
#
# This is loaded once image creation has completed.
#
#####################
def after_create_image_function ():
generate_image_btn_update = gr.Button(
value = "Generate",
variant = "primary",
interactive = True
)
output_text_field_update = gr.Textbox(
visible = True
)
return {
generate_image_btn: generate_image_btn_update,
output_text_field: output_text_field_update
}
#####################
#
# Create Image Function
#
# This is the main image creation function.
#
#####################
def create_image_function (
base_model_field_index,
prompt_text,
negative_prompt_text,
scheduler_index,
image_width,
image_height,
guidance_scale,
base_model_num_inference_steps,
base_model_steps_field_for_sdxl_turbo,
actual_seed,
add_seed_into_pipe,
refining_selection_default_config_field_value,
refining_selection_online_config_normal_field_value,
refining_selection_online_config_automatically_selected_field_value,
refining_denoise_start_for_default_config_field_value,
refining_use_denoising_start_in_base_model_when_using_refiner_field_value,
refining_base_model_output_to_refiner_is_in_latent_space_field_value,
refining_denoise_start_for_online_config_field_value,
refining_steps_for_sdxl_online_config_field_value,
upscaling_selection_field_value,
upscaling_num_inference_steps,
image_gallery_array_state_value,
prompt_information_array_state_value,
last_model_configuration_name_selected_state_value,
last_refiner_name_selected_state_value,
last_upscaler_name_selected_state_value,
stored_pipe_state,
stored_refiner_state,
stored_upscaler_state,
*model_configuration_dropdown_fields_array,
progress = gr.Progress()
):
image_width = int(image_width)
image_height = int(image_height)
guidance_scale = float(guidance_scale)
base_model_num_inference_steps = int(base_model_num_inference_steps)
base_model_steps_field_for_sdxl_turbo = int(base_model_steps_field_for_sdxl_turbo)
actual_seed = int(actual_seed)
refining_denoise_start_for_default_config_field_value = rounded_number(refining_denoise_start_for_default_config_field_value, 2)
refining_denoise_start_for_online_config_field_value = rounded_number(refining_denoise_start_for_online_config_field_value, 2)
refining_steps_for_sdxl_online_config_field_value = int(refining_steps_for_sdxl_online_config_field_value)
upscaling_num_inference_steps = int(upscaling_num_inference_steps)
base_model_name_value = base_model_array[base_model_field_index]
position_in_array = 0
model_configuration_field_object = {}
for model_configuration_field_index in model_configuration_dropdown_fields_array:
this_base_model = base_model_array[position_in_array]
model_configuration_field_object[this_base_model] = model_configuration_field_index
position_in_array += 1
model_configuration_field_index = model_configuration_field_object[base_model_name_value]
model_configuration_name_value = base_model_object_of_model_configuration_arrays[base_model_name_value][model_configuration_field_index]
current_estimated_total_base_model_steps = base_model_num_inference_steps
current_estimated_total_refiner_steps = 0
current_estimated_total_upscaler_steps = upscaling_num_inference_steps
global current_actual_total_base_model_steps
global current_actual_total_refiner_steps
global current_actual_total_upscaler_steps
current_actual_total_base_model_steps = 0
current_actual_total_refiner_steps = 0
current_actual_total_upscaler_steps = 0
if base_model_name_value == "sdxl_turbo":
negative_prompt_text = ""
base_model_num_inference_steps = base_model_steps_field_for_sdxl_turbo
current_estimated_total_base_model_steps = base_model_num_inference_steps
guidance_scale = 0
scheduler_value = schedulers_array[scheduler_index]
if not base_model_valid(base_model_name_value):
error_function("Base model is not valid.")
if not model_configuration_valid(base_model_name_value, model_configuration_name_value):
error_function("Model configuration is not valid.")
if not prompt_valid(prompt_text):
error_function("Prompt is not valid.")
if not negative_prompt_valid(negative_prompt_text):
error_function("Negative prompt is not valid.")
if not scheduler_valid(scheduler_value):
error_function("Scheduler/sampler is not valid.")
if not width_valid(image_width):
error_function("Image width is not valid.")
if not height_valid(image_height):
error_function("Image height is not valid.")
if base_model_name_value != "sdxl_turbo":
if not guidance_scale_valid(guidance_scale):
error_function("Guidance scale is not valid.")
base_model_steps = base_model_num_inference_steps
if base_model_name_value == "sdxl_turbo":
steps = base_model_steps_field_for_sdxl_turbo
if not steps_valid(base_model_steps, base_model_name_value):
error_function("Steps option is not valid.")
if not seed_valid(actual_seed):
error_function("Seed is not valid.")
add_seed_into_pipe = numerical_bool(add_seed_into_pipe)
refining_selection_default_config_field_value = numerical_bool(refining_selection_default_config_field_value)
refining_selection_online_config_normal_field_value = numerical_bool(refining_selection_online_config_normal_field_value)
refining_selection_online_config_automatically_selected_field_value = numerical_bool(refining_selection_online_config_automatically_selected_field_value)
refining_use_denoising_start_in_base_model_when_using_refiner_field_value = numerical_bool(refining_use_denoising_start_in_base_model_when_using_refiner_field_value)
refining_base_model_output_to_refiner_is_in_latent_space_field_value = numerical_bool(refining_base_model_output_to_refiner_is_in_latent_space_field_value)
use_upscaler = numerical_bool(upscaling_selection_field_value)
is_default_config_state = 1
if model_configuration_name_value in online_configurations_object:
is_default_config_state = 0
is_sdxl_online_config = 0
is_photoreal_online_config = 0
if (
(model_configuration_name_value in sdxl_online_configurations_object) and
(sdxl_online_configurations_object[model_configuration_name_value])
):
is_sdxl_online_config = 1
elif (
(model_configuration_name_value in photoreal_online_configurations_object) and
photoreal_online_configurations_object[model_configuration_name_value]
):
is_photoreal_online_config = 1
use_refiner = 0
if (
(
(is_default_config_state == 1) and
refining_selection_default_config_field_value
) or (
(is_default_config_state != 1) and
(
(
(model_configuration_name_value not in model_configuration_force_refiner_object) and
refining_selection_online_config_normal_field_value
) or (
(model_configuration_name_value in model_configuration_force_refiner_object) and
refining_selection_online_config_automatically_selected_field_value
)
)
)
):
use_refiner = 1
if is_default_config_state == 1:
if not refiner_denoise_start_valid(refining_denoise_start_for_default_config_field_value):
error_function("Refiner denoise start is not valid.")
else:
if (model_configuration_name_value not in model_configuration_force_refiner_object):
if not refiner_denoise_start_valid(refining_denoise_start_for_online_config_field_value):
error_function("Refiner denoise start is not valid.")
if (is_sdxl_online_config == 1):
if not refining_steps_valid(refining_steps_for_sdxl_online_config_field_value):
error_function("Refining steps option is not valid.")
if use_upscaler == 1:
if not upscaling_steps_valid(upscaling_num_inference_steps):
error_function("Upscaling steps option is not valid.")
if (
(last_model_configuration_name_selected_state_value == "") or
(model_configuration_name_value != last_model_configuration_name_selected_state_value)
):
if (last_model_configuration_name_selected_state_value != ""):
if "pipe" in globals():
del pipe
if show_messages_in_command_prompt == 1:
print ("Base model is loading.");
progress(
progress = 0,
desc = "Base model is loading"
)
(
pipe
) = construct_pipe(
base_model_name_value,
model_configuration_name_value
)
last_model_configuration_name_selected_state_value = model_configuration_name_value
else:
pipe = stored_pipe_state
(
scheduler_used
) = configure_scheduler(
pipe,
scheduler_value
)
if use_refiner == 1:
if (last_refiner_name_selected_state_value == ""):
if show_messages_in_command_prompt == 1:
print ("Refiner is loading.");
progress(
progress = 0,
desc = "Refiner is loading"
)
refiner = construct_refiner()
last_refiner_name_selected_state_value = "refiner"
else:
refiner = stored_refiner_state
else:
refiner = {}
if use_upscaler == 1:
if (last_upscaler_name_selected_state_value == ""):
if show_messages_in_command_prompt == 1:
print ("Upscaler is loading.");
progress(
progress = 0,
desc = "Upscaler is loading"
)
upscaler = construct_upscaler()
last_upscaler_name_selected_state_value = "upscaler"
else:
upscaler = stored_upscaler_state
else:
upscaler = ""
if log_generation_times == 1:
start_time = time.time()
# Only 77 tokens are allowed in the prompt. 2 are reserved, meaning it is
# truncated to 75. This happens automatically, but we want to tell people
# that
tokenizer = pipe.tokenizer
max_token_length_of_model = pipe.tokenizer.model_max_length - number_of_reserved_tokens
token_length_of_prompt_text = len(tokenizer.tokenize(prompt_text))
token_length_of_negative_prompt_text = len(tokenizer.tokenize(negative_prompt_text))
prompt_text_not_used_substring = ""
message_about_prompt_truncation = ""
if token_length_of_prompt_text > max_token_length_of_model:
(
prompt_text,
prompt_text_not_used_substring
) = truncate_prompt(
pipe,
prompt_text
)
message_about_prompt_truncation += "Your prompt has been truncated because it is too long. This part has been truncated:
" + prompt_text_not_used_substring + ""
negative_prompt_text_not_used_substring = ""
if token_length_of_negative_prompt_text > max_token_length_of_model:
(
negative_prompt_text,
negative_prompt_text_not_used_substring
) = truncate_prompt(
pipe,
negative_prompt_text
)
if len(message_about_prompt_truncation) > 0:
message_about_prompt_truncation += "
"
message_about_prompt_truncation += "Your negative prompt has been truncated because it is too long. This part has been truncated:
" + negative_prompt_text_not_used_substring + ""
prompt_truncated_field_update = gr.HTML(
value = "",
visible = False
)
if len(message_about_prompt_truncation) > 0:
prompt_truncated_field_update = gr.HTML(
value = "