chore: code cleanup

This commit is contained in:
Manuel Schmid 2024-02-02 01:14:40 +01:00
parent bc9b625221
commit ea6839be83
No known key found for this signature in database
GPG Key ID: 32C4F7569B40B84B
1 changed files with 1 additions and 121 deletions

View File

@ -1,12 +1,10 @@
import json
import re
from pathlib import Path
from abc import ABC, abstractmethod
from pathlib import Path
from PIL import Image
import modules.config
import fooocus_version
# import advanced_parameters
from modules.flags import MetadataScheme, Performance, Steps, lora_count_with_lcm
from modules.util import quote, unquote, extract_styles_from_prompt, is_json, calculate_sha256
@ -30,7 +28,6 @@ class MetadataParser(ABC):
def parse_json(self, metadata: dict) -> dict:
raise NotImplementedError
# TODO add data to parse
@abstractmethod
def parse_string(self, metadata: dict) -> str:
raise NotImplementedError
@ -219,84 +216,6 @@ class FooocusMetadataParser(MetadataParser):
metadata[li] = (label, key, value, show_in_log, copy_in_log)
return json.dumps({k: v for _, k, v, _, _ in metadata})
# metadata = {
# # prompt with wildcards
# 'prompt': raw_prompt, 'negative_prompt': raw_negative_prompt,
# # prompt with resolved wildcards
# 'real_prompt': task['log_positive_prompt'], 'real_negative_prompt': task['log_negative_prompt'],
# # prompt with resolved wildcards, styles and prompt expansion
# 'complete_prompt_positive': task['positive'], 'complete_prompt_negative': task['negative'],
# 'styles': str(raw_style_selections),
# 'seed': task['task_seed'], 'width': width, 'height': height,
# 'sampler': sampler_name, 'scheduler': scheduler_name, 'performance': performance_selection,
# 'steps': steps, 'refiner_switch': refiner_switch, 'sharpness': sharpness, 'cfg': cfg_scale,
# 'base_model': base_model_name, 'base_model_hash': base_model_hash, 'refiner_model': refiner_model_name,
# 'denoising_strength': denoising_strength,
# 'freeu': advanced_parameters.freeu_enabled,
# 'img2img': input_image_checkbox,
# 'prompt_expansion': task['expansion']
# }
#
# if advanced_parameters.freeu_enabled:
# metadata |= {
# 'freeu_b1': advanced_parameters.freeu_b1, 'freeu_b2': advanced_parameters.freeu_b2,
# 'freeu_s1': advanced_parameters.freeu_s1, 'freeu_s2': advanced_parameters.freeu_s2
# }
#
# if 'vary' in goals:
# metadata |= {
# 'uov_method': uov_method
# }
#
# if 'upscale' in goals:
# metadata |= {
# 'uov_method': uov_method, 'scale': f
# }
#
# if 'inpaint' in goals:
# if len(outpaint_selections) > 0:
# metadata |= {
# 'outpaint_selections': outpaint_selections
# }
#
# metadata |= {
# 'inpaint_additional_prompt': inpaint_additional_prompt,
# 'inpaint_mask_upload': advanced_parameters.inpaint_mask_upload_checkbox,
# 'invert_mask': advanced_parameters.invert_mask_checkbox,
# 'inpaint_disable_initial_latent': advanced_parameters.inpaint_disable_initial_latent,
# 'inpaint_engine': advanced_parameters.inpaint_engine,
# 'inpaint_strength': advanced_parameters.inpaint_strength,
# 'inpaint_respective_field': advanced_parameters.inpaint_respective_field,
# }
#
# if 'cn' in goals:
# metadata |= {
# 'canny_low_threshold': advanced_parameters.canny_low_threshold,
# 'canny_high_threshold': advanced_parameters.canny_high_threshold,
# }
#
# ip_list = {x: [] for x in flags.ip_list}
# cn_task_index = 1
# for cn_type in ip_list:
# for cn_task in cn_tasks[cn_type]:
# cn_img, cn_stop, cn_weight = cn_task
# metadata |= {
# f'image_prompt_{cn_task_index}': {
# 'cn_type': cn_type, 'cn_stop': cn_stop, 'cn_weight': cn_weight,
# }
# }
# cn_task_index += 1
#
# metadata |= {
# 'software': f'Fooocus v{fooocus_version.version}',
# }
# TODO add metadata_created_by
# if modules.config.metadata_created_by != '':
# metadata |= {
# 'created_by': modules.config.metadata_created_by
# }
# # return json.dumps(metadata, ensure_ascii=True) TODO check if possible
# return json.dumps(metadata, ensure_ascii=False)
@staticmethod
def replace_value_with_filename(key, value, filenames):
@ -319,12 +238,6 @@ def get_metadata_parser(metadata_scheme: MetadataScheme) -> MetadataParser:
case _:
raise NotImplementedError
# IGNORED_INFO_KEYS = {
# 'jfif', 'jfif_version', 'jfif_unit', 'jfif_density', 'dpi', 'exif',
# 'loop', 'background', 'timestamp', 'duration', 'progressive', 'progression',
# 'icc_profile', 'chromaticity', 'photoshop',
# }
def read_info_from_image(filepath) -> tuple[str | None, dict, MetadataScheme | None]:
with Image.open(filepath) as image:
@ -346,37 +259,4 @@ def read_info_from_image(filepath) -> tuple[str | None, dict, MetadataScheme | N
if metadata_scheme is None and isinstance(parameters, str):
metadata_scheme = modules.metadata.MetadataScheme.A1111
# TODO code cleanup
# if "exif" in items:
# exif_data = items["exif"]
# try:
# exif = piexif.load(exif_data)
# except OSError:
# # memory / exif was not valid so piexif tried to read from a file
# exif = None
# exif_comment = (exif or {}).get("Exif", {}).get(piexif.ExifIFD.UserComment, b'')
# try:
# exif_comment = piexif.helper.UserComment.load(exif_comment)
# except ValueError:
# exif_comment = exif_comment.decode('utf8', errors="ignore")
#
# if exif_comment:
# items['exif comment'] = exif_comment
# parameters = exif_comment
# for field in IGNORED_INFO_KEYS:
# items.pop(field, None)
# if items.get("Software", None) == "NovelAI":
# try:
# json_info = json.loads(items["Comment"])
# sampler = sd_samplers.samplers_map.get(json_info["sampler"], "Euler a")
#
# geninfo = f"""{items["Description"]}
# Negative prompt: {json_info["uc"]}
# Steps: {json_info["steps"]}, Sampler: {sampler}, CFG scale: {json_info["scale"]}, Seed: {json_info["seed"]}, Size: {image.width}x{image.height}, Clip skip: 2, ENSD: 31337"""
# except Exception:
# errors.report("Error parsing NovelAI image generation parameters",
# exc_info=True)
return parameters, items, metadata_scheme