-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsummarize.sh
executable file
·277 lines (248 loc) · 6.02 KB
/
summarize.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
#!/bin/bash
#
# All the actual work performed by this script is thanks to the work of others.
#
# Thank you: yt-dlp, whisper.cpp, ffmpeg, ollama, curl and Mixtral
#
OLLAMA_HOST=127.0.0.1:11434
OLLAMA_MODEL=llama3
#OLLAMA_MODEL=mixtral
OLLAMA_CONTEXT_SIZE=8192
# >80GB RAM needed to use the full context width of Mixtral
#OLLAMA_CONTEXT_SIZE=32768
WHISPER_CPP_PATH="$HOME/whisper.cpp"
WHISPER_CPP_MODEL=ggml-large-v3.bin
WHISPER_CPP_NTHREADS=4
WHISPER_CPP_NPROCESSORS=1
# Possible speedup to the transcribing phase
#WHISPER_CPP_NPROCESSORS=3
STORAGE_DIR=".summarize.data"
function help {
echo "Usage: summarize.sh [-q] [-c] [-d] <Youtube url>"
echo
echo "Optional parameters:"
echo
echo " -q : Quiet, does not print any progress information or execution time"
echo " -c : Conversational, waits for further questions after the summary is printed"
echo " -C : Conversational, immediately prompts without first doing a summary"
echo " -d : Debug, prints all output from the called programs"
echo
}
# unload ollama model from GPU in the background
function unload {
(
arg="{\"model\": \"$OLLAMA_MODEL\", \
\"keep_alive\": 0
}"
resp=$(echo ${arg} | curl -s http://$OLLAMA_HOST/api/generate -d @-)
resp=$(echo ${resp} | jq '.done')
if [[ "true" != "${resp}" ]]; then
error
error "Ollama failed to unload memory used by the model. This might mean subsequent running of the script will fail."
error
fi
) &
}
function cleanup {
if [ ! -z ${orig_audio} ]; then
rm ${orig_audio}.wav
fi
if [ ! -z ${audio} ]; then
rm ${audio}.wav
fi
}
function print {
if [[ -z $quiet ]]; then
echo -n "$1"
fi
}
function error {
>&2 echo "$1"
}
function query {
arg="{";
arg+="\"model\": \"$OLLAMA_MODEL\","
arg+="\"prompt\": \"$@\","
arg+="\"stream\": false,"
if [ ! -z "$context" ]; then
arg+="\"context\": ${context},"
fi
arg+="\"options\": {"
arg+="\"seed\": ${seed},"
arg+="\"temperature\": ${temp},"
arg+="\"num_ctx\": $OLLAMA_CONTEXT_SIZE"
arg+="}"
arg+="}"
resp=$(echo ${arg} | curl -s http://$OLLAMA_HOST/api/generate -d @-)
if (( $? )); then
error
error "Unable to reach ollama server at $OLLAMA_HOST."
error
fail=true
fi
error=$(echo ${resp} | jq '.error')
if [ ! -z "$error" ] && [ "null" != "$error" ]; then
error
error "LLM summary failed with the message: $error"
error
fail=true
fi
if [ true = $fail ]; then
cleanup
exit
fi
notokens=$(echo ${resp} | jq '.prompt_eval_count')
if (( $notokens > $OLLAMA_CONTEXT_SIZE )); then
resp+="WARNING: The size of the input to the LLM exceeded its configured max size! Output might be unreliable.\n\n"
fi
echo "$resp"
}
# setting these makes the output reproducible
seed=99065467
temp=1
echo
fail=false
debug=false
context=""
if [ -z $1 ]; then
help
fail=true
fi
while (( "$#" )); do
case $1 in
-h )
help
fail=true
;;
-q )
quiet=true
;;
-c )
conv=true
;;
-C )
conv=true
skipsum=true
;;
-d )
debug=true
;;
http* )
video="$1"
;;
* )
help
fail=true
;;
esac
shift
done
if [ -z $(which yt-dlp) ]; then
echo "Unable to find the yt-dlp executable"
echo
fail=true
fi
if [ -z $(which jq) ]; then
echo "Unable to find the JSON parser 'jq', please install it"
echo
fail=true
fi
if [ -z $(which ffmpeg) ]; then
echo "Unable to find the ffmpeg executable"
echo
fail=true
fi
if [ ! -f "${WHISPER_CPP_PATH}"/main ]; then
echo "Unable to find the main executable for the whisper.cpp project"
echo
fail=true
fi
if [ ! -f "${WHISPER_CPP_PATH}/models/${WHISPER_CPP_MODEL}" ]; then
echo "Unable to find the $WHISPER_CPP_MODEL model in the whisper.cpp project"
echo
fail=true
fi
if [ true = $fail ]; then
exit
fi
start=$(date +%s)
# create storage directory if it doesn't exist
mkdir -p "$STORAGE_DIR"
# create a hash from the URL to store/recall data
storage=$(echo $video | md5sum - | awk '{print $1}')
if [ -d "$STORAGE_DIR/$storage" ]; then
print "Existing data found for this URL ($storage) - reusing"
else
mkdir -p "$STORAGE_DIR/$storage"
echo "$video" > "$STORAGE_DIR/$storage/url.txt"
fi
if [ -f "$STORAGE_DIR/$storage/transcription.txt" ]; then
print " transcribed audio"
input=$(cat "$STORAGE_DIR/$storage/transcription.txt")
else
orig_audio=$(mktemp -u)
print "Downloading audio... "
if [ true = $debug ]; then
yt-dlp -x --audio-format wav -o ${orig_audio}.wav "$video"
else
yt-dlp -x --audio-format wav -o ${orig_audio}.wav "$video" &>/dev/null
fi
print "Converting audio... "
audio=$(mktemp -u)
if [ true = $debug ]; then
ffmpeg -i ${orig_audio}.wav -ar 16000 -ac 1 -c:a pcm_s16le ${audio}.wav
else
ffmpeg -i ${orig_audio}.wav -ar 16000 -ac 1 -c:a pcm_s16le ${audio}.wav &>/dev/null
fi
print "Transcribing audio... "
if [ true = $debug ]; then
input=$($WHISPER_CPP_PATH/main -p $WHISPER_CPP_NPROCESSORS -t $WHISPER_CPP_NTHREADS -m $WHISPER_CPP_PATH/models/$WHISPER_CPP_MODEL -np -nt -f ${audio}.wav)
else
input=$($WHISPER_CPP_PATH/main -p $WHISPER_CPP_NPROCESSORS -t $WHISPER_CPP_NTHREADS -m $WHISPER_CPP_PATH/models/$WHISPER_CPP_MODEL -np -nt -f ${audio}.wav 2>/dev/null)
fi
echo "$input" > $STORAGE_DIR/$storage/transcription.txt
fi
if [ -z "${input}" ]; then
error
error "Failure when transcribing, or no audio possible to transcribe detected."
error
cleanup
exit
fi
if [ -z $skipsum ]; then
print "Summarizing text... "
input=${input//\"/\\\"}
prompt="Please summarize the following audio transcription: $input"
resp=$(query "$prompt")
summary=$(echo ${resp} | jq '.response')
context=$(echo ${resp} | jq '.context')
fi
end=$(date +%s)
if [ -z $skipsum ]; then
print "Completed in $(($end-$start)) seconds."
echo
echo
echo -e $summary
fi
echo
cleanup
if [ -z $conv ]; then
unload
exit
fi
# Conversational code follows
while true; do
echo
read -p "Enter question (enter to exit): " question
echo
if [ -z "$question" ]; then
break
else
resp=$(query "$question. Make sure to only use knowledge found in the following audio transcription: $input")
answer=$(echo ${resp} | jq '.response')
context=$(echo ${resp} | jq '.context')
echo -e $answer
echo
fi
done
unload