Add settings tab

main
natanjunges 2022-12-31 15:59:53 -03:00
parent ebc187a1c4
commit 3d05814ae9
2 changed files with 110 additions and 6 deletions

View File

@ -25,6 +25,9 @@ import os.path
import numpy
import itertools
import torch
import json
settings_file = os.path.join(scripts.basedir(), "settings.json")
class FakeModel:
sd_model_hash=""
@ -50,12 +53,6 @@ class Main(scripts.Script):
}
KARRAS = {"LMS Karras", "DPM2 Karras", "DPM2 a Karras", "DPM++ 2S a Karras", "DPM++ 2M Karras"}
POST_PROCESSINGS = {"CodeFormers (Restore faces)", "GFPGAN (Restore faces)", "RealESRGAN_x4plus (Upscale)"}
#settings tab
api_endpoint = "https://stablehorde.net/api"
api_key = "0000000000"
censor_nsfw = True
trusted_workers = True
workers = []
def title(self):
return self.TITLE
@ -63,7 +60,26 @@ class Main(scripts.Script):
def show(self, is_img2img):
return True
def load_settings(self):
if os.path.exists(settings_file):
with open(settings_file) as file:
opts = json.load(file)
self.api_endpoint = opts["api_endpoint"]
self.api_key = opts["api_key"]
self.censor_nsfw = opts["censor_nsfw"]
self.trusted_workers = opts["trusted_workers"]
self.workers = opts["workers"]
else:
self.api_endpoint = "https://stablehorde.net/api"
self.api_key = "0000000000"
self.censor_nsfw = True
self.trusted_workers = True
self.workers = []
def load_models(self):
self.load_settings()
try:
models = requests.get("{}/v2/status/models".format(self.api_endpoint))
assert models.status_code == 200
@ -310,6 +326,7 @@ class Main(scripts.Script):
"n": p.batch_size
}
}
self.load_settings()
if nsfw:
payload["nsfw"] = True

87
scripts/settings.py Normal file
View File

@ -0,0 +1,87 @@
# stable-diffusion-webui-stable-horde, a Stable Horde client integration to AUTOMATIC1111's Stable Diffusion web UI
# Copyright (C) 2022 Natan Junges <natanajunges@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from modules import script_callbacks, scripts, ui
import gradio
import json
import os.path
settings_file = os.path.join(scripts.basedir(), "settings.json")
def settings():
with gradio.Blocks(analytics_enabled=False) as settings_tab:
api_endpoint = gradio.Textbox(max_lines=1, placeholder="https://stablehorde.net/api", label="API endpoint", interactive=True)
api_key = gradio.Textbox(max_lines=1, placeholder="0000000000", label="API key", interactive=True)
censor_nsfw = gradio.Checkbox(label="Censor NSFW when NSFW is disabled", interactive=True)
trusted_workers = gradio.Checkbox(label="Only send requests to trusted workers", interactive=True)
workers = gradio.Textbox(max_lines=1, label="Only send requests to these workers", interactive=True)
with gradio.Row():
reset = gradio.Button(value=ui.reuse_symbol + " Reset settings")
reload = gradio.Button(value=ui.refresh_symbol + " Reload settings")
apply = gradio.Button(value=ui.save_style_symbol + " Apply settings", variant="primary")
def reset_click():
opts = {
"api_endpoint": "https://stablehorde.net/api",
"api_key": "0000000000",
"censor_nsfw": True,
"trusted_workers": True,
"workers": []
}
return (gradio.update(value=opts["api_endpoint"]), gradio.update(value=opts["api_key"]), gradio.update(value=opts["censor_nsfw"]), gradio.update(value=opts["trusted_workers"]), gradio.update(value=", ".join(opts["workers"])))
def reload_click():
if os.path.exists(settings_file):
with open(settings_file) as file:
opts = json.load(file)
return (gradio.update(value=opts["api_endpoint"]), gradio.update(value=opts["api_key"]), gradio.update(value=opts["censor_nsfw"]), gradio.update(value=opts["trusted_workers"]), gradio.update(value=", ".join(opts["workers"])))
else:
return reset_click()
def apply_click(api_endpoint, api_key, censor_nsfw, trusted_workers, workers):
if len(api_endpoint) == 0:
api_endpoint = "https://stablehorde.net/api"
if len(api_key) == 0:
api_key = "0000000000"
if len(workers) == 0:
workers = []
else:
workers = list(map(lambda w: w.strip(), workers.split(",")))
opts = {
"api_endpoint": api_endpoint,
"api_key": api_key,
"censor_nsfw": censor_nsfw,
"trusted_workers": trusted_workers,
"workers": workers
}
with open(settings_file, "w") as file:
json.dump(opts, file)
reset.click(fn=reset_click, outputs=[api_endpoint, api_key, censor_nsfw, trusted_workers, workers])
reload.click(fn=reload_click, outputs=[api_endpoint, api_key, censor_nsfw, trusted_workers, workers])
apply.click(fn=apply_click, inputs=[api_endpoint, api_key, censor_nsfw, trusted_workers, workers])
settings_tab.load(fn=reload_click, outputs=[api_endpoint, api_key, censor_nsfw, trusted_workers, workers])
return [(settings_tab, "Stable Horde Settings", "stable_horde_settings")]
script_callbacks.on_ui_tabs(settings)