提交 9e2fd39b 编写于 作者: 走神的阿圆's avatar 走神的阿圆 提交者: wuzewu

Add Serving demo (#215)

* add serving demo
上级 5700aaad
## 数据格式
input: {files: {"image": [file_1, file_2, ...]}}
output: {"results":[result_1, result_2, ...]}
## Serving快速启动命令
```shell
$ hub serving start -m vgg11_imagenet
```
## python脚本
```shell
$ python vgg11_imagenet_serving_demo.py
```
## 结果示例
```python
{
"results": "[[{'Egyptian cat': 0.540287435054779}], [{'daisy': 0.9976677298545837}]]"
}
```
结果含有生成图片的base64编码,可提取生成图片,示例python脚本生成图片位置为当前目录下的output文件夹下。
# coding: utf8
import requests
import json
if __name__ == "__main__":
file_list = ["cat.jpg", "flower.jpg"]
files = [("image", (open(item, "rb"))) for item in file_list]
url = "http://127.0.0.1:8866/predict/image/vgg11_imagenet"
r = requests.post(url=url, files=files)
print(json.dumps(r.json(), indent=4, ensure_ascii=False))
## 数据格式
#### 模型所需参数可通过嵌套字典形式传递
input: {files: {"image": [file_1, file_2, ...]}, data: {...}}
output: {"results":[result_1, result_2, ...]}
## Serving快速启动命令
```shell
$ hub serving start -m stgan_celeba
```
## python脚本
```shell
$ python yolov3_coco2017_serving_demo.py
```
## 结果示例
```python
[
{
"path": "cat.jpg",
"data": [
{
"left": 322.2323,
"right": 1420.4119,
"top": 208.81363,
"bottom": 996.04395,
"label": "cat",
"confidence": 0.9289875
}
]
},
{
"path": "dog.jpg",
"data": [
{
"left": 204.74722,
"right": 746.02637,
"top": 122.793274,
"bottom": 566.6292,
"label": "dog",
"confidence": 0.86698055
}
]
}
]
```
结果含有生成图片的base64编码,可提取生成图片,示例python脚本生成图片位置为当前目录下的output文件夹下。
# coding: utf8
import requests
import json
import base64
import os
if __name__ == "__main__":
file_list = ["../img/woman.png"]
files = [("image", (open(item, "rb"))) for item in file_list]
url = "http://127.0.0.1:8866/predict/image/stgan_celeba"
data = {"info": ["Female,Brown_Hair"], "style": ["Aged"]}
r = requests.post(url=url, data=data, files=files)
results = eval(r.json()["results"])
if not os.path.exists("output"):
os.mkdir("output")
for item in results:
output_path = os.path.join("output", item["path"].split("/")[-1])
with open(output_path, "wb") as fp:
fp.write(base64.b64decode(item["base64"].split(',')[-1]))
item.pop("base64")
print(json.dumps(results, indent=4, ensure_ascii=False))
## 数据格式
input: {"text": [text_1, text_2, ...]}
output: {"results":[result_1, result_2, ...]}
## Serving快速启动命令
```shell
$ hub serving start -m lm_lstm
```
## python脚本
```shell
$ python lm_lstm_serving_demo.py
```
## 结果示例
```python
{
"results": [
{
"perplexity": 4.584166451916099,
"text": "the plant which is owned by <unk> & <unk> co. was under contract with <unk> to make the cigarette filter"
},
{
"perplexity": 6.038358983397484,
"text": "more common <unk> fibers are <unk> and are more easily rejected by the body dr. <unk> explained"
}
]
}
```
# coding: utf8
import requests
import json
if __name__ == "__main__":
text_list = [
"the plant which is owned by <unk> & <unk> co. was under contract with <unk> to make the cigarette filter",
"more common <unk> fibers are <unk> and are more easily rejected by the body dr. <unk> explained"
]
text = {"text": text_list}
url = "http://127.0.0.1:8866/predict/text/lm_lstm"
r = requests.post(url=url, data=text)
print(json.dumps(r.json(), indent=4, ensure_ascii=False))
## 数据格式
input: {"text": [text_1, text_2, ...]}
output: {"results":[result_1, result_2, ...]}
## Serving快速启动命令
```shell
$ hub serving start -m lac
```
## python脚本
#### 不携带用户自定义词典
```shell
$ python lac_no_dict_serving_demo.py
```
#### 携带用户自定义词典
```shell
$ python lac_with_dict_serving_demo.py
```
## 结果示例
```python
{
"results": [
{
"tag": [
"TIME",
"v",
"q",
"n"
],
"word": [
"今天",
"是",
"个",
"好日子"
]
},
{
"tag": [
"n",
"v",
"TIME",
"v",
"v"
],
"word": [
"天气预报",
"说",
"今天",
"要",
"下雨"
]
}
]
}
```
天气 n 400000
经 v 1000
常 d 1000
# coding: utf8
import requests
import json
if __name__ == "__main__":
text_list = ["今天是个好日子", "天气预报说今天要下雨"]
text = {"text": text_list}
url = "http://127.0.0.1:8866/predict/text/lac"
r = requests.post(url=url, data=text)
print(json.dumps(r.json(), indent=4, ensure_ascii=False))
# coding: utf8
import requests
import json
if __name__ == "__main__":
text_list = ["今天是个好日子", "天气预报说今天要下雨"]
text = {"text": text_list}
# 将用户自定义词典文件发送到预测接口即可
with open("dict.txt", "rb") as fp:
file = {"user_dict": fp.read()}
url = "http://127.0.0.1:8866/predict/text/lac"
r = requests.post(url=url, files=file, data=text)
print(json.dumps(r.json(), indent=4, ensure_ascii=False))
## 数据格式
input: {files: {"image": [file_1, file_2, ...]}}
output: {"results":[result_1, result_2, ...]}
## Serving快速启动命令
```shell
$ hub serving start -m yolov3_coco2017
```
## python脚本
```shell
$ python yolov3_coco2017_serving_demo.py
```
## 结果示例
```python
[
{
"path": "cat.jpg",
"data": [
{
"left": 322.2323,
"right": 1420.4119,
"top": 208.81363,
"bottom": 996.04395,
"label": "cat",
"confidence": 0.9289875
}
]
},
{
"path": "dog.jpg",
"data": [
{
"left": 204.74722,
"right": 746.02637,
"top": 122.793274,
"bottom": 566.6292,
"label": "dog",
"confidence": 0.86698055
}
]
}
]
```
结果含有生成图片的base64编码,可提取生成图片,示例python脚本生成图片位置为当前目录下的output文件夹下。
# coding: utf8
import requests
import json
import base64
import os
if __name__ == "__main__":
file_list = ["../img/cat.jpg", "../img/dog.jpg"]
files = [("image", (open(item, "rb"))) for item in file_list]
url = "http://127.0.0.1:8866/predict/image/yolov3_coco2017"
r = requests.post(url=url, files=files)
results = eval(r.json()["results"])
if not os.path.exists("output"):
os.mkdir("output")
for item in results:
with open(os.path.join("output", item["path"]), "wb") as fp:
fp.write(base64.b64decode(item["base64"].split(',')[-1]))
item.pop("base64")
print(json.dumps(results, indent=4, ensure_ascii=False))
## 数据格式
input: {"text1": [text_a1, text_a2, ...], "text2": [text_b1, text_b2, ...]}
output: {"results":[result_1, result_2, ...]}
## Serving快速启动命令
```shell
$ hub serving start -m simnet_bow
```
## python脚本
```shell
$ python simnet_bow_serving_demo.py
```
## 结果示例
```python
{
"results": [
{
"similarity": 0.8445,
"text_1": "这道题太难了",
"text_2": "这道题是上一年的考题"
},
{
"similarity": 0.9275,
"text_1": "这道题太难了",
"text_2": "这道题不简单"
},
{
"similarity": 0.9083,
"text_1": "这道题太难了",
"text_2": "这道题很有意思"
}
]
}
```
# coding: utf8
import requests
import json
if __name__ == "__main__":
text = {
"text_1": ["这道题太难了", "这道题太难了", "这道题太难了"],
"text_2": ["这道题是上一年的考题", "这道题不简单", "这道题很有意思"]
}
url = "http://127.0.0.1:8866/predict/text/simnet_bow"
r = requests.post(url=url, data=text)
print(json.dumps(r.json(), indent=4, ensure_ascii=False))
## 数据格式
input: {files: {"image": [file_1, file_2, ...]}}
output: {"results":[result_1, result_2, ...]}
## Serving快速启动命令
```shell
$ hub serving start -m deeplabv3p_xception65_humanseg
```
## python脚本
```shell
$ python deeplabv3p_xception65_humanseg_serving_demo.py
```
## 结果示例
```python
[
{
"origin": "girl.jpg",
"processed": "humanseg_output/girl_2.png"
}
]
```
结果含有生成图片的base64编码,可提取生成图片,示例python脚本生成图片位置为当前目录下的output文件夹中。
# coding: utf8
import requests
import json
import base64
import os
if __name__ == "__main__":
file_list = ["../img/girl.jpg"]
files = [("image", (open(item, "rb"))) for item in file_list]
url = "http://127.0.0.1:8866/predict/image/deeplabv3p_xception65_humanseg"
r = requests.post(url=url, files=files)
results = eval(r.json()["results"])
if not os.path.exists("output"):
os.mkdir("output")
for item in results:
with open(
os.path.join("output", item["processed"].split("/")[-1]),
"wb") as fp:
fp.write(base64.b64decode(item["base64"].split(',')[-1]))
item.pop("base64")
print(json.dumps(results, indent=4, ensure_ascii=False))
## 数据格式
input: {"text": [text_1, text_2, ...]}
output: {"results":[result_1, result_2, ...]}
## Serving快速启动命令
```shell
$ hub serving start -m senta_lstm
```
## python脚本
``` shell
$ python senta_lstm_serving_demo.py
```
## 结果示例
```python
{
"results": [
{
"negative_probs": 0.7079,
"positive_probs": 0.2921,
"sentiment_key": "negative",
"sentiment_label": 0,
"text": "我不爱吃甜食"
},
{
"negative_probs": 0.0149,
"positive_probs": 0.9851,
"sentiment_key": "positive",
"sentiment_label": 1,
"text": "我喜欢躺在床上看电影"
}
]
}
```
# coding: utf8
import requests
import json
if __name__ == "__main__":
text_list = ["我不爱吃甜食", "我喜欢躺在床上看电影"]
text = {"text": text_list}
url = "http://127.0.0.1:8866/predict/text/senta_lstm"
r = requests.post(url=url, data=text)
print(json.dumps(r.json(), indent=4, ensure_ascii=False))
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册