2022-10-24 03:31:41 +03:00
# This file is part of sygil-webui (https://github.com/Sygil-Dev/sygil-webui/).
2022-09-26 16:02:48 +03:00
2022-10-24 03:17:50 +03:00
# Copyright 2022 Sygil-Dev team.
2022-09-26 16:02:48 +03:00
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
2022-10-11 11:27:44 +03:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2022-09-28 19:33:54 +03:00
2022-09-15 13:48:55 +03:00
# base webui import and utils.
2022-09-28 19:33:54 +03:00
#import streamlit as st
# We import hydralit like this to replace the previous stuff
# we had with native streamlit as it lets ur replace things 1:1
2022-11-03 09:57:48 +03:00
from sd_utils import st , hc , load_configs , load_css , set_logger_verbosity , \
logger , quiesce_logger , set_page_title , threading , random
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
2022-10-20 05:55:34 +03:00
#from st_on_hover_tabs import on_hover_tabs
2022-09-25 10:03:05 +03:00
from streamlit_server_state import server_state , server_state_lock
2022-09-15 13:48:55 +03:00
#other imports
2022-10-14 22:09:47 +03:00
import argparse
2022-11-03 09:57:48 +03:00
from sd_utils . bridge import run_bridge
2022-09-07 05:30:04 +03:00
2022-10-19 17:52:08 +03:00
# import custom components
2022-10-20 12:39:09 +03:00
from custom_components import draggable_number_input
2022-09-26 11:12:06 +03:00
2022-09-15 13:48:55 +03:00
# end of imports
#---------------------------------------------------------------------------------------------------------------
2022-09-07 05:30:04 +03:00
2022-10-18 15:49:30 +03:00
load_configs ( )
2022-10-14 22:09:47 +03:00
help = """
A double dash ( ` - - ` ) is used to separate streamlit arguments from app arguments .
As a result using " streamlit run webui_streamlit.py --headless "
will show the help for streamlit itself and not pass any argument to our app ,
we need to use " streamlit run webui_streamlit.py -- --headless "
in order to pass a command argument to this app . """
parser = argparse . ArgumentParser ( formatter_class = argparse . ArgumentDefaultsHelpFormatter )
parser . add_argument ( " --headless " , action = ' store_true ' , help = " Don ' t launch web server, util if you just want to run the stable horde bridge. " , default = False )
parser . add_argument ( " --bridge " , action = ' store_true ' , help = " don ' t launch web server, but make this instance into a Horde bridge. " , default = False )
parser . add_argument ( ' --horde_api_key ' , action = " store " , required = False , type = str , help = " The API key corresponding to the owner of this Horde instance " )
parser . add_argument ( ' --horde_name ' , action = " store " , required = False , type = str , help = " The server name for the Horde. It will be shown to the world and there can be only one. " )
parser . add_argument ( ' --horde_url ' , action = " store " , required = False , type = str , help = " The SH Horde URL. Where the bridge will pickup prompts and send the finished generations. " )
parser . add_argument ( ' --horde_priority_usernames ' , type = str , action = ' append ' , required = False , help = " Usernames which get priority use in this horde instance. The owner ' s username is always in this list. " )
parser . add_argument ( ' --horde_max_power ' , type = int , required = False , help = " How much power this instance has to generate pictures. Min: 2 " )
parser . add_argument ( ' --horde_sfw ' , action = ' store_true ' , required = False , help = " Set to true if you do not want this worker generating NSFW images. " )
parser . add_argument ( ' --horde_blacklist ' , nargs = ' + ' , required = False , help = " List the words that you want to blacklist. " )
parser . add_argument ( ' --horde_censorlist ' , nargs = ' + ' , required = False , help = " List the words that you want to censor. " )
parser . add_argument ( ' --horde_censor_nsfw ' , action = ' store_true ' , required = False , help = " Set to true if you want this bridge worker to censor NSFW images. " )
2022-10-14 23:27:56 +03:00
parser . add_argument ( ' --horde_model ' , action = ' store ' , required = False , help = " Which model to run on this horde. " )
2022-10-15 00:27:37 +03:00
parser . add_argument ( ' -v ' , ' --verbosity ' , action = ' count ' , default = 0 , help = " The default logging level is ERROR or higher. This value increases the amount of logging seen in your screen " )
parser . add_argument ( ' -q ' , ' --quiet ' , action = ' count ' , default = 0 , help = " The default logging level is ERROR or higher. This value decreases the amount of logging seen in your screen " )
2022-10-14 22:09:47 +03:00
opt = parser . parse_args ( )
with server_state_lock [ " bridge " ] :
server_state [ " bridge " ] = opt . bridge
2022-09-28 19:33:54 +03:00
2022-10-17 15:09:47 +03:00
@logger.catch ( reraise = True )
2022-10-14 22:09:47 +03:00
def layout ( ) :
""" Layout functions to define all the streamlit layout here. """
if not st . session_state [ " defaults " ] . debug . enable_hydralit :
2022-10-19 22:44:56 +03:00
st . set_page_config ( page_title = " Stable Diffusion Playground " , layout = " wide " , initial_sidebar_state = " collapsed " )
2022-10-14 22:09:47 +03:00
#app = st.HydraApp(title='Stable Diffusion WebUI', favicon="", sidebar_state="expanded", layout="wide",
#hide_streamlit_markers=False, allow_url_nav=True , clear_cross_app_sessions=False)
2022-11-03 09:57:48 +03:00
# 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-10-14 22:09:47 +03:00
2022-10-19 22:44:56 +03:00
#
# specify the primary menu definition
menu_data = [
{ ' id ' : ' Stable Diffusion ' , ' label ' : ' Stable Diffusion ' , ' icon ' : ' bi bi-grid-1x2-fill ' } ,
2022-10-31 15:37:41 +03:00
{ ' id ' : ' Train ' , ' label ' : " Train " , ' icon ' : " bi bi-lightbulb-fill " , ' submenu ' : [
{ ' id ' : ' Textual Inversion ' , ' label ' : ' Textual Inversion ' , ' icon ' : ' bi bi-lightbulb-fill ' } ,
{ ' id ' : ' Fine Tunning ' , ' label ' : ' Fine Tunning ' , ' icon ' : ' bi bi-lightbulb-fill ' } ,
] } ,
2022-10-19 22:44:56 +03:00
{ ' id ' : ' Model Manager ' , ' label ' : ' Model Manager ' , ' icon ' : ' bi bi-cloud-arrow-down-fill ' } ,
2022-10-27 09:48:48 +03:00
{ ' id ' : ' Tools ' , ' label ' : " Tools " , ' icon ' : " bi bi-tools " , ' submenu ' : [
{ ' id ' : ' API Server ' , ' label ' : ' API Server ' , ' icon ' : ' bi bi-server ' } ,
2022-11-01 20:29:44 +03:00
{ ' id ' : ' Barfi/BaklavaJS ' , ' label ' : ' Barfi/BaklavaJS ' , ' icon ' : ' bi bi-diagram-3-fill ' } ,
2022-10-27 09:48:48 +03:00
#{'id': 'API Server', 'label': 'API Server', 'icon': 'bi bi-server'},
] } ,
2022-10-19 22:44:56 +03:00
{ ' id ' : ' Settings ' , ' label ' : ' Settings ' , ' icon ' : ' bi bi-gear-fill ' } ,
]
over_theme = { ' txc_inactive ' : ' #FFFFFF ' , " menu_background " : ' #000000 ' }
menu_id = hc . nav_bar (
menu_definition = menu_data ,
#home_name='Home',
#login_name='Logout',
hide_streamlit_markers = False ,
override_theme = over_theme ,
sticky_nav = True ,
sticky_mode = ' pinned ' ,
)
2022-10-27 09:48:48 +03:00
#
#if menu_id == "Home":
#st.info("Under Construction. :construction_worker:")
2022-10-19 22:44:56 +03:00
if menu_id == " Stable Diffusion " :
2022-10-14 22:09:47 +03:00
# set the page url and title
2022-10-19 22:44:56 +03:00
#st.experimental_set_query_params(page='stable-diffusion')
2022-10-14 22:09:47 +03:00
try :
set_page_title ( " Stable Diffusion Playground " )
except NameError :
st . experimental_rerun ( )
2022-11-01 20:29:44 +03:00
txt2img_tab , img2img_tab , txt2vid_tab , img2txt_tab , post_processing_tab , concept_library_tab = st . tabs ( [ " Text-to-Image " , " Image-to-Image " ,
#"Inpainting",
" Text-to-Video " , " Image-To-Text " ,
" Post-Processing " , " Concept Library " ] )
2022-10-14 22:09:47 +03:00
#with home_tab:
#from home import layout
#layout()
with txt2img_tab :
from txt2img import layout
layout ( )
with img2img_tab :
from img2img import layout
layout ( )
#with inpainting_tab:
#from inpainting import layout
#layout()
with txt2vid_tab :
from txt2vid import layout
layout ( )
with img2txt_tab :
from img2txt import layout
layout ( )
2022-11-01 20:29:44 +03:00
with post_processing_tab :
from post_processing import layout
layout ( )
2022-10-14 22:09:47 +03:00
with concept_library_tab :
from sd_concept_library import layout
layout ( )
#
2022-10-19 22:44:56 +03:00
elif menu_id == ' Model Manager ' :
2022-10-14 22:09:47 +03:00
set_page_title ( " Model Manager - Stable Diffusion Playground " )
from ModelManager import layout
layout ( )
2022-10-19 22:44:56 +03:00
elif menu_id == ' Textual Inversion ' :
2022-10-14 22:09:47 +03:00
from textual_inversion import layout
layout ( )
2022-10-31 15:37:41 +03:00
elif menu_id == ' Fine Tunning ' :
#from textual_inversion import layout
#layout()
st . info ( " Under Construction. :construction_worker: " )
2022-10-20 05:55:34 +03:00
elif menu_id == ' API Server ' :
set_page_title ( " API Server - Stable Diffusion Playground " )
from APIServer import layout
layout ( )
2022-11-01 20:29:44 +03:00
elif menu_id == ' Barfi/BaklavaJS ' :
set_page_title ( " Barfi/BaklavaJS - Stable Diffusion Playground " )
from barfi_baklavajs import layout
layout ( )
2022-10-27 09:48:48 +03:00
2022-10-19 22:44:56 +03:00
elif menu_id == ' Settings ' :
2022-10-14 22:09:47 +03:00
set_page_title ( " Settings - Stable Diffusion Playground " )
from Settings import layout
layout ( )
2022-10-11 11:27:44 +03:00
2022-10-19 17:52:08 +03:00
# calling dragable input component module at the end, so it works on all pages
2022-10-20 12:39:09 +03:00
draggable_number_input . load ( )
2022-10-19 17:52:08 +03:00
2022-10-12 21:03:59 +03:00
2022-10-14 22:09:47 +03:00
if __name__ == ' __main__ ' :
2022-10-14 23:27:56 +03:00
set_logger_verbosity ( opt . verbosity )
quiesce_logger ( opt . quiet )
2022-10-15 14:13:38 +03:00
2022-10-14 22:09:47 +03:00
if not opt . headless :
layout ( )
with server_state_lock [ " bridge " ] :
if server_state [ " bridge " ] :
try :
import bridgeData as cd
except ModuleNotFoundError as e :
2022-10-14 23:27:56 +03:00
logger . warning ( " No bridgeData found. Falling back to default where no CLI args are set. " )
logger . debug ( str ( e ) )
2022-10-14 22:09:47 +03:00
except SyntaxError as e :
2022-10-14 23:27:56 +03:00
logger . warning ( " bridgeData found, but is malformed. Falling back to default where no CLI args are set. " )
logger . debug ( str ( e ) )
2022-10-14 22:09:47 +03:00
except Exception as e :
2022-10-14 23:27:56 +03:00
logger . warning ( " No bridgeData found, use default where no CLI args are set " )
logger . debug ( str ( e ) )
2022-10-14 22:09:47 +03:00
finally :
try : # check if cd exists (i.e. bridgeData loaded properly)
cd
except : # if not, create defaults
class temp ( object ) :
def __init__ ( self ) :
random . seed ( )
self . horde_url = " https://stablehorde.net "
# Give a cool name to your instance
self . horde_name = f " Automated Instance # { random . randint ( - 100000000 , 100000000 ) } "
# The api_key identifies a unique user in the horde
self . horde_api_key = " 0000000000 "
# Put other users whose prompts you want to prioritize.
# The owner's username is always included so you don't need to add it here, unless you want it to have lower priority than another user
self . horde_priority_usernames = [ ]
self . horde_max_power = 8
self . nsfw = True
2022-10-14 23:27:56 +03:00
self . censor_nsfw = False
self . blacklist = [ ]
self . censorlist = [ ]
self . models_to_load = [ " stable_diffusion " ]
2022-10-14 22:09:47 +03:00
cd = temp ( )
horde_api_key = opt . horde_api_key if opt . horde_api_key else cd . horde_api_key
horde_name = opt . horde_name if opt . horde_name else cd . horde_name
horde_url = opt . horde_url if opt . horde_url else cd . horde_url
horde_priority_usernames = opt . horde_priority_usernames if opt . horde_priority_usernames else cd . horde_priority_usernames
horde_max_power = opt . horde_max_power if opt . horde_max_power else cd . horde_max_power
2022-10-14 23:31:16 +03:00
# Not used yet
2022-10-15 00:27:37 +03:00
horde_models = [ opt . horde_model ] if opt . horde_model else cd . models_to_load
2022-10-14 22:09:47 +03:00
try :
horde_nsfw = not opt . horde_sfw if opt . horde_sfw else cd . horde_nsfw
except AttributeError :
horde_nsfw = True
try :
horde_censor_nsfw = opt . horde_censor_nsfw if opt . horde_censor_nsfw else cd . horde_censor_nsfw
except AttributeError :
horde_censor_nsfw = False
try :
horde_blacklist = opt . horde_blacklist if opt . horde_blacklist else cd . horde_blacklist
except AttributeError :
horde_blacklist = [ ]
try :
horde_censorlist = opt . horde_censorlist if opt . horde_censorlist else cd . horde_censorlist
except AttributeError :
horde_censorlist = [ ]
if horde_max_power < 2 :
horde_max_power = 2
horde_max_pixels = 64 * 64 * 8 * horde_max_power
2022-10-16 02:59:39 +03:00
logger . info ( f " Joining Horde with parameters: Server Name ' { horde_name } ' . Horde URL ' { horde_url } ' . Max Pixels { horde_max_pixels } " )
2022-10-15 14:13:38 +03:00
2022-10-14 22:09:47 +03:00
try :
2022-10-15 14:13:38 +03:00
thread = threading . Thread ( target = run_bridge ( 1 , horde_api_key , horde_name , horde_url ,
horde_priority_usernames , horde_max_pixels ,
horde_nsfw , horde_censor_nsfw , horde_blacklist ,
horde_censorlist ) , args = ( ) )
thread . daemon = True
thread . start ( )
#run_bridge(1, horde_api_key, horde_name, horde_url, horde_priority_usernames, horde_max_pixels, horde_nsfw, horde_censor_nsfw, horde_blacklist, horde_censorlist)
2022-10-14 22:09:47 +03:00
except KeyboardInterrupt :
print ( f " Keyboard Interrupt Received. Ending Bridge " )