stable-diffusion-paperspace/other/fix-diffusers-root-director...

42 lines
1.1 KiB
Bash
Raw Normal View History

2022-11-07 10:28:27 -07:00
#!/bin/bash
# Diffusers creates a pickle file with the file name as the root directory.
# This trips the WebUI's pickle checker which expects the root directory to be
# named "archive". This script renames the root directory to "archive".
if [ -z "$1" ]; then echo -e "Usage:\n./fix-diffusers-root-directory.sh [file or directory of files]" && exit 1; fi
2022-11-07 11:33:50 -07:00
TARGET_PATH="${@%/}"
2022-11-07 10:28:27 -07:00
test_zip () {
2022-11-07 11:33:50 -07:00
if file "$1" | grep "Zip archive" > /dev/null 2>&1; then
return 0
else
return 1
fi
2022-11-07 10:28:27 -07:00
}
do_convert () {
TARGET_FILE="$1"
FILENAME=$(basename "$TARGET_FILE")
NAME="${FILENAME%.*}"
EXT="${FILENAME##*.}"
2022-11-07 11:22:58 -07:00
NEW_NAME="${NAME}_fixed.$EXT"
echo "$TARGET_FILE -> $NEW_NAME"
# Passing the file back through torch seems to fix it
python3 -c "import torch; torch.save(torch.load('$TARGET_FILE', map_location='cpu'), '$NEW_NAME')"
2022-11-07 10:28:27 -07:00
}
if [ -d "$TARGET_PATH" ]; then
for file in "$TARGET_PATH"/*; do
if [ -f "$file" ] && test_zip "$file"; then
do_convert "$file"
fi
done
else
2022-11-07 11:22:58 -07:00
for file in $TARGET_PATH; do
if test_zip "$file"; then
do_convert "$file"
fi
done
2022-11-07 10:28:27 -07:00
fi