Update fix-diffusers-root-directory.sh

This commit is contained in:
Cyberes 2022-11-07 11:22:58 -07:00 committed by GitHub
parent 44dd0f05b5
commit a2e4b9511a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 26 deletions

View File

@ -5,17 +5,10 @@
# 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
TARGET_PATH="$1"
# Prompt the user to install 7zip if they don't have it insalled
if [ $(dpkg-query -W -f='${Status}' p7zip-full 2>/dev/null | grep -c "ok installed") = 0 ]; then
sudo apt update && sudo apt install p7zip-full
fi
TMP=$(mktemp -d)
TARGET_PATH="$@"
test_zip () {
unzip -l "$1"
unzip -l "$1" > /dev/null
return $?
}
@ -24,18 +17,10 @@ do_convert () {
FILENAME=$(basename "$TARGET_FILE")
NAME="${FILENAME%.*}"
EXT="${FILENAME##*.}"
7z x "$TARGET_FILE" -o"$TMP"
# There should only be one file in this tmp directory.
# We don't know what its name is so we will do one loop
# which will be only our target directory.
for file in "$TMP"/*; do
mv "$file" "$TMP/archive"
NEW_NAME="${NAME}_fixed.zip"
7z a -tzip -mx=9 "${NAME}_fixed.$EXT" "$TMP/archive"
break
done
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')"
}
if [ -d "$TARGET_PATH" ]; then
@ -45,9 +30,9 @@ if [ -d "$TARGET_PATH" ]; then
fi
done
else
if test_zip "$TARGET_PATH"; then
do_convert "$TARGET_PATH"
fi
for file in $TARGET_PATH; do
if test_zip "$file"; then
do_convert "$file"
fi
done
fi
rm -rf "$TMP"