Skip to content

Commit a55770f

Browse files
committed
add utility methods. (util_*)
1 parent 56aedc7 commit a55770f

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

Diff for: README.md

+15
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,18 @@ api.get_artist_categories()
154154
api.get_artists()
155155
156156
```
157+
158+
### Utility methods
159+
```
160+
# save current model name
161+
old_model = api.util_get_current_model()
162+
163+
# get list of available models
164+
models = api.util_get_model_names()
165+
166+
# set model (use exact name)
167+
api.util_set_model(models[0])
168+
169+
# set model (find closest match)
170+
api.util_set_model('robodiffusion')
171+
```

Diff for: setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
setup(
1313
name="webuiapi",
14-
version="0.0.3",
14+
version="0.1.0",
1515
description="Python API client for AUTOMATIC1111/stable-diffusion-webui",
1616
url="https://github.com/mix1009/sdwebuiapi",
1717
author="ChunKoo Park",

Diff for: webuiapi/webuiapi.py

+30
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,36 @@ def custom_post(self, endpoint, payload={}, baseurl=True):
399399
response = self.session.post(url=url, json=payload)
400400
return self._to_api_result(response)
401401

402+
def util_get_model_names(self):
403+
return sorted([x['title'] for x in self.get_sd_models()])
404+
def util_set_model(self, name, find_closest=True):
405+
name = name.lower()
406+
models = self.util_get_model_names()
407+
found_model = None
408+
if name in models:
409+
found_model = name
410+
elif find_closest:
411+
import difflib
412+
def str_simularity(a, b):
413+
return difflib.SequenceMatcher(None, a, b).ratio()
414+
max_sim = 0.0
415+
max_model = models[0]
416+
for model in models:
417+
sim = str_simularity(name, model)
418+
if sim >= max_sim:
419+
max_sim = sim
420+
max_model = model
421+
found_model = max_model
422+
if found_model:
423+
print(f'loading {found_model}')
424+
options = {}
425+
options['sd_model_checkpoint'] = found_model
426+
self.set_options(options)
427+
print(f'model changed to {found_model}')
428+
else:
429+
print('model not found')
430+
def util_get_current_model(self):
431+
return self.get_options()['sd_model_checkpoint']
402432

403433

404434
class Upscaler(str, Enum):

0 commit comments

Comments
 (0)