因为部分模型必须在linux环境中才能运行,所以python也要安装linux环境中。
魔塔框架实际就是python的模块:modelscope
所以需要依赖python语言的运行环境,这里使用Miniconda(可以管理python虚拟环境的一个命令行工具)。
魔塔社区里面的安装教程的可选性很多,乱花迷眼,所以这里是一个简略步骤。
一、安装conda
1、进入conda官网(Download Now | Anaconda),下载linux版本的安装文件Miniconda3-latest-Linux-x86_64.sh。
2、运行这个安装脚本。
3、根据提示完成安装。
4、安装完成后在命令行前面会有一个(base)标志。脚本会将启动命令写入~/.bashrc,如果本次没有进入conda环境,可以重新进行一个命令行。
退出conda环境:conda deactivate
删除虚拟环境:conda env remove --name 环境名
二、安装魔塔
(参考:https://zhuanlan.zhihu.com/p/631529004)
1、创建一个虚拟环境:conda create -n modelscope python=3.8
2、进入环境:conda activate modelscope
3、安装魔塔Lib:pip install modelscope
!!!魔塔Lib安装已经完成,以下为安装nlp模型的特有步骤,执行以完成验证。!!!
4、安装torch:pip3 install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple
torch模块主要用于自然语言处理,因为很大,所以用国内源单独安装,否则如果后面自动安装可能非常慢。
5、安装自然语言处理的模型:pip install "modelscope[nlp]" -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html
6、使用分词处理进行测试:python -c "from modelscope.pipelines import pipeline;print(pipeline('word-segmentation')('今天天气不错,适合 出去游玩'))"
分词成功,就代表安装成功。然后就是如果想要使用哪个模型,就需要再下载该模型,然后写python程序,即重复5、6步骤。
难点:魔塔Lib的安装比较简单,难点在于模型的安装,不同模型可能有不同依赖和版本要求,所以每个模型可能都需要单独一个环境。
安装魔塔Lib完成,后续是其他模型的补充。
三、使用魔塔的其他模型
1、SenseVoice项目
自然语言处理,或者说语音识别模型。
1)环境安装
参考:魔搭社区
下载funasr模块(语音识别):
pip3 install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install funasr -i https://pypi.tuna.tsinghua.edu.cn/simple
2)代码示例
将下属代码保存为funasr.py,然后运行python funasr.py
from funasr import AutoModel
from funasr.utils.postprocess_utils import rich_transcription_postprocessmodel_dir = "iic/SenseVoiceSmall"
model = AutoModel(
model=model_dir,
trust_remote_code=True,
remote_code="./model.py",
vad_model="fsmn-vad",
vad_kwargs={"max_single_segment_time": 30000},
device="cuda:0",
)# en
print("model path: "+model.model_path+"\n")
res = model.generate(
input=f"{model.model_path}/example/en.mp3",
cache={},
language="auto", # "zn", "en", "yue", "ja", "ko", "nospeech"
use_itn=True,
batch_size_s=60,
merge_vad=True, #
merge_length_s=15,
)
text = rich_transcription_postprocess(res[0]["text"])
print("res: "+text+"\n")print("ok"+"\n")
(识别结果:The tribal chieftain called for the boy and presented him with 50 pieces of gold.)
3)模型
模型数据由上述代码自动下载。
下载位置:/home/用户名/.cache/modelscope/hub/iic/SenseVoiceSmall