微信医聊自动问答 WeChaty + PaddleHub

一、项目背景

基于wechaty和PaddleHub实现一个简单的中文医学问答功能。

wechaty使用相关参考项目: https://aistudio.baidu.com/aistudio/projectdetail/1847009

文本匹配参考项目: https://aistudio.baidu.com/aistudio/projectdetail/1456984

b站视频链接:https://www.bilibili.com/video/BV1G541137zm/

二、文本生成模型简介

  • 模型概述

ERNIE-GEN 是面向生成任务的预训练-微调框架,首次在预训练阶段加入span-by-span 生成任务,让模型每次能够生成一个语义完整的片段。在预训练和微调中通过填充式生成机制和噪声感知机制来缓解曝光偏差问题。此外, ERNIE-GEN 采样多片段-多粒度目标文本采样策略, 增强源文本和目标文本的关联性,加强了编码器和解码器的交互。ernie_gen module是一个具备微调功能的module,可以快速完成特定场景module的制作。

  • 模型链接

https://www.paddlepaddle.org.cn/hubdetail?name=ernie_gen&en_category=TextGeneration

三、准备数据集

1. 解压数据集

!unzip /home/aistudio/data//data52093/data4807.zip

2. 背景描述

中文医药方面的问答数据集,超过10万条。

3. 数据说明

questions.csv:所有的问题及其内容。

answers.csv :所有问题的答案。

train_candidates.txt, dev_candidates.txt, test_candidates.txt :将上述两个文件进行了拆分。

4. 数据来源

https://github.com/zhangsheng93/cMedQA2

5. 制作数据集

原数据集我已使用脚本处理过, 也可以自己使用脚本调整train和dev的比例, 相关工具脚本可使用data_prepare.py和cut_txt.py

在上方大家也看到了数据集格式为:

  • train_path(str): 训练集路径。训练集的格式应为:“序号\t输入文本\t标签”,例如:“1\t床前明月光\t疑是地上霜”
  • dev_path(str): 验证集路径。验证集的格式应为:“序号\t输入文本\t标签”,例如:“1\t举头望明月\t低头思故乡”

#整理数据格式
#!python data_prepare.py 

#调整数据比例
#!cut_txt.py

#解压已处理好的数据
#!unzip data/data84360/list.zip -d ./
Archive:  data/data84360/list.zip
replace ./test_list.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: ^C

四、安装Fine-Tune环境

!pip install paddle-ernie==0.0.4.dev1
!pip install  paddlehub==1.8.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://mirror.baidu.com/pypi/simple/
Collecting paddle-ernie==0.0.4.dev1
  Downloading https://mirror.baidu.com/pypi/packages/2c/83/1081e7f4c9463c87a37d240bc611ce1dfebb19be9a78d81e171bea55642e/paddle-ernie-0.0.4.dev1.tar.gz
