home · contact · privacy
Minor code style fixes.
authorChristian Heller <c.heller@plomlompom.de>
Thu, 22 Aug 2024 13:16:51 +0000 (15:16 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 22 Aug 2024 13:16:51 +0000 (15:16 +0200)
browser.py
stable.py
stable/core.py
stable/gen_params.py

index cf597642486b92c60448b776bd4874d006e5e2b3..c2ab5b8349406c545daf9e1bf525e41449270ff9 100755 (executable)
@@ -2,11 +2,11 @@
 from os import scandir
 from os.path import splitext
 from exiftool import ExifToolHelper  # type: ignore
-from stable.gen_params import GenParams
-import gi
+import gi  # type: ignore
 gi.require_version('Gtk', '4.0')
 # pylint: disable=wrong-import-position
-from gi.repository import Gtk  # noqa: E402
+from gi.repository import Gtk  # type: ignore  # noqa: E402
+from stable.gen_params import GenParams  # noqa: E402
 
 
 class Window(Gtk.ApplicationWindow):
@@ -44,7 +44,7 @@ class Window(Gtk.ApplicationWindow):
         self.current_index = -1
         self.newest(None)
 
-    def _reload(self, set_max = False):
+    def _reload(self, set_max=False):
         self.entries = [e for e in scandir('.')
                         if e.is_file()
                         and splitext(e)[1] in {'.png', '.jpg', '.jpeg'}]
index cf9cbcc50f7dc267526cc5c156dacb3fc6998cbd..71320d2dbb1926c056ab9d65a9add13ee2e13cc4 100755 (executable)
--- a/stable.py
+++ b/stable.py
@@ -7,12 +7,8 @@ from stable.gen_params import GenParams
 
 
 DEFAULT_SCHEDULER = 'EulerAncestralDiscreteScheduler'
-
-
-def save_path(count: int) -> str:
-    n_iterations = len(args.gen_paramses) * args.quantity
-    filename_count = f'_{count:08}' if n_iterations > 1 else ''
-    return path_join(dir_path, f'{filename_sans_ext}{filename_count}.{ext}')
+SEED_MIN = -(2**31-1)
+SEED_MAX = 2**31
 
 
 def parse_args():
@@ -65,7 +61,7 @@ def parse_args():
         prefix += 'unless calling with -H/--help or --list-schedulers, '
     prefix += 'requiring '
 
-     # Re-seed defaults from stdin, if -D.
+    # Re-seed defaults from stdin, if -D.
     temp_gen_paramses = [None]
     if parsed_args.defaults_from_stdin:
         temp_gen_paramses = []
@@ -73,9 +69,9 @@ def parse_args():
             temp_gen_paramses += [GenParams.from_str(line)]
     parsed_args.models = []
     parsed_args.gen_paramses = []
-    for gen_params in temp_gen_paramses:
-        if gen_params:
-            parser.set_defaults(**gen_params.as_dict)
+    for gen_params_ in temp_gen_paramses:
+        if gen_params_:
+            parser.set_defaults(**gen_params_.as_dict)
         new_parsed_args = parser.parse_args()
 
         # Validate input.
@@ -100,45 +96,58 @@ def parse_args():
     return parsed_args
 
 
-# guard against overwriting
-args = parse_args()
-if not args.list_schedulers:
-    dir_path = dirname(args.output)
-    filename = basename(args.output)
-    filename_sans_ext, ext = splitext(filename)
-    ext = ext[1:] if ext else 'png'
-    for n in range(args.quantity * len(args.gen_paramses)):
-        path = save_path(n)
-        if exists(path):
-            raise Exception(f'Would overwrite file: {path}')
-
-# pylint: disable=wrong-import-position
-from stable.core import ImageMaker  # noqa: E402
-old_model_path = ''
-maker = None
-for i, model_name in enumerate(args.models):
-    new_model_path = path_join(args.models_dir, model_name)
-    if new_model_path != old_model_path:
-        maker = ImageMaker(new_model_path)
-    old_model_path = new_model_path
-
-    # only list available schedulers, if -L
-    if args.list_schedulers:
-        print(f'AVAILABLE SCHEDULERS FOR MODEL {new_model_path}:\n')
-        for name in maker.compatible_schedulers:
-            print(name)
-        continue
-
-    # otherwise generate pictures
-    gen_params = args.gen_paramses[i]
-    start_seed = gen_params.seed
-    start_seed = start_seed if start_seed != 0 else randint(-(2**31-1), 2**31)
-    seed_corrector = 0
-    for n in range(args.quantity):
-        if 0 == start_seed + n + seed_corrector:
-            seed_corrector += 1
-        gen_params.seed = start_seed + n + seed_corrector
-        path = save_path(i*args.quantity + n)
-        maker.set_gen_params(gen_params)
-        print(f'GENERATING: {path}; {gen_params.to_str}')
-        maker.gen_image_to(path)
+def run():
+
+    def save_path(count: int) -> str:
+        n_iterations = len(args.gen_paramses)
+        filename_count = f'_{count:08}' if n_iterations > 1 else ''
+        # pylint: disable=possibly-used-before-assignment
+        return path_join(dir_path,
+                         f'{filename_sans_ext}{filename_count}.{ext}')
+
+    # guard against overwriting
+    args = parse_args()
+    if not args.list_schedulers:
+        dir_path = dirname(args.output)
+        filename = basename(args.output)
+        filename_sans_ext, ext = splitext(filename)
+        ext = ext[1:] if ext else 'png'
+        for n in range(args.quantity * len(args.gen_paramses)):
+            path = save_path(n)
+            if exists(path):
+                raise Exception(f'Would overwrite file: {path}')
+
+    # pylint: disable=wrong-import-position, import-outside-toplevel
+    from stable.core import ImageMaker  # noqa: E402
+    old_model_path = ''
+    maker = None
+    for i, model_name in enumerate(args.models):
+        new_model_path = path_join(args.models_dir, model_name)
+        if new_model_path != old_model_path:
+            maker = ImageMaker(new_model_path)
+        old_model_path = new_model_path
+
+        # only list available schedulers, if -L
+        if args.list_schedulers:
+            print(f'AVAILABLE SCHEDULERS FOR MODEL {new_model_path}:\n')
+            for name in maker.compatible_schedulers:
+                print(name)
+            continue
+
+        # otherwise generate pictures
+        gen_params = args.gen_paramses[i]
+        start_seed = gen_params.seed
+        start_seed = start_seed if start_seed != 0 else randint(SEED_MIN,
+                                                                SEED_MAX)
+        seed_corrector = 0
+        for n in range(args.quantity):
+            if 0 == start_seed + n + seed_corrector:
+                seed_corrector += 1
+            gen_params.seed = start_seed + n + seed_corrector
+            path = save_path(i*args.quantity + n)
+            maker.set_gen_params(gen_params)
+            print(f'GENERATING: {path}; {gen_params.to_str}')
+            maker.gen_image_to(path)
+
+
+run()
index f2792fc5d6cc755632c99fb3160510c05b19c950..5c506c7e31a503c67ea5b21edd0ab3ad91523512 100644 (file)
@@ -1,11 +1,9 @@
 from logging import (Formatter as LogFormatter, captureWarnings,
                      Filter as LogFilter)
-from os.path import basename
 from diffusers import StableDiffusionPipeline
 from diffusers.utils import logging
 from torch import Generator, float16
 from exiftool import ExifToolHelper  # type: ignore
-from stable.gen_params import GenParams
 
 SAFETY_CHECKER_WARNING_PATTERN = 'You have disabled the safety checker'
 
@@ -43,7 +41,7 @@ class ImageMaker:
         scheduler_selection = [s for s in self.pipe.scheduler.compatibles
                                if s.__name__ == gen_params.scheduler_name]
         if not scheduler_selection:
-            raise Exception(f'unknown scheduler: {scheduler_name}')
+            raise Exception(f'unknown scheduler: {gen_params.scheduler_name}')
         self.pipe.scheduler = scheduler_selection[0].from_config(
                 self.pipe.scheduler.config)
         self.gen_params = gen_params
index e3ae86f9880323c9a9a1f1969e2be0e66fd9c83e..fc1eba1ff488191ca59ee09056caee88c8a0796f 100644 (file)
@@ -1,6 +1,7 @@
 class GenParams:
 
-    def __init__(self, seed, guidance, n_steps, height, width, scheduler, model, prompt):
+    def __init__(self, seed, guidance, n_steps, height, width, scheduler,
+                 model, prompt):
         if '; ' in model:
             raise Exception('illegal model filename (must not contain "; ")')
         self.seed = seed
@@ -9,7 +10,7 @@ class GenParams:
         self.height, self.width = height, width
         self.scheduler_name = scheduler
         self.model_name = model
-        self.prompt = prompt 
+        self.prompt = prompt
 
     @property
     def to_str(self):
@@ -27,7 +28,7 @@ class GenParams:
         d = {}
         first_split = string.split('; PROMPT: ', maxsplit=1)
         if 2 == len(first_split):
-            d['prompt'] = first_split[1] 
+            d['prompt'] = first_split[1]
         for section in first_split[0].split('; '):
             key, val = section.split(': ', maxsplit=1)
             key = key.lower()