cozeASR.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- coding: utf-8 -*-
  2. from ..builder import ASREngines
  3. from ..engineBase import BaseASREngine
  4. import io, base64
  5. from digitalHuman.protocol import AudioMessage, TextMessage, AUDIO_TYPE
  6. from digitalHuman.utils import logger, httpxAsyncClient, wavToMp3, checkResponse
  7. __all__ = ["CozeApiAsr"]
  8. @ASREngines.register("Coze")
  9. class CozeApiAsr(BaseASREngine):
  10. def setup(self):
  11. self.url = "https://api.coze.cn/v1/audio/transcriptions"
  12. async def run(self, input: AudioMessage, **kwargs) -> TextMessage:
  13. # 参数校验
  14. paramters = self.checkParameter(**kwargs)
  15. API_TOKEN = paramters["token"]
  16. headers = {
  17. 'Authorization': f'Bearer {API_TOKEN}'
  18. }
  19. files = {
  20. 'file': ('adh.mp3', input.data)
  21. }
  22. if isinstance(input.data, str):
  23. input.data = base64.b64decode(input.data)
  24. if input.type == AUDIO_TYPE.WAV:
  25. input.data = wavToMp3(input.data)
  26. input.type = AUDIO_TYPE.MP3
  27. response = await httpxAsyncClient.post(self.url, headers=headers, files=files)
  28. resp = checkResponse(response, "CozeApiAsr")
  29. result = resp["data"]["text"]
  30. logger.debug(f"[ASR] Engine response: {result}")
  31. message = TextMessage(data=result)
  32. return message