refactor: 讯飞接口校验发送demo验证
This commit is contained in:
parent
b0f443f3d2
commit
467b864895
Binary file not shown.
@ -8,6 +8,8 @@ import datetime
|
|||||||
import hashlib
|
import hashlib
|
||||||
import hmac
|
import hmac
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from urllib.parse import urlencode, urlparse
|
from urllib.parse import urlencode, urlparse
|
||||||
@ -25,6 +27,7 @@ ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
|||||||
ssl_context.check_hostname = False
|
ssl_context.check_hostname = False
|
||||||
ssl_context.verify_mode = ssl.CERT_NONE
|
ssl_context.verify_mode = ssl.CERT_NONE
|
||||||
|
|
||||||
|
max_kb = logging.getLogger("max_kb")
|
||||||
|
|
||||||
class XFSparkSpeechToText(MaxKBBaseModel, BaseSpeechToText):
|
class XFSparkSpeechToText(MaxKBBaseModel, BaseSpeechToText):
|
||||||
spark_app_id: str
|
spark_app_id: str
|
||||||
@ -89,11 +92,9 @@ class XFSparkSpeechToText(MaxKBBaseModel, BaseSpeechToText):
|
|||||||
return url
|
return url
|
||||||
|
|
||||||
def check_auth(self):
|
def check_auth(self):
|
||||||
async def check():
|
cwd = os.path.dirname(os.path.abspath(__file__))
|
||||||
async with websockets.connect(self.create_url(), ssl=ssl_context) as ws:
|
with open(f'{cwd}/iat_mp3_16k.mp3', 'rb') as f:
|
||||||
pass
|
self.speech_to_text(f)
|
||||||
|
|
||||||
asyncio.run(check())
|
|
||||||
|
|
||||||
def speech_to_text(self, file):
|
def speech_to_text(self, file):
|
||||||
async def handle():
|
async def handle():
|
||||||
@ -112,8 +113,7 @@ class XFSparkSpeechToText(MaxKBBaseModel, BaseSpeechToText):
|
|||||||
sid = message["sid"]
|
sid = message["sid"]
|
||||||
if code != 0:
|
if code != 0:
|
||||||
errMsg = message["message"]
|
errMsg = message["message"]
|
||||||
print("sid:%s call error:%s code is:%s" % (sid, errMsg, code))
|
raise Exception(f"sid: {sid} call error: {errMsg} code is: {code}")
|
||||||
return errMsg
|
|
||||||
else:
|
else:
|
||||||
data = message["data"]["result"]["ws"]
|
data = message["data"]["result"]["ws"]
|
||||||
result = ""
|
result = ""
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import datetime
|
|||||||
import hashlib
|
import hashlib
|
||||||
import hmac
|
import hmac
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
@ -20,6 +21,8 @@ import websockets
|
|||||||
from setting.models_provider.base_model_provider import MaxKBBaseModel
|
from setting.models_provider.base_model_provider import MaxKBBaseModel
|
||||||
from setting.models_provider.impl.base_tts import BaseTextToSpeech
|
from setting.models_provider.impl.base_tts import BaseTextToSpeech
|
||||||
|
|
||||||
|
max_kb = logging.getLogger("max_kb")
|
||||||
|
|
||||||
STATUS_FIRST_FRAME = 0 # 第一帧的标识
|
STATUS_FIRST_FRAME = 0 # 第一帧的标识
|
||||||
STATUS_CONTINUE_FRAME = 1 # 中间帧标识
|
STATUS_CONTINUE_FRAME = 1 # 中间帧标识
|
||||||
STATUS_LAST_FRAME = 2 # 最后一帧的标识
|
STATUS_LAST_FRAME = 2 # 最后一帧的标识
|
||||||
@ -92,11 +95,7 @@ class XFSparkTextToSpeech(MaxKBBaseModel, BaseTextToSpeech):
|
|||||||
return url
|
return url
|
||||||
|
|
||||||
def check_auth(self):
|
def check_auth(self):
|
||||||
async def check():
|
self.text_to_speech("你好")
|
||||||
async with websockets.connect(self.create_url(), max_size=1000000000, ssl=ssl_context) as ws:
|
|
||||||
pass
|
|
||||||
|
|
||||||
asyncio.run(check())
|
|
||||||
|
|
||||||
def text_to_speech(self, text):
|
def text_to_speech(self, text):
|
||||||
|
|
||||||
@ -119,13 +118,13 @@ class XFSparkTextToSpeech(MaxKBBaseModel, BaseTextToSpeech):
|
|||||||
# print(message)
|
# print(message)
|
||||||
code = message["code"]
|
code = message["code"]
|
||||||
sid = message["sid"]
|
sid = message["sid"]
|
||||||
audio = message["data"]["audio"]
|
|
||||||
audio = base64.b64decode(audio)
|
|
||||||
|
|
||||||
if code != 0:
|
if code != 0:
|
||||||
errMsg = message["message"]
|
errMsg = message["message"]
|
||||||
print("sid:%s call error:%s code is:%s" % (sid, errMsg, code))
|
raise Exception(f"sid: {sid} call error: {errMsg} code is: {code}")
|
||||||
else:
|
else:
|
||||||
|
audio = message["data"]["audio"]
|
||||||
|
audio = base64.b64decode(audio)
|
||||||
audio_bytes += audio
|
audio_bytes += audio
|
||||||
# 退出
|
# 退出
|
||||||
if message["data"]["status"] == 2:
|
if message["data"]["status"] == 2:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user