北大最强代码大模型CodeShell-7B 本地docker部署记录
环境
显卡内存参考20G大小
服务器:创建部署目录
# 若有git则直接进入step1,没有进入step2
# step 1: 安装git
apt-get update
apt-get install git -y
# step2: 在/home/自定义目录(一般用户名)/,目录下载github示例文件,在下载的/home/自定义目录/codeshell/目录下载模型文件
# github示例文件 (下载的git本地目录叫codeshell,故目录全路径为 "/home/自定义目录/codeshell/" )
git clone https://github.com/WisdomShell/codeshell.git
# 模型文件,二选一。国外好处是可以找老板要科学上网,并多摸一会🐟
cd ./codeshell
git clone https://huggingface.co/WisdomShell/CodeShell-7B-Chat # 国外 需科学上网
# or
git clone https://www.modelscope.cn/WisdomShell/CodeShell-7B-Chat.git # 国内
至此,/home/自定义目录/codeshell/下存放示例文件,codeshell目录下有一个叫CodeShell-7B-Chat的目录存放模型。
服务器:创建容器
docker run --gpus "device=0" -it --name 自定义容器名(这里我用的codeshell) -v /home/自定义目录/codeshell:/workspace/codeshell -p 7861:7861 -d pytorch/pytorch:2.1.0-cuda12.1-cudnn8-devel
创建容器的同时,做了目录挂载-v,方便后续操作文件,也做了端口映射,方便后续使用特定端口。
服务器:启动容器
docker start codeshell
服务器:进入容器
docker exec -it codeshell /bin/bash
容器内:初次使用创建python环境
conda create -n codeshell python=3.10
按y
容器内:激活conda环境
#针对没激活activate的容器,初次使用激活activate
source activate
conda activate codeshell
容器内:更新pip源
python -m pip install --upgrade pip
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
容器内:下载环境依赖库
# 默认进入容器后在workspace目录下,所以需要切换目录
cd ./codeshell
pip install -r requirement.txt
cd ./demos
pip install -r requirements_web_demo.txt
容器内:检测torch、gpu是否可用
python
import torch
print(torch.cuda.is_available()) # 期望输出为True
exit()
容器内:修改示例文件web_demo.py
# 修改模型目录
DEFAULT_CKPT_PATH = '/workspace/codeshell/CodeShell-7B-Chat'
# 修改web端口为7861,和创建容器时的映射端口一致
parser.add_argument("--server-port", type=int, default=7861, help="Demo server port.")
# 修改默认ip为0.0.0.0,方便远程映射时保留主机ip
parser.add_argument("--server-name", type=str, default="0.0.0.0", help="Demo server name.")
容器内:运行web_demo.py
# 当前目录为/workspace/codeshell/demos,若为其他目录,运行时可cd目录,也可python /workspace/codeshell/demos/web_demo.py
python web_demo.py
本地客户机:即与服务器非同一台电脑,远程连接服务器部署网页
unix服务器部署模型,自己的笔记本windows连接服务器~
ssh -L 7861:0.0.0.0:7861 用户名@服务器ip
#回车输入服务器用户密码
本地客户机:打开浏览器输入127.0.0.1:7861
显示codeshell主页
至此部署完毕,若要尝试自定义api接口,可在服务器查看示例文件demos下的api示例、cli示例,利用huggingface transformer接口加载模型使用。(记得切换模型路径等配置)
参考引用
[1] https://github.com/WisdomShell/codeshell