-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_demo.sh
278 lines (234 loc) · 9.82 KB
/
setup_demo.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/bash
# ITERATION/005_final/setup_demo.sh
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${GREEN}Welcome to the Image Embedding and Search Setup Script${NC}"
# Step 0: Verify Dependencies
echo -e "\n${YELLOW}Step 0: Verifying Dependencies${NC}"
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check if Python is installed
if ! command_exists python; then
echo -e "${RED}Error: Python is not installed. Please install Python 3.6 or higher.${NC}"
exit 1
fi
# Define required Python packages
REQUIRED_PKG=("torch" "transformers" "einops" "flask" "safetensors")
# Initialize an empty array to hold missing packages
MISSING_PKG=()
# Check for each required Python package
for pkg in "${REQUIRED_PKG[@]}"; do
python -c "import $pkg" &>/dev/null
if [ $? -ne 0 ]; then
MISSING_PKG+=("$pkg")
fi
done
# If there are missing packages, notify the user and exit
if [ ${#MISSING_PKG[@]} -ne 0 ]; then
echo -e "${RED}Error: The following Python packages are missing:${NC}"
for pkg in "${MISSING_PKG[@]}"; do
echo " - $pkg"
done
echo -e "\nPlease install them using pip:"
echo " pip install ${MISSING_PKG[*]}"
echo -e "Or ensure you're in the correct Conda environment with the required packages installed."
exit 1
else
echo -e "${GREEN}All required Python packages are installed.${NC}"
fi
# Optional: Check if Conda is installed and notify the user
if command_exists conda; then
# Check if a Conda environment is active
if [ -z "$CONDA_DEFAULT_ENV" ]; then
echo -e "${YELLOW}Warning: Conda is installed but no environment is active.${NC}"
echo -e "It's recommended to activate the appropriate Conda environment before running this script."
else
echo -e "${GREEN}Conda environment '$CONDA_DEFAULT_ENV' is active.${NC}"
fi
else
echo -e "${YELLOW}Note: Conda is not installed. Ensure that Python packages are installed in your current environment.${NC}"
fi
# Define project base directory
BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ALTCLIP_DIR="$BASE_DIR/altclip"
MODEL_DIR="$ALTCLIP_DIR/classify-model"
echo -e "\n${YELLOW}Step 1: Verifying Model Configuration${NC}"
CONFIG_FILE="$MODEL_DIR/config.json"
# Debug Statements
echo -e "${YELLOW}Debug: BASE_DIR is set to $BASE_DIR${NC}"
echo -e "${YELLOW}Debug: ALTCLIP_DIR is set to $ALTCLIP_DIR${NC}"
echo -e "${YELLOW}Debug: MODEL_DIR is set to $MODEL_DIR${NC}"
echo -e "${YELLOW}Debug: CONFIG_FILE is set to $CONFIG_FILE${NC}"
if [ ! -f "$CONFIG_FILE" ]; then
echo -e "${RED}Error: Configuration file not found at $CONFIG_FILE${NC}"
echo "Please ensure the 'classify-model/config.json' file exists."
exit 1
else
echo -e "${GREEN}Found configuration file at $CONFIG_FILE${NC}"
fi
# Step 2: Generate Embeddings
echo -e "\n${YELLOW}Step 2: Generating Embeddings${NC}"
echo "This may take a while depending on the number of images..."
# Change directory to ALTCLIP_DIR to ensure relative paths work correctly
cd "$ALTCLIP_DIR" || { echo -e "${RED}Failed to change directory to $ALTCLIP_DIR${NC}"; exit 1; }
# Debug Statement before running Python script
echo -e "${YELLOW}Debug: Executing Python script with MODEL_DIR=$MODEL_DIR${NC}"
python "embed_altClip_class_threading.py" --model_dir "$MODEL_DIR"
# Capture the exit status of the Python script
PYTHON_EXIT_STATUS=$?
# Return to the base directory
cd "$BASE_DIR" || { echo -e "${RED}Failed to return to base directory $BASE_DIR${NC}"; exit 1; }
# Check if the embedding generation was successful
if [ $PYTHON_EXIT_STATUS -eq 0 ]; then
echo -e "${GREEN}Embeddings generated successfully!${NC}"
else
echo -e "${YELLOW}Warning: There might have been issues generating embeddings. Check the logs for details.${NC}"
fi
# Step 3: Configure XAMPP htdocs Directory
echo -e "\n${YELLOW}Step 3: Configuring XAMPP htdocs Directory${NC}"
# Function to display typical htdocs locations
display_typical_htdocs() {
echo -e "\n${YELLOW}Typical XAMPP htdocs Locations:${NC}"
echo -e "\n**For Windows Users**"
echo "- Default Location: The `htdocs` folder is typically located at:"
echo "C:\xampp\htdocs"
echo -e "\n**For Mac Users**"
echo "- Default Location: The `htdocs` folder is usually found at:"
echo "/Applications/XAMPP/xamppfiles/htdocs"
}
# Display typical htdocs locations
display_typical_htdocs
# Prompt user for htdocs directory
if [ -z "$1" ]; then
read -p "Enter the path to your XAMPP htdocs directory (you can copy and paste one of the typical locations above): " HTDOCS_DIR
else
HTDOCS_DIR="$1"
fi
# Validate HTDOCS_DIR
if [ ! -d "$HTDOCS_DIR" ]; then
echo -e "${RED}Error: The specified htdocs directory does not exist: $HTDOCS_DIR${NC}"
exit 1
else
echo -e "${GREEN}Using XAMPP htdocs directory at $HTDOCS_DIR${NC}"
fi
# Step 4: Setting Demo Subdirectory
echo -e "\n${YELLOW}Step 4: Setting Demo Subdirectory${NC}"
if [ -z "$2" ]; then
read -p "Enter the name for the demo subdirectory (default: altclip): " DEMO_DIR
DEMO_DIR=${DEMO_DIR:-altclip}
else
DEMO_DIR="$2"
fi
# Function to create directory with appropriate permissions
create_directory() {
local dir_path="$1"
mkdir -p "$dir_path" 2>/dev/null
if [ $? -ne 0 ]; then
echo -e "${YELLOW}Permission denied when creating $dir_path. Attempting to use sudo...${NC}"
if [ "$(uname)" == "Darwin" ] || [ "$(uname)" == "Linux" ]; then
sudo mkdir -p "$dir_path"
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to create or access demo subdirectory at $dir_path${NC}"
exit 1
else
echo -e "${GREEN}Demo subdirectory created at $dir_path with sudo.${NC}"
fi
elif [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]]; then
echo -e "${RED}Please run this script with Administrator privileges on Windows.${NC}"
exit 1
else
echo -e "${RED}Unsupported OS. Please manually create the demo subdirectory at $dir_path.${NC}"
exit 1
fi
else
echo -e "${GREEN}Demo subdirectory created at $dir_path${NC}"
fi
}
# Create the demo subdirectory
TARGET_DIR="$HTDOCS_DIR/$DEMO_DIR"
create_directory "$TARGET_DIR"
# Step 5: Start the API server
echo -e "\n${YELLOW}Step 5: Starting the API Server${NC}"
echo "Starting the API server in the background..."
python "$ALTCLIP_DIR/api_embeddings_altclip.py" &
API_PID=$!
# Give the API server a moment to start
sleep 5
# Check if the API server is running
if ps -p $API_PID > /dev/null; then
echo -e "${GREEN}API server is running with PID $API_PID${NC}"
else
echo -e "${YELLOW}Warning: API server may not have started correctly. Please check for errors.${NC}"
fi
# Step 6: Copy Files to Demo Subdirectory
echo -e "\n${YELLOW}Step 6: Copying Files to Demo Subdirectory${NC}"
echo "Copying necessary files to $TARGET_DIR..."
# Function to copy files with permission handling
copy_file() {
local source_file="$1"
local destination_dir="$2"
cp "$source_file" "$destination_dir" 2>/dev/null
if [ $? -ne 0 ]; then
echo -e "${YELLOW}Permission denied when copying $source_file to $destination_dir. Attempting to use sudo...${NC}"
if [ "$(uname)" == "Darwin" ] || [ "$(uname)" == "Linux" ]; then
sudo cp "$source_file" "$destination_dir"
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to copy $source_file to $destination_dir${NC}"
exit 1
else
echo -e "${GREEN}Copied $source_file to $destination_dir with sudo.${NC}"
fi
elif [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]]; then
echo -e "${RED}Please copy $source_file to $destination_dir with Administrator privileges on Windows.${NC}"
exit 1
else
echo -e "${RED}Unsupported OS. Please manually copy $source_file to $destination_dir.${NC}"
exit 1
fi
else
echo -e "${GREEN}Copied $source_file to $destination_dir${NC}"
fi
}
# Function to copy directories with permission handling
copy_directory() {
local source_dir="$1"
local destination_dir="$2"
cp -r "$source_dir" "$destination_dir" 2>/dev/null
if [ $? -ne 0 ]; then
echo -e "${YELLOW}Permission denied when copying $source_dir to $destination_dir. Attempting to use sudo...${NC}"
if [ "$(uname)" == "Darwin" ] || [ "$(uname)" == "Linux" ]; then
sudo cp -r "$source_dir" "$destination_dir"
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to copy $source_dir to $destination_dir${NC}"
exit 1
else
echo -e "${GREEN}Copied $source_dir to $destination_dir with sudo.${NC}"
fi
elif [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]]; then
echo -e "${RED}Please copy $source_dir to $destination_dir with Administrator privileges on Windows.${NC}"
exit 1
else
echo -e "${RED}Unsupported OS. Please manually copy $source_dir to $destination_dir.${NC}"
exit 1
fi
else
echo -e "${GREEN}Copied $source_dir to $destination_dir${NC}"
fi
}
# Copy necessary files
copy_file "$ALTCLIP_DIR/index_tags.html" "$TARGET_DIR/"
copy_file "$ALTCLIP_DIR/search.php" "$TARGET_DIR/"
copy_file "$ALTCLIP_DIR/get_tags.php" "$TARGET_DIR/"
copy_directory "$BASE_DIR/data" "$TARGET_DIR/data"
echo -e "${GREEN}Files copied successfully to $TARGET_DIR${NC}"
# Step 7: Final Instructions
echo -e "\n${GREEN}Setup Complete!${NC}"
echo "To access the demo, open your web browser and navigate to:"
echo -e "${YELLOW}http://localhost/$DEMO_DIR/index_tags.html${NC}"
echo -e "\n${GREEN}Remember to stop the API server (PID: $API_PID) when you're done.${NC}"
echo "You can stop it by running: kill $API_PID"