ModelScope 是什么?
ModelScope旨在打造下一代开源的模型即服务共享平台,为泛AI开发者提供灵活、易用、低成本的一站式模型服务产品,让模型应用更简单!
环境搭建
Step 1 Python环境配置
环境配置
Step 2 Conda安装配置
安装
wget https://repo.anaconda.com/archive/Anaconda3-2024.02-1-Linux-x86_64.sh
sh Anaconda3-2024.02-1-Linux-x86_64.sh
配置Conda环境
安装完成后,执行如下命令为modelscope library
创建对应的python环境。
conda create -n tinywan-modelscope python=3.10
conda activate tinywan-modelscope
执行conda activate tinywan-modelscope
,可能会出现一下问题
CondaError: Run \\\'conda init\\\' before \\\'conda activate
解决方案:执行
source ~/.bashrc
或其他相应的shell配置文件,以让初始化生效
Step 3 安装深度学习框架(pytorch)
执行以下命令
pip3 install torch torchvision torchaudio
执行结果
(tinywan-modelscope) www@ShaoBoWan:~/build$ pip3 install torch torchvision torchaudio
Collecting torch
Downloading torch-2.1.2-cp38-cp38-manylinux1_x86_64.whl.metadata (25 kB)
Collecting torchvision
Downloading torchvision-0.16.2-cp38-cp38-manylinux1_x86_64.whl.metadata (6.6 kB)
Collecting torchaudio
Downloading torchaudio-2.1.2-cp38-cp38-manylinux1_x86_64.whl.metadata (6.4 kB)
Collecting filelock (from torch)
Downloading filelock-3.13.1-py3-none-any.whl.metadata (2.8 kB)
Collecting typing-extensions (from torch)
Downloading typing_extensions-4.9.0-py3-none-any.whl.metadata (3.0 kB)
Collecting sympy (from torch)
Downloading sympy-1.12-py3-none-any.whl (5.7 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.7/5.7 MB 6.3 MB/s eta 0:00:00
Collecting networkx (from torch)
Downloading networkx-3.1-py3-none-any.whl (2.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1/2.1 MB 65.7 MB/s eta 0:00:00
Collecting jinja2 (from torch)
Downloading Jinja2-3.1.3-py3-none-any.whl.metadata (3.3 kB)
Collecting fsspec (from torch)
Downloading fsspec-2023.12.2-py3-none-any.whl.metadata (6.8 kB)
Collecting nvidia-cuda-nvrtc-cu12==12.1.105 (from torch)
Downloading nvidia_cuda_nvrtc_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (23.7 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 23.7/23.7 MB 20.3 MB/s eta 0:00:00
Collecting nvidia-cuda-runtime-cu12==12.1.105 (from torch)
Downloading nvidia_cuda_runtime_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (823 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 823.6/823.6 kB 54.6 MB/s eta 0:00:00
Collecting nvidia-cuda-cupti-cu12==12.1.105 (from torch)
Downloading nvidia_cuda_cupti_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (14.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14.1/14.1 MB 21.4 MB/s eta 0:00:00
Collecting nvidia-cudnn-cu12==8.9.2.26 (from torch)
Downloading nvidia_cudnn_cu12-8.9.2.26-py3-none-manylinux1_x86_64.whl.metadata (1.6 kB)
Collecting nvidia-cublas-cu12==12.1.3.1 (from torch)
Downloading nvidia_cublas_cu12-12.1.3.1-py3-none-manylinux1_x86_64.whl (410.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 410.6/410.6 MB 2.1 MB/s eta 0:00:00
Collecting nvidia-cufft-cu12==11.0.2.54 (from torch)
Downloading nvidia_cufft_cu12-11.0.2.54-py3-none-manylinux1_x86_64.whl (121.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 121.6/121.6 MB 9.2 MB/s eta 0:00:00
Collecting nvidia-curand-cu12==10.3.2.106 (from torch)
Downloading nvidia_curand_cu12-10.3.2.106-py3-none-manylinux1_x86_64.whl (56.5 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 56.5/56.5 MB 11.7 MB/s eta 0:00:00
Collecting nvidia-cusolver-cu12==11.4.5.107 (from torch)
Downloading nvidia_cusolver_cu12-11.4.5.107-py3-none-manylinux1_x86_64.whl (124.2 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 124.2/124.2 MB 7.3 MB/s eta 0:00:00
截图
安装成功
Installing collected packages: mpmath, urllib3, typing-extensions, sympy, pillow,
nvidia-nvtx-cu12, nvidia-nvjitlink-cu12, nvidia-nccl-cu12, nvidia-curand-cu12,
nvidia-cufft-cu12, nvidia-cuda-runtime-cu12, nvidia-cuda-nvrtc-cu12,
nvidia-cuda-cupti-cu12, nvidia-cublas-cu12, numpy, networkx, MarkupSafe,
idna, fsspec, filelock, charset-normalizer, certifi, triton, requests,
nvidia-cusparse-cu12, nvidia-cudnn-cu12, jinja2, nvidia-cusolver-cu12,
torch, torchvision, torchaudio
Successfully installed MarkupSafe-2.1.3 certifi-2023.11.17 charset-normalizer-3.3.2
filelock-3.13.1 fsspec-2023.12.2 idna-3.6 jinja2-3.1.3 mpmath-1.3.0 networkx-3.1
numpy-1.24.4 nvidia-cublas-cu12-12.1.3.1 nvidia-cuda-cupti-cu12-12.1.105
nvidia-cuda-nvrtc-cu12-12.1.105 nvidia-cuda-runtime-cu12-12.1.105
nvidia-cudnn-cu12-8.9.2.26 nvidia-cufft-cu12-11.0.2.54 nvidia-curand-cu12-10.3.2.106
nvidia-cusolver-cu12-11.4.5.107 nvidia-cusparse-cu12-12.1.0.106 nvidia-nccl-cu12-2.18.1
nvidia-nvjitlink-cu12-12.3.101 nvidia-nvtx-cu12-12.1.105 pillow-10.2.0 requests-2.31.0
sympy-1.12 torch-2.1.2 torchaudio-2.1.2 torchvision-0.16.2 triton-2.1.0
typing-extensions-4.9.0 urllib3-2.1.0
Step 4 ModelScope Library 安装
pip3 install modelscope
pip install setuptools_scm
Step 5 体验NLP领域模型
pip3 install \\\"modelscope[nlp]\\\" -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html
Step 6 安装验证
python -c \\\"from modelscope.pipelines import pipeline;print(pipeline(\\\'word-segmentation\\\')(\\\'今天天气不错,适合 出去游玩,你说呢\\\'))\\\"
2024-03-12 19:03:07,719 - modelscope - INFO - PyTorch version 2.2.1 Found.
2024-03-12 19:03:07,720 - modelscope - INFO - Loading ast index from /home/www/.cache/modelscope/ast_indexer
2024-03-12 19:03:07,755 - modelscope - INFO - Loading done! Current index file version is 1.13.0, with md5 f54e9d2dceb89a6c989540d66db83a65 and a total number of 972 components indexed
2024-03-12 19:03:09,127 - modelscope - WARNING - Model revision not specified, use revision: v1.0.3
2024-03-12 19:03:09,383 - modelscope - INFO - initiate model from /home/www/.cache/modelscope/hub/damo/nlp_structbert_word-segmentation_chinese-base
2024-03-12 19:03:09,388 - modelscope - INFO - initiate model from location /home/www/.cache/modelscope/hub/damo/nlp_structbert_word-segmentation_chinese-base.
2024-03-12 19:03:09,392 - modelscope - INFO - initialize model from /home/www/.cache/modelscope/hub/damo/nlp_structbert_word-segmentation_chinese-base
You are using a model of type bert to instantiate a model of type structbert. This is not supported for all configurations of models and can yield errors.
2024-03-12 19:03:11,112 - modelscope - WARNING - No preprocessor field found in cfg.
2024-03-12 19:03:11,112 - modelscope - WARNING - No val key and type key found in preprocessor domain of configuration.json file.
2024-03-12 19:03:11,112 - modelscope - WARNING - Cannot find available config to build preprocessor at mode inference, current config: {\\\'model_dir\\\': \\\'/home/www/.cache/modelscope/hub/damo/nlp_structbert_word-segmentation_chinese-base\\\'}. trying to build by task and model information.
2024-03-12 19:03:11,116 - modelscope - INFO - cuda is not available, using cpu instead.
2024-03-12 19:03:11,117 - modelscope - WARNING - No preprocessor field found in cfg.
2024-03-12 19:03:11,117 - modelscope - WARNING - No val key and type key found in preprocessor domain of configuration.json file.
2024-03-12 19:03:11,117 - modelscope - WARNING - Cannot find available config to build preprocessor at mode inference, current config: {\\\'model_dir\\\': \\\'/home/www/.cache/modelscope/hub/damo/nlp_structbert_word-segmentation_chinese-base\\\', \\\'sequence_length\\\': 512}. trying to build by task and model information.
/home/www/anaconda3/envs/tinywan-modelscope/lib/python3.10/site-packages/transformers/modeling_utils.py:962: FutureWarning: The `device` argument is deprecated and will be removed in v5 of Transformers.
warnings.warn(
{\\\'output\\\': [\\\'今天\\\', \\\'天气\\\', \\\'不错\\\', \\\',\\\', \\\'适合\\\', \\\'出去\\\', \\\'游玩\\\', \\\',\\\', \\\'你\\\', \\\'说\\\', \\\'呢\\\']}
文本分类任务
文本分类任务是将单个文本、文本对转换为张量并输入模型后,转换为具有最大概率的已知标签的过程。 文本分类任务可以解决例如情感分析、句子相似度等具体问题,ModelHub中文本分类模型具体可以解决的问题有:
-
情感分析(sentiment-classification):给出一个句子,判断该句子属于负向情感,正向情感或者中立 -
句子相似度(sentence-similarity):给出两个句子,判断两个句子是否相似 -
自然语言推理(nli):给出两个句子,判断这两个句子是逻辑无关、逻辑蕴含或者逻辑矛盾 -
其他问题:模型经过训练,将用户的输入句子分类为用户指定的标签中的一个
文本分类任务的推理
ModelScope框架的推理能力依赖于pipeline
模块实现。
text.py
分类文件
from modelscope.pipelines import pipeline
pipeline_ins = pipeline(task=\\\'nli\\\', model=\\\'damo/nlp_structbert_nli_chinese-base\\\')
print(pipeline_ins(input=(\\\'四川商务职业学院和四川财经职业学院哪个好?\\\', \\\'四川商务职业学院商务管理在哪个校区?\\\')));
执行结果
python text.py
2024-03-12 19:26:20,407 - modelscope - INFO - PyTorch version 2.2.1 Found.
2024-03-12 19:26:20,407 - modelscope - INFO - Loading ast index from /home/www/.cache/modelscope/ast_indexer
2024-03-12 19:26:20,442 - modelscope - INFO - Loading done! Current index file version is 1.13.0, with md5 f54e9d2dceb89a6c989540d66db83a65 and a total number of 972 components indexed
2024-03-12 19:26:21,806 - modelscope - WARNING - Model revision not specified, use revision: v1.0.1
2024-03-12 19:26:22,007 - modelscope - INFO - initiate model from /home/www/.cache/modelscope/hub/damo/nlp_structbert_nli_chinese-base
2024-03-12 19:26:22,007 - modelscope - INFO - initiate model from location /home/www/.cache/modelscope/hub/damo/nlp_structbert_nli_chinese-base.
2024-03-12 19:26:22,008 - modelscope - INFO - initialize model from /home/www/.cache/modelscope/hub/damo/nlp_structbert_nli_chinese-base
2024-03-12 19:26:23,618 - modelscope - INFO - The key of sentence1: sentence1, The key of sentence2: sentence2, The key of label: label
2024-03-12 19:26:23,620 - modelscope - INFO - cuda is not available, using cpu instead.
2024-03-12 19:26:23,622 - modelscope - INFO - The key of sentence1: text, The key of sentence2: None, The key of label: label
/home/www/anaconda3/envs/tinywan-modelscope/lib/python3.10/site-packages/transformers/modeling_utils.py:962: FutureWarning: The `device` argument is deprecated and will be removed in v5 of Transformers.
warnings.warn(
{\\\'scores\\\': [0.8382568955421448, 0.08336956799030304, 0.07837346941232681], \\\'labels\\\': [\\\'中立\\\', \\\'矛盾\\\', \\\'蕴涵\\\']}
使用PHP库PHPY 调用魔塔库
安装 PHPY
// 下载
git clone https://github.com/swoole/phpy.git
// 进入目录
cd phpy/
// 生成配置文件
/usr/local/php-8.2.14/bin/phpize --with-php-config=/usr/local/php-8.2.14/bin/php-config
// 指定python目录文件
./configure --with-php-config=/usr/local/php-8.2.14/bin/php-config --with-python-dir=/home/www/anaconda3/envs/tinywan-modelscope --with-python-version=3.10
// 编译
make -j4
// 安装
sudo make install
// 添加扩展
vim /usr/local/php-8.2.14/etc/php.ini
// 添加
extension=phpy.so
// 查看扩展
/usr/local/php-8.2.14/bin/php -m |grep phpy
执行脚本
执行脚本
text.php
<?php
$operator = PyCore::import(\\\"operator\\\");
$builtins = PyCore::import(\\\"builtins\\\");
$pipeline = PyCore::import(\\\'modelscope.pipelines\\\')->pipeline;
$pipeline_ins = $pipeline(\\\"nli\\\", \\\"damo/nlp_structbert_nli_chinese-base\\\");
$arr = [\\\"四川商务职业学院和四川财经职业学院哪个好?\\\", \\\"四川商务职业学院商务管理在哪个校区?\\\"];
echo $pipeline_ins(new \\\\PyTuple($arr));
执行结果
/usr/local/php-8.2.14/bin/php text.php
2024-03-12 19:30:26,197 - modelscope - INFO - PyTorch version 2.2.1 Found.
2024-03-12 19:30:26,197 - modelscope - INFO - Loading ast index from /home/www/.cache/modelscope/ast_indexer
2024-03-12 19:30:26,232 - modelscope - INFO - Loading done! Current index file version is 1.13.0, with md5 f54e9d2dceb89a6c989540d66db83a65 and a total number of 972 components indexed
2024-03-12 19:30:27,716 - modelscope - WARNING - Model revision not specified, use revision: v1.0.1
2024-03-12 19:30:27,899 - modelscope - INFO - initiate model from /home/www/.cache/modelscope/hub/damo/nlp_structbert_nli_chinese-base
2024-03-12 19:30:27,899 - modelscope - INFO - initiate model from location /home/www/.cache/modelscope/hub/damo/nlp_structbert_nli_chinese-base.
2024-03-12 19:30:27,900 - modelscope - INFO - initialize model from /home/www/.cache/modelscope/hub/damo/nlp_structbert_nli_chinese-base
2024-03-12 19:30:29,528 - modelscope - INFO - The key of sentence1: sentence1, The key of sentence2: sentence2, The key of label: label
2024-03-12 19:30:29,530 - modelscope - INFO - cuda is not available, using cpu instead.
2024-03-12 19:30:29,532 - modelscope - INFO - The key of sentence1: text, The key of sentence2: None, The key of label: label
/home/www/anaconda3/envs/tinywan-modelscope/lib/python3.10/site-packages/transformers/modeling_utils.py:962: FutureWarning: The `device` argument is deprecated and will be removed in v5 of Transformers.
warnings.warn(
{\\\'scores\\\': [0.8382568955421448, 0.08336956799030304, 0.07837346941232681], \\\'labels\\\': [\\\'中立\\\', \\\'矛盾\\\', \\\'蕴涵\\\']}(tinywan-modelscope)
原创文章,作者:网络技术联盟站,如若转载,请注明出处:https://www.sudun.com/ask/49738.html
原创文章,作者:网络技术联盟站,如若转载,请注明出处:https://www.sudun.com/ask/49738.html