aboutsummaryrefslogtreecommitdiffstats
path: root/tools/stt.sh
blob: 13a1b5a596ac83827873d20d19fa928e3d1dd00a (plain)
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
#!/bin/bash

# Function to print usage information
usage() {
	echo "Usage: $0 [record|transcription] <options>"
	echo ""
	echo "Actions:"
	echo "  record        Record audio from a selected source"
	echo "  transcription Transcribe audio from a .wav file"
	echo ""
	echo "Options for 'record':"
	echo "  -s, --source  Specify the audio source (required)"
	echo ""
	echo "Options for 'transcription':"
	echo "  -f, --file    Specify the audio file to transcribe (required)"
	echo "  -l, --lang    Specify the audio file language (default: en)"
	exit 1
}

if [[ $# -eq 0 ]]; then
	usage
fi

# Check for required environment variable
if [[ -z "${LLM_API_KEY}" ]]; then
	echo "The environment variable LLM_API_KEY is not set."
	echo 'You can use the following command: export $(xargs < ../.env))'
	exit 1
fi

ACTION=$1
shift

host=${STT_API_HOST:-"http://localhost:8001"}
LANG="en" # Default language

if [ "$ACTION" == "record" ]; then
	if [ "$#" -eq 0 ]; then
		echo "Error: Source is required for record action."
		echo "Available sources:"
		pactl list short sources | awk '{print $2}'
		exit 1
	fi

	SOURCE=""
	while [[ "$#" -gt 0 ]]; do
		case $1 in
		-s | --source)
			SOURCE="$2"
			shift
			;;
		*)
			echo "Unknown parameter passed: $1"
			usage
			;;
		esac
		shift
	done

	# Validate the provided source
	if ! pactl list short sources | awk '{print $2}' | grep -q "^$SOURCE$"; then
		echo "Error: Invalid audio source. Available sources:"
		pactl list short sources | awk '{print $2}'
		exit 1
	fi

	timestamp=$(date +"%Y%m%d_%H%M%S")
	filename="record_${timestamp}.wav"
	echo "Start recording to ${filename} ; use CTRL+C to terminate."
	parec -d "${SOURCE}" --file-format=wav "${filename}"
elif [ "$ACTION" == "transcription" ]; then
	if [ "$#" -eq 0 ]; then
		echo "Error: File is required for transcription action."
		usage
	fi

	FILE=""
	while [[ "$#" -gt 0 ]]; do
		case $1 in
		-f | --file)
			FILE="$2"
			shift
			;;
		-l | --lang)
			LANG="$2"
			shift
			;;
		*)
			echo "Unknown parameter passed: $1"
			usage
			;;
		esac
		shift
	done

	if [ -z "$FILE" ]; then
		echo "Error: File is required for transcription action."
		usage
	fi

	# Check if the file exists
	if [ ! -f "$FILE" ]; then
		echo "Error: File '$FILE' does not exist."
		exit 1
	fi

	# Ensure that curl is available
	if ! command -v curl &>/dev/null; then
		echo "curl is required for transcription but could not be found on your system. Please install it."
		exit 1
	fi

	# Transcribe the specified file
	echo "Transcribing file $FILE, be patient"
	curl "${host}/v1/audio/transcriptions" -H "Authorization: Bearer ${LLM_API_KEY}" \
		-F "file=@${FILE}" \
		-F "stream=true" \
		-F "language=${LANG}"
else
	usage
fi