From 1bb21f35102326da28e1360750a879386c015716 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 29 Aug 2023 06:25:16 +0800 Subject: [PATCH 1/3] feat: display file metadata path https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/12289 --- modules/ui_extra_networks_user_metadata.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/modules/ui_extra_networks_user_metadata.py b/modules/ui_extra_networks_user_metadata.py index b11622a1a19..877d0285945 100644 --- a/modules/ui_extra_networks_user_metadata.py +++ b/modules/ui_extra_networks_user_metadata.py @@ -2,10 +2,29 @@ import html import json import os.path +from pathlib import Path import gradio as gr from modules import generation_parameters_copypaste, images, sysinfo, errors +from modules.paths_internal import models_path + + +def windows_to_unix_style(path): + return Path(path).as_posix() + + +def exclude_root_path(root_path, path_to_exclude): + try: + relative_path = os.path.relpath(path_to_exclude, root_path) + # 如果路径已经在 root_path 之外,relpath 会返回绝对路径 + # 所以需要检查路径是否在 root_path 之内 + if not relative_path.startswith('..'): + return windows_to_unix_style(relative_path) + except ValueError: + pass + # 如果路径无法相对化,或者位于 root_path 之外,则返回原始路径 + return windows_to_unix_style(path_to_exclude) class UserMetadataEditor: @@ -98,6 +117,7 @@ def get_metadata_table(self, name): stats = os.stat(filename) params = [ ('Filename: ', os.path.basename(filename)), + ('Path: ', exclude_root_path(models_path, filename)), ('File size: ', sysinfo.pretty_bytes(stats.st_size)), ('Hash: ', shorthash), ('Modified: ', datetime.datetime.fromtimestamp(stats.st_mtime).strftime('%Y-%m-%d %H:%M')), From d83a1ba65be1b0fbdba8f10212193c52dc8f5e90 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 29 Aug 2023 06:33:00 +0800 Subject: [PATCH 2/3] feat: display file metadata ss_output_name https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/12289 --- extensions-builtin/Lora/ui_edit_user_metadata.py | 1 + 1 file changed, 1 insertion(+) diff --git a/extensions-builtin/Lora/ui_edit_user_metadata.py b/extensions-builtin/Lora/ui_edit_user_metadata.py index 390d9dde3fb..c7011909055 100644 --- a/extensions-builtin/Lora/ui_edit_user_metadata.py +++ b/extensions-builtin/Lora/ui_edit_user_metadata.py @@ -70,6 +70,7 @@ def get_metadata_table(self, name): metadata = item.get("metadata") or {} keys = { + 'ss_output_name': "Output name:", 'ss_sd_model_name': "Model:", 'ss_clip_skip': "Clip skip:", 'ss_network_module': "Kohya module:", From f564d8ed2c5c5644101c5670d2cec15b03ccb51b Mon Sep 17 00:00:00 2001 From: bluelovers Date: Tue, 29 Aug 2023 22:11:18 +0800 Subject: [PATCH 3/3] refactor: refactor function --- modules/ui_extra_networks_user_metadata.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/modules/ui_extra_networks_user_metadata.py b/modules/ui_extra_networks_user_metadata.py index 877d0285945..588f84c75e2 100644 --- a/modules/ui_extra_networks_user_metadata.py +++ b/modules/ui_extra_networks_user_metadata.py @@ -10,21 +10,12 @@ from modules.paths_internal import models_path -def windows_to_unix_style(path): - return Path(path).as_posix() - - -def exclude_root_path(root_path, path_to_exclude): - try: - relative_path = os.path.relpath(path_to_exclude, root_path) - # 如果路径已经在 root_path 之外,relpath 会返回绝对路径 - # 所以需要检查路径是否在 root_path 之内 - if not relative_path.startswith('..'): - return windows_to_unix_style(relative_path) - except ValueError: - pass - # 如果路径无法相对化,或者位于 root_path 之外,则返回原始路径 - return windows_to_unix_style(path_to_exclude) +def exclude_root_path(root_path, path): + path_object = Path(path) + if path_object.is_relative_to(root_path): + path_object = path_object.relative_to(root_path) + + return path_object.as_posix() class UserMetadataEditor: