Guest User

Untitled

a guest
Jul 9th, 2023
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | Fixit | 0 0
  1. #!/bin/bash
  2. cd "$(dirname "$0")"
  3. if [ -z "$API_KEY" ]; then
  4. echo api key was not set 1>&2
  5. exit 1
  6. fi
  7.  
  8. echo "retrieving new jsons from civitai.."
  9. mkdir -p my/civitai-by-hash/
  10. ls -1 {embeddings/,models/{Stable-diffusion,LyCORIS,Lora,hypernetworks}}/*.{safetensors,ckpt,pt,bin} |
  11. parallel -n 1 -j 16 '
  12. t="$(echo {} |sed -r "s/\.(safetensors|ckpt|pt|bin)$/\.civitai\.info/")"
  13. [ -e "$t" ] || {
  14. h=$(sha256sum {} |tee /dev/stderr | cut -c -64)
  15. curl https://civitai.com/api/v1/model-versions/by-hash/$h \
  16. -H "User-Agent: model-metadata-updater, Authorization: \"Bearer '"$API_KEY"'.\", Content-Type: application/json" \
  17. -X GET | json_pp > "$t"
  18. }
  19. '
  20.  
  21. echo "move files to the correct folders (if misplaced).."
  22. # correct folders also updates additional-networks links and json file names.
  23. ls -1 {embeddings/,models/{Stable-diffusion,LyCORIS,Lora,hypernetworks}}/*.civitai.info | while read j; do
  24. db=$(jq '.model.type' "$j" | tr -d '"')
  25. case "$db" in
  26. Checkpoint) t=models/Stable-diffusion;;
  27. LORA) t=models/Lora;;
  28. LoCon|LyCORIS) t=models/LyCORIS;;
  29. Hypernetwork) t=models/hypernetworks;;
  30. TextualInversion) t=embeddings;;
  31. AestheticGradient) t=extensions/stable-diffusion-webui-aesthetic-gradients/aesthetic_embeddings/;;
  32. null) continue;;
  33. esac
  34. f="$(ls -1 "${j%.civitai.info}".{safetensors,ckpt,pt,bin} 2>/dev/null)"
  35. [ -z "$f" ] && continue;
  36. echo "$j" | sed -r 's~^((models/)?[^/]+)/(.*)\.civitai\.info$~\1 \3~' | while read d b ; do
  37. l=
  38. [ -d extensions/sd-webui-additional-networks ] && l="$(pwd)/extensions/sd-webui-additional-networks/models/lora/$(basename "$f")"
  39. if [ "$d" != "$t" ]; then
  40. mv "$d/$b".* "$t/"
  41. if [ "$db" != AestheticGradient ]; then
  42. mv "$j" "${j/${d/\//_}/${t/\//_}}"
  43. else
  44. mv "$j" "${j/${d/\//_}/esthetic_embeddings}"
  45. fi
  46. #if [[ "$f" =~ \.safetensors$ ]]; then
  47. if [ "$t" = "models/Lora" ]; then
  48. [ -e "$l" ] && rm "$l"
  49. fi
  50. fi
  51. #if [[ "$f" =~ \.safetensors$ ]]; then
  52. if [ "$t" = "models/Lora" ]; then
  53. [ -n "$l" -a -e "$l" ] || ln -s -T "$(pwd)/$t/$(basename "$f")" "$l"
  54. fi
  55. done
  56. done
  57.  
  58. echo "write .txt files with text .."
  59. ls -1 {embeddings/,models/{Stable-diffusion,LyCORIS,Lora,hypernetworks}}/*.civitai.info | while read j; do
  60. b="${j%.civitai.info}"
  61. [ -s "$b.txt" ] && continue
  62. jq '.trainedWords[]' "$j" | sed -n 's/"//g;$!s/$/, /p' > "$b.txt"
  63. # remove if empty txt
  64. [ -s "$b.txt" ] || rm "$b.txt"
  65. done
  66.  
  67. ls -1 {embeddings/,models/{Stable-diffusion,LyCORIS,Lora,hypernetworks}}/*.civitai.info | while read j; do
  68. b="${j%.civitai.info}"
  69. [ -s "$b.preview.png" ] && continue
  70. echo will download $b;
  71. jq '.images[0] | "\(.width) \(.url)"' "$j" | head -n 1 | tr -d '"' |while read w u; do
  72. u="$(echo "$u" | sed -r "s~(/width=)[0-9]+(/.*)\.jpeg$~\1$w\2.png~")"
  73. curl -H "User-Agent: model-metadata-updater, Authorization: \"Bearer $API_KEY\"" "$u" > "$b.preview.png" && break
  74. # remove if empty png
  75. [ -s "$b.preview.png" ] || rm "$b.preview.png"
  76. done
  77. done
  78.  
Advertisement
Add Comment
Please, Sign In to add comment