Requirement already satisfied: requests in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddle-ernie==0.0.4.dev1) (2.22.0)
Requirement already satisfied: tqdm in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddle-ernie==0.0.4.dev1) (4.36.1)
Requirement already satisfied: idna<2.9,>=2.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->paddle-ernie==0.0.4.dev1) (2.8)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->paddle-ernie==0.0.4.dev1) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->paddle-ernie==0.0.4.dev1) (1.25.6)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->paddle-ernie==0.0.4.dev1) (2019.9.11)
Building wheels for collected packages: paddle-ernie
  Building wheel for paddle-ernie (setup.py) ... [?25ldone
[?25h  Created wheel for paddle-ernie: filename=paddle_ernie-0.0.4.dev1-cp37-none-any.whl size=21722 sha256=809bedd5bad94db7b8f73b662252e3dc57448e227addfcf1be2bd16713da79fb
  Stored in directory: /home/aistudio/.cache/pip/wheels/8a/b6/b9/58e36cdf58f588998b54d6f0233a176e537576bba8bbe10def
Successfully built paddle-ernie
Installing collected packages: paddle-ernie
Successfully installed paddle-ernie-0.0.4.dev1
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting paddlehub==1.8.0
[?25l  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a4/f2/b8f125d3ba75550572b612e90dff89ee928003ed7fb689025e21d61f2fde/paddlehub-1.8.0-py3-none-any.whl (336kB)
[K     |████████████████████████████████| 337kB 11.5MB/s eta 0:00:01
[?25hRequirement already satisfied: protobuf>=3.6.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.8.0) (3.14.0)
Requirement already satisfied: tqdm in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.8.0) (4.36.1)
Requirement already satisfied: yapf==0.26.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.8.0) (0.26.0)
Requirement already satisfied: colorlog in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.8.0) (4.1.0)
Requirement already satisfied: flask>=1.1.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.8.0) (1.1.1)
Requirement already satisfied: pandas; python_version >= "3" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.8.0) (1.1.5)
Requirement already satisfied: sentencepiece in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.8.0) (0.1.85)
Requirement already satisfied: cma>=2.7.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.8.0) (2.7.0)
Requirement already satisfied: nltk in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.8.0) (3.4.5)
Requirement already satisfied: gunicorn>=19.10.0; sys_platform != "win32" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.8.0) (20.0.4)
Requirement already satisfied: visualdl>=2.0.0b in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.8.0) (2.1.1)
Requirement already satisfied: six>=1.10.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.8.0) (1.15.0)
Requirement already satisfied: flake8 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.8.0) (3.8.2)
Requirement already satisfied: pre-commit in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub==1.8.0) (1.21.0)
Requirement already satisfied: Jinja2>=2.10.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.0->paddlehub==1.8.0) (2.10.1)
Requirement already satisfied: Werkzeug>=0.15 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.0->paddlehub==1.8.0) (0.16.0)
Requirement already satisfied: itsdangerous>=0.24 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.0->paddlehub==1.8.0) (1.1.0)
Requirement already satisfied: click>=5.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.0->paddlehub==1.8.0) (7.0)
Requirement already satisfied: python-dateutil>=2.7.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pandas; python_version >= "3"->paddlehub==1.8.0) (2.8.0)
Requirement already satisfied: numpy>=1.15.4 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pandas; python_version >= "3"->paddlehub==1.8.0) (1.16.4)
Requirement already satisfied: pytz>=2017.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pandas; python_version >= "3"->paddlehub==1.8.0) (2019.3)
Requirement already satisfied: setuptools>=3.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from gunicorn>=19.10.0; sys_platform != "win32"->paddlehub==1.8.0) (41.4.0)
Requirement already satisfied: shellcheck-py in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0b->paddlehub==1.8.0) (0.7.1.1)
Requirement already satisfied: Flask-Babel>=1.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0b->paddlehub==1.8.0) (1.0.0)
Requirement already satisfied: bce-python-sdk in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0b->paddlehub==1.8.0) (0.8.53)
Requirement already satisfied: requests in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0b->paddlehub==1.8.0) (2.22.0)
Requirement already satisfied: Pillow>=7.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl>=2.0.0b->paddlehub==1.8.0) (7.1.2)
Requirement already satisfied: pycodestyle<2.7.0,>=2.6.0a1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8->paddlehub==1.8.0) (2.6.0)
Requirement already satisfied: importlib-metadata; python_version < "3.8" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8->paddlehub==1.8.0) (0.23)
Requirement already satisfied: pyflakes<2.3.0,>=2.2.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8->paddlehub==1.8.0) (2.2.0)
Requirement already satisfied: mccabe<0.7.0,>=0.6.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8->paddlehub==1.8.0) (0.6.1)
Requirement already satisfied: virtualenv>=15.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub==1.8.0) (16.7.9)
Requirement already satisfied: aspy.yaml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub==1.8.0) (1.3.0)
Requirement already satisfied: pyyaml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub==1.8.0) (5.1.2)
Requirement already satisfied: nodeenv>=0.11.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub==1.8.0) (1.3.4)
Requirement already satisfied: identify>=1.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub==1.8.0) (1.4.10)
Requirement already satisfied: cfgv>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub==1.8.0) (2.0.1)
Requirement already satisfied: toml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub==1.8.0) (0.10.0)
Requirement already satisfied: MarkupSafe>=0.23 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Jinja2>=2.10.1->flask>=1.1.0->paddlehub==1.8.0) (1.1.1)
Requirement already satisfied: Babel>=2.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Flask-Babel>=1.0.0->visualdl>=2.0.0b->paddlehub==1.8.0) (2.8.0)
Requirement already satisfied: future>=0.6.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from bce-python-sdk->visualdl>=2.0.0b->paddlehub==1.8.0) (0.18.0)
Requirement already satisfied: pycryptodome>=3.8.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from bce-python-sdk->visualdl>=2.0.0b->paddlehub==1.8.0) (3.9.9)
Requirement already satisfied: idna<2.9,>=2.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->visualdl>=2.0.0b->paddlehub==1.8.0) (2.8)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->visualdl>=2.0.0b->paddlehub==1.8.0) (2019.9.11)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->visualdl>=2.0.0b->paddlehub==1.8.0) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->visualdl>=2.0.0b->paddlehub==1.8.0) (1.25.6)
Requirement already satisfied: zipp>=0.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from importlib-metadata; python_version < "3.8"->flake8->paddlehub==1.8.0) (0.6.0)
Requirement already satisfied: more-itertools in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from zipp>=0.5->importlib-metadata; python_version < "3.8"->flake8->paddlehub==1.8.0) (7.2.0)
Installing collected packages: paddlehub
  Found existing installation: paddlehub 2.0.4
    Uninstalling paddlehub-2.0.4:
      Successfully uninstalled paddlehub-2.0.4
