Skip to content

Commit

Permalink
Shell edge cases
Browse files Browse the repository at this point in the history
Handle bash/zsh differences. Get number of CPU cores w builtins
  • Loading branch information
QuantiusBenignus committed Feb 10, 2024
1 parent f95cb5f commit dad32b0
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions wsi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/zsh
#If you replace the shell above with bash, note also lines 56 to 66
# Copyright (c) 2024 Quantius Benignus
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Expand Down Expand Up @@ -30,16 +31,16 @@ set -e

# Store temp files in memory for speed and to reduce SSD/HDD "grinding":
TEMPD='/dev/shm'
# Default whisper.cpp model file for inference (PLEASE UPDATE WITH THE LOCATION OF YOUR WHISPER.CPP MODELS):
#model="$HOME/YOUR_AI_MODEL-FILES/Models/whisper/ggml-tiny.en.bin"
model="$HOME/YOUR_AI_MODEL-FILES/Models/whisper/ggml-base.en.bin"
# Default whisper.cpp model file for inference:
#model="$HOME/AI/Models/whisper/ggml-tiny.en.bin"
model="$HOME/AI/Models/whisper/ggml-base.en.bin"
# 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 CPU processing cores:
#NTHR="$(( $(lscpu | awk '/^Socket\(s\)/{ print $2 }') * $(lscpu | awk '/^Core\(s\) per socket/{ print $4 }') ))"
#NTHR=8
#It seems that the optimum number of transcribe threads should equal half CPU processing cores:
NTHR=$(( $(getconf _NPROCESSORS_ONLN) / 2 ))
#--------------------------------------------------------------------------

rec -q -t wav $ramf rate 16k silence 1 0.1 1% 1 2.0 6% 2>/dev/null
Expand All @@ -50,13 +51,21 @@ str="$(transcribe -t $NTHR -nt -m $model -f $ramf 2>/dev/null)"
str="${str/\(*\)}"
str="${str/\[*\]}"
str="${str#$'\n'}"
str="${str#$'\n'}"
#This is strictly zsh:
str="${str#*([[:space:]])}"
str="${(u)str}"
# For bash, comment the last 2 lines above and use this instead (I think):
#str="${str##+([[:space:]])}"
#str="${str^}"
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
Expand Down

0 comments on commit dad32b0

Please sign in to comment.