Files

1 line
22 KiB
JSON
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{"repo": "asd123pwj/asdtools", "files": {"asdTools/Tools/API/XFYunAPI_TTS.py": {"f5e531b5d94d5c4e308555c1099de4dd165c047b": {"t": 1785674326.991185, "n": 5689, "h": "d6b578c5a4e18806", "c": "# 由讯飞云提供的DEMO:https://www.xfyun.cn/doc/tts/online_tts/API.html#%E8%B0%83%E7%94%A8%E7%A4%BA%E4%BE%8B\n# -*- coding:utf-8 -*-\n#\n# author: iflytek\n#\n# 本demo测试时运行的环境为:Windows + Python3.7\n# 本demo测试成功运行时所安装的第三方库及其版本如下:\n# cffi==1.12.3\n# gevent==1.4.0\n# greenlet==0.4.15\n# pycparser==2.19\n# six==1.12.0\n# websocket==0.2.1\n# websocket-client==0.56.0\n# 合成小语种需要传输小语种文本、使用小语种发音人vcn、tte=unicode以及修改文本编码方式\n# 错误码链接:https://www.xfyun.cn/document/error-code (code返回错误码时必看)\n# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #\nimport websocket\nimport datetime\nimport hashlib\nimport base64\nimport hmac\nimport json\nfrom urllib.parse import urlencode\nimport time\nimport ssl\nfrom wsgiref.handlers import format_date_time\nfrom datetime import datetime\nfrom time import mktime\nimport _thread as thread\nimport os\nfrom asdTools.Secret.key import XFYunAPIKeyTTS, XFYunAPISecretTTS, XFYunAPPID\nimport pyaudio\nfrom functools import partial\n\n\nSTATUS_FIRST_FRAME = 0 # 第一帧的标识\nSTATUS_CONTINUE_FRAME = 1 # 中间帧标识\nSTATUS_LAST_FRAME = 2 # 最后一帧的标识\n\n\nclass Ws_Param(object):\n # 初始化\n def __init__(self, APPID, APIKey, APISecret, Text):\n self.APPID = APPID\n self.APIKey = APIKey\n self.APISecret = APISecret\n self.Text = Text\n\n # 公共参数(common)\n self.CommonArgs = {\"app_id\": self.APPID}\n # 业务参数(business),更多个性化参数可在官网查看\n self.BusinessArgs = {\"aue\": \"raw\", \"auf\": \"audio/L16;rate=16000\", \"vcn\": \"xiaoyan\", \"tte\": \"utf8\"}\n self.Data = {\"status\": 2, \"text\": str(base64.b64encode(self.Text.encode('utf-8')), \"UTF8\")}\n #使用小语种须使用以下方式,此处的unicode指的是 utf16小端的编码方式,即\"UTF-16LE\"”\n #self.Data = {\"status\": 2, \"text\": str(base64.b64encode(self.Text.encode('utf-16')), \"UTF8\")}\n\n # 生成url\n def create_url(self):\n url = 'wss://tts-api.xfyun.cn/v2/tts'\n # 生成RFC1123格式的时间戳\n now = datetime.now()\n date = format_date_time(mktime(now.timetuple()))\n\n # 拼接字符串\n signature_origin = \"host: \" + \"ws-api.xfyun.cn\" + \"\\n\"\n signature_origin += \"date: \" + date + \"\\n\"\n signature_origin += \"GET \" + \"/v2/tts \" + \"HTTP/1.1\"\n # 进行hmac-sha256进行加密\n signature_sha = hmac.new(self.APISecret.encode('utf-8'), signature_origin.encode('utf-8'),\n digestmod=hashlib.sha256).digest()\n signature_sha = base64.b64encode(signature_sha).decode(encoding='utf-8')\n\n authorization_origin = \"api_key=\\\"%s\\\", algorithm=\\\"%s\\\", headers=\\\"%s\\\", signature=\\\"%s\\\"\" % (\n self.APIKey, \"hmac-sha256\", \"host date request-line\", signature_sha)\n authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')\n # 将请求的鉴权参数组合为字典\n v = {\n \"authorization\": authorization,\n \"date\": date,\n \"host\": \"ws-api.xfyun.cn\"\n }\n # 拼接鉴权参数,生成url\n url = url + '?' + urlencode(v)\n # print(\"date: \",date)\n # print(\"v: \",v)\n # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致\n # print('websocket url :', url)\n return url\n\ndef on_message(ws, message):\n try:\n message =json.loads(message)\n code = message[\"code\"]\n sid = message[\"sid\"]\n audio = message[\"data\"][\"audio\"]\n audio = base64.b64decode(audio)\n status = message[\"data\"][\"status\"]\n # print(\"----\\n\\n\")\n # print(message)\n if status == 2:\n print(\"ws is closed\")\n ws.close()\n if code != 0:\n errMsg = message[\"message\"]\n print(\"sid:%s call error:%s code is:%s\" % (sid, errMsg, code))\n else:\n\n # with open('./demo.pcm', 'ab') as f:\n # f.write(audio)\n play_audio(audio)\n\n except Exception as e:\n print(\"receive msg,but parse exception:\", e)\n\n# 收到websocket错误的处理\ndef on_error(ws, error):\n print(\"### error:\", error)\n\n\n# 收到websocket关闭的处理\ndef on_close(ws, *args):\n print(\"### closed ###\")\n\n\n# 收到websocket连接建立的处理\ndef on_open(ws, wsParam=None):\n def run(*args):\n d = {\"common\": wsParam.CommonArgs,\n \"business\": wsParam.BusinessArgs,\n \"data\": wsParam.Data,\n }\n d = json.dumps(d)\n # print(\"------>开始发送文本数据\")\n ws.send(d)\n if os.path.exists('./demo.pcm'):\n os.remove('./demo.pcm')\n\n thread.start_new_thread(run, ())\n\ndef play_audio(audio_data):\n p = pyaudio.PyAudio()\n stream = p.open(format=pyaudio.paInt16,\n channels=1,\n rate=16000,\n output=True)\n\n # 播放音频数据\n stream.write(audio_data)\n\n stream.stop_stream()\n stream.close()\n p.terminate()\n\ndef convert_text_to_audio(Text):\n wsParam = Ws_Param(APPID=XFYunAPPID, APISecret=XFYunAPISecretTTS,\n APIKey=XFYunAPIKeyTTS,\n Text=Text)\n websocket.enableTrace(False)\n wsUrl = wsParam.create_url()\n ws = websocket.WebSocketApp(wsUrl, on_message=on_message, on_error=on_error, on_close=on_close)\n ws.on_open = lambda ws: on_open(ws, wsParam)\n ws.run_forever(sslopt={\"cert_reqs\": ssl.CERT_NONE})\n\nif __name__ == \"__main__\":\n # 测试时候在此处正确填写相关信息即可运行\n wsParam = Ws_Param(APPID=XFYunAPPID, APISecret=XFYunAPISecretTTS,\n APIKey=XFYunAPIKeyTTS,\n Text=\"这是一个语音合成示例\")\n websocket.enableTrace(False)\n wsUrl = wsParam.create_url()\n ws = websocket.WebSocketApp(wsUrl, on_message=on_message, on_error=on_error, on_close=on_close)\n ws.on_open = partial(on_open, wsParam)\n ws.run_forever(sslopt={\"cert_reqs\": ssl.CERT_NONE})\n"}}, "asdTools/Tools/API/XFYunAPI_Spark_Stream.py": {"f5e531b5d94d5c4e308555c1099de4dd165c047b": {"t": 1785674327.547385, "n": 4561, "h": "9cec30a590441e79", "c": "import _thread as thread\nimport base64\nimport datetime\nimport hashlib\nimport hmac\nimport json\nfrom urllib.parse import urlparse\nimport ssl\nfrom datetime import datetime\nfrom time import mktime\nfrom urllib.parse import urlencode\nfrom wsgiref.handlers import format_date_time\nimport websocket\nfrom asdTools.Secret.key import XFYunAPIKeySpark, XFYunAPISecretSpark, XFYunAPPID\n\nclass Ws_Param(object):\n # 初始化\n def __init__(self):\n self.APPID = XFYunAPPID\n self.APIKey = XFYunAPIKeySpark\n self.APISecret = XFYunAPISecretSpark\n self.gpt_url = \"ws://spark-api.xf-yun.com/v1.1/chat\"\n self.host = urlparse(self.gpt_url).netloc\n self.path = urlparse(self.gpt_url).path\n\n # 生成url\n def create_url(self):\n # 生成RFC1123格式的时间戳\n now = datetime.now()\n date = format_date_time(mktime(now.timetuple()))\n\n # 拼接字符串\n signature_origin = \"host: \" + self.host + \"\\n\"\n signature_origin += \"date: \" + date + \"\\n\"\n signature_origin += \"GET \" + self.path + \" HTTP/1.1\"\n\n # 进行hmac-sha256进行加密\n signature_sha = hmac.new(self.APISecret.encode('utf-8'), signature_origin.encode('utf-8'),\n digestmod=hashlib.sha256).digest()\n\n signature_sha_base64 = base64.b64encode(signature_sha).decode(encoding='utf-8')\n\n authorization_origin = f'api_key=\"{self.APIKey}\", algorithm=\"hmac-sha256\", headers=\"host date request-line\", signature=\"{signature_sha_base64}\"'\n\n authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')\n\n # 将请求的鉴权参数组合为字典\n v = {\n \"authorization\": authorization,\n \"date\": date,\n \"host\": self.host\n }\n # 拼接鉴权参数,生成url\n url = self.gpt_url + '?' + urlencode(v)\n # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致\n return url\n\n\n# 收到websocket错误的处理\ndef on_error(ws, error, *args):\n print(\"### error:\", error)\n\n\n# 收到websocket关闭的处理\ndef on_close(ws):\n pass\n # print(\"### closed ###\")\n\n\n# 收到websocket连接建立的处理\ndef on_open(ws):\n thread.start_new_thread(run, (ws,))\n\n\ndef run(ws, *args):\n data = json.dumps(gen_params(\n appid=ws.appid, \n question=ws.question, \n history=ws.history,\n max_length=ws.max_length,\n temperature=ws.temperature))\n ws.send(data)\n\n\n# 收到websocket消息的处理\ndef on_message(ws, message):\n # print(message)\n data = json.loads(message)\n code = data['header']['code']\n if code != 0:\n print(f'请求错误: {code}, {data}')\n ws.close()\n else:\n choices = data[\"payload\"][\"choices\"]\n status = choices[\"status\"]\n content = choices[\"text\"][0][\"content\"]\n # print(content, end='')\n ws.response += content\n if status == 2:\n ws.history.append([ws.question, ws.response])\n ws.result = ws.response\n ws.response = \"\"\n ws.close()\n\n\ndef gen_params(appid, question, history=None, max_length=2048, temperature=0.01):\n \"\"\"\n 通过appid和用户的提问来生成请参数\n \"\"\"\n data = {\n \"header\": {\n \"app_id\": appid,\n \"uid\": \"1234\"\n },\n \"parameter\": {\n \"chat\": {\n \"domain\": \"general\",\n \"random_threshold\": 0.5,\n \"max_tokens\": max_length,\n \"auditing\": \"default\",\n \"temperature\": temperature,\n }\n },\n \"payload\": {\n \"message\": {\n \"text\": []\n }\n }\n }\n for his in history:\n data[\"payload\"][\"message\"][\"text\"].append({\"role\": \"user\", \"content\": his[0]})\n data[\"payload\"][\"message\"][\"text\"].append({\"role\": \"assistant\", \"content\": his[1]})\n data[\"payload\"][\"message\"][\"text\"].append({\"role\": \"user\", \"content\": question})\n return data\n\n\ndef call_spark(question, history=[], max_length=2048, temperture=0.01):\n wsParam = Ws_Param()\n websocket.enableTrace(False)\n wsUrl = wsParam.create_url()\n ws = websocket.WebSocketApp(wsUrl, on_message=on_message, on_error=on_error, on_close=on_close, on_open=on_open)\n ws.appid = XFYunAPPID\n ws.history = history\n ws.max_length = max_length\n ws.temperture = temperture\n ws.response = \"\"\n ws.question = question\n ws.run_forever(sslopt={\"cert_reqs\": ssl.CERT_NONE})\n return ws.result, ws.history\n\n\nif __name__ == \"__main__\":\n # 测试时候在此处正确填写相关信息即可运行\n result, history = call_spark(\"请回答‘你好’\")\n result, history = call_spark(\"请回答'我在'\", history)\n print(result)"}}, "asdTools/Tools/API/XFYunAPI_Spark.py": {"f5e531b5d94d5c4e308555c1099de4dd165c047b": {"t": 1785674328.6901126, "n": 4564, "h": "a65f3a13ef3f6d5d", "c": "import _thread as thread\nimport base64\nimport datetime\nimport hashlib\nimport hmac\nimport json\nfrom urllib.parse import urlparse\nimport ssl\nfrom datetime import datetime\nfrom time import mktime\nfrom urllib.parse import urlencode\nfrom wsgiref.handlers import format_date_time\nimport websocket\nfrom asdTools.Secret.key import XFYunAPIKeySpark, XFYunAPISecretSpark, XFYunAPPID\n\nclass Ws_Param(object):\n # 初始化\n def __init__(self):\n self.APPID = XFYunAPPID\n self.APIKey = XFYunAPIKeySpark\n self.APISecret = XFYunAPISecretSpark\n self.gpt_url = \"ws://spark-api.xf-yun.com/v1.1/chat\"\n self.host = urlparse(self.gpt_url).netloc\n self.path = urlparse(self.gpt_url).path\n\n # 生成url\n def create_url(self):\n # 生成RFC1123格式的时间戳\n now = datetime.now()\n date = format_date_time(mktime(now.timetuple()))\n\n # 拼接字符串\n signature_origin = \"host: \" + self.host + \"\\n\"\n signature_origin += \"date: \" + date + \"\\n\"\n signature_origin += \"GET \" + self.path + \" HTTP/1.1\"\n\n # 进行hmac-sha256进行加密\n signature_sha = hmac.new(self.APISecret.encode('utf-8'), signature_origin.encode('utf-8'),\n digestmod=hashlib.sha256).digest()\n\n signature_sha_base64 = base64.b64encode(signature_sha).decode(encoding='utf-8')\n\n authorization_origin = f'api_key=\"{self.APIKey}\", algorithm=\"hmac-sha256\", headers=\"host date request-line\", signature=\"{signature_sha_base64}\"'\n\n authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')\n\n # 将请求的鉴权参数组合为字典\n v = {\n \"authorization\": authorization,\n \"date\": date,\n \"host\": self.host\n }\n # 拼接鉴权参数,生成url\n url = self.gpt_url + '?' + urlencode(v)\n # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致\n return url\n\n\n# 收到websocket错误的处理\ndef on_error(ws, error, *args):\n print(\"### error:\", error)\n\n\n# 收到websocket关闭的处理\ndef on_close(ws):\n pass\n # print(\"### closed ###\")\n\n\n# 收到websocket连接建立的处理\ndef on_open(ws):\n thread.start_new_thread(run, (ws,))\n\n\ndef run(ws, *args):\n data = json.dumps(gen_params(\n appid=ws.appid, \n question=ws.question, \n history=ws.history,\n max_length=ws.max_length,\n temperature=ws.temperature))\n ws.send(data)\n\n\n# 收到websocket消息的处理\ndef on_message(ws, message):\n # print(message)\n data = json.loads(message)\n code = data['header']['code']\n if code != 0:\n print(f'请求错误: {code}, {data}')\n ws.close()\n else:\n choices = data[\"payload\"][\"choices\"]\n status = choices[\"status\"]\n content = choices[\"text\"][0][\"content\"]\n # print(content, end='')\n ws.response += content\n if status == 2:\n ws.history.append([ws.question, ws.response])\n ws.result = ws.response\n ws.response = \"\"\n ws.close()\n\n\ndef gen_params(appid, question, history=None, max_length=2048, temperature=0.01):\n \"\"\"\n 通过appid和用户的提问来生成请参数\n \"\"\"\n data = {\n \"header\": {\n \"app_id\": appid,\n \"uid\": \"1234\"\n },\n \"parameter\": {\n \"chat\": {\n \"domain\": \"general\",\n \"random_threshold\": 0.5,\n \"max_tokens\": max_length,\n \"auditing\": \"default\",\n \"temperature\": temperature,\n }\n },\n \"payload\": {\n \"message\": {\n \"text\": []\n }\n }\n }\n for his in history:\n data[\"payload\"][\"message\"][\"text\"].append({\"role\": \"user\", \"content\": his[0]})\n data[\"payload\"][\"message\"][\"text\"].append({\"role\": \"assistant\", \"content\": his[1]})\n data[\"payload\"][\"message\"][\"text\"].append({\"role\": \"user\", \"content\": question})\n return data\n\n\ndef call_spark(question, history=[], max_length=2048, temperature=0.01):\n wsParam = Ws_Param()\n websocket.enableTrace(False)\n wsUrl = wsParam.create_url()\n ws = websocket.WebSocketApp(wsUrl, on_message=on_message, on_error=on_error, on_close=on_close, on_open=on_open)\n ws.appid = XFYunAPPID\n ws.history = history\n ws.max_length = max_length\n ws.temperature = temperature\n ws.response = \"\"\n ws.question = question\n ws.run_forever(sslopt={\"cert_reqs\": ssl.CERT_NONE})\n return ws.result, ws.history\n\n\nif __name__ == \"__main__\":\n # 测试时候在此处正确填写相关信息即可运行\n result, history = call_spark(\"请回答‘你好’\")\n result, history = call_spark(\"请回答'我在'\", history)\n print(result)"}}, "asdTools/Tools/API/XFYunAPI_ASR.py": {"f5e531b5d94d5c4e308555c1099de4dd165c047b": {"t": 1785674328.9759414, "n": 4359, "h": "9469ef70e29d604c", "c": "# -*- coding: utf-8 -*-\nfrom asdTools.Secret.key import XFYunAPPID, XFYunAPISecretASR\nfrom asdTools.Classes.Base.BaseModel import BaseModel\nimport requests\nimport hashlib\nimport urllib\nimport base64\nimport hmac\nimport json\nimport time\nimport os\n\nlfasr_host = 'https://raasr.xfyun.cn/v2/api'\n# 请求的接口名\napi_upload = '/upload'\napi_get_result = '/getResult'\n\n\nclass RequestApi(object):\n def __init__(self, appid, secret_key, upload_file_path):\n self.appid = appid\n self.secret_key = secret_key\n self.upload_file_path = upload_file_path\n self.ts = str(int(time.time()))\n self.signa = self.get_signa()\n\n def get_signa(self):\n appid = self.appid\n secret_key = self.secret_key\n m2 = hashlib.md5()\n m2.update((appid + self.ts).encode('utf-8'))\n md5 = m2.hexdigest()\n md5 = bytes(md5, encoding='utf-8')\n # 以secret_key为key, 上面的md5为msg, 使用hashlib.sha1加密结果为signa\n signa = hmac.new(secret_key.encode('utf-8'), md5, hashlib.sha1).digest()\n signa = base64.b64encode(signa)\n signa = str(signa, 'utf-8')\n return signa\n\n\n def upload(self):\n print(\"上传部分:\")\n upload_file_path = self.upload_file_path\n file_len = os.path.getsize(upload_file_path)\n file_name = os.path.basename(upload_file_path)\n\n param_dict = {}\n param_dict['appId'] = self.appid\n param_dict['signa'] = self.signa\n param_dict['ts'] = self.ts\n param_dict[\"fileSize\"] = file_len\n param_dict[\"fileName\"] = file_name\n param_dict[\"duration\"] = \"200\"\n # print(\"upload参数:\", param_dict)\n data = open(upload_file_path, 'rb').read(file_len)\n\n response = requests.post(url =lfasr_host + api_upload+\"?\"+urllib.parse.urlencode(param_dict),\n headers = {\"Content-type\":\"application/json\"},data=data)\n # print(\"upload_url:\",response.request.url)\n result = json.loads(response.text)\n # print(\"upload resp:\", result)\n return result\n\n\n def get_result(self):\n uploadresp = self.upload()\n orderId = uploadresp['content']['orderId']\n param_dict = {}\n param_dict['appId'] = self.appid\n param_dict['signa'] = self.signa\n param_dict['ts'] = self.ts\n param_dict['orderId'] = orderId\n param_dict['resultType'] = \"transfer,predict\"\n # print(\"\")\n # print(\"查询部分:\")\n # print(\"get result参数:\", param_dict)\n status = 3\n # 建议使用回调的方式查询结果,查询接口有请求频率限制\n while status == 3:\n response = requests.post(url=lfasr_host + api_get_result + \"?\" + urllib.parse.urlencode(param_dict),\n headers={\"Content-type\": \"application/json\"})\n # print(\"get_result_url:\",response.request.url)\n result = json.loads(response.text)\n # print(result)\n status = result['content']['orderInfo']['status']\n # print(\"status=\",status)\n if status == 4:\n break\n time.sleep(5)\n # print(\"get_result resp:\",result)\n return result\n\ndef convert_audio_to_txt(audio_file):\n api = RequestApi(appid=XFYunAPPID,\n secret_key=XFYunAPISecretASR,\n upload_file_path=audio_file)\n json_recover = BaseModel()\n response = api.get_result()\n info = json.loads(response[\"content\"][\"orderResult\"])\n lattice_data = json_recover.convert_val_adaptive(info[\"lattice\"])\n text = \"\"\n for sentence in lattice_data:\n for word in sentence[\"json_1best\"][\"st\"][\"rt\"][0][\"ws\"]:\n text += word[\"cw\"][0][\"w\"]\n return text\n\n\n# 输入讯飞开放平台的appid,secret_key和待转写的文件路径\nif __name__ == '__main__':\n api = RequestApi(appid=XFYunAPPID,\n secret_key=XFYunAPISecretASR,\n upload_file_path=r\"F:\\0_DATA\\1_DATA\\CODE\\PYTHON\\202304_RJB_C4\\other\\audio\\lfasr_涉政.wav\")\n json_recover = BaseModel()\n response = api.get_result()\n info = json.loads(response[\"content\"][\"orderResult\"])\n lattice_data = json_recover.convert_val_adaptive(info[\"lattice\"])\n text = \"\"\n for sentence in lattice_data:\n for word in sentence[\"json_1best\"][\"st\"][\"rt\"][0][\"ws\"]:\n text += word[\"cw\"][0][\"w\"]\n # text = \"\".join([item['ws'][0]['cw'][0]['w'] for item in lattice_data])\n 0\n"}}}}