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

打印 上一主题 下一主题

主题 504|帖子 504|积分 1512

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

  1. # 创建python3.9的虚拟环境
  2. conda create -n Chat python=3.9
  3. # 进入创建的虚拟环境
  4. source activate Chat
  5. # 安装pytorch,小编采用轮子的方式安装,之前下载过,安装起来比较快
  6. pip install torch-2.2.0+cu121-cp39-cp39-linux_x86_64.whl -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
  7. # 安装torchvision
  8. 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
拉取代码

  1. git clone https://github.com/QwenLM/Qwen.git
复制代码
其他依赖情况

  1. cd Qwen
  2. pip install  -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
  3. pip install  -r requirements_web_demo.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
复制代码
停止到此处,运行情况就安装的差不多了,接下来测试模型,运行demo需要先下载模型,可以切换魔塔进行下载模型,这样下载速率比力快。
  1. pip install modelscope transformers -i https://pypi.tuna.tsinghua.edu.cn/simple
复制代码
安装完魔塔之后,创建一个chat-7b.py的文件,文件内容如下
  1. # 创建chat-7b.py文件
  2. touch chat-7b.py
复制代码
  1. from modelscope import AutoModelForCausalLM, AutoTokenizer
  2. from modelscope import GenerationConfig
  3. # 可选的模型包括: "qwen/Qwen-7B-Chat", "qwen/Qwen-14B-Chat"
  4. tokenizer = AutoTokenizer.from_pretrained("qwen/Qwen-7B-Chat", trust_remote_code=True)
  5. model = AutoModelForCausalLM.from_pretrained("qwen/Qwen-7B-Chat", device_map="auto", trust_remote_code=True, fp16=True).eval()
  6. model.generation_config = GenerationConfig.from_pretrained("Qwen/Qwen-7B-Chat", trust_remote_code=True) # 可指定不同的生成长度、top_p等相关超参
  7. response, history = model.chat(tokenizer, "你好", history=None)
  8. print(response)
  9. response, history = model.chat(tokenizer, "浙江的省会在哪里?", history=history)
  10. print(response)
  11. response, history = model.chat(tokenizer, "它有什么好玩的景点", history=history)
  12. print(response)
复制代码
  1. # 运行上面代码
  2. 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.

  1. #报以上错误时,是因为模型没有下载完成,新创建一个文件,执行以下代码进行模型下载,我下载的是Int4量化版本
  2. from modelscope import snapshot_download
  3. model_dir = snapshot_download('qwen/Qwen-7B-Chat-Int4')
复制代码
再次执行chat-7b.py,成功运行

摆设为web端

修改web_demo.py,将transformers修改为modelscope,如下所示
  1. import gradio as gr
  2. import mdtex2html
  3. import torch
  4. #from transformers import AutoModelForCausalLM, AutoTokenizer
  5. #from transformers.generation import GenerationConfig
  6. from modelscope import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
  7. DEFAULT_CKPT_PATH = 'qwen/Qwen-7B-Chat-Int4'
复制代码
运行web_demo.py,访问本机ip:8000,出现以下页面即摆设成功

摆设完成之后,大家可以做一些个性化的实验,接下来小编会实验对模型进行微调,增长自己的知识库做练习,感爱好的小同伴可以阅读后续更新的文章。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

小小小幸运

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表