-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
417 lines (324 loc) · 10.4 KB
/
setup.py
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
from dotenv import dotenv_values, set_key
import os
from getpass import getpass
# Load existing .env if it exists
env_file_path = ".env"
env_data = dotenv_values(dotenv_path=env_file_path)
def get_input_for_key(key):
return getpass(f"Enter your {key}: ")
# Collect new values
keys = ["HUGGINGFACE_API_KEY", "OPENAI_API_KEY", "ANTHROPIC_API_KEY", "DOCKER_BUILDKIT"]
for key in keys:
if key == "DOCKER_BUILDKIT":
"DOCKER_BUILDKIT"="1"
set_key(env_file_path, key, "")
new_value = get_input_for_key(key)
set_key(env_file_path, key, new_value)
build_sh = """
# build.sh
#!/bin/bash
# Export variables from .env to the shell
export $(grep -v '^#' .env | xargs)
# Build Docker image
docker build --build-arg $HUGGINGFACE_API_KEY --build-arg $OPENAI_API_KEY --build-arg $ANTHROPIC_API_KEY -t .
print("Configuration saved to .env")
"""
build_path = "build.sh"
run_sh="""
#! /bin/bash
sudo docker run --rm -it -v $(pwd):/app/server -p 8888:8888 python:3.8-slim bash
"""
run_path = "run.sh"
docker_file = """
FROM gcr.io/tpu-pytorch/xla:nightly
WORKDIR /root
# Installs Tensorflow to resolve the TPU name to IP Address
RUN pip install tensorflow
# Installs google cloud sdk, this is mostly for using gsutil to
# export the model.
RUN wget -nv \\
https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz && \\
mkdir /root/tools && \\
tar xvzf google-cloud-sdk.tar.gz -C /root/tools && \\
rm google-cloud-sdk.tar.gz && \\
/root/tools/google-cloud-sdk/install.sh --usage-reporting=false \\
--path-update=false --bash-completion=false \\
--disable-installation-options && \\
rm -rf /root/.config/* && \\
ln -s /root/.config /config && \\
# Remove the backup directory that gcloud creates
rm -rf /root/tools/google-cloud-sdk/.install/.backup
# Path configuration
ENV PATH $PATH:/root/tools/google-cloud-sdk/bin
# Make sure gsutil will use the default service account
RUN echo '[GoogleCompute]\\nservice_account = default' > /etc/boto.cfg
# Set work directory
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app/
# Use ARG for build-time variables
ARG HUGGINGFACE_API_KEY
ARG OPENAI_API_KEY
ARG ANTHROPIC_API_KEY
# Use ENV for runtime variables
ENV HUGGINGFACE_API_KEY=$HUGGINGFACE_API_KEY
ENV OPENAI_API_KEY=$OPENAI_API_KEY
ENV ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY
# Install packages
RUN python install.py
# Change the below to your exec. Make sure you chmod +x the script first
ENTRYPOINT ["sh", "run.sh"]
"""
docker_path = "Dockerfile"
install_sh = """#!/bin/bash
# Install CMake, gcc, g++ and other build essentials
sudo apt update
sudo apt upgrade -y
sudo apt dist-upgrade -y
sudo apt install python3-dev
sudo apt python-is-python3 -y
sudo apt install build-essential -y
echo installed build essentials
# Install git
if command -v git &> /dev/null
then sudo apt install git -y
echo installed git
fi
git lfs install
pip install --upgrade huggingface_hub
huggingface-cli lfs-enable-largefiles .
# Check virtual environment
if file ~/.pyenv/bin/ &> /dev/null]; then
curl https://pyenv.run | bash
pyenv install 3.10.13
pyenv global 3.10.13
echo installed pyenv
fi
pyenv local 3.10.13
echo activated local pyenv
if file ~/.local/bin/poetry &> /dev/null]; then
curl -sSL https://install.python-poetry.org | python3 -
echo installed poetry
fi
poetry shell
echo activate poetry
# upgrade pip
python -m pip install --upgrade pip
echo updated pip
# install notebook resources
pip install testresources
pip install wheel setuptools
pip install jupyter lab
echo installed jupyter resources
# install requirements.txt
poetry install
echo installed requirements
# Install ffmpeg
sudo apt install ffmpeg -y
echo installed ffmpeg
# Install zlib
sudo apt-get install zlib1g -y
echo installed zlib
# Create a symbolic link for CUDA
sudo ln -sf /sbin/ldconfig.real /usr/lib/wsl/lib/libcuda.so.1
echo created symbolic link
# Delete the 7fa2af80 key
sudo apt-key del 7fa2af80
echo deleted 7fa2af80 key
# Download and Install CUDA
# This is setup for CUDA 12.2 on ubuntu 20.04. Do not install this on a different operating system. Go to this site to get the correct cuda version: https://developer.nvidia.com/cuda-downloads
# download pin for keyring
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
echo downloaded and moved pin
# Check if the local installer is already downloaded if not download it
if command -v nvcc &> /dev/null
then
wget https://developer.download.nvidia.com/compute/cuda/12.2.2/local_installers/cuda-repo-ubuntu2004-12-2-local_12.2.2-535.104.05-1_amd64.deb
fi
# Install CUDA
if file cuda-repo-ubuntu2004-12-2-local_12.2.2-535.104.05-1_amd64.deb &> /dev/null]; then
sudo dpkg -i cuda-repo-ubuntu2004-12-2-local_12.2.2-535.104.05-1_amd64.deb
sudo cp /var/cuda-repo-ubuntu2004-12-2-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda
echo installed cuda
fi
# install libsdl2
sudo apt-get install libsdl2-dev
echo installed libsdl2
# Install CLBlast
curl https://github.com/CNugteren/CLBlast/releases/download/1.6.1/CLBlast-1.6.1-windows-x64.zip -o CLBlast-1.6.1-windows-x64.zip
echo installed CLBlast
# Install PyTorch.
if command python -c "import torch; print(torch.cuda.is_available())" ; then
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
echo installed PyTorch
fi
# Check if CUDA is available
if python -c "import torch; print(torch.cuda.is_available())" ; then
nvidia-smi
echo "CUDA is available"
else
echo "CUDA is not available. Instillation failed"
fi
if file ./whisper/ &> /dev/null; then
git clone git@github.com:ggerganov/whisper.cpp.git
mv whisper.cpp whipser
cd whisper
CMake:
cd whisper.cpp
cmake -B build -DWHISPER_CLBLAST=ON
cmake --build build -j --config Release
cd ..
make samples
make tiny.en
make tiny
make base.en
make base
make small.en
make small
make medium.en
make medium
make large-v1
make large
bash livestream.sh
make stream
fi
if command -v nvm &> /dev/nulll; then
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
echo installed nvm
fi
if command -v npm &> /dev/nulll; then
nvm install 18
nvm use 18
echo installed npm
fi
if command -v pm2 &> /dev/nulll; then
pm2 install pm2-logrotate
echo installed pm2
fi
if command -v ngrok &> /dev/nulll; then
ngrok authtoken $NGROK_TOKEN
echo installed ngrok
fi
# Update repositories
sudo apt update -y
echo updated repositories
# clean up apt
sudo apt auto remove -y
echo cleaned up apt
# Start ngrok
node pm2 start "npm run ngrok http 8888"
echo started pm2 with ngrok on port 8888
# start server
uvicorn app.main:app --host localhost --port 8888
echo started server on port 8888
echo instilation complete
EOF
"""
livestream_sh = """
#!/bin/bash
#
# Transcribe audio livestream by feeding ffmpeg output to whisper.cpp at regular intervals
# Idea by @semiformal-net
# ref: https://github.com/ggerganov/whisper.cpp/issues/185
#
set -eo pipefail
url="http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/nonuk/sbr_low/ak/bbc_world_service.m3u8"
fmt=aac # the audio format extension of the stream (TODO: auto detect)
step_s=30
model="base.en"
check_requirements()
{
if ! command -v ./main &>/dev/null; then
echo "whisper.cpp main executable is required (make)"
exit 1
fi
if ! command -v ffmpeg &>/dev/null; then
echo "ffmpeg is required (https://ffmpeg.org)"
exit 1
fi
}
check_requirements
if [ -z "$1" ]; then
echo "Usage: $0 stream_url [step_s] [model]"
echo ""
echo " Example:"
echo " $0 $url $step_s $model"
echo ""
echo "No url specified, using default: $url"
else
url="$1"
fi
if [ -n "$2" ]; then
step_s="$2"
fi
if [ -n "$3" ]; then
model="$3"
fi
# Whisper models
models=( "tiny.en" "tiny" "base.en" "base" "small.en" "small" "medium.en" "medium" "large-v1" "large" )
# list available models
function list_models {
printf "\\n"
printf " Available models:"
for model in "${models[@]}"; do
printf " $model"
done
printf "\\n\\n"
}
if [[ ! " ${models[@]} " =~ " ${model} " ]]; then
printf "Invalid model: $model\\n"
list_models
exit 1
fi
running=1
trap "running=0" SIGINT SIGTERM
printf "[+] Transcribing stream with model '$model', step_s $step_s (press Ctrl+C to stop):\\n\\n"
# continuous stream in native fmt (this file will grow forever!)
ffmpeg -loglevel quiet -y -re -probesize 32 -i $url -c copy /tmp/whisper-live0.${fmt} &
if [ $? -ne 0 ]; then
printf "Error: ffmpeg failed to capture audio stream\\n"
exit 1
fi
printf "Buffering audio. Please wait...\\n\\n"
sleep $(($step_s))
# do not stop script on error
set +e
i=0
SECONDS=0
while [ $running -eq 1 ]; do
# extract the next piece from the main file above and transcode to wav. -ss sets start time and nudges it by -0.5s to catch missing words (??)
err=1
while [ $err -ne 0 ]; do
if [ $i -gt 0 ]; then
ffmpeg -loglevel quiet -v error -noaccurate_seek -i /tmp/whisper-live0.${fmt} -y -ar 16000 -ac 1 -c:a pcm_s16le -ss $(($i*$step_s-1)).5 -t $step_s /tmp/whisper-live.wav 2> /tmp/whisper-live.err
else
ffmpeg -loglevel quiet -v error -noaccurate_seek -i /tmp/whisper-live0.${fmt} -y -ar 16000 -ac 1 -c:a pcm_s16le -ss $(($i*$step_s)) -t $step_s /tmp/whisper-live.wav 2> /tmp/whisper-live.err
fi
err=$(cat /tmp/whisper-live.err | wc -l)
done
./main -t 8 -m ./models/ggml-${model}.bin -f /tmp/whisper-live.wav --no-timestamps -otxt 2> /tmp/whispererr | tail -n 1
while [ $SECONDS -lt $((($i+1)*$step_s)) ]; do
sleep 1
done
((i=i+1))
done
killall -v ffmpeg
killall -v main"""
install_path = "bash_install.sh"
livestream_path = "livestream.sh"
def write_scripts(script, file_path):
with open(file_path, "w", encoding="utf-8") as f:
f.write(script)
if file_path.is_file() or file_path.is_dir():
file_path.chmod(0o777)
print("Building build and run script")
write_scripts(docker_file, docker_path)
write_scripts(build_sh, build_path)
write_script(livestream_sh, livestream_path)
write_script(install_sh, install_path)
write_scripts(run_sh, run_path)
print("Build Docker image")
subprocess.run(["sudo", "bash ./build.sh"])