Successfully installed paddlehub-1.8.0

Archive:  data/data84360/list.zip
  inflating: ./test_list.txt         
  inflating: ./train_list.txt        

五、模型微调Fine-Tune

微调API,

参数

  • train_path(str): 训练集路径。训练集的格式应为:“序号\t输入文本\t标签”,例如:“1\t床前明月光\t疑是地上霜”
  • dev_path(str): 验证集路径。验证集的格式应为:“序号\t输入文本\t标签”,例如:“1\t举头望明月\t低头思故乡”
  • save_dir(str): 模型保存以及验证集预测输出路径。
  • init_ckpt_path(str): 模型初始化加载路径,可实现增量训练。
  • use_gpu(bool): 是否使用GPU。
  • max_steps(int): 最大训练步数。
  • batch_size(int): 训练时的batch大小。
  • max_encode_len(int): 最长编码长度。
  • max_decode_len(int): 最长解码长度。
  • learning_rate(float): 学习率大小。
  • warmup_proportion(float): 学习率warmup比例。
  • weight_decay(float): 权值衰减大小。
  • noise_prob(float): 噪声概率,详见ernie gen论文
  • label_smooth(float): 标签平滑权重。
  • beam_width(int): 验证集预测时的beam大小。
  • length_penalty(float): 验证集预测时的长度惩罚权重。
  • log_interval(int): 训练时的日志打印间隔步数。
  • save_interval(int): 训练时的模型保存间隔部署。验证集将在模型保存完毕后进行预测。

返回

  • result(dict): 运行结果。包含2个键:

    last_save_path(str): 训练结束时的模型保存路径。
    last_ppl(float): 训练结束时的模型困惑度。
    
import paddlehub as hub

module = hub.Module(name="ernie_gen")

