openai_voice.py 826B

il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
1234567891011121314151617181920212223242526272829
  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("[Openai] voiceToText text={} voice file name={}".format(text, voice_file))
  21. except Exception as e:
  22. reply = Reply(ReplyType.ERROR, str(e))
  23. finally:
  24. return reply