57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
# usr/bin/python2.7
|
|
import requests
|
|
import json
|
|
|
|
# 需填写appkey
|
|
appkey = "app-XXX"
|
|
|
|
# 上传图片
|
|
def upload_img(imgpath="test.jpg"):
|
|
headers = {
|
|
'Authorization': 'Bearer {0}'.format(appkey),
|
|
}
|
|
|
|
with open(imgpath, 'rb') as f:
|
|
files = {'file': (imgpath, f, 'image/jpg')}
|
|
|
|
response = requests.post(url = 'http://sl.vrgon.com:6062/v1/files/upload', headers=headers, files=files, data={'user': 'robot'})
|
|
|
|
img_id = json.loads(response.content)["id"]
|
|
|
|
return img_id
|
|
|
|
# 计算
|
|
def img2text(path="./img/test.jpg"):
|
|
img_id = upload_img(path)
|
|
payload = {
|
|
"inputs": {},
|
|
"query": "请问这道数学题的答案是多少,请直接给出数字答案",
|
|
"response_mode": "blocking",
|
|
"conversation_id": "",
|
|
"user": "robot",
|
|
"files": [
|
|
{
|
|
"type": "image",
|
|
"transfer_method": "local_file",
|
|
"upload_file_id": img_id
|
|
}
|
|
]
|
|
}
|
|
|
|
headers = {
|
|
'Authorization': 'Bearer {0}'.format(appkey),
|
|
'Content-Type': 'application/json',
|
|
}
|
|
|
|
response = requests.post(url = "http://sl.vrgon.com:6062/v1/chat-messages", headers=headers, data=json.dumps(payload))
|
|
|
|
ans = ""
|
|
if response.status_code == 200:
|
|
for line in response.iter_lines():
|
|
temp = json.loads(line)
|
|
ans += temp["answer"]
|
|
|
|
return ans
|
|
|
|
if __name__=="__main__":
|
|
print img2text(path="./img/2.jpg") |