Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
KoboldAI
GitHub Repository: KoboldAI/KoboldAI-Client
Path: blob/main/colabkobold.sh
471 views
1
#!/bin/bash
2
# KoboldAI Easy Colab Deployment Script by Henk717
3
4
# read the options
5
TEMP=`getopt -o m:i:p:c:d:x:a:l:z:g:t:n:b:s: --long model:,init:,path:,configname:,download:,aria2:,dloc:,xloc:,7z:,git:,tar:,ngrok:,branch:,savemodel:,localtunnel:,lt: -- "$@"`
6
eval set -- "$TEMP"
7
8
# extract options and their arguments into variables.
9
while true ; do
10
case "$1" in
11
-m|--model)
12
model=" --model $2" ; shift 2 ;;
13
-i|--init)
14
init=$2 ; shift 2 ;;
15
-p|--path)
16
mpath="$2" ; shift 2 ;;
17
-c|--configname)
18
configname=" --configname $2" ; shift 2 ;;
19
-n|--ngrok)
20
ngrok=" --ngrok" ; shift 2 ;;
21
--lt|--localtunnel)
22
localtunnel=" --localtunnel" ; shift 2 ;;
23
-d|--download)
24
download="$2" ; shift 2 ;;
25
-a|--aria2)
26
aria2="$2" ; shift 2 ;;
27
-l|--dloc)
28
dloc="$2" ; shift 2 ;;
29
-x|--xloc)
30
xloc="$2" ; shift 2 ;;
31
-z|--7z)
32
z7="$2" ; shift 2 ;;
33
-t|--tar)
34
tar="$2" ; shift 2 ;;
35
-g|--git)
36
git="$2" ; shift 2 ;;
37
-b|--branch)
38
branch="$2" ; shift 2 ;;
39
-s|--savemodel)
40
savemodel=" --savemodel" ; shift 2 ;;
41
--) shift ; break ;;
42
*) echo "Internal error!" ; exit 1 ;;
43
esac
44
done
45
46
# Create the Launch function so we can run KoboldAI at different places in the script
47
function launch
48
{
49
#End the script if "--init only" was specified.
50
if [ "$init" == "only" ]; then
51
echo Initialization complete...
52
exit 0
53
else
54
cd /content/KoboldAI-Client
55
echo "Launching KoboldAI with the following options : python3 aiserver.py$model$kmpath$configname$ngrok$localtunnel$savemodel --colab"
56
python3 aiserver.py$model$kmpath$configname$ngrok$localtunnel$savemodel --colab
57
exit
58
fi
59
}
60
61
git_default_branch() {
62
(git remote show $git | grep 'HEAD branch' | cut -d' ' -f5) 2>/dev/null
63
}
64
65
# Don't allow people to mess up their system
66
if [[ ! -d "/content" ]]; then
67
echo You can only use this script on Google Colab
68
echo Use aiserver.py to play KoboldAI locally.
69
echo Check our Readme for Colab links if you wish to play on Colab.
70
exit
71
fi
72
73
# Redefine the download location
74
if [ "$dloc" == "colab" ]; then
75
dloc="/content"
76
else
77
dloc="/content/drive/MyDrive/KoboldAI/models"
78
fi
79
80
# Redefine the extraction location
81
if [ "$xloc" == "drive" ]; then
82
xloc="/content/drive/MyDrive/KoboldAI/models/"
83
dloc="/content"
84
else
85
xloc="/content/"
86
fi
87
88
# Redefine the Path to be in the relevant location
89
if [[ -v mpath ]];then
90
mpath="$xloc$mpath"
91
kmpath=" --path $mpath"
92
fi
93
94
# Create folders on Google Drive
95
mkdir /content/drive/MyDrive/KoboldAI/
96
mkdir /content/drive/MyDrive/KoboldAI/stories/
97
mkdir /content/drive/MyDrive/KoboldAI/models/
98
mkdir /content/drive/MyDrive/KoboldAI/settings/
99
mkdir /content/drive/MyDrive/KoboldAI/softprompts/
100
mkdir /content/drive/MyDrive/KoboldAI/userscripts/
101
if [ "$init" == "drive" ]; then
102
echo Google Drive folders created.
103
exit 0
104
fi
105
106
# Install and/or Update KoboldAI
107
if [ "$init" != "skip" ]; then
108
cd /content
109
if [ ! -z ${git+x} ]; then
110
if [ "$git" == "Official" ]; then
111
git=https://github.com/koboldai/KoboldAI-Client
112
fi
113
if [ "$git" == "United" ]; then
114
git=https://github.com/henk717/KoboldAI-Client
115
fi
116
if [ "$git" == "united" ]; then
117
git=https://github.com/henk717/KoboldAI-Client
118
fi
119
else
120
git=https://github.com/koboldai/KoboldAI-Client
121
fi
122
123
mkdir /content/KoboldAI-Client
124
cd /content/KoboldAI-Client
125
126
git init
127
git remote remove origin
128
git remote add origin $git
129
git fetch --all
130
131
if [ ! -z ${branch+x} ]; then
132
git checkout $branch -f
133
git reset --hard origin/$branch
134
else
135
git checkout $(git_default_branch) -f
136
git reset --hard origin/$(git_default_branch)
137
fi
138
139
cd /content/KoboldAI-Client
140
141
cp -rn stories/* /content/drive/MyDrive/KoboldAI/stories/
142
cp -rn userscripts/* /content/drive/MyDrive/KoboldAI/userscripts/
143
cp -rn softprompts/* /content/drive/MyDrive/KoboldAI/softprompts/
144
rm stories
145
rm -rf stories/
146
rm userscripts
147
rm -rf userscripts/
148
rm softprompts
149
rm -rf softprompts/
150
rm models
151
rm -rf models/
152
ln -s /content/drive/MyDrive/KoboldAI/stories/ stories
153
ln -s /content/drive/MyDrive/KoboldAI/settings/ settings
154
ln -s /content/drive/MyDrive/KoboldAI/softprompts/ softprompts
155
ln -s /content/drive/MyDrive/KoboldAI/userscripts/ userscripts
156
ln -s /content/drive/MyDrive/KoboldAI/models/ models
157
158
if [ -n "${COLAB_TPU_ADDR+set}" ]; then
159
pip install -r requirements_mtj.txt
160
else
161
pip install -r requirements.txt
162
fi
163
164
# Make sure Colab has the system dependencies
165
sudo apt install netbase aria2 -y
166
npm install -g localtunnel
167
fi
168
169
cd /content
170
171
# Models extracted? Then we skip anything beyond this point for faster loading.
172
if [ -f "/content/extracted" ]; then
173
launch
174
fi
175
176
# Is the model extracted on Google Drive? Skip the download and extraction
177
# Only on Google Drive since it has a big impact there if we don't, and locally we have better checks in place
178
if [ "$xloc" == "/content/drive/MyDrive/KoboldAI/models/" ] && [[ -d $mpath ]];then
179
launch
180
fi
181
182
#Download routine for regular Downloads
183
if [ ! -z ${download+x} ]; then
184
wget -c $download -P $dloc
185
fi
186
187
#Download routine for Aria2c scripts
188
if [ ! -z ${aria2+x} ]; then
189
curl -L $aria2 | aria2c -x 10 -s 10 -j 10 -c -i- -d$dloc --user-agent=KoboldAI --file-allocation=none
190
fi
191
192
#Extract the model with 7z
193
if [ ! -z ${z7+x} ]; then
194
7z x -o$xloc $dloc/$z7 -aos
195
touch /content/extracted
196
fi
197
198
#Extract the model in a ZSTD Tar file
199
if [ ! -z ${tar+x} ]; then
200
git clone https://github.com/VE-FORBRYDERNE/pv
201
cd pv
202
./configure
203
make
204
make install
205
cd ..
206
apt install zstd -y
207
pv $dloc/$tar | tar -I zstd -C $xloc -x
208
touch /content/extracted
209
fi
210
211
launch
212
213