-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwsiml
321 lines (309 loc) · 9.96 KB
/
wsiml
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
#!/usr/bin/zsh
#If you replace the shell above with bash, note also lines 121 to 124
# Copyright (c) 2024 Quantius Benignus
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#--------------------------------------------------------------------------
# NAME: wsiml (Multi-lingual version of wsi)
# PREREQUSITES:
# - whisper.cpp 'main' AND/OR 'server' installation (see https://github.com/ggerganov/whisper.cpp) or a whisperfile
# - recent versions of 'sox', 'curl', 'xsel' or 'wl-copy' CLI tools from your system's repositories.
#--------------------------------------------------------------------------
#Use those timestamps for timing your app/server performance:
#echo $(date +%s.%N)
#X11 or Wayland (2nd line may catch edge cases):
wm="$XDG_SESSION_TYPE"
#wm="${XDG_SESSION_TYPE:-$(loginctl show-session $(loginctl | grep $(whoami) | awk '{print $1}') -p Type --value)}"
#---USER CONFIGURATION BLOCK----------------------------------------------------------------------
#Please, adjust the variables here to suit your environment:
# Store temp files in memory for speed and to reduce SSD/HDD "grinding":
TEMPD='/dev/shm'
# Hardcoded temp wav file to store the voice memo and get overwritten every time (in RAM):
ramf="$TEMPD/wfile"
#Set the number of processing threads for whisper.cpp inference (adjust for your case):
NTHR=8
#It seems that the optimum number of transcribe threads should equal half CPU processing cores:
#NTHR=$(( $(getconf _NPROCESSORS_ONLN) / 2 ))
# Use PRIMARY selection (middle mouse button to paste) unless CLIPBOARD selected from GUI:
CLIPBOARD=false
# Use available whisperfile
WHISPERFILE=whisper-tiny.en.llamafile
#Provide hardcoded whisper.cpp hostname and port. To be used when invoked with -n option from CLI:
#(Can be overwritten when command line argument IP:PORT is supplied instead of '-n')
WHOST="127.0.0.1"
WPORT="58080"
# Choose a default language for speech input (only those with WER > 50%):
#lang="auto"
# Afrikaans:
#lang="af"
# Arabic:
#lang="ar"
# Armenian:
#lang="hy"
# Azerbaijani:
#lang="az"
# Belarusian:
#lang="be"
# Bosnian:
#lang="bs"
# Bulgarian:
#lang="bg"
# Catalan:
#lang="ca"
# Chinese:
#lang="zh"
# Croatian:
#lang="hr"
# Czech:
#lang="cs"
# Danish:
#lang="da"
# Dutch:
#lang="nl"
# English:
lang="en"
# Estonian:
#lang="et"
# Finnish:
#lang="fi"
# French:
#lang="fr"
# Galician:
#lang="gl"
# German:
#lang="de"
# Greek:
#lang="el"
# Hebrew:
#lang="he"
# Hindi:
#lang="hi"
# Hungarian:
#lang="hu"
# Icelandic:
#lang="is"
# Indonesian:
#lang="id"
# Italian:
#lang="it"
# Japanese:
#lang="ja"
# Kannada:
#lang="kn"
# Kazakh:
#lang="kk"
# Korean:
#lang="ko"
# Latvian:
#lang="lv"
# Lithuanian:
#lang="lt"
# Macedonian:
#lang="mk"
# Malay:
#lang="ms"
# Maori:
#lang="mi"
# Marathi:
#lang="mr"
# Nepali:
#lang="ne"
# Norwegian:
#lang="no"
# Persian:
#lang="fa"
# Polish:
#lang="pl"
# Portuguese:
#lang="pt"
# Romanian:
#lang="ro"
# Russian:
#lang="ru"
# Serbian:
#lang="sr"
# Slovak:
#lang="sk"
# Slovenian:
#lang="sl"
# Spanish:
#lang="es"
# Swahili:
#lang="sw"
# Swedish:
#lang="sv"
# Tagalog:
#lang="tl"
# Tamil:
#lang="ta"
# Thai:
#lang="th"
# Turkish:
#lang="tr"
# Ukrainian:
#lang="uk"
# Urdu:
#lang="ur"
# Vietnamese:
#lang="vi"
# Welsh:
#lang="cy"
# Default whisper.cpp model file for inference (Use at least ggml-small for good non-english performance):
#model="$TEMPD/ggml-small.bin
model=${WHISPER_DMODEL:-"$HOME/CHANGE_PATH_TO/WHISPER_CPP/MODELS/HERE/ggml-small.bin"}
#---END USER CONFIG BLOCK------------------------------------------------------------------------
# Process command line arguments first
while [ $# -gt 0 ]; do
case "$1" in
-c|--clipboard)
CLIPBOARD=true
shift
;;
-w|--whisperfile)
whf="$WHISPERFILE"
shift
;;
-t|--translate)
TRANSLATING="true"
shift
;;
-l|--language)
shift
lang=$1
shift
;;
-n|--netapi)
#This uses the hostname or IP and port specified in the config block
#Can be overwritten when a command line argument IP:PORT is supplied
IPnPORT="$WHOST:$WPORT"
if [[ "$(curl -s -f -o /dev/null -w '%{http_code}' $IPnPORT)" != "200" ]]; then
echo "Can't connect to whisper.cpp server at provided address!" >&2
exit 1
fi
shift
;;
-h|--help)
echo "Usage: $0 [-c|--clipboard]"
echo " -c, --clipboard: Use clipboard instead of PRIMARY selection"
echo " -l, --language: Choose an input language (different than the default)"
echo " -n, --netapi: Use whisper.cpp server with the host:port in the GONFIG block"
echo " -t, --translate: Translate to english (default is no translation)"
echo "Any other command line flag is expected to come after and be a valid whisper.cpp flag/argument and will be passed to it as is."
echo "For example: 'wsiml -l pl -t --best-of 7' is a valid call to wsiml"
exit 0
;;
*)
#Anything else is passed along to whisper.cpp, so use the above flags first and know what you are doing:
break
;;
esac
done
# Store the remaining arguments in a variable
rem_args=("$@")
#---CHECK DEPENDENCIES. This block can be commented out once dependencies confirmed-----------------------------------
command -v curl &>/dev/null || { echo "curl is required. Please, install curl" >&2 ; exit 1 ; }
#The next is only needed if curl requests json output from the whisper.cpp server;
#command -v jq &>/dev/null || { echo "jq is required. Please, install jq" >&2 ; exit 1 ; }
command -v sox &>/dev/null || { echo "sox is required. Please, install sox" >&2 ; exit 1 ; }
[ -n $whf ] || command -v transcribe &>/dev/null || { echo -e "Please, install whisper.cpp (see https://github.com/ggerganov/whisper.cpp)\
\nand create 'transcribe' in your PATH as a symbolic link to the main executable, e.g.\n \
'ln -s /full/path/to/whisper.cpp/main \$HOME/.local/bin/transcribe'" >&2 ; exit 1 ; }
#Now let's check if we are in X11 or Wayland and use the right utility:
if [[ wm == "wayland" ]]; then
command -v wl-copy &>/dev/null || { echo "wl-copy is needed for the clipboard. Please, install wl-copy" >&2 ; exit 1 ; }
elif [[ wm == "X11" ]]; then
command -v xsel &>/dev/null || { echo "We rely on xsel for the clipboard. Please, install xsel." >&2 ; exit 1 ; }
fi
#---END CHECK DEPENDENCIES. The above block can be commented out after successful 1st run----------------------------
#Hear the complaints of the above tools and do not continue with the sequence:
set -e
#echo "Recording now: "$(date +%s.%N)
rec -q -t wav $ramf rate 16k silence 1 0.1 1% 1 2.0 5% 2>/dev/null
#echo "Sending now: "$(date +%s.%N)
if [ -n "$IPnPORT" ]; then
# This is switching the models on the fly, but takes time, moving to sepparate apps for 'en' and 'multilingual'.
# if [ "$lang" != "en" ]; then
# str=$(curl -S -s $IPnPORT/load \
# -H "Content-Type: multipart/form-data" \
# -F model="$TEMPD/ggml-small.bin")
# echo $str
# multi=1
# else
# if [ multi = 1 ]; then
# str=$(curl -S -s $IPnPORT/load \
# -H "Content-Type: multipart/form-data" \
# -F model="$TEMPD/ggml-base.en.bin")
# echo $str
# multi=0
# fi
# fi
#In principle, this request can be built up to handle most of the whisper parameters, but for now, only language and translation:
str=$(curl -S -s $IPnPORT/inference \
-H "Content-Type: multipart/form-data" \
-F file="@$ramf" \
-F language="$lang" \
-F translate="${TRANSLATING:-false}" \
-F temperature="0.0" \
-F temperature_inc="0.2" \
-F response_format="text")
# | jq -r '.text' )
elif [[ "$whf" == *.llamafile ]]; then
echo "Using whisperfile: "
str="$($whf -t $NTHR -nt -f $ramf -l $lang ${TRANSLATING:+-tr} ${rem_args[@]} 2>/dev/null)"
else
str="$(transcribe -t $NTHR -nt -m $model -f $ramf -l $lang ${TRANSLATING:+-tr} ${rem_args[@]} 2>/dev/null)"
fi
#echo "$TRANSLATING from $lang"
#echo "Got text now: "$(date +%s.%N)
# Whisper detected non-speech events such as (wind blowing):
str="${str/\(*\)}"
str="${str/\[*\]}"
str="${str#$'\n'}"
str="${str#$'\n'}"
#Prefer the power of zsh, but loose full POSIX compliance.
if [ -n "$ZSH_NAME" ]; then
str="${str#*([[:space:]])}"
str="${(u)str}"
elif [ -n "$BASH" ]; then
#Running in bash because you changed the shebang on line 1
str="${str##+([[:space:]])}"
str="${str^}"
else
echo "Unknown shell, assuming bash compliance"
str="${str##+([[:space:]])}"
str="${str^}"
fi
#We have a result, now we make a few decisions:
#If this is somehow run in a text console:
if [[ -z "${DISPLAY}" ]] || [[ -z "${DESKTOP_SESSION}" ]] || [[ -z "${XDG_CURRENT_DESKTOP}" ]]; then
#"Not running in a known graphics environment. Using standard output:
echo $str ; exit 0
else
#xsel or wl-copy:
case "$wm" in
"x11")
if [ "$CLIPBOARD" = true ]; then
echo $str | xsel -ib
else
echo $str | xsel -ip
fi
;;
"wayland")
if [ "$CLIPBOARD" = true ]; then
echo $str | wl-copy
else
echo $str | wl-copy -p
fi
;;
*)
echo $str
;;
esac
fi
#echo "Text processed and in clipboard now: "$(date +%s.%N)