小小小幸运 发表于 2024-8-9 13:34:53

【Ubuntu20.04摆设通义千问Qwen-7B,实测成功】

Ubuntu20.04摆设通义千问Qwen-7B,实测成功

运行情况

Ubuntu 20.04
GPU:RTX 2080Ti
显卡驱动:535.154.05
cuda:12.1
cudnn:8.9.3.28
python:3.9(anaconda3)
pytorch版本:2.2-cu121
torchvision版本:0.17-cu121
anaconda3安装python3.9

# 创建python3.9的虚拟环境
conda create -n Chat python=3.9
# 进入创建的虚拟环境
source activate Chat
# 安装pytorch,小编采用轮子的方式安装,之前下载过,安装起来比较快
pip install torch-2.2.0+cu121-cp39-cp39-linux_x86_64.whl -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
# 安装torchvision
pip install torchvision==0.17.0+cu121 -f https://download.pytorch.org/whl/torch_stable.html -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
需要轮子安装的小同伴,也可以去以下所在进行下载或利用-f拼接下载所在,如小编下载torchvision一样
https://download.pytorch.org/whl/torch_stable.html
拉取代码

git clone https://github.com/QwenLM/Qwen.git
其他依赖情况

cd Qwen
pip install-r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install-r requirements_web_demo.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
停止到此处,运行情况就安装的差不多了,接下来测试模型,运行demo需要先下载模型,可以切换魔塔进行下载模型,这样下载速率比力快。
pip install modelscope transformers -i https://pypi.tuna.tsinghua.edu.cn/simple
安装完魔塔之后,创建一个chat-7b.py的文件,文件内容如下
# 创建chat-7b.py文件
touch chat-7b.py
from modelscope import AutoModelForCausalLM, AutoTokenizer
from modelscope import GenerationConfig

# 可选的模型包括: "qwen/Qwen-7B-Chat", "qwen/Qwen-14B-Chat"
tokenizer = AutoTokenizer.from_pretrained("qwen/Qwen-7B-Chat", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("qwen/Qwen-7B-Chat", device_map="auto", trust_remote_code=True, fp16=True).eval()
model.generation_config = GenerationConfig.from_pretrained("Qwen/Qwen-7B-Chat", trust_remote_code=True) # 可指定不同的生成长度、top_p等相关超参

response, history = model.chat(tokenizer, "你好", history=None)
print(response)
response, history = model.chat(tokenizer, "浙江的省会在哪里?", history=history)
print(response)
response, history = model.chat(tokenizer, "它有什么好玩的景点", history=history)
print(response)
# 运行上面代码
python chat-7b.py
运行的时候出了个错误

OSError: Error no file named pytorch_model.bin, tf_model.h5, model.ckpt.index or flax_model.msgpack found in directory /home/taoxifa/.cache/modelscope/hub/qwen/Qwen-7B-Chat.
https://i-blog.csdnimg.cn/blog_migrate/4b549f42fb817a4cc805f1c19c1aa9b3.png
#报以上错误时,是因为模型没有下载完成,新创建一个文件,执行以下代码进行模型下载,我下载的是Int4量化版本
from modelscope import snapshot_download
model_dir = snapshot_download('qwen/Qwen-7B-Chat-Int4')
再次执行chat-7b.py,成功运行
https://i-blog.csdnimg.cn/blog_migrate/22aec797af219ace1a2afddd5b9d43f1.png
摆设为web端

修改web_demo.py,将transformers修改为modelscope,如下所示
import gradio as gr
import mdtex2html

import torch
#from transformers import AutoModelForCausalLM, AutoTokenizer
#from transformers.generation import GenerationConfig
from modelscope import AutoModelForCausalLM, AutoTokenizer, GenerationConfig



DEFAULT_CKPT_PATH = 'qwen/Qwen-7B-Chat-Int4'
运行web_demo.py,访问本机ip:8000,出现以下页面即摆设成功
https://i-blog.csdnimg.cn/blog_migrate/2b0c1c0792065248e2615e70f16389e6.png
摆设完成之后,大家可以做一些个性化的实验,接下来小编会实验对模型进行微调,增长自己的知识库做练习,感爱好的小同伴可以阅读后续更新的文章。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 【Ubuntu20.04摆设通义千问Qwen-7B,实测成功】