Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/add-model-to-log-file #14663

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions modules/ui_common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import csv
import dataclasses
import json
import html
Expand Down Expand Up @@ -36,8 +37,33 @@ def plaintext_to_html(text, classname=None):
return f"<p class='{classname}'>{content}</p>" if classname else f"<p>{content}</p>"


def update_logfile(logfile_path, fields):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend using typehints whenever adding new functions. The repo doesn't have them now but it would definitely be nice if new functions were implemented with them for functions/classes.

Also a simple docstring just to satisfy linters wouldn't hurt. This is just an example description of the function so if you can think of a better one then feel free to modify it.

-def update_logfile(logfile_path, fields):
+def update_logfile(logfile_path: str, fields: list[str]) -> None:
+    """Updates existing log file rows to match current log file column count."""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, I mostly wanted to retain the style of the surrounding, but if @AUTOMATIC1111 is in agreement, I'd be happy to add the typehints

"""Update a logfile from old format to new format to maintain CSV integrity."""
with open(logfile_path, "r", encoding="utf8", newline="") as file:
reader = csv.reader(file)
rows = list(reader)

# blank file: leave it as is
if not rows:
return

# file is already synced, do nothing
if len(rows[0]) == len(fields):
return

rows[0] = fields

# append new fields to each row as empty values
for row in rows[1:]:
while len(row) < len(fields):
row.append("")
Comment on lines +57 to +59
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit nit picky on my part but this approach could be a bit more pythonic.

-    for row in rows[1:]:
-        while len(row) < len(fields):
-            row.append("")
+    rows = [row + ([""] * (len(fields) - len(row))) for row in rows[1:]]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I'll have to push back on this, while list comprehensions are usually my preference, this becomes nigh unreadable.

Copy link
Contributor

@Sj-Si Sj-Si Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I'll have to push back on this, while list comprehensions are usually my preference, this becomes nigh unreadable.

Yeah fair enough. I got a bit carried away code golfing it seems. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha it happens, I appreciate your input though 👍


with open(logfile_path, "w", encoding="utf8", newline="") as file:
writer = csv.writer(file)
writer.writerows(rows)


def save_files(js_data, images, do_make_zip, index):
import csv
filenames = []
fullfns = []
parsed_infotexts = []
Expand All @@ -63,11 +89,31 @@ def __init__(self, d=None):

os.makedirs(shared.opts.outdir_save, exist_ok=True)

with open(os.path.join(shared.opts.outdir_save, "log.csv"), "a", encoding="utf8", newline='') as file:
fields = [
"prompt",
"seed",
"width",
"height",
"sampler",
"cfgs",
"steps",
"filename",
"negative_prompt",
"sd_model_name",
"sd_model_hash",
]
logfile_path = os.path.join(shared.opts.outdir_save, "log.csv")

# NOTE: ensure csv integrity when fields are added by
# updating headers and padding with delimeters where needed
if os.path.exists(logfile_path):
update_logfile(logfile_path, fields)

with open(logfile_path, "a", encoding="utf8", newline='') as file:
at_start = file.tell() == 0
writer = csv.writer(file)
if at_start:
writer.writerow(["prompt", "seed", "width", "height", "sampler", "cfgs", "steps", "filename", "negative_prompt"])
writer.writerow(fields)

for image_index, filedata in enumerate(images, start_index):
image = image_from_url_text(filedata)
Expand All @@ -87,7 +133,7 @@ def __init__(self, d=None):
filenames.append(os.path.basename(txt_fullfn))
fullfns.append(txt_fullfn)

writer.writerow([parsed_infotexts[0]['Prompt'], parsed_infotexts[0]['Seed'], data["width"], data["height"], data["sampler_name"], data["cfg_scale"], data["steps"], filenames[0], parsed_infotexts[0]['Negative prompt']])
writer.writerow([parsed_infotexts[0]['Prompt'], parsed_infotexts[0]['Seed'], data["width"], data["height"], data["sampler_name"], data["cfg_scale"], data["steps"], filenames[0], parsed_infotexts[0]['Negative prompt'], data["sd_model_name"], data["sd_model_hash"]])

# Make Zip
if do_make_zip:
Expand Down