base.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # -*- coding: utf-8 -*-
  2. from __future__ import division
  3. from ..__load__ import *
  4. class Base(object):
  5. def __init__(self, sdk = False):
  6. self.name = self.__class__.__name__
  7. self.name = self.name.lower()
  8. if sdk == True:
  9. self.sdk = Demeter.service(self.name, 'sdk')
  10. pass
  11. # Automatic Speech Recognition asr 自动语音识别
  12. @abstractmethod
  13. def asr(self, fp):
  14. pass
  15. # AI 聊天机器人
  16. @abstractmethod
  17. def chat(self, texts):
  18. pass
  19. # Text To Speech 人机对话 文本转语音
  20. @abstractmethod
  21. def tts(self, phrase):
  22. pass
  23. # Natural Language Understanding 自然语言理解
  24. @abstractmethod
  25. def nlu(self, query):
  26. pass
  27. # 设置工具
  28. @abstractmethod
  29. def setting(self, **args):
  30. pass
  31. def asrLog(self, state, msg = ''):
  32. if state == 1:
  33. method = 'info'
  34. text = '语音识别成功'
  35. else:
  36. method = 'critical'
  37. text = '语音识别失败'
  38. return self.log(method, text, msg)
  39. def chatLog(self, state, msg = ''):
  40. if state == 1:
  41. method = 'info'
  42. text = '回答'
  43. else:
  44. method = 'critical'
  45. text = '未做回答'
  46. return self.log(method, text, msg)
  47. def ttsLog(self, state, msg = ''):
  48. if state == 1:
  49. method = 'info'
  50. text = '语音合成成功,合成路径'
  51. else:
  52. method = 'critical'
  53. text = '语音合成失败'
  54. return self.log(method, text, msg)
  55. def log(self, method, text, msg = ''):
  56. logger = getattr(Demeter.logger, method)
  57. logger('{}-{}:{}'.format(self.name, text, msg))
  58. return msg;