result = module.finetune(
    train_path='/home/aistudio/train_list_new.txt',
    # dev_path='/home/aistudio/test_list_tiny.txt',
    save_dir="ernie_gen_ch_med",
    max_steps=40000,
    max_encode_len=256,
    max_decode_len=256,
    noise_prob=0.1,
    batch_size=24,
    log_interval=50,
    save_interval=2000
)
[2021-04-27 00:27:33,228] [    INFO] - Installing ernie_gen module
[2021-04-27 00:27:33,243] [    INFO] - Module ernie_gen already installed in /home/aistudio/.paddlehub/modules/ernie_gen
2021-04-27 00:27:33,246-INFO: get pretrain dir from https://ernie-github.cdn.bcebos.com/model-ernie1.0.1.tar.gz
[INFO 2021-04-27 00:27:33,246 tokenizing_ernie.py:89] get pretrain dir from https://ernie-github.cdn.bcebos.com/model-ernie1.0.1.tar.gz
[INFO] 2021-04-27 00:27:33,262 [feature_column.py:  349]:	reading raw files from /home/aistudio/train_list_new.txt
2021-04-27 00:27:33,264-INFO: get pretrain dir from https://ernie-github.cdn.bcebos.com/model-ernie1.0.1.tar.gz
[INFO 2021-04-27 00:27:33,264 modeling_ernie.py:196] get pretrain dir from https://ernie-github.cdn.bcebos.com/model-ernie1.0.1.tar.gz
2021-04-27 00:27:37,774-INFO: loading pretrained model from /tmp/466eabcffd6d6a83ae9cb97dd1a167bd
[INFO 2021-04-27 00:27:37,774 modeling_ernie.py:214] loading pretrained model from /tmp/466eabcffd6d6a83ae9cb97dd1a167bd
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py:718: UserWarning: Varibale [ pooler.weight pooler.bias ] are not used, because not included in layers state_dict
  format(" ".join(unused_para_list)))
[2021-04-27 00:27:48,772] [    INFO] - [step 0 / 40000]train loss 5.92354, ppl 373.73404, elr 0.000e+00

六、导出module

参数

  • params_path(str): 模型参数路径。
  • module_name(str): module名称,例如"ernie_gen_couplet"。
  • author(str): 作者名称。
  • version(str): 版本号。
  • summary(str): module的英文简介。
  • author_email(str): 作者的邮箱地址。
  • export_path(str): module的导出路径。

module.export(params_path='ernie_gen_ch_med/step_40001_ppl_9.14520.pdparams', module_name="ernie_gen_ch_med_save", author="thunder95")
[2021-04-27 13:03:32,032] [    INFO] - Begin export the model save in ernie_gen_ch_med/step_40001_ppl_9.14520.pdparams ...
[2021-04-27 13:03:32,702] [    INFO] - The module has exported to /home/aistudio/ernie_gen_ch_med_save

七、安装新场景的module

!hub install ernie_gen_ch_med_save
2021-04-27 13:03:55,042-INFO: font search path ['/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf', '/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/afm', '/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/pdfcorefonts']
[INFO 2021-04-27 13:03:55,042 font_manager.py:1071] font search path ['/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf', '/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/afm', '/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/pdfcorefonts']
2021-04-27 13:03:55,388-INFO: generated new fontManager
[INFO 2021-04-27 13:03:55,388 font_manager.py:1458] generated new fontManager
Successfully installed ernie_gen_ch_med_save

八、使用命令行预测

上方我们就已经安装成功了,这里我们进行命令行测试

!hub run ernie_gen_ch_med_save --input_text="手和脚出汗,晚上失眠多梦是不是肾虚" --use_gpu True --beam_width 5
[['考虑是肾阴虚引起的症状,建议服用六味地黄丸和谷维素片以及黄芪精口服液治疗。另外多喝白开水,注意休息。', '考虑是肾阴虚引起的,建议服用六味地黄丸和谷维素片以及黄芪精口服液,另外多喝白开水,注意休息。', '考虑是肾阴虚引起的,建议服用六味地黄丸和谷维素片以及黄芪精口服液治疗。另外多喝白开水,注意休息。', '考虑是肾阴虚引起的症状,建议服用六味地黄丸和谷维素片以及黄芪精口服液,另外多喝白开水,注意休息。', '考虑是肾阴虚引起的症状,建议服用六味地黄丸和谷维素片以及黄芪精口服液治疗。另外多喝白开水,注意休息,']]
[0m

九、API预测

这里我们进行API测试,大家可以在下方查看结果

import paddlehub as hub
import os

os.environ["CUDA_VISIBLE_DEVICES"] ="0"

module = hub.Module(name="ernie_gen_ch_med_save")

test_texts = ["我身上起小红点还痒猫耳疮, 怎么办"]
# generate包含3个参数,texts为输入文本列表,use_gpu指定是否使用gpu,beam_width指定beam search宽度。
results = module.generate(texts=test_texts, use_gpu=True, beam_width=1)
for result in results:
=1)
for result in results:
    print(result)
