Skip to content

Commit

Permalink
Merge pull request #82 from houseofsecrets/develop
Browse files Browse the repository at this point in the history
Get samplers and upscalers from API
  • Loading branch information
Danamir authored Nov 11, 2023
2 parents 097b8da + 1a28b3c commit c1400a9
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
3 changes: 3 additions & 0 deletions configs/config.json-dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
1.5,
2
],
"override_hr_upscalers": "false",
"hr_upscalers": [
"Latent (bicubic)",
"R-ESRGAN 4x+"
Expand All @@ -48,8 +49,10 @@
"pidinet_sketch",
"pidinet_scribble"
],
"override_samplers": "false",
"samplers": [
"DPM++ 2M Karras",
"DPM++ 2M SDE Karras",
"DDIM",
"Euler",
"Euler a",
Expand Down
26 changes: 26 additions & 0 deletions scripts/common/cn_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,32 @@ def skip_rendering(self):

return self.request("sdapi/v1/skip", method="post")

def get_samplers(self):
"""
Request current samplers from the webui API.
:return: The samplers JSON.
"""

response = self.request('sdapi/v1/samplers')
if response.status_code == 200:
r = response.json()
return r
else:
return []

def get_upscalers(self):
"""
Request current upscalers from the webui API.
:return: The upscalers JSON.
"""

response = self.request('sdapi/v1/upscalers')
if response.status_code == 200:
r = response.json()
return r
else:
return []


# Type hinting imports:
# from .state import State
63 changes: 58 additions & 5 deletions scripts/common/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,62 @@ def __init__(self, img2img=""):
self.update_config()
self.update_settings()
self.update_webui_config()

def update_samplers(self):
"""
Update samplers list from available samplers.
"""
if self.configuration["config"].get("override_samplers", 'false') == 'true' and self.configuration["config"].get("samplers", []):
self.samplers["list"] = self.configuration["config"].get("samplers", ["DDIM"])
self.samplers["sampler"] = self.samplers["list"][0]
return

def get_sampler_priority(smp):
priorities = {
"Euler": -2,
"PLMS": -2,
"Heun": -1,
"LMS": -1,
"DPM": 1,
"DPM2": 3,
"Karras": 3,
"UniPC": 3,
"++": 4,
"DDIM": 4,
}
priority = 0
for weight in priorities:
if weight in smp:
priority += priorities[weight]
# ancestral samplers are more random
if " a " in smp or smp.endswith(" a"):
priority -= 2
return priority

samplers_data = self.api.get_samplers()
samplers_names = list(map(lambda x: x["name"], samplers_data))
if len(samplers_names) == 0:
samplers_names = self.configuration["config"].get("samplers", ["DDIM"])
samplers_names.sort(key=lambda x: get_sampler_priority(x), reverse=True)
self.samplers["list"] = samplers_names
self.samplers["sampler"] = self.samplers["list"][0]

def update_upscalers(self):
"""
Update upscalers list from available upscalers.
"""
if self.configuration["config"].get("override_hr_upscalers", 'false') == 'true' and self.configuration["config"].get("hr_upscalers", []):
self.render["hr_upscalers"] = self.configuration["config"].get("hr_upscalers", ['Latent (bicubic)'])
self.render["hr_upscaler"] = self.render["hr_upscalers"][0]
return

upscalers_data = self.api.get_upscalers()
upscalers_names = list(map(lambda x: x["name"], upscalers_data))
if len(upscalers_names) == 0:
upscalers_names = self.configuration["config"].get("hr_upscalers", ["Latent (bicubic)"])
upscalers_names.sort()
self.render["hr_upscalers"] = upscalers_names
self.render["hr_upscaler"] = self.render["hr_upscalers"][0]

def update_config(self, preload=False):
"""
Expand All @@ -107,13 +163,10 @@ def update_config(self, preload=False):
hr_scale = hr_scales[0]

self.render["hr_scale_prev"] = hr_scales[1]
self.render["hr_upscalers"] = self.configuration["config"].get("hr_upscalers", ['Latent (bicubic)'])
self.render["hr_upscaler"] = self.render["hr_upscalers"][0]
self.render["denoising_strengths"] = self.configuration["config"].get("denoising_strengths", [0.6])
self.render["denoising_strength"] = self.render["denoising_strengths"][0]

self.samplers["list"] = self.configuration["config"].get("samplers", ["DDIM"])
self.samplers["sampler"] = self.samplers["list"][0]
self.update_samplers()
self.update_upscalers()

self.detectors["list"] = self.configuration["config"].get('detectors', ('lineart',))
self.detectors["detector"] = self.detectors["list"][0]
Expand Down

0 comments on commit c1400a9

Please sign in to comment.