34 lines
894B

  1. """
  2. google voice service
  3. """
  4. import json
  5. import openai
  6. from bridge.reply import Reply, ReplyType
  7. from common.log import logger
  8. from config import conf
  9. from voice.voice import Voice
  10. class OpenaiVoice(Voice):
  11. def __init__(self):
  12. openai.api_key = conf().get("open_ai_api_key")
  13. def voiceToText(self, voice_file):
  14. logger.debug("[Openai] voice file name={}".format(voice_file))
  15. try:
  16. file = open(voice_file, "rb")
  17. result = openai.Audio.transcribe("whisper-1", file)
  18. text = result["text"]
  19. reply = Reply(ReplyType.TEXT, text)
  20. logger.info(
  21. "[Openai] voiceToText text={} voice file name={}".format(
  22. text, voice_file
  23. )
  24. )
  25. except Exception as e:
  26. reply = Reply(ReplyType.ERROR, str(e))
  27. finally:
  28. return reply