-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
Β·198 lines (169 loc) Β· 6.36 KB
/
run.sh
File metadata and controls
executable file
Β·198 lines (169 loc) Β· 6.36 KB
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
#!/bin/bash
set -e
ENV_NAME="transformerlab"
TLAB_DIR="$HOME/.transformerlab"
TLAB_CODE_DIR="${TLAB_DIR}/src"
MINIFORGE_ROOT=${TLAB_DIR}/miniforge3
CONDA_BIN=${MINIFORGE_ROOT}/bin/conda
ENV_DIR=${TLAB_DIR}/envs/${ENV_NAME}
CUSTOM_ENV=false
TLABHOST="0.0.0.0"
PORT="8338"
RELOAD=false
HTTPS=false
# Load environment variables from .env files
load_env_files() {
# Look for .env files in current directory only
local env_files=(
".env"
".env.local"
".env.production"
".env.development"
)
for env_file in "${env_files[@]}"; do
# Check in current directory only
if [ -f "$env_file" ]; then
echo "π Loading environment variables from $env_file"
# Export variables from .env file, ignoring comments and empty lines
set -a # automatically export all variables
source "$env_file"
set +a # stop automatically exporting
fi
done
}
# Load environment variables
load_env_files
# echo "Your shell is $SHELL"
# echo "Conda's binary is at ${CONDA_BIN}"
# echo "Your current directory is $(pwd)"
err_report() {
echo "Error in run.sh on line $1"
}
# trap 'err_report $LINENO' ERR
if ! command -v ${CONDA_BIN} &> /dev/null; then
echo "β Conda is not installed at ${MINIFORGE_ROOT}. Please run ./install.sh and try again."
else
echo "β
Conda is installed."
fi
while getopts crsp:h: flag
do
case "${flag}" in
c) CUSTOM_ENV=true;;
r) RELOAD=true;;
s) HTTPS=true;;
p) PORT=${OPTARG};;
h) TLABHOST=${OPTARG};;
esac
done
# Print out everything that was discovered above
# echo "π Using host: ${HOST}
# π Using port: ${PORT}
# π Using reload: ${RELOAD}
# π Using custom environment: ${CUSTOM_ENV}"
if [ "$CUSTOM_ENV" = true ]; then
echo "π§ Using current conda environment, I won't activate for you"
else
# echo "π Using default conda environment: ${ENV_DIR}"
echo "π Enabling conda in shell"
eval "$(${CONDA_BIN} shell.bash hook)"
echo "π Activating transformerlab conda environment"
conda activate "${ENV_DIR}"
fi
# Check if the uvicorn command works:
if ! command -v uvicorn &> /dev/null; then
echo "β Uvicorn is not installed. This usually means that the installation of dependencies failed. Run ./install.sh to install the dependencies."
exit 1
else
echo -n ""
# echo "β
Uvicorn is installed."
fi
# Check if NVIDIA GPU is available and add necessary paths
if command -v nvidia-smi &> /dev/null; then
echo "β
NVIDIA GPU detected, adding CUDA libraries to path"
# Add common NVIDIA library paths
export LD_LIBRARY_PATH=${ENV_DIR}/lib:$LD_LIBRARY_PATH
elif command -v rocminfo &> /dev/null; then
echo "β
AMD GPU detected, adding appropriate libraries to path"
export PATH=$PATH:/opt/rocm/bin:/opt/rocm/rocprofiler/bin:/opt/rocm/opencl/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/rocm/lib:/opt/rocm/lib64
fi
# Check if multitenant mode is enabled
if [ "$TFL_MULTITENANT" = "true" ]; then
echo "π’ Multitenant mode detected, setting up remote workspace"
# # Create remote workspace directory if it doesn't exist
# REMOTE_WORKSPACE_DIR="$HOME/.transformerlab/orgs/org_1/workspace"
# if [ ! -d "$REMOTE_WORKSPACE_DIR" ]; then
# echo "π Creating remote workspace directory: $REMOTE_WORKSPACE_DIR"
# mkdir -p "$REMOTE_WORKSPACE_DIR"
# fi
# Setup AWS credentials in ~/.aws directory
if [ -n "$AWS_ACCESS_KEY_ID" ] && [ -n "$AWS_SECRET_ACCESS_KEY" ]; then
echo "π Setting up AWS credentials in ~/.aws directory"
# Create .aws directory if it doesn't exist
AWS_DIR="$HOME/.aws"
if [ ! -d "$AWS_DIR" ]; then
mkdir -p "$AWS_DIR"
chmod 700 "$AWS_DIR"
fi
# Update credentials file - preserve existing profiles
CREDENTIALS_FILE="$AWS_DIR/credentials"
if [ -f "$CREDENTIALS_FILE" ]; then
echo "π Updating existing credentials file, preserving other profiles"
# Remove existing transformerlab-s3 profile if it exists
awk 'BEGIN{in_profile=0} /^\[transformerlab-s3\]/{in_profile=1; next} /^\[/ && !/^\[transformerlab-s3\]/{in_profile=0} !in_profile{print}' "$CREDENTIALS_FILE" > "$CREDENTIALS_FILE.tmp"
mv "$CREDENTIALS_FILE.tmp" "$CREDENTIALS_FILE"
else
echo "π Creating new credentials file"
fi
# Append transformerlab-s3 profile
cat >> "$CREDENTIALS_FILE" << EOF
[transformerlab-s3]
aws_access_key_id=$AWS_ACCESS_KEY_ID
aws_secret_access_key=$AWS_SECRET_ACCESS_KEY
EOF
chmod 600 "$CREDENTIALS_FILE"
# Update config file - preserve existing profiles
CONFIG_FILE="$AWS_DIR/config"
if [ -f "$CONFIG_FILE" ]; then
echo "π Updating existing config file, preserving other profiles"
# Remove existing transformerlab-s3 profile if it exists
awk 'BEGIN{in_profile=0} /^\[profile transformerlab-s3\]/{in_profile=1; next} /^\[/ && !/^\[profile transformerlab-s3\]/{in_profile=0} !in_profile{print}' "$CONFIG_FILE" > "$CONFIG_FILE.tmp"
mv "$CONFIG_FILE.tmp" "$CONFIG_FILE"
else
echo "π Creating new config file"
fi
# Append transformerlab-s3 profile
if [ -n "$AWS_DEFAULT_REGION" ]; then
cat >> "$CONFIG_FILE" << EOF
[profile transformerlab-s3]
region=$AWS_DEFAULT_REGION
output=json
EOF
else
cat >> "$CONFIG_FILE" << EOF
[profile transformerlab-s3]
region=us-east-1
output=json
EOF
fi
chmod 600 "$CONFIG_FILE"
echo "β
AWS credentials configured in ~/.aws (transformerlab-s3 profile)"
else
echo "β οΈ AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY not set, skipping AWS setup"
fi
fi
echo "βΆοΈ Starting the API server:"
if [ "$RELOAD" = true ]; then
echo "π Reload the server on file changes"
if [ "$HTTPS" = true ]; then
uv run -v python api.py --https --reload --port ${PORT} --host ${TLABHOST}
else
uv run -v uvicorn api:app --reload --port ${PORT} --host ${TLABHOST}
fi
else
if [ "$HTTPS" = true ]; then
uv run -v python api.py --https --port ${PORT} --host ${TLABHOST}
else
uv run -v uvicorn api:app --port ${PORT} --host ${TLABHOST} --no-access-log
fi
fi