Skip to content

Commit 4849875

Browse files
contractorwolfiThalay
authored andcommitted
examples : add python example for transcription (ggml-org#1744)
* rebase and add simple python interface * moved python files to examples/python
1 parent cfa9539 commit 4849875

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import whisper_processor
2+
3+
try:
4+
result = whisper_processor.process_audio("./audio/wake_word_detected16k.wav", "base.en")
5+
print(result)
6+
except Exception as e:
7+
print(f"Error: {e}")

examples/python/whisper_processor.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import subprocess
2+
import sys
3+
import os
4+
5+
def process_audio(wav_file, model_name="base.en"):
6+
"""
7+
Processes an audio file using a specified model and returns the processed string.
8+
9+
:param wav_file: Path to the WAV file
10+
:param model_name: Name of the model to use
11+
:return: Processed string output from the audio processing
12+
:raises: Exception if an error occurs during processing
13+
"""
14+
15+
model = f"./models/ggml-{model_name}.bin"
16+
17+
# Check if the file exists
18+
if not os.path.exists(model):
19+
raise FileNotFoundError(f"Model file not found: {model} \n\nDownload a model with this command:\n\n> bash ./models/download-ggml-model.sh {model_name}\n\n")
20+
21+
if not os.path.exists(wav_file):
22+
raise FileNotFoundError(f"WAV file not found: {wav_file}")
23+
24+
full_command = f"./main -m {model} -f {wav_file} -np -nt"
25+
26+
# Execute the command
27+
process = subprocess.Popen(full_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
28+
29+
# Get the output and error (if any)
30+
output, error = process.communicate()
31+
32+
if error:
33+
raise Exception(f"Error processing audio: {error.decode('utf-8')}")
34+
35+
# Process and return the output string
36+
decoded_str = output.decode('utf-8').strip()
37+
processed_str = decoded_str.replace('[BLANK_AUDIO]', '').strip()
38+
39+
return processed_str
40+
41+
def main():
42+
if len(sys.argv) >= 2:
43+
wav_file = sys.argv[1]
44+
model_name = sys.argv[2] if len(sys.argv) == 3 else "base.en"
45+
try:
46+
result = process_audio(wav_file, model_name)
47+
print(result)
48+
except Exception as e:
49+
print(f"Error: {e}")
50+
else:
51+
print("Usage: python whisper_processor.py <wav_file> [<model_name>]")
52+
53+
if __name__ == "__main__":
54+
main()

0 commit comments

Comments
 (0)