2022-09-15 13:48:55 +03:00
# base webui import and utils.
2022-09-07 05:30:04 +03:00
import streamlit as st
2022-09-12 09:11:42 +03:00
2022-09-15 13:48:55 +03:00
# streamlit imports
import streamlit_nested_layout
#streamlit components section
from st_on_hover_tabs import on_hover_tabs
#other imports
2022-09-14 00:08:40 +03:00
import warnings
import os
2022-09-07 05:30:04 +03:00
import k_diffusion as K
2022-09-14 14:19:24 +03:00
from omegaconf import OmegaConf
2022-09-07 05:30:04 +03:00
2022-09-14 00:08:40 +03:00
from sd_utils import *
2022-09-08 15:33:45 +03:00
2022-09-15 13:48:55 +03:00
# end of imports
#---------------------------------------------------------------------------------------------------------------
2022-09-07 05:30:04 +03:00
try :
# this silences the annoying "Some weights of the model checkpoint were not used when initializing..." message at start.
from transformers import logging
logging . set_verbosity_error ( )
except :
pass
# remove some annoying deprecation warnings that show every now and then.
warnings . filterwarnings ( " ignore " , category = DeprecationWarning )
# this should force GFPGAN and RealESRGAN onto the selected gpu as well
2022-09-18 01:25:55 +03:00
#os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID" # see issue #152
#os.environ["CUDA_VISIBLE_DEVICES"] = str(st.session_state["defaults"].general.gpu)
2022-09-07 05:30:04 +03:00
2022-09-08 05:10:03 +03:00
# functions to load css locally OR remotely starts here. Options exist for future flexibility. Called as st.markdown with unsafe_allow_html as css injection
# TODO, maybe look into async loading the file especially for remote fetching
def local_css ( file_name ) :
2022-09-14 00:08:40 +03:00
with open ( file_name ) as f :
st . markdown ( f ' <style> { f . read ( ) } </style> ' , unsafe_allow_html = True )
2022-09-08 05:10:03 +03:00
def remote_css ( url ) :
2022-09-14 00:08:40 +03:00
st . markdown ( f ' <link href= " { url } " rel= " stylesheet " > ' , unsafe_allow_html = True )
2022-09-08 05:10:03 +03:00
def load_css ( isLocal , nameOrURL ) :
if ( isLocal ) :
local_css ( nameOrURL )
else :
remote_css ( nameOrURL )
2022-09-07 05:30:04 +03:00
def layout ( ) :
2022-09-14 00:08:40 +03:00
""" Layout functions to define all the streamlit layout here. """
2022-09-12 09:11:42 +03:00
st . set_page_config ( page_title = " Stable Diffusion Playground " , layout = " wide " )
2022-09-07 05:30:04 +03:00
2022-09-08 05:10:03 +03:00
with st . empty ( ) :
# load css as an external file, function has an option to local or remote url. Potential use when running from cloud infra that might not have access to local path.
load_css ( True , ' frontend/css/streamlit.main.css ' )
2022-09-12 21:44:00 +03:00
2022-09-07 05:30:04 +03:00
# check if the models exist on their respective folders
2022-09-14 16:40:56 +03:00
if os . path . exists ( os . path . join ( st . session_state [ " defaults " ] . general . GFPGAN_dir , " experiments " , " pretrained_models " , " GFPGANv1.3.pth " ) ) :
2022-09-14 00:08:40 +03:00
st . session_state [ " GFPGAN_available " ] = True
2022-09-07 05:30:04 +03:00
else :
2022-09-14 00:08:40 +03:00
st . session_state [ " GFPGAN_available " ] = False
2022-09-07 06:20:29 +03:00
2022-09-14 16:40:56 +03:00
if os . path . exists ( os . path . join ( st . session_state [ " defaults " ] . general . RealESRGAN_dir , " experiments " , " pretrained_models " , f " { st . session_state [ ' defaults ' ] . general . RealESRGAN_model } .pth " ) ) :
2022-09-14 00:08:40 +03:00
st . session_state [ " RealESRGAN_available " ] = True
2022-09-07 05:30:04 +03:00
else :
2022-09-14 00:08:40 +03:00
st . session_state [ " RealESRGAN_available " ] = False
2022-09-08 15:33:45 +03:00
# Allow for custom models to be used instead of the default one,
# an example would be Waifu-Diffusion or any other fine tune of stable diffusion
2022-09-14 00:08:40 +03:00
st . session_state [ " custom_models " ] : sorted = [ ]
2022-09-08 15:33:45 +03:00
for root , dirs , files in os . walk ( os . path . join ( " models " , " custom " ) ) :
for file in files :
if os . path . splitext ( file ) [ 1 ] == ' .ckpt ' :
2022-09-14 00:08:40 +03:00
#fullpath = os.path.join(root, file)
2022-09-08 15:33:45 +03:00
#print(fullpath)
2022-09-14 00:08:40 +03:00
st . session_state [ " custom_models " ] . append ( os . path . splitext ( file ) [ 0 ] )
2022-09-08 15:33:45 +03:00
#print (os.path.splitext(file)[0])
2022-09-14 00:08:40 +03:00
if len ( st . session_state [ " custom_models " ] ) > 0 :
st . session_state [ " CustomModel_available " ] = True
st . session_state [ " custom_models " ] . append ( " Stable Diffusion v1.4 " )
2022-09-08 15:33:45 +03:00
else :
2022-09-14 00:08:40 +03:00
st . session_state [ " CustomModel_available " ] = False
2022-09-07 06:20:29 +03:00
2022-09-07 05:30:04 +03:00
with st . sidebar :
2022-09-12 09:11:42 +03:00
# The global settings section will be moved to the Settings page.
2022-09-07 05:30:04 +03:00
#with st.expander("Global Settings:"):
2022-09-12 09:11:42 +03:00
#st.write("Global Settings:")
#defaults.general.update_preview = st.checkbox("Update Image Preview", value=defaults.general.update_preview,
#help="If enabled the image preview will be updated during the generation instead of at the end. You can use the Update Preview \
#Frequency option bellow to customize how frequent it's updated. By default this is enabled and the frequency is set to 1 step.")
#st.session_state.update_preview_frequency = st.text_input("Update Image Preview Frequency", value=defaults.general.update_preview_frequency,
#help="Frequency in steps at which the the preview image is updated. By default the frequency is set to 1 step.")
tabs = on_hover_tabs ( tabName = [ ' Stable Diffusion ' , " Textual Inversion " , " Model Manager " , " Settings " ] ,
iconName = [ ' dashboard ' , ' model_training ' , ' cloud_download ' , ' settings ' ] , default_choice = 0 )
if tabs == ' Stable Diffusion ' :
2022-09-16 00:23:09 +03:00
txt2img_tab , img2img_tab , txt2vid_tab , postprocessing_tab = st . tabs ( [ " Text-to-Image Unified " , " Image-to-Image Unified " ,
2022-09-15 16:29:41 +03:00
" Text-to-Video " , " Post-Processing " ] )
2022-09-16 00:23:09 +03:00
#with home_tab:
#from home import layout
#layout()
2022-09-15 16:29:41 +03:00
2022-09-14 00:08:40 +03:00
with txt2img_tab :
from txt2img import layout
layout ( )
2022-09-12 09:11:42 +03:00
2022-09-14 00:08:40 +03:00
with img2img_tab :
from img2img import layout
layout ( )
2022-09-12 09:11:42 +03:00
2022-09-14 00:08:40 +03:00
with txt2vid_tab :
from txt2vid import layout
layout ( )
2022-09-12 09:11:42 +03:00
#
elif tabs == ' Model Manager ' :
2022-09-14 00:08:40 +03:00
from ModelManager import layout
2022-09-14 09:02:16 +03:00
layout ( )
2022-09-14 00:08:40 +03:00
2022-09-15 00:48:13 +03:00
elif tabs == ' Textual Inversion ' :
2022-09-16 00:23:09 +03:00
from textual_inversion import layout
2022-09-15 00:48:13 +03:00
layout ( )
2022-09-07 05:30:04 +03:00
if __name__ == ' __main__ ' :
2022-09-14 00:08:40 +03:00
layout ( )