-
Notifications
You must be signed in to change notification settings - Fork 27.4k
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
Changes from all commits
a75dfe1
315e40a
d0b65e1
8459015
f190b85
4aa99f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import csv | ||
import dataclasses | ||
import json | ||
import html | ||
|
@@ -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): | ||
"""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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:]] There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah fair enough. I got a bit carried away code golfing it seems. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = [] | ||
|
@@ -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) | ||
|
@@ -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: | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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