Upload whisper_processor.py with huggingface_hub
Browse files- whisper_processor.py +67 -0
whisper_processor.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import subprocess
|
2 |
+
import sys
|
3 |
+
import os
|
4 |
+
import time
|
5 |
+
|
6 |
+
def process_audio(wav_file, model_name="base.en"):
|
7 |
+
"""
|
8 |
+
Processes an audio file using a specified model and returns the processed string.
|
9 |
+
|
10 |
+
:param wav_file: Path to the WAV file
|
11 |
+
:param model_name: Name of the model to use
|
12 |
+
:return: Processed string output from the audio processing
|
13 |
+
:raises: Exception if an error occurs during processing
|
14 |
+
"""
|
15 |
+
|
16 |
+
# model = f"./models/ggml-{model_name}.bin"
|
17 |
+
model = f"{model_name}.bin"
|
18 |
+
|
19 |
+
# Check if the file exists
|
20 |
+
if not os.path.exists(model):
|
21 |
+
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")
|
22 |
+
|
23 |
+
if not os.path.exists(wav_file):
|
24 |
+
raise FileNotFoundError(f"WAV file not found: {wav_file}")
|
25 |
+
|
26 |
+
full_command = f"./main -m {model} -f {wav_file} -np -nt"
|
27 |
+
|
28 |
+
start_time = time.time()
|
29 |
+
|
30 |
+
# Execute the command
|
31 |
+
process = subprocess.Popen(full_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
32 |
+
|
33 |
+
|
34 |
+
# Get the output and error (if any)
|
35 |
+
output, error = process.communicate()
|
36 |
+
|
37 |
+
end_time = time.time()
|
38 |
+
|
39 |
+
# Calculate the duration
|
40 |
+
duration = end_time - start_time
|
41 |
+
print(f"Time taken for CPP request: {duration:.2f} seconds")
|
42 |
+
|
43 |
+
if error:
|
44 |
+
raise Exception(f"Error processing audio: {error.decode('utf-8')}")
|
45 |
+
|
46 |
+
# Process and return the output string
|
47 |
+
decoded_str = output.decode('utf-8').strip()
|
48 |
+
processed_str = decoded_str.replace('[BLANK_AUDIO]', '').strip()
|
49 |
+
|
50 |
+
return processed_str
|
51 |
+
|
52 |
+
def main():
|
53 |
+
# if len(sys.argv) >= 2:
|
54 |
+
wav_file = sys.argv[1]
|
55 |
+
model_name = sys.argv[2] if len(sys.argv) == 3 else "base.en"
|
56 |
+
print("wav_file: ", wav_file)
|
57 |
+
print("model_name: ", model_name)
|
58 |
+
# try:
|
59 |
+
result = process_audio(wav_file, model_name)
|
60 |
+
print(result)
|
61 |
+
# except Exception as e:
|
62 |
+
# print(f"Error: {e}")
|
63 |
+
# else:
|
64 |
+
# print("Usage: python whisper_processor.py <wav_file> [<model_name>]")
|
65 |
+
|
66 |
+
if __name__ == "__main__":
|
67 |
+
main()
|