-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuling_demo.py
63 lines (54 loc) · 1.61 KB
/
tuling_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# coding:utf-8
# Author: Jeremy Tsui
# Date : 2019-06-30 20:50
# File : tuling_demo.py
# IDE : PyCharm
import urllib.request
import json
import time
APIkey = '4a8892827e9440ed80f9f01905c808b2'
def get_response(msg):
api_url = "http://openapi.tuling123.com/openapi/api/v2"
data = {
"perception":
{
"inputText":
{
"text": msg
},
"selfInfo":
{
"location":
{
"city": "Beijing",
"province": "Beijing",
"street": "GuangquRoad"
}
}
},
"userInfo":
{
"apiKey": "85fa9349884747f5b72913a7c60bbcab",
"userId": "OnlyUseAlphabet"
}
}
try:
req = json.dumps(data).encode('utf-8')
http_post = urllib.request.Request(api_url, data=req, headers={'content-type': 'application/json'})
response = urllib.request.urlopen(http_post)
response_str = response.read().decode('utf-8')
response_dic = json.loads(response_str)
results_text = response_dic['results'][0]['values']['text']
return results_text
except:
return None
def tuling_reply_text(message):
print("You said [{}] to AI.".format(message))
r = get_response(message)
return r
# 你好
while True:
time.sleep(0.5)
msg = input("直男/女:")
r = tuling_reply_text(msg)
print("AI:", r)