|
| 1 | +import asyncio |
| 2 | +import json |
| 3 | +import base64 |
| 4 | +import hmac |
| 5 | +import hashlib |
| 6 | +import ssl |
| 7 | +import traceback |
| 8 | +from typing import Dict |
| 9 | +from urllib.parse import urlencode |
| 10 | +from datetime import datetime, timezone, UTC |
| 11 | +import websockets |
| 12 | +import os |
| 13 | + |
| 14 | +from future.backports.urllib.parse import urlparse |
| 15 | + |
| 16 | +from common.utils.logger import maxkb_logger |
| 17 | +from models_provider.base_model_provider import MaxKBBaseModel |
| 18 | +from models_provider.impl.base_stt import BaseSpeechToText |
| 19 | + |
| 20 | +ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 21 | +ssl_context.check_hostname = False |
| 22 | +ssl_context.verify_mode = ssl.CERT_NONE |
| 23 | + |
| 24 | + |
| 25 | +class XFZhEnSparkSpeechToText(MaxKBBaseModel, BaseSpeechToText): |
| 26 | + spark_app_id: str |
| 27 | + spark_api_key: str |
| 28 | + spark_api_secret: str |
| 29 | + spark_api_url: str |
| 30 | + |
| 31 | + def __init__(self, **kwargs): |
| 32 | + super().__init__(**kwargs) |
| 33 | + self.spark_api_url = kwargs.get('spark_api_url') |
| 34 | + self.spark_app_id = kwargs.get('spark_app_id') |
| 35 | + self.spark_api_key = kwargs.get('spark_api_key') |
| 36 | + self.spark_api_secret = kwargs.get('spark_api_secret') |
| 37 | + |
| 38 | + @staticmethod |
| 39 | + def is_cache_model(): |
| 40 | + return False |
| 41 | + |
| 42 | + @staticmethod |
| 43 | + def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs): |
| 44 | + optional_params = {} |
| 45 | + if 'max_tokens' in model_kwargs and model_kwargs['max_tokens'] is not None: |
| 46 | + optional_params['max_tokens'] = model_kwargs['max_tokens'] |
| 47 | + if 'temperature' in model_kwargs and model_kwargs['temperature'] is not None: |
| 48 | + optional_params['temperature'] = model_kwargs['temperature'] |
| 49 | + return XFZhEnSparkSpeechToText( |
| 50 | + spark_app_id=model_credential.get('spark_app_id'), |
| 51 | + spark_api_key=model_credential.get('spark_api_key'), |
| 52 | + spark_api_secret=model_credential.get('spark_api_secret'), |
| 53 | + spark_api_url=model_credential.get('spark_api_url'), |
| 54 | + **optional_params |
| 55 | + ) |
| 56 | + |
| 57 | + # 生成url |
| 58 | + def create_url(self): |
| 59 | + url = self.spark_api_url |
| 60 | + host = urlparse(url).hostname |
| 61 | + |
| 62 | + gmt_format = '%a, %d %b %Y %H:%M:%S GMT' |
| 63 | + date = datetime.now(UTC).strftime(gmt_format) |
| 64 | + # 拼接字符串 |
| 65 | + signature_origin = "host: " + host + "\n" |
| 66 | + signature_origin += "date: " + date + "\n" |
| 67 | + signature_origin += "GET " + "/v1 HTTP/1.1" |
| 68 | + # 进行hmac-sha256进行加密 |
| 69 | + signature_sha = hmac.new( |
| 70 | + self.spark_api_secret.encode('utf-8'), |
| 71 | + signature_origin.encode('utf-8'), |
| 72 | + hashlib.sha256 |
| 73 | + ).digest() |
| 74 | + signature = base64.b64encode(signature_sha).decode(encoding='utf-8') |
| 75 | + |
| 76 | + authorization_origin = ( |
| 77 | + f'api_key="{self.spark_api_key}", algorithm="hmac-sha256", ' |
| 78 | + f'headers="host date request-line", signature="{signature}"' |
| 79 | + ) |
| 80 | + authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8') |
| 81 | + |
| 82 | + params = { |
| 83 | + 'authorization': authorization, |
| 84 | + 'date': date, |
| 85 | + 'host': host |
| 86 | + } |
| 87 | + auth_url = url + '?' + urlencode(params) |
| 88 | + return auth_url |
| 89 | + |
| 90 | + def check_auth(self): |
| 91 | + cwd = os.path.dirname(os.path.abspath(__file__)) |
| 92 | + with open(f'{cwd}/iat_mp3_16k.mp3', 'rb') as f: |
| 93 | + self.speech_to_text(f) |
| 94 | + |
| 95 | + def speech_to_text(self, audio_file_path): |
| 96 | + async def handle(): |
| 97 | + async with websockets.connect(self.create_url(), max_size=1000000000, ssl=ssl_context) as ws: |
| 98 | + # print("连接成功") |
| 99 | + # 发送音频数据 |
| 100 | + await self.send_audio(ws, audio_file_path) |
| 101 | + # 接收识别结果 |
| 102 | + return await self.handle_message(ws) |
| 103 | + try: |
| 104 | + return asyncio.run(handle()) |
| 105 | + except Exception as err: |
| 106 | + maxkb_logger.error(f"语音识别错误: {str(err)}: {traceback.format_exc()}") |
| 107 | + return "" |
| 108 | + |
| 109 | + async def send_audio(self, ws, audio_file): |
| 110 | + """发送音频数据""" |
| 111 | + chunk_size = 4000 |
| 112 | + seq = 1 |
| 113 | + max_chunks = 10000 |
| 114 | + while True: |
| 115 | + chunk = audio_file.read(chunk_size) |
| 116 | + if not chunk or seq > max_chunks: |
| 117 | + break |
| 118 | + |
| 119 | + chunk_base64 = base64.b64encode(chunk).decode('utf-8') |
| 120 | + # 第一帧 |
| 121 | + if seq == 1: |
| 122 | + frame = { |
| 123 | + "header": {"app_id": self.spark_app_id, "status": 0}, |
| 124 | + "parameter": { |
| 125 | + "iat": { |
| 126 | + "domain": "slm", "language": "zh_cn", "accent": "mandarin", |
| 127 | + "eos": 10000, "vinfo": 1, |
| 128 | + "result": {"encoding": "utf8", "compress": "raw", "format": "json"} |
| 129 | + } |
| 130 | + }, |
| 131 | + "payload": { |
| 132 | + "audio": { |
| 133 | + "encoding": "lame", "sample_rate": 16000, "channels": 1, |
| 134 | + "bit_depth": 16, "seq": seq, "status": 0, "audio": chunk_base64 |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + # 中间帧 |
| 139 | + else: |
| 140 | + frame = { |
| 141 | + "header": {"app_id": self.spark_app_id, "status": 1}, |
| 142 | + "payload": { |
| 143 | + "audio": { |
| 144 | + "encoding": "lame", "sample_rate": 16000, "channels": 1, |
| 145 | + "bit_depth": 16, "seq": seq, "status": 1, "audio": chunk_base64 |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + await ws.send(json.dumps(frame)) |
| 151 | + seq += 1 |
| 152 | + |
| 153 | + # 发送结束帧 |
| 154 | + end_frame = { |
| 155 | + "header": {"app_id": self.spark_app_id, "status": 2}, |
| 156 | + "payload": { |
| 157 | + "audio": { |
| 158 | + "encoding": "lame", "sample_rate": 16000, "channels": 1, |
| 159 | + "bit_depth": 16, "seq": seq, "status": 2, "audio": "" |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + await ws.send(json.dumps(end_frame)) |
| 164 | + |
| 165 | + |
| 166 | +# 接受信息处理器 |
| 167 | + async def handle_message(self, ws): |
| 168 | + result_text = "" |
| 169 | + while True: |
| 170 | + try: |
| 171 | + message = await asyncio.wait_for(ws.recv(), timeout=30.0) |
| 172 | + data = json.loads(message) |
| 173 | + |
| 174 | + if data['header']['code'] != 0: |
| 175 | + raise Exception("") |
| 176 | + |
| 177 | + if 'payload' in data and 'result' in data['payload']: |
| 178 | + result = data['payload']['result'] |
| 179 | + text = result.get('text', '') |
| 180 | + if text: |
| 181 | + text_data = json.loads(base64.b64decode(text).decode('utf-8')) |
| 182 | + for ws_item in text_data.get('ws', []): |
| 183 | + for cw in ws_item.get('cw', []): |
| 184 | + for sw in cw.get('sw', []): |
| 185 | + result_text += sw['w'] |
| 186 | + |
| 187 | + if data['header'].get('status') == 2: |
| 188 | + break |
| 189 | + except asyncio.TimeoutError: |
| 190 | + break |
| 191 | + |
| 192 | + return result_text |
0 commit comments