通义千问7B和7B-chat模型本地部署复现成功

先说一下我的电脑配置:

1.CUP i7-13700KF

2.GPU RTX4090

3.内存64G

在github上下载了千问开源包GitHub - QwenLM/Qwen-7B: The official repo of Qwen-7B (通义千问-7B) chat & pretrained large language model proposed by Alibaba Cloud.

新建虚拟环境,我的环境配置如下:

1.python版本3.9

2.CUDA版本12.2

3.pytorch版本2.0.1

其他的直接

pip install  -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install  -r rrequirements_web_demo -i https://pypi.tuna.tsinghua.edu.cn/simple

到这里环境配置基本完事了。

如果你直接运行web_demo.py,基本没戏。因为他自动在github上下载千文7B的模型。模型很大,8个bin的文件加到一起14.3G,国内基本下载不了。

这时,需要在国内源下载。

pip install modelscope

这是魔搭社区库,和阿里啥关系我也没关心。

安装完魔搭库以后,新建7b-chat.py文件。

代码如下:

from modelscope import AutoModelForCausalLM, AutoTokenizer
from modelscope import GenerationConfig

tokenizer = AutoTokenizer.from_pretrained("qwen/Qwen-7B-Chat", revision = 'v1.0.5',trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("qwen/Qwen-7B-Chat", revision = 'v1.0.5',device_map="auto", trust_remote_code=True,fp16 = True).eval()
model.generation_config = GenerationConfig.from_pretrained("Qwen/Qwen-7B-Chat",revision = 'v1.0.5', 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)

运行上面的代码后,自动在下载很多个模型,速度一秒65MB左右,很爽。

 

都下载完就可以使用千问7B-chat模型对话了。

 如果你想运行通义千问开源包里面的web_demo.py。直接运行还是会重新下载模型。所以更改如下代码即可调用刚才你下载好的模型。

from argparse import ArgumentParser

import gradio as gr
import mdtex2html
#from transformers import AutoModelForCausalLM, AutoTokenizer
#from transformers.generation import GenerationConfig
from modelscope import AutoModelForCausalLM, AutoTokenizer, GenerationConfig

DEFAULT_CKPT_PATH = 'qwen/Qwen-7B-Chat'

运行代码后,产生一个链接,点击链接就可以对话了。

 

 以上是7B-chat的复现。

7B的复现也是一样的,只不过他需要再次下载7B的模型。

以下是7B和7B-chat自动下载的文件夹。

 

 

 

 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值