Files
annescribe/transcription/transcription.py
T
2024-12-29 21:27:11 +01:00

58 lines
1.3 KiB
Python

import whisper
# clojure for model
def model_loader() -> callable:
"""
Clojure for loading the model
:return: function that loads the model
"""
model = None
def load_model(config: dict):
"""
Load the model
:return: model
"""
nonlocal model
if model is not None:
return model
else:
model_type = config['model']['type']
model = whisper.load_model(model_type)
return model
return load_model
load_model = model_loader()
def transcribe_audio(audio_file_path: str,
output_file_path: str,
config: dict) -> str:
"""
Transcribe an audio file with openai whisper
:param audio_file_path: filepath of the audio file
:param output_file_path: filepath of the output file
:param config: app config
:return: transcribed text
"""
# load the model
model = load_model(config)
# move model to desired device
desired_device = config['model']['device']
model.to(desired_device)
result = model.transcribe(audio_file_path)
result_text = result['text']
# write the result to a text file
with open(output_file_path, 'w') as result_file:
result_file.write(result["text"])
return result_text