Spaces:
Runtime error
Runtime error
DiamondYin
commited on
Commit
•
bb6b177
1
Parent(s):
8bffefe
0805try
Browse files- SparkApi_jayce.py +164 -0
- app.py +11 -12
SparkApi_jayce.py
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import _thread as thread
|
2 |
+
import base64
|
3 |
+
import datetime
|
4 |
+
import hashlib
|
5 |
+
import hmac
|
6 |
+
import json
|
7 |
+
from urllib.parse import urlparse
|
8 |
+
import ssl
|
9 |
+
from datetime import datetime
|
10 |
+
from time import mktime
|
11 |
+
from urllib.parse import urlencode
|
12 |
+
from wsgiref.handlers import format_date_time
|
13 |
+
|
14 |
+
import websocket
|
15 |
+
|
16 |
+
result = ""
|
17 |
+
|
18 |
+
class Ws_Param(object):
|
19 |
+
# 初始化
|
20 |
+
def __init__(self, APPID, APIKey, APISecret, gpt_url):
|
21 |
+
self.APPID = APPID
|
22 |
+
self.APIKey = APIKey
|
23 |
+
self.APISecret = APISecret
|
24 |
+
self.host = urlparse(gpt_url).netloc
|
25 |
+
self.path = urlparse(gpt_url).path
|
26 |
+
self.gpt_url = gpt_url
|
27 |
+
|
28 |
+
# 生成url
|
29 |
+
def create_url(self):
|
30 |
+
# 生成RFC1123格式的时间戳
|
31 |
+
now = datetime.now()
|
32 |
+
date = format_date_time(mktime(now.timetuple()))
|
33 |
+
|
34 |
+
# 拼接字符串
|
35 |
+
signature_origin = "host: " + self.host + "\n"
|
36 |
+
signature_origin += "date: " + date + "\n"
|
37 |
+
signature_origin += "GET " + self.path + " HTTP/1.1"
|
38 |
+
|
39 |
+
# 进行hmac-sha256进行加密
|
40 |
+
signature_sha = hmac.new(self.APISecret.encode('utf-8'), signature_origin.encode('utf-8'),
|
41 |
+
digestmod=hashlib.sha256).digest()
|
42 |
+
|
43 |
+
signature_sha_base64 = base64.b64encode(signature_sha).decode(encoding='utf-8')
|
44 |
+
|
45 |
+
authorization_origin = f'api_key="{self.APIKey}", algorithm="hmac-sha256", headers="host date request-line", signature="{signature_sha_base64}"'
|
46 |
+
|
47 |
+
authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')
|
48 |
+
|
49 |
+
# 将请求的鉴权参数组合为字典
|
50 |
+
v = {
|
51 |
+
"authorization": authorization,
|
52 |
+
"date": date,
|
53 |
+
"host": self.host
|
54 |
+
}
|
55 |
+
# 拼接鉴权参数,生成url
|
56 |
+
url = self.gpt_url + '?' + urlencode(v)
|
57 |
+
# 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致
|
58 |
+
return url
|
59 |
+
|
60 |
+
|
61 |
+
# 收到websocket错误的处理
|
62 |
+
def on_error(ws, error):
|
63 |
+
print("### error:", error)
|
64 |
+
|
65 |
+
|
66 |
+
# 收到websocket关闭的处理
|
67 |
+
def on_close(ws, *args):
|
68 |
+
print("### closed ###")
|
69 |
+
|
70 |
+
|
71 |
+
# 收到websocket连接建立的处理
|
72 |
+
def on_open(ws):
|
73 |
+
thread.start_new_thread(run, (ws,))
|
74 |
+
|
75 |
+
|
76 |
+
def run(ws, *args):
|
77 |
+
data = json.dumps(gen_params(appid=ws.appid, question=ws.question))
|
78 |
+
ws.send(data)
|
79 |
+
|
80 |
+
|
81 |
+
# 收到websocket消息的处理
|
82 |
+
def on_message(ws, message):
|
83 |
+
# print(message)
|
84 |
+
global result
|
85 |
+
data = json.loads(message)
|
86 |
+
code = data['header']['code']
|
87 |
+
if code != 0:
|
88 |
+
print(f'请求错误: {code}, {data}')
|
89 |
+
ws.close()
|
90 |
+
else:
|
91 |
+
choices = data["payload"]["choices"]
|
92 |
+
status = choices["status"]
|
93 |
+
content = choices["text"][0]["content"]
|
94 |
+
|
95 |
+
# by zxd
|
96 |
+
#print(content, end='')
|
97 |
+
result += content
|
98 |
+
#received_data.append(content)
|
99 |
+
|
100 |
+
if status == 2:
|
101 |
+
ws.close()
|
102 |
+
|
103 |
+
|
104 |
+
def gen_params(appid, question):
|
105 |
+
"""
|
106 |
+
通过appid和用户的提问来生成请参数
|
107 |
+
"""
|
108 |
+
data = {
|
109 |
+
"header": {
|
110 |
+
"app_id": appid,
|
111 |
+
"uid": "1234"
|
112 |
+
},
|
113 |
+
"parameter": {
|
114 |
+
"chat": {
|
115 |
+
"domain": "general",
|
116 |
+
"random_threshold": 0.5,
|
117 |
+
"max_tokens": 2048,
|
118 |
+
"auditing": "default"
|
119 |
+
}
|
120 |
+
},
|
121 |
+
"payload": {
|
122 |
+
"message": {
|
123 |
+
"text": [
|
124 |
+
{"role": "user", "content": question}
|
125 |
+
]
|
126 |
+
}
|
127 |
+
}
|
128 |
+
}
|
129 |
+
return data
|
130 |
+
|
131 |
+
|
132 |
+
def main(appid, api_key, api_secret, gpt_url, question):
|
133 |
+
wsParam = Ws_Param(appid, api_key, api_secret, gpt_url)
|
134 |
+
websocket.enableTrace(False)
|
135 |
+
wsUrl = wsParam.create_url()
|
136 |
+
ws = websocket.WebSocketApp(wsUrl, on_message=on_message, on_error=on_error, on_close=on_close, on_open=on_open)
|
137 |
+
ws.appid = appid
|
138 |
+
ws.question = question
|
139 |
+
|
140 |
+
# by zxd
|
141 |
+
result = ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
|
142 |
+
return result
|
143 |
+
|
144 |
+
|
145 |
+
def create(question):
|
146 |
+
global result
|
147 |
+
result = ""
|
148 |
+
main(appid="d2ff57e0",
|
149 |
+
api_secret="YjlmNDdkYjFmMGMzYjc5MmJiODFjN2Fi",
|
150 |
+
api_key="07963fbc530a42f4ad223517decfd5fe",
|
151 |
+
gpt_url="ws://spark-api.xf-yun.com/v1.1/chat",
|
152 |
+
question=question)
|
153 |
+
return result
|
154 |
+
|
155 |
+
|
156 |
+
|
157 |
+
if __name__ == "__main__":
|
158 |
+
# 测试时候在此处正确填写相关信息即可运行
|
159 |
+
#print(create("你是谁"))
|
160 |
+
A = create("你是谁")
|
161 |
+
print(type(A))
|
162 |
+
print(A)
|
163 |
+
|
164 |
+
|
app.py
CHANGED
@@ -4,7 +4,7 @@ import openai
|
|
4 |
import time
|
5 |
import gradio as gr
|
6 |
from threading import Thread #线程 用于定时器
|
7 |
-
import
|
8 |
|
9 |
#from assets.char_poses_base64 import ( #角色动作
|
10 |
# CHAR_IDLE_HTML, CHAR_THINKING_HTML, CHAR_TALKING_HTML)
|
@@ -79,17 +79,17 @@ def get_response(history, audio_input):
|
|
79 |
question = 'hello'
|
80 |
|
81 |
#answer = conv_model.run(question)
|
82 |
-
answer =
|
83 |
api_secret="YjlmNDdkYjFmMGMzYjc5MmJiODFjN2Fi",
|
84 |
api_key="07963fbc530a42f4ad223517decfd5fe",
|
85 |
gpt_url="ws://spark-api.xf-yun.com/v1.1/chat",
|
86 |
question= question)
|
87 |
-
answer1 = str(answer)
|
88 |
-
LOGGER.info("\ndocument_response: %s",
|
89 |
-
print('\ndocument_response:',
|
90 |
|
91 |
for trigger in GENERAL_RSPONSE_TRIGGERS:
|
92 |
-
if trigger in
|
93 |
MESSAGES.append({"role": "user", "content": question})
|
94 |
#chat = openai.ChatCompletion.create(
|
95 |
# model="gpt-3.5-turbo",
|
@@ -99,13 +99,12 @@ def get_response(history, audio_input):
|
|
99 |
# stop="\n"
|
100 |
# )
|
101 |
#answer = chat.choices[0].message.content
|
102 |
-
MESSAGES.append({"role": "assistant", "content":
|
103 |
-
LOGGER.info("general_response: %s",
|
104 |
-
print('\ngeneral_response:',
|
105 |
-
|
106 |
-
AUDIO_HTML = text_to_speech_gen(answer1)
|
107 |
-
history[-1][1] = answer1
|
108 |
|
|
|
|
|
109 |
return history, AUDIO_HTML
|
110 |
|
111 |
# buzz_usr_proc = Thread(target=idle_timer)
|
|
|
4 |
import time
|
5 |
import gradio as gr
|
6 |
from threading import Thread #线程 用于定时器
|
7 |
+
import SparkApi_jayce
|
8 |
|
9 |
#from assets.char_poses_base64 import ( #角色动作
|
10 |
# CHAR_IDLE_HTML, CHAR_THINKING_HTML, CHAR_TALKING_HTML)
|
|
|
79 |
question = 'hello'
|
80 |
|
81 |
#answer = conv_model.run(question)
|
82 |
+
answer = SparkApi_jayce.create(appid="d2ff57e0",
|
83 |
api_secret="YjlmNDdkYjFmMGMzYjc5MmJiODFjN2Fi",
|
84 |
api_key="07963fbc530a42f4ad223517decfd5fe",
|
85 |
gpt_url="ws://spark-api.xf-yun.com/v1.1/chat",
|
86 |
question= question)
|
87 |
+
#answer1 = str(answer)
|
88 |
+
LOGGER.info("\ndocument_response: %s", answer)
|
89 |
+
print('\ndocument_response:', answer)
|
90 |
|
91 |
for trigger in GENERAL_RSPONSE_TRIGGERS:
|
92 |
+
if trigger in answer:
|
93 |
MESSAGES.append({"role": "user", "content": question})
|
94 |
#chat = openai.ChatCompletion.create(
|
95 |
# model="gpt-3.5-turbo",
|
|
|
99 |
# stop="\n"
|
100 |
# )
|
101 |
#answer = chat.choices[0].message.content
|
102 |
+
MESSAGES.append({"role": "assistant", "content": answer})
|
103 |
+
LOGGER.info("general_response: %s", answer)
|
104 |
+
print('\ngeneral_response:', answer)
|
|
|
|
|
|
|
105 |
|
106 |
+
AUDIO_HTML = text_to_speech_gen(answer)
|
107 |
+
history[-1][1] = answer
|
108 |
return history, AUDIO_HTML
|
109 |
|
110 |
# buzz_usr_proc = Thread(target=idle_timer)
|