[2021-04-27 13:09:40,758] [    INFO] - Installing ernie_gen_ch_med_save module
[2021-04-27 13:09:40,788] [    INFO] - Module ernie_gen_ch_med_save already installed in /home/aistudio/.paddlehub/modules/ernie_gen_ch_med_save


['这个情况考虑是有毛囊炎的可能,建议你可以用高锰酸钾溶液清洗后涂抹百多邦软膏治疗,注意饮食,多喝水,多']

十、部署WeChaty服务端

这里使用docker脚本可方便快速部署。请将your_token处替换成你的WeChaty token (puppet_padlocal_xxxxxxxxxxxxx)

export WECHATY_LOG="verbose"
export WECHATY_PUPPET="wechaty-puppet-padlocal"
export WECHATY_PUPPET_PADLOCAL_TOKEN="your_token"

export WECHATY_PUPPET_SERVER_PORT="8080"
export WECHATY_TOKEN="your_token"

docker run -ti \
  --name wechaty_puppet_service_token_gateway \
  --rm \
  -e WECHATY_LOG \
  -e WECHATY_PUPPET \
  -e WECHATY_PUPPET_PADLOCAL_TOKEN \
  -e WECHATY_PUPPET_SERVER_PORT \
  -e WECHATY_TOKEN \
  -p "$WECHATY_PUPPET_SERVER_PORT:$WECHATY_PUPPET_SERVER_PORT" \
  wechaty/wechaty:latest
~                        

运行成功后如下图:

十一、扫码登录微信

在控制台找到二维码图片链接, 如Online QR Code Image: https://wechaty.js.org/qrcode/xxxxxxx

在浏览器中打开并进行扫码登录

十二、对接PaddleHub模型和WeChaty处理逻辑

配置WeChaty相关信息以及安装相关依赖包

import os
os.environ['WECHATY_PUPPET']="wechaty-puppet-service"
os.environ['WECHATY_PUPPET_SERVICE_TOKEN']="your token" # 这里填Wechaty token
os.environ['CUDA_VISIBLE_DEVICES']="0"
os.environ['WECHATY_PUPPET_SERVICE_ENDPOINT']="xx.xx.xx.xx:8080" #这里填Wechaty服务端的IP和端口
os.system("pip install wechaty==0.7dev17")
os.system("hub install ernie_gen_ch_med_save") #安装已保存的模型

主要代码逻辑,非常简单,就几行代码

model = hub.Module(name='animegan_v2_shinkai_33', use_gpu=True)
async def on_message(msg: Message):
    msg_src = msg.text()
    if msg_src.startswith('Q'): #只处理已Q开头的字符串
        results = module.generate(texts=[msg_src[1:]], use_gpu=True, beam_width=1)
        ans = "A" + results[0][0]
        await msg.say(msg_src + "  " + ans)

PS: 可能由于网络原因在aistuido上无法运行,可以切换到本地或者脚本任务运行

十三、效果测试

Q开头表示提问,过滤其他消息, 回复的时候会把问题(Q)和答案(A)一起发送。也可以自己给自己发送消息,以便测试。

至此你的新场景module就已经测试完毕,大家有没有新场景呢?不妨来试试吧!

Logo

学大模型,用大模型上飞桨星河社区!每天8点V100G算力免费领!免费领取ERNIE 4.0 100w Token >>>

更多推荐