add app
Browse files- api.py +0 -0
- apidemo/TranslateDemo.py +40 -0
- apidemo/__pycache__/TranslateDemo.cpython-310.pyc +0 -0
- apidemo/__pycache__/__init__.cpython-310.pyc +0 -0
- apidemo/utils/AuthV3Util.py +55 -0
- apidemo/utils/AuthV4Util.py +45 -0
- apidemo/utils/WebSocketUtil.py +74 -0
- apidemo/utils/__init__.py +0 -0
- apidemo/utils/__pycache__/AuthV3Util.cpython-310.pyc +0 -0
- apidemo/utils/__pycache__/__init__.cpython-310.pyc +0 -0
- app.py +35 -0
- requirements.txt +1 -0
api.py
ADDED
File without changes
|
apidemo/TranslateDemo.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import os
|
3 |
+
from .utils.AuthV3Util import addAuthParams
|
4 |
+
|
5 |
+
# 您的应用ID
|
6 |
+
APP_KEY = os.environ.get("APP_KEY",None)
|
7 |
+
# 您的应用密钥
|
8 |
+
|
9 |
+
APP_SECRET = os.environ.get("APP_SECRET",None)
|
10 |
+
|
11 |
+
|
12 |
+
def createRequest(msq):
|
13 |
+
'''
|
14 |
+
note: 将下列变量替换为需要请求的参数
|
15 |
+
'''
|
16 |
+
q = msq
|
17 |
+
lang_from = 'auto'
|
18 |
+
lang_to = 'en'
|
19 |
+
# vocab_id = '您的用户词表ID'
|
20 |
+
|
21 |
+
data = {'q': q, 'from': lang_from, 'to': lang_to}
|
22 |
+
|
23 |
+
addAuthParams(APP_KEY, APP_SECRET, data)
|
24 |
+
|
25 |
+
header = {'Content-Type': 'application/x-www-form-urlencoded'}
|
26 |
+
res = doCall('https://openapi.youdao.com/api', header, data, 'post')
|
27 |
+
print(str(res.content, 'utf-8'))
|
28 |
+
return str(res.content, 'utf-8')
|
29 |
+
|
30 |
+
|
31 |
+
def doCall(url, header, params, method):
|
32 |
+
if 'get' == method:
|
33 |
+
return requests.get(url, params)
|
34 |
+
elif 'post' == method:
|
35 |
+
return requests.post(url, params, header)
|
36 |
+
|
37 |
+
# 网易有道智云翻译服务api调用demo
|
38 |
+
# api接口: https://openapi.youdao.com/api
|
39 |
+
if __name__ == '__main__':
|
40 |
+
createRequest()
|
apidemo/__pycache__/TranslateDemo.cpython-310.pyc
ADDED
Binary file (1.03 kB). View file
|
|
apidemo/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (147 Bytes). View file
|
|
apidemo/utils/AuthV3Util.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import hashlib
|
2 |
+
import time
|
3 |
+
import uuid
|
4 |
+
|
5 |
+
'''
|
6 |
+
添加鉴权相关参数 -
|
7 |
+
appKey : 应用ID
|
8 |
+
salt : 随机值
|
9 |
+
curtime : 当前时间戳(秒)
|
10 |
+
signType : 签名版本
|
11 |
+
sign : 请求签名
|
12 |
+
|
13 |
+
@param appKey 您的应用ID
|
14 |
+
@param appSecret 您的应用密钥
|
15 |
+
@param paramsMap 请求参数表
|
16 |
+
'''
|
17 |
+
def addAuthParams(appKey, appSecret, params):
|
18 |
+
q = params.get('q')
|
19 |
+
if q is None:
|
20 |
+
q = params.get('img')
|
21 |
+
salt = str(uuid.uuid1())
|
22 |
+
curtime = str(int(time.time()))
|
23 |
+
sign = calculateSign(appKey, appSecret, q, salt, curtime)
|
24 |
+
params['appKey'] = appKey
|
25 |
+
params['salt'] = salt
|
26 |
+
params['curtime'] = curtime
|
27 |
+
params['signType'] = 'v3'
|
28 |
+
params['sign'] = sign
|
29 |
+
|
30 |
+
'''
|
31 |
+
计算鉴权签名 -
|
32 |
+
计算方式 : sign = sha256(appKey + input(q) + salt + curtime + appSecret)
|
33 |
+
@param appKey 您的应用ID
|
34 |
+
@param appSecret 您的应用密钥
|
35 |
+
@param q 请求内容
|
36 |
+
@param salt 随机值
|
37 |
+
@param curtime 当前时间戳(秒)
|
38 |
+
@return 鉴权签名sign
|
39 |
+
'''
|
40 |
+
def calculateSign(appKey, appSecret, q, salt, curtime):
|
41 |
+
strSrc = appKey + getInput(q) + salt + curtime + appSecret
|
42 |
+
return encrypt(strSrc)
|
43 |
+
|
44 |
+
|
45 |
+
def encrypt(strSrc):
|
46 |
+
hash_algorithm = hashlib.sha256()
|
47 |
+
hash_algorithm.update(strSrc.encode('utf-8'))
|
48 |
+
return hash_algorithm.hexdigest()
|
49 |
+
|
50 |
+
|
51 |
+
def getInput(input):
|
52 |
+
if input is None:
|
53 |
+
return input
|
54 |
+
inputLen = len(input)
|
55 |
+
return input if inputLen <= 20 else input[0:10] + str(inputLen) + input[inputLen - 10:inputLen]
|
apidemo/utils/AuthV4Util.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import hashlib
|
2 |
+
import time
|
3 |
+
import uuid
|
4 |
+
|
5 |
+
'''
|
6 |
+
添加鉴权相关参数 -
|
7 |
+
appKey : 应用ID
|
8 |
+
salt : 随机值
|
9 |
+
curtime : 当前时间戳(秒)
|
10 |
+
signType : 签名版本
|
11 |
+
sign : 请求签名
|
12 |
+
|
13 |
+
@param appKey 您的应用ID
|
14 |
+
@param appSecret 您的应用密钥
|
15 |
+
@param paramsMap 请求参数表
|
16 |
+
'''
|
17 |
+
def addAuthParams(appKey, appSecret, params):
|
18 |
+
salt = str(uuid.uuid1())
|
19 |
+
curtime = str(int(time.time()))
|
20 |
+
sign = calculateSign(appKey, appSecret, salt, curtime)
|
21 |
+
params['appKey'] = appKey
|
22 |
+
params['salt'] = salt
|
23 |
+
params['curtime'] = curtime
|
24 |
+
params['signType'] = 'v4'
|
25 |
+
params['sign'] = sign
|
26 |
+
|
27 |
+
|
28 |
+
'''
|
29 |
+
计算鉴权签名 -
|
30 |
+
计算方式 : sign = sha256(appKey + input(q) + salt + curtime + appSecret)
|
31 |
+
@param appKey 您的应用ID
|
32 |
+
@param appSecret 您的应用密钥
|
33 |
+
@param salt 随机值
|
34 |
+
@param curtime 当前时间戳(秒)
|
35 |
+
@return 鉴权签名sign
|
36 |
+
'''
|
37 |
+
def calculateSign(appKey, appSecret, salt, curtime):
|
38 |
+
strSrc = appKey + salt + curtime + appSecret
|
39 |
+
return encrypt(strSrc)
|
40 |
+
|
41 |
+
|
42 |
+
def encrypt(strSrc):
|
43 |
+
hash_algorithm = hashlib.sha256()
|
44 |
+
hash_algorithm.update(strSrc.encode('utf-8'))
|
45 |
+
return hash_algorithm.hexdigest()
|
apidemo/utils/WebSocketUtil.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
import threading
|
3 |
+
import urllib.parse
|
4 |
+
|
5 |
+
import websocket
|
6 |
+
|
7 |
+
"""
|
8 |
+
初始化websocket连接
|
9 |
+
"""
|
10 |
+
def init_connection(url):
|
11 |
+
ws = websocket.WebSocketApp(url, on_open=ClientThread.on_open, on_message=ClientThread.on_message,
|
12 |
+
on_close=ClientThread.on_closed, on_error=ClientThread.on_error)
|
13 |
+
# 异步监听返回结果
|
14 |
+
client = ClientThread(ws=ws)
|
15 |
+
client.start()
|
16 |
+
return client
|
17 |
+
|
18 |
+
|
19 |
+
"""
|
20 |
+
初始化websocket连接, 并附带相关参数
|
21 |
+
"""
|
22 |
+
def init_connection_with_params(url, params):
|
23 |
+
url_prams_builder = urllib.parse.urlencode(params)
|
24 |
+
url = url + '?' + url_prams_builder
|
25 |
+
return init_connection(url)
|
26 |
+
|
27 |
+
|
28 |
+
"""
|
29 |
+
发送text message
|
30 |
+
"""
|
31 |
+
def send_text_message(ws, message):
|
32 |
+
ws.send(message)
|
33 |
+
print("send text message: " + message)
|
34 |
+
|
35 |
+
|
36 |
+
"""
|
37 |
+
发送binary message
|
38 |
+
"""
|
39 |
+
def send_binary_message(ws, message):
|
40 |
+
ws.send(message, websocket.ABNF.OPCODE_BINARY)
|
41 |
+
print("send binary message length: " + str(len(message)))
|
42 |
+
|
43 |
+
|
44 |
+
class ClientThread(threading.Thread):
|
45 |
+
def __init__(self, ws):
|
46 |
+
threading.Thread.__init__(self)
|
47 |
+
self.ws = ws
|
48 |
+
ws.is_connect = False
|
49 |
+
|
50 |
+
def run(self):
|
51 |
+
self.ws.run_forever()
|
52 |
+
|
53 |
+
def return_is_connect(self):
|
54 |
+
return self.ws.is_connect
|
55 |
+
|
56 |
+
def on_message(ws, message):
|
57 |
+
print("received message: " + message)
|
58 |
+
# 该判断方式仅用作demo展示, 生产环境请使用json解析
|
59 |
+
if "\"errorCode\":\"0\"" not in message:
|
60 |
+
sys.exit()
|
61 |
+
|
62 |
+
def on_open(ws):
|
63 |
+
print("connection open")
|
64 |
+
ws.is_connect = True
|
65 |
+
|
66 |
+
def on_closed(ws, close_status_code, close_msg):
|
67 |
+
if not close_status_code:
|
68 |
+
close_status_code = 'None'
|
69 |
+
if not close_msg:
|
70 |
+
close_msg = 'None'
|
71 |
+
print("connection closed, code: " + close_status_code + ", reason: " + close_msg)
|
72 |
+
|
73 |
+
def on_error(ws, error):
|
74 |
+
print(error)
|
apidemo/utils/__init__.py
ADDED
File without changes
|
apidemo/utils/__pycache__/AuthV3Util.cpython-310.pyc
ADDED
Binary file (1.16 kB). View file
|
|
apidemo/utils/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (153 Bytes). View file
|
|
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import json
|
4 |
+
# api_key = os.environ.get('OPENAI_KEY', None)
|
5 |
+
from apidemo.TranslateDemo import createRequest
|
6 |
+
|
7 |
+
def get_responce(msg):
|
8 |
+
|
9 |
+
result_str = createRequest(msg)
|
10 |
+
result = json.loads(result_str)
|
11 |
+
return result["translation"][0]
|
12 |
+
# pass
|
13 |
+
|
14 |
+
with gr.Blocks() as demo:
|
15 |
+
chatbot = gr.Chatbot()
|
16 |
+
msg = gr.Textbox()
|
17 |
+
clear = gr.ClearButton([msg, chatbot])
|
18 |
+
|
19 |
+
def respond(message, chat_history):
|
20 |
+
bot_message = get_responce(message)
|
21 |
+
chat_history.append((message, bot_message))
|
22 |
+
# time.sleep(2)
|
23 |
+
return "", chat_history
|
24 |
+
|
25 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
26 |
+
|
27 |
+
if __name__ == "__main__":
|
28 |
+
demo.launch()
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
websocket
|