megaMUR_LG/chatbot/chat/speech_recognition.py

26 lines
799 B
Python
Raw Normal View History

import os
import json
import openai
def transcribe(dirname, openai_key, file = "recorded.wav"):
print("###################")
openai.api_key = openai_key
fname = os.path.join(dirname, file)
audio_file = open(fname, "rb")
print(f"Transcribing audio via OpenAI Whisper ...")
transcript = openai.Audio.transcribe("whisper-1", audio_file)
recognized_text = transcript.text
print(f"Recognized text: \n > {recognized_text}")
return recognized_text
if __name__ == "__main__":
dirname = os.path.dirname(__file__)
with open(os.path.join(dirname, "config.json")) as config_file:
config = json.load(config_file)
openai_key=config['OPENAI_KEY']
transcribed = transcribe(dirname, openai_key, file="test.m4a")
print(transcribed)