Skip to content

Commit ef21ace

Browse files
authored
Merge pull request #1 from 1456319/patch-1
Create imagen.sh
2 parents 101b2cb + db9630e commit ef21ace

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed

imagen.sh

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#This version offers to reuse last prompt
2+
#this version can evaluate the contents of /home/deck/stable-diffusion.cpp/Checkpoints and prompt the user to select a checkpoint from that dir. logic to differentiate SD vs SDXL to be added
3+
#This version saves output files as . To change back to Output_#.png, set line 11 to base_filename="output" and 15 to while [[ -e "$output_dir/${base_filename}_${i}.png" ]]; do
4+
#This version will query the user for which LORA, if any, they want to use and the strength of the LORA. The script assumes LORAS are at /home/deck/stable-diffusion.cpp/LORAs/
5+
#This version has the capability to specify a neg prompt and will ask if you want to reuse the previous one
6+
#This version will verify SD vs SDXL prompt/parameter compliance with the selected model. If noncompliant, will offer to attempt to automatically comply the prompt/parameters with the SDXL model
7+
#!/bin/bash
8+
9+
#!/bin/bash
10+
11+
# Directory to save images
12+
output_dir="output_images"
13+
mkdir -p "$output_dir"
14+
15+
# Base filename
16+
base_filename="output"
17+
18+
# Find the next available filename
19+
i=1
20+
while [[ -e "$output_dir/${base_filename}_${i}.png" ]]; do
21+
((i++))
22+
done
23+
24+
# Function to get a random seed
25+
get_random_seed() {
26+
echo $((RANDOM * RANDOM))
27+
}
28+
29+
# Function to list files in a directory and prompt the user to choose one
30+
list_checkpoints() {
31+
local dir=$1
32+
local files=("$dir"/*)
33+
echo "Checkpoints found in $dir:"
34+
for idx in "${!files[@]}"; do
35+
echo "$((idx + 1))) ${files[$idx]##*/}"
36+
done
37+
read -p "Please input the number corresponding to the Checkpoint to be used in this generation: " choice
38+
if [[ $choice -gt 0 && $choice -le ${#files[@]} ]]; then
39+
selected_checkpoint="${files[$((choice - 1))]##*/}"
40+
echo "You've chosen: $choice) $selected_checkpoint"
41+
else
42+
echo "Invalid choice. Exiting."
43+
exit 1
44+
fi
45+
}
46+
47+
# Function to list LoRAs in a directory and prompt the user to choose one
48+
list_loras() {
49+
local dir=$1
50+
local files=("$dir"/*)
51+
echo "LoRAs found in $dir:"
52+
for idx in "${!files[@]}"; do
53+
echo "$((idx + 1))) ${files[$idx]##*/}"
54+
done
55+
read -p "Please input the number corresponding to the LORA to be used in this generation: " choice
56+
if [[ $choice -gt 0 && $choice -le ${#files[@]} ]]; then
57+
selected_lora="${files[$((choice - 1))]##*/}"
58+
echo "You've chosen: $choice) $selected_lora"
59+
60+
# Prompt the user to indicate the strength of the LORA
61+
read -p "Enter the strength of the LORA (e.g., 1): " lora_strength
62+
63+
# Amend the user's prompt to include the selected LORA and its strength
64+
prompt="$prompt <lora:${selected_lora%.safetensors}:$lora_strength>"
65+
else
66+
echo "Invalid choice. Exiting."
67+
exit 1
68+
fi
69+
}
70+
71+
# List checkpoints and prompt the user to choose one
72+
checkpoint_dir="/home/deck/stable-diffusion.cpp/Checkpoints"
73+
list_checkpoints "$checkpoint_dir"
74+
75+
# Change directory to Checkpoints to look for the model
76+
cd /home/deck/stable-diffusion.cpp/Checkpoints
77+
78+
# Check if the selected checkpoint exists in the Checkpoints directory
79+
if [[ ! -f "$selected_checkpoint" ]]; then
80+
echo "Model $selected_checkpoint not found in /home/deck/stable-diffusion.cpp/Checkpoints"
81+
exit 1
82+
fi
83+
84+
# Return to the base working directory
85+
cd /home/deck/stable-diffusion.cpp/
86+
87+
# Prompt the user for a new prompt or reuse the previous one
88+
if [[ -f "last_prompt.txt" ]]; then
89+
echo "Do you want to reuse the previous prompt? (y/n)"
90+
read reuse_prompt
91+
if [[ "$reuse_prompt" == "y" ]]; then
92+
prompt=$(cat last_prompt.txt)
93+
else
94+
echo "Enter your new prompt:"
95+
read prompt
96+
echo "$prompt" > last_prompt.txt
97+
fi
98+
else
99+
echo "Enter your prompt:"
100+
read prompt
101+
echo "$prompt" > last_prompt.txt
102+
fi
103+
104+
# Prompt the user for a negative prompt or reuse the previous one
105+
if [[ -f "last_negative_prompt.txt" ]]; then
106+
echo "Do you want to reuse the previous negative prompt? (y/n)"
107+
read reuse_negative_prompt
108+
if [[ "$reuse_negative_prompt" == "y" ]]; then
109+
negative_prompt=$(cat last_negative_prompt.txt)
110+
else
111+
read -p "Enter a negative prompt (things you don't want to see in the output): " negative_prompt
112+
echo "$negative_prompt" > last_negative_prompt.txt
113+
fi
114+
else
115+
read -p "Enter a negative prompt (things you don't want to see in the output): " negative_prompt
116+
echo "$negative_prompt" > last_negative_prompt.txt
117+
fi
118+
119+
# Prompt the user if they want to use a LORA for generating this image
120+
echo "Would you like to use a LORA for generating this image? (y/n)"
121+
read use_lora
122+
123+
if [[ "$use_lora" == "y" ]]; then
124+
# List LoRAs and prompt the user to choose one
125+
lora_dir="/home/deck/stable-diffusion.cpp/LORAs"
126+
list_loras "$lora_dir"
127+
fi
128+
129+
# Display height/width presets and prompt the user to choose one
130+
echo "Choose a resolution preset:"
131+
echo "1) Low (512x512)"
132+
echo "2) Medium (1024x1024)"
133+
echo "3) High (1536x1536)"
134+
echo "4) Very High (2048x2048)"
135+
echo "5) Ultra-HD (4096x4096)"
136+
read -p "Enter the number corresponding to your choice: " resolution_choice
137+
138+
case $resolution_choice in
139+
1)
140+
height=512
141+
width=512
142+
;;
143+
2)
144+
height=1024
145+
width=1024
146+
;;
147+
3)
148+
height=1536
149+
width=1536
150+
;;
151+
4)
152+
height=2048
153+
width=2048
154+
;;
155+
5)
156+
height=4096
157+
width=4096
158+
;;
159+
*)
160+
echo "Invalid choice, defaulting to Low (512x512)"
161+
height=512
162+
width=512
163+
;;
164+
esac
165+
166+
# Prompt the user to specify the number of steps (default to 8)
167+
read -p "Enter the number of steps (default is 8): " steps
168+
steps=${steps:-8}
169+
170+
# Prompt the user to enter the number of pictures to generate
171+
read -p "Enter the number of pictures to generate: " num_pictures
172+
173+
# Check if model is SDXL and warn user if true, ask if they want automatic adjustment for SDXL compliance
174+
if [[ "$selected_checkpoint" == *"SDXL"* ]]; then
175+
echo "WARNING: The model being used is SDXL."
176+
read -p "Would you like your prompt automatically adjusted for SDXL compliance? (y/n): " adjust_sdxl
177+
if [[ "$adjust_sdxl" == "y" ]]; then
178+
sdxl_compliance="--vae-on-cpu"
179+
else
180+
sdxl_compliance=""
181+
fi
182+
else
183+
sdxl_compliance=""
184+
fi
185+
186+
# Generate the specified number of pictures
187+
for ((j=1; j<=num_pictures; j++)); do
188+
# Get a random seed
189+
seed=$(get_random_seed)
190+
191+
# Generate a safe filename based on the prompt and checkpoint name, incrementing the number to avoid duplicates
192+
safe_prompt=$(echo "$prompt" | tr ' ' '_')
193+
safe_checkpoint=$(echo "$selected_checkpoint" | tr ' ' '_')
194+
output_filename="${safe_prompt}-${safe_checkpoint}_${i}.png"
195+
196+
# Run the stable-diffusion command with the new filename and random seed, using the selected checkpoint from Checkpoints directory
197+
./sd -m "/home/deck/stable-diffusion.cpp/Checkpoints/$selected_checkpoint" -H "$height" -W "$width" --vae-on-cpu --sampling-method lcm --steps "$steps" --cfg-scale 1 --seed "$seed" --prompt "$prompt" --negative-prompt "$negative_prompt" $sdxl_compliance -o "$output_dir/$output_filename"
198+
199+
# Check for memory allocation error
200+
if grep -q "ErrorOutOfDeviceMemory" <<< "$(tail -n 10 output.log)"; then
201+
echo "Memory allocation error encountered. Stopping the script."
202+
exit 1
203+
fi
204+
205+
# Increment the filename counter
206+
((i++))
207+
done
208+
209+
210+
211+
# create dir with
212+
#cd /home/deck/stable-diffusion.cpp/
213+
#chmod +x imgen.sh
214+
215+
#run this script to generate multiple images in 1 command
216+
#./imgen

0 commit comments

Comments
 (0)