Files
ai_env/tools/speech.sh
2025-08-21 21:53:39 +02:00

87 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "$SCRIPT")
commands=("argc" "curl" "ffplay")
for cmd in "${commands[@]}"; do
if ! command -v "$cmd" &>/dev/null; then
echo "Error: $cmd is required." >&2
exit 1
fi
done
# Check for required environment variable
if [[ -z "${LLM_API_KEY}" ]]; then
echo "The environment variable LLM_API_KEY is not set."
echo "Try to load ${SCRIPTPATH}/../.env file"
export $(xargs <${SCRIPTPATH}/../.env)
fi
if [[ -z "${LLM_API_KEY}" ]]; then
echo "Failed to load ${SCRIPTPATH}/../.env file"
exit 1
fi
tts_host=${TTS_API_HOST:-"http://localhost:8000"}
declare -A voices_model=(
[ff_siwis]="speaches-ai/Kokoro-82M-v1.0-ONNX-int8"
[tom]="speaches-ai/piper-fr_FR-tom-medium"
[upmc]="speaches-ai/piper-fr_FR-upmc-medium"
[alba]="speaches-ai/piper-en_GB-alba-medium"
[northern_english_male]="speaches-ai/piper-en_GB-northern_english_male-medium"
[john]="speaches-ai/piper-en_US-john-medium"
[bryce]="speaches-ai/piper-en_US-bryce-medium"
[ryan]="speaches-ai/piper-en_US-ryan-high"
)
_choice_voice() {
if [[ "${argc_lang}" == "fr" ]]; then
echo ff_siwis
echo tom
echo upmc
fi
if [[ "${argc_lang}" == "en" ]]; then
echo alba
echo northern_english_male
echo john
echo bryce
echo ryan
fi
}
# @cmd
# @flag -p --play Play the generated speech
# @option -l --lang![en|fr] Set the language
# @option -v --voice![`_choice_voice`] Set the voice
# @option -s --speed=1.0 Set the speed
# @option -f --filename=speech.wav Set the output filename
# @arg text! Set the text
synthesize() {
model=$(echo ${voices_model[$argc_voice]})
http_status_code=$(curl -s -X POST "${tts_host}/v1/audio/speech" -o "${argc_filename}" -w "%{http_code}" \
-H "Authorization: Bearer ${LLM_API_KEY}" \
-H "Content-Type: application/json" \
-d "{\"model\": \"${model}\",\"input\": \"${argc_text}\",\"voice\": \"${argc_voice}\",\"response_format\": \"wav\",\"speed\": ${argc_speed}}")
# Check the response code for successful HTTP request
if [[ "${http_status_code}" -ne 200 ]]; then
echo "Error: Failed to fetch audio file. Received HTTP status code: $http_status_code"
exit 1
fi
if [[ $argc_play -eq 1 ]]; then
ffplay "${argc_filename}" -nodisp -nostats -hide_banner -autoexit -v quiet
fi
echo "Audio file ${argc_filename} generated successfully."
}
_choice_lang() {
echo 'af', 'am', 'ar', 'as', 'az', 'ba', 'be', 'bg', 'bn', 'bo', 'br', 'bs', 'ca', 'cs', 'cy', 'da', 'de', 'el', 'en', 'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fr', 'gl', 'gu', 'ha', 'haw', 'he', 'hi', 'hr', 'ht', 'hu', 'hy', 'id', 'is', 'it', 'ja', 'jw', 'ka', 'kk', 'km', 'kn', 'ko', 'la', 'lb', 'ln', 'lo', 'lt', 'lv', 'mg', 'mi', 'mk', 'ml', 'mn', 'mr', 'ms', 'mt', 'my', 'ne', 'nl', 'nn', 'no', 'oc', 'pa', 'pl', 'ps', 'pt', 'ro', 'ru', 'sa', 'sd', 'si', 'sk', 'sl', 'sn', 'so', 'sq', 'sr', 'su', 'sv', 'sw', 'ta', 'te', 'tg', 'th', 'tk', 'tl', 'tr', 'tt', 'uk', 'ur', 'uz', 'vi', 'yi', 'yo', 'yue', 'zh' | sed 's/, /\n/g'
}
eval "$(argc --argc-eval "$0" "$@")"