]> git.djapps.eu Git - pkg/ggml/sources/whisper.cpp/commitdiff
examples : add python example for transcription (#1744)
authorjames wolf <redacted>
Sat, 13 Jan 2024 17:37:18 +0000 (12:37 -0500)
committerGitHub <redacted>
Sat, 13 Jan 2024 17:37:18 +0000 (19:37 +0200)
* rebase and add simple python interface

* moved python files to examples/python

examples/python/test_whisper_processor.py [new file with mode: 0644]
examples/python/whisper_processor.py [new file with mode: 0644]

diff --git a/examples/python/test_whisper_processor.py b/examples/python/test_whisper_processor.py
new file mode 100644 (file)
index 0000000..3ea6a0a
--- /dev/null
@@ -0,0 +1,7 @@
+import whisper_processor
+
+try:
+    result = whisper_processor.process_audio("./audio/wake_word_detected16k.wav", "base.en")
+    print(result)
+except Exception as e:
+    print(f"Error: {e}")
\ No newline at end of file
diff --git a/examples/python/whisper_processor.py b/examples/python/whisper_processor.py
new file mode 100644 (file)
index 0000000..3e84e58
--- /dev/null
@@ -0,0 +1,54 @@
+import subprocess
+import sys
+import os
+
+def process_audio(wav_file, model_name="base.en"):
+    """
+    Processes an audio file using a specified model and returns the processed string.
+
+    :param wav_file: Path to the WAV file
+    :param model_name: Name of the model to use
+    :return: Processed string output from the audio processing
+    :raises: Exception if an error occurs during processing
+    """
+
+    model = f"./models/ggml-{model_name}.bin"
+
+    # Check if the file exists
+    if not os.path.exists(model):
+        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")
+
+    if not os.path.exists(wav_file):
+        raise FileNotFoundError(f"WAV file not found: {wav_file}")
+
+    full_command = f"./main -m {model} -f {wav_file} -np -nt"
+
+    # Execute the command
+    process = subprocess.Popen(full_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+
+    # Get the output and error (if any)
+    output, error = process.communicate()
+
+    if error:
+        raise Exception(f"Error processing audio: {error.decode('utf-8')}")
+
+    # Process and return the output string
+    decoded_str = output.decode('utf-8').strip()
+    processed_str = decoded_str.replace('[BLANK_AUDIO]', '').strip()
+
+    return processed_str
+
+def main():
+    if len(sys.argv) >= 2:
+        wav_file = sys.argv[1]
+        model_name = sys.argv[2] if len(sys.argv) == 3 else "base.en"
+        try:
+            result = process_audio(wav_file, model_name)
+            print(result)
+        except Exception as e:
+            print(f"Error: {e}")
+    else:
+        print("Usage: python whisper_processor.py <wav_file> [<model_name>]")
+
+if __name__ == "__main__":
+    main()