提交 09736415 编写于 作者: W wuzewu

Merge branch 'release/v2.1' into develop

......@@ -178,8 +178,8 @@ The release of this project is certified by the <a href="./LICENSE">Apache 2.0 l
<a href="https://github.com/jm12138"><img src="https://avatars.githubusercontent.com/u/15712990?v=4" width=75 height=75></a>
<a href="https://github.com/DesmonDay"><img src="https://avatars.githubusercontent.com/u/20554008?v=4" width=75 height=75></a>
<a href="https://github.com/chunzhang-hub"><img src="https://avatars.githubusercontent.com/u/63036966?v=4" width=75 height=75></a>
<a href="https://github.com/adaxiadaxi"><img src="https://avatars.githubusercontent.com/u/58928121?v=4" width=75 height=75></a>
<a href="https://github.com/rainyfly"><img src="https://avatars.githubusercontent.com/u/22424850?v=4" width=75 height=75></a>
<a href="https://github.com/adaxiadaxi"><img src="https://avatars.githubusercontent.com/u/58928121?v=4" width=75 height=75></a>
<a href="https://github.com/linjieccc"><img src="https://avatars.githubusercontent.com/u/40840292?v=4" width=75 height=75></a>
<a href="https://github.com/linshuliang"><img src="https://avatars.githubusercontent.com/u/15993091?v=4" width=75 height=75></a>
<a href="https://github.com/eepgxxy"><img src="https://avatars.githubusercontent.com/u/15946195?v=4" width=75 height=75></a>
......@@ -187,6 +187,7 @@ The release of this project is certified by the <a href="./LICENSE">Apache 2.0 l
<a href="https://github.com/zbp-xxxp"><img src="https://avatars.githubusercontent.com/u/58476312?v=4" width=75 height=75></a>
<a href="https://github.com/houj04"><img src="https://avatars.githubusercontent.com/u/35131887?v=4" width=75 height=75></a>
<a href="https://github.com/Wgm-Inspur"><img src="https://avatars.githubusercontent.com/u/89008682?v=4" width=75 height=75></a>
<a href="https://github.com/AK391"><img src="https://avatars.githubusercontent.com/u/81195143?v=4" width=75 height=75></a>
<a href="https://github.com/apps/dependabot"><img src="https://avatars.githubusercontent.com/in/29110?v=4" width=75 height=75></a>
<a href="https://github.com/dxxxp"><img src="https://avatars.githubusercontent.com/u/15886898?v=4" width=75 height=75></a>
<a href="https://github.com/jianganbai"><img src="https://avatars.githubusercontent.com/u/50263321?v=4" width=75 height=75></a>
......@@ -201,6 +202,7 @@ The release of this project is certified by the <a href="./LICENSE">Apache 2.0 l
<a href="https://github.com/MRXLT"><img src="https://avatars.githubusercontent.com/u/16594411?v=4" width=75 height=75></a>
<a href="https://github.com/cclauss"><img src="https://avatars.githubusercontent.com/u/3709715?v=4" width=75 height=75></a>
<a href="https://github.com/hu-qi"><img src="https://avatars.githubusercontent.com/u/17986122?v=4" width=75 height=75></a>
<a href="https://github.com/itegel"><img src="https://avatars.githubusercontent.com/u/8164474?v=4" width=75 height=75></a>
<a href="https://github.com/jayhenry"><img src="https://avatars.githubusercontent.com/u/4285375?v=4" width=75 height=75></a>
<a href="https://github.com/hlmu"><img src="https://avatars.githubusercontent.com/u/30133236?v=4" width=75 height=75></a>
<a href="https://github.com/shinichiye"><img src="https://avatars.githubusercontent.com/u/76040149?v=4" width=75 height=75></a>
......@@ -227,3 +229,5 @@ We welcome you to contribute code to PaddleHub, and thank you for your feedback.
* Many thanks to [paopjian](https://github.com/paopjian) for correcting the wrong website address [#1424](https://github.com/PaddlePaddle/PaddleHub/issues/1424)
* Many thanks to [Wgm-Inspur](https://github.com/Wgm-Inspur) for correcting the demo errors in readme, and updating the RNN illustration in the text classification and sequence labeling demo
* Many thanks to [zl1271](https://github.com/zl1271) for fixing serving docs typo
* Many thanks to [AK391](https://github.com/AK391) for adding the webdemo of UGATIT and deoldify models in Hugging Face spaces
* Many thanks to [itegel](https://github.com/itegel) for fixing quick start docs typo
......@@ -195,8 +195,8 @@ print(results)
<a href="https://github.com/jm12138"><img src="https://avatars.githubusercontent.com/u/15712990?v=4" width=75 height=75></a>
<a href="https://github.com/DesmonDay"><img src="https://avatars.githubusercontent.com/u/20554008?v=4" width=75 height=75></a>
<a href="https://github.com/chunzhang-hub"><img src="https://avatars.githubusercontent.com/u/63036966?v=4" width=75 height=75></a>
<a href="https://github.com/adaxiadaxi"><img src="https://avatars.githubusercontent.com/u/58928121?v=4" width=75 height=75></a>
<a href="https://github.com/rainyfly"><img src="https://avatars.githubusercontent.com/u/22424850?v=4" width=75 height=75></a>
<a href="https://github.com/adaxiadaxi"><img src="https://avatars.githubusercontent.com/u/58928121?v=4" width=75 height=75></a>
<a href="https://github.com/linjieccc"><img src="https://avatars.githubusercontent.com/u/40840292?v=4" width=75 height=75></a>
<a href="https://github.com/linshuliang"><img src="https://avatars.githubusercontent.com/u/15993091?v=4" width=75 height=75></a>
<a href="https://github.com/eepgxxy"><img src="https://avatars.githubusercontent.com/u/15946195?v=4" width=75 height=75></a>
......@@ -204,6 +204,7 @@ print(results)
<a href="https://github.com/zbp-xxxp"><img src="https://avatars.githubusercontent.com/u/58476312?v=4" width=75 height=75></a>
<a href="https://github.com/houj04"><img src="https://avatars.githubusercontent.com/u/35131887?v=4" width=75 height=75></a>
<a href="https://github.com/Wgm-Inspur"><img src="https://avatars.githubusercontent.com/u/89008682?v=4" width=75 height=75></a>
<a href="https://github.com/AK391"><img src="https://avatars.githubusercontent.com/u/81195143?v=4" width=75 height=75></a>
<a href="https://github.com/apps/dependabot"><img src="https://avatars.githubusercontent.com/in/29110?v=4" width=75 height=75></a>
<a href="https://github.com/dxxxp"><img src="https://avatars.githubusercontent.com/u/15886898?v=4" width=75 height=75></a>
<a href="https://github.com/jianganbai"><img src="https://avatars.githubusercontent.com/u/50263321?v=4" width=75 height=75></a>
......@@ -218,6 +219,7 @@ print(results)
<a href="https://github.com/MRXLT"><img src="https://avatars.githubusercontent.com/u/16594411?v=4" width=75 height=75></a>
<a href="https://github.com/cclauss"><img src="https://avatars.githubusercontent.com/u/3709715?v=4" width=75 height=75></a>
<a href="https://github.com/hu-qi"><img src="https://avatars.githubusercontent.com/u/17986122?v=4" width=75 height=75></a>
<a href="https://github.com/itegel"><img src="https://avatars.githubusercontent.com/u/8164474?v=4" width=75 height=75></a>
<a href="https://github.com/jayhenry"><img src="https://avatars.githubusercontent.com/u/4285375?v=4" width=75 height=75></a>
<a href="https://github.com/hlmu"><img src="https://avatars.githubusercontent.com/u/30133236?v=4" width=75 height=75></a>
<a href="https://github.com/shinichiye"><img src="https://avatars.githubusercontent.com/u/76040149?v=4" width=75 height=75></a>
......@@ -243,3 +245,5 @@ print(results)
* 非常感谢[paopjian](https://github.com/paopjian)修改了中文readme模型搜索指向的的网站地址错误[#1424](https://github.com/PaddlePaddle/PaddleHub/issues/1424)
* 非常感谢[Wgm-Inspur](https://github.com/Wgm-Inspur)修复了readme中的代码示例问题,并优化了文本分类、序列标注demo中的RNN示例图
* 非常感谢[zl1271](https://github.com/zl1271)修复了serving文档中的错别字
* 非常感谢[AK391](https://github.com/AK391)在Hugging Face spaces中添加了UGATIT和deoldify模型的web demo
* 非常感谢[itegel](https://github.com/itegel)修复了快速开始文档中的错别字
......@@ -39,6 +39,8 @@
</table>
</div>
**Deoldify Huggingface Web Demo**: Integrated to [Huggingface Spaces](https://huggingface.co/spaces) with [Gradio](https://github.com/gradio-app/gradio). See demo: [![Hugging Face Spaces](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue)](https://huggingface.co/spaces/akhaliq/deoldify)
### Image Generation
- Including portrait cartoonization, street scene cartoonization, and style transfer.
- Many thanks to CopyRight@[PaddleGAN](https://github.com/PaddlePaddle/PaddleGAN)、CopyRight@[AnimeGAN](https://github.com/TachibanaYoshino/AnimeGANv2)for the pre-trained models.
......@@ -46,6 +48,8 @@
<img src="../imgs/Readme_Related/ImageGAN.gif" width = "640" height = "600" />
</div>
**UGATIT Selfie2anime Huggingface Web Demo**: Integrated to [Huggingface Spaces](https://huggingface.co/spaces) with [Gradio](https://github.com/gradio-app/gradio). See demo: [![Hugging Face Spaces](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue)](https://huggingface.co/spaces/akhaliq/U-GAT-IT-selfie2anime)
### Object Detection
- Pedestrian detection, vehicle detection, and more industrial-grade ultra-large-scale pretrained models are provided.
......
```shell
$ hub install transformer_en-de==1.0.0
```
# transformer_en-de
|模型名称|transformer_en-de|
| :--- | :---: |
|类别|文本-机器翻译|
|网络|Transformer|
|数据集|WMT14 EN-DE|
|是否支持Fine-tuning|否|
|模型大小|481MB|
|最新更新日期|2021-07-21|
|数据指标|-|
## 概述
## 一、模型基本信息
2017 年,Google机器翻译团队在其发表的论文[Attention Is All You Need](https://arxiv.org/abs/1706.03762)中,提出了用于完成机器翻译(Machine Translation)等序列到序列(Seq2Seq)学习任务的一种全新网络结构——Transformer。Tranformer网络完全使用注意力(Attention)机制来实现序列到序列的建模,并且取得了很好的效果。
- ### 模型介绍
transformer_en-de包含6层的transformer结构,头数为8,隐藏层参数为512,参数量为64M。该模型在[WMT'14 EN-DE数据集](http://www.statmt.org/wmt14/translation-task.html)进行了预训练,加载后可直接用于预测,提供了英文翻译为德文的能力
- 2017 年,Google机器翻译团队在其发表的论文[Attention Is All You Need](https://arxiv.org/abs/1706.03762)中,提出了用于完成机器翻译(Machine Translation)等序列到序列(Seq2Seq)学习任务的一种全新网络结构——Transformer。Tranformer网络完全使用注意力(Attention)机制来实现序列到序列的建模,并且取得了很好的效果
关于机器翻译的Transformer模型训练方式和详情,可查看[Machine Translation using Transformer](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/examples/machine_translation/transformer)
- transformer_en-de包含6层的transformer结构,头数为8,隐藏层参数为512,参数量为64M。该模型在[WMT'14 EN-DE数据集](http://www.statmt.org/wmt14/translation-task.html)进行了预训练,加载后可直接用于预测,提供了英文翻译为德文的能力
## API
- 关于机器翻译的Transformer模型训练方式和详情,可查看[Machine Translation using Transformer](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/examples/machine_translation/transformer)
## 二、安装
```python
def __init__(max_length: int = 256,
max_out_len: int = 256,
beam_size: int = 5):
```
初始化module,可配置模型的输入输出文本的最大长度和解码时beam search的宽度。
- ### 1、环境依赖
**参数**
- `max_length`(int): 输入文本的最大长度,默认值为256。
- `max_out_len`(int): 输出文本的最大解码长度,默认值为256。
- `beam_size`(int): beam search方式解码的beam宽度,默认为5。
- paddlepaddle >= 2.1.0
- paddlehub >= 2.1.0 | [如何安装PaddleHub](../../../../docs/docs_ch/get_start/installation.rst)
- ### 2、安装
```python
def predict(data: List[str],
batch_size: int = 1,
n_best: int = 1,
use_gpu: bool = False):
```
预测API,输入源语言的文本句子,解码后输出翻译后的目标语言的文本候选句子。
- ```shell
$ hub install transformer_en-de
```
- 如您安装时遇到问题,可参考:[零基础windows安装](../../../../docs/docs_ch/get_start/windows_quickstart.md)
| [零基础Linux安装](../../../../docs/docs_ch/get_start/linux_quickstart.md) | [零基础MacOS安装](../../../../docs/docs_ch/get_start/mac_quickstart.md)
## 三、模型API预测
- ### 1、预测代码示例
**参数**
- `data`(List[str]): 源语言的文本列表,数据类型为List[str]
- `batch_size`(int): 进行预测的batch_size,默认为1
- `n_best`(int): 每个输入文本经过模型解码后,输出的得分最高的候选句子的数量,必须小于beam_size,默认为1
- `use_gpu`(bool): 是否使用gpu执行预测,默认为False
- ```python
import paddlehub as hub
**返回**
* `results`(List[str]): 翻译后的目标语言的候选句子,长度为`len(data)*n_best`
model = hub.Module(name='transformer_en-de', beam_size=5)
src_texts = [
'What are you doing now?',
'The change was for the better; I eat well, I exercise, I take my drugs.',
'Such experiments are not conducted for ethical reasons.',
]
n_best = 3 # 每个输入样本的输出候选句子数量
trg_texts = model.predict(src_texts, n_best=n_best)
for idx, st in enumerate(src_texts):
print('-'*30)
print(f'src: {st}')
for i in range(n_best):
print(f'trg[{i+1}]: {trg_texts[idx*n_best+i]}')
```
**代码示例**
- ### 2、API
```python
import paddlehub as hub
- ```python
def __init__(max_length: int = 256,
max_out_len: int = 256,
beam_size: int = 5):
```
- 初始化module,可配置模型的输入输出文本的最大长度和解码时beam search的宽度。
- **参数**
model = hub.Module(name='transformer_en-de', beam_size=5)
src_texts = [
'What are you doing now?',
'The change was for the better; I eat well, I exercise, I take my drugs.',
'Such experiments are not conducted for ethical reasons.',
]
- `max_length`(int): 输入文本的最大长度,默认值为256。
- `max_out_len`(int): 输出文本的最大解码长度,默认值为256。
- `beam_size`(int): beam search方式解码的beam宽度,默认为5。
n_best = 3 # 每个输入样本的输出候选句子数量
trg_texts = model.predict(src_texts, n_best=n_best)
for idx, st in enumerate(src_texts):
print('-'*30)
print(f'src: {st}')
for i in range(n_best):
print(f'trg[{i+1}]: {trg_texts[idx*n_best+i]}')
```
- ```python
def predict(data: List[str],
batch_size: int = 1,
n_best: int = 1,
use_gpu: bool = False):
```
## 服务部署
- 预测API,输入源语言的文本句子,解码后输出翻译后的目标语言的文本候选句子。
通过启动PaddleHub Serving,可以加载模型部署在线翻译服务。
- **参数**
### Step1: 启动PaddleHub Serving
- `data`(List[str]): 源语言的文本列表,数据类型为List[str]
- `batch_size`(int): 进行预测的batch_size,默认为1
- `n_best`(int): 每个输入文本经过模型解码后,输出的得分最高的候选句子的数量,必须小于beam_size,默认为1
- `use_gpu`(bool): 是否使用gpu执行预测,默认为False
- **返回**
运行启动命令:
- `results`(List[str]): 翻译后的目标语言的候选句子,长度为`len(data)*n_best`
```shell
$ hub serving start -m transformer_en-de
```
## 四、服务部署
通过以上命令可完成一个英德机器翻译API的部署,默认端口号为8866
- 通过启动PaddleHub Serving,可以加载模型部署在线翻译服务
**NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA_VISIBLE_DEVICES环境变量,否则不用设置。
- ### 第一步:启动PaddleHub Serving
### Step2: 发送预测请求
- 运行启动命令:
配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
- ```shell
$ hub serving start -m transformer_en-de
```
```python
import requests
import json
- 通过以上命令可完成一个英德机器翻译API的部署,默认端口号为8866。
texts = [
'What are you doing now?',
'The change was for the better; I eat well, I exercise, I take my drugs.',
'Such experiments are not conducted for ethical reasons.',
]
data = {"data": texts}
# 发送post请求,content-type类型应指定json方式,url中的ip地址需改为对应机器的ip
url = "http://127.0.0.1:8866/predict/transformer_en-de"
# 指定post请求的headers为application/json方式
headers = {"Content-Type": "application/json"}
- **NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA_VISIBLE_DEVICES环境变量,否则不用设置。
r = requests.post(url=url, headers=headers, data=json.dumps(data))
print(r.json())
```
- ## 第二步:发送预测请求
## 查看代码
- 配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
https://github.com/PaddlePaddle/PaddleNLP/tree/develop/examples/machine_translation/transformer
- ```python
import requests
import json
## 依赖
texts = [
'What are you doing now?',
'The change was for the better; I eat well, I exercise, I take my drugs.',
'Such experiments are not conducted for ethical reasons.',
]
data = {"data": texts}
# 发送post请求,content-type类型应指定json方式,url中的ip地址需改为对应机器的ip
url = "http://127.0.0.1:8866/predict/transformer_en-de"
# 指定post请求的headers为application/json方式
headers = {"Content-Type": "application/json"}
paddlepaddle >= 2.0.0
r = requests.post(url=url, headers=headers, data=json.dumps(data))
print(r.json())
```
paddlehub >= 2.1.0
- 关于PaddleHub Serving更多信息参考:[服务部署](../../../../docs/docs_ch/tutorial/serving.md)
## 更新历史
## 五、更新历史
* 1.0.0
......@@ -123,3 +144,6 @@ paddlehub >= 2.1.0
* 1.0.1
修复模型初始化的兼容性问题
- ```shell
$ hub install transformer_en-de==1.0.1
```
......@@ -13,7 +13,6 @@
# limitations under the License.
import os
from packaging.version import Version
from typing import List
import paddle
......@@ -56,15 +55,12 @@ class MTTransformer(nn.Layer):
# Vocabularies in source and target should be same for weight sharing.
"weight_sharing": True,
# Dropout rate
'dropout': 0
'dropout': 0,
# Number of sub-layers to be stacked in the encoder and decoder.
"num_encoder_layers": 6,
"num_decoder_layers": 6
}
# Number of sub-layers to be stacked in the encoder and decoder.
if Version(paddlenlp.__version__) <= Version('2.0.5'):
model_config.update({"n_layer": 6})
else:
model_config.update({"num_encoder_layers": 6, "num_decoder_layers": 6})
# Vocab config
vocab_config = {
# Used to pad vocab size to be multiple of pad_factor.
......
```shell
$ hub install transformer_zh-en==1.0.0
```
# transformer_zh-en
|模型名称|transformer_zh-en|
| :--- | :---: |
|类别|文本-机器翻译|
|网络|Transformer|
|数据集|CWMT2021|
|是否支持Fine-tuning|否|
|模型大小|614MB|
|最新更新日期|2021-07-21|
|数据指标|-|
## 概述
## 一、模型基本信息
2017 年,Google机器翻译团队在其发表的论文[Attention Is All You Need](https://arxiv.org/abs/1706.03762)中,提出了用于完成机器翻译(Machine Translation)等序列到序列(Seq2Seq)学习任务的一种全新网络结构——Transformer。Tranformer网络完全使用注意力(Attention)机制来实现序列到序列的建模,并且取得了很好的效果。
- ### 模型介绍
transformer_zh-en包含6层的transformer结构,头数为8,隐藏层参数为512,参数量为64M。该模型在[CWMT2021的数据集](http://nlp.nju.edu.cn/cwmt-wmt)进行了预训练,加载后可直接用于预测, 提供了中文翻译为英文的能力
- 2017 年,Google机器翻译团队在其发表的论文[Attention Is All You Need](https://arxiv.org/abs/1706.03762)中,提出了用于完成机器翻译(Machine Translation)等序列到序列(Seq2Seq)学习任务的一种全新网络结构——Transformer。Tranformer网络完全使用注意力(Attention)机制来实现序列到序列的建模,并且取得了很好的效果
关于机器翻译的Transformer模型训练方式和详情,可查看[Machine Translation using Transformer](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/examples/machine_translation/transformer)
- transformer_zh-en包含6层的transformer结构,头数为8,隐藏层参数为512,参数量为64M。该模型在[CWMT2021的数据集](http://nlp.nju.edu.cn/cwmt-wmt)进行了预训练,加载后可直接用于预测, 提供了中文翻译为英文的能力
## API
- 关于机器翻译的Transformer模型训练方式和详情,可查看[Machine Translation using Transformer](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/examples/machine_translation/transformer)
## 二、安装
```python
def __init__(max_length: int = 256,
max_out_len: int = 256,
beam_size: int = 5):
```
初始化module,可配置模型的输入输出文本的最大长度和解码时beam search的宽度。
- ### 1、环境依赖
**参数**
- `max_length`(int): 输入文本的最大长度,默认值为256。
- `max_out_len`(int): 输出文本的最大解码长度,默认值为256。
- `beam_size`(int): beam search方式解码的beam宽度,默认为5。
- paddlepaddle >= 2.1.0
- paddlehub >= 2.1.0 | [如何安装PaddleHub](../../../../docs/docs_ch/get_start/installation.rst)
- ### 2、安装
```python
def predict(data: List[str],
batch_size: int = 1,
n_best: int = 1,
use_gpu: bool = False):
```
预测API,输入源语言的文本句子,解码后输出翻译后的目标语言的文本候选句子。
- ```shell
$ hub install transformer_zh-en
```
**参数**
- `data`(List[str]): 源语言的文本列表,数据类型为List[str]
- `batch_size`(int): 进行预测的batch_size,默认为1
- `n_best`(int): 每个输入文本经过模型解码后,输出的得分最高的候选句子的数量,必须小于beam_size,默认为1
- `use_gpu`(bool): 是否使用gpu执行预测,默认为False
- 如您安装时遇到问题,可参考:[零基础windows安装](../../../../docs/docs_ch/get_start/windows_quickstart.md)
| [零基础Linux安装](../../../../docs/docs_ch/get_start/linux_quickstart.md) | [零基础MacOS安装](../../../../docs/docs_ch/get_start/mac_quickstart.md)
**返回**
* `results`(List[str]): 翻译后的目标语言的候选句子,长度为`len(data)*n_best`
## 三、模型API预测
- ### 1、预测代码示例
**代码示例**
- ```python
import paddlehub as hub
```python
import paddlehub as hub
model = hub.Module(name='transformer_zh-en', beam_size=5)
src_texts = [
'今天天气怎么样?',
'我们一起去吃饭吧。',
]
model = hub.Module(name='transformer_zh-en', beam_size=5)
src_texts = [
'今天天气怎么样?',
'我们一起去吃饭吧。',
]
n_best = 3 # 每个输入样本的输出候选句子数量
trg_texts = model.predict(src_texts, n_best=n_best)
for idx, st in enumerate(src_texts):
print('-'*30)
print(f'src: {st}')
for i in range(n_best):
print(f'trg[{i+1}]: {trg_texts[idx*n_best+i]}')
```
n_best = 3 # 每个输入样本的输出候选句子数量
trg_texts = model.predict(src_texts, n_best=n_best)
for idx, st in enumerate(src_texts):
print('-'*30)
print(f'src: {st}')
for i in range(n_best):
print(f'trg[{i+1}]: {trg_texts[idx*n_best+i]}')
```
- ### 2、API
## 服务部署
- ```python
def __init__(max_length: int = 256,
max_out_len: int = 256,
beam_size: int = 5):
```
通过启动PaddleHub Serving,可以加载模型部署在线翻译服务
- 初始化module,可配置模型的输入输出文本的最大长度和解码时beam search的宽度
### Step1: 启动PaddleHub Serving
- **参数**
运行启动命令:
- `max_length`(int): 输入文本的最大长度,默认值为256。
- `max_out_len`(int): 输出文本的最大解码长度,默认值为256。
- `beam_size`(int): beam search方式解码的beam宽度,默认为5。
```shell
$ hub serving start -m transformer_zh-en
```
- ```python
def predict(data: List[str],
batch_size: int = 1,
n_best: int = 1,
use_gpu: bool = False):
```
通过以上命令可完成一个中英机器翻译API的部署,默认端口号为8866
- 预测API,输入源语言的文本句子,解码后输出翻译后的目标语言的文本候选句子
**NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA_VISIBLE_DEVICES环境变量,否则不用设置。
- **参数**
- `data`(List[str]): 源语言的文本列表,数据类型为List[str]
- `batch_size`(int): 进行预测的batch_size,默认为1
- `n_best`(int): 每个输入文本经过模型解码后,输出的得分最高的候选句子的数量,必须小于beam_size,默认为1
- `use_gpu`(bool): 是否使用gpu执行预测,默认为False
### Step2: 发送预测请求
- **返回**
- `results`(List[str]): 翻译后的目标语言的候选句子,长度为`len(data)*n_best`
配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
## 四、服务部署
```python
import requests
import json
- 通过启动PaddleHub Serving,可以加载模型部署在线翻译服务。
texts = [
'今天天气怎么样啊?',
'我们一起去吃饭吧。',
]
data = {"data": texts}
# 发送post请求,content-type类型应指定json方式,url中的ip地址需改为对应机器的ip
url = "http://127.0.0.1:8866/predict/transformer_zh-en"
# 指定post请求的headers为application/json方式
headers = {"Content-Type": "application/json"}
- ### 第一步: 启动PaddleHub Serving
r = requests.post(url=url, headers=headers, data=json.dumps(data))
print(r.json())
```
- 运行启动命令:
## 查看代码
- ```shell
$ hub serving start -m transformer_zh-en
```
https://github.com/PaddlePaddle/PaddleNLP/tree/develop/examples/machine_translation/transformer
- 通过以上命令可完成一个中英机器翻译API的部署,默认端口号为8866。
## 依赖
- **NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA_VISIBLE_DEVICES环境变量,否则不用设置。
paddlepaddle >= 2.0.0
- ### 第二步: 发送预测请求
paddlehub >= 2.1.0
- 配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
## 更新历史
- ```python
import requests
import json
texts = [
'今天天气怎么样啊?',
'我们一起去吃饭吧。',
]
data = {"data": texts}
# 发送post请求,content-type类型应指定json方式,url中的ip地址需改为对应机器的ip
url = "http://127.0.0.1:8866/predict/transformer_zh-en"
# 指定post请求的headers为application/json方式
headers = {"Content-Type": "application/json"}
r = requests.post(url=url, headers=headers, data=json.dumps(data))
print(r.json())
```
- 关于PaddleHub Serving更多信息参考:[服务部署](../../../../docs/docs_ch/tutorial/serving.md)
## 五、更新历史
* 1.0.0
......@@ -121,3 +141,6 @@ paddlehub >= 2.1.0
* 1.0.1
修复模型初始化的兼容性问题
- ```shell
$ hub install transformer_zh-en==1.0.1
```
......@@ -13,7 +13,6 @@
# limitations under the License.
import os
from packaging.version import Version
from typing import List
import paddle
......@@ -56,15 +55,12 @@ class MTTransformer(nn.Layer):
# Vocabularies in source and target should be same for weight sharing.
"weight_sharing": False,
# Dropout rate
'dropout': 0
'dropout': 0,
# Number of sub-layers to be stacked in the encoder and decoder.
"num_encoder_layers": 6,
"num_decoder_layers": 6
}
# Number of sub-layers to be stacked in the encoder and decoder.
if Version(paddlenlp.__version__) <= Version('2.0.5'):
model_config.update({"n_layer": 6})
else:
model_config.update({"num_encoder_layers": 6, "num_decoder_layers": 6})
# Vocab config
vocab_config = {
# Used to pad vocab size to be multiple of pad_factor.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册