[大模型]DeepSeek-7B-chat WebDemo 部署
0
2025-08-03
DeepSeek-7B-chat WebDemo部署DeepSpeek介绍
由70亿个参数组成的高级语言模型deepseek llm。它是在一个包含2万亿个中文和中文代币的庞大数据集上从零开始训练的。为了促进研究,deepseek已经为研究社区开放了deepseek llm 7b/67b base和deepseek llm 7b/67b环境准备
在autodl平台中租一个3090等24G显存的显卡机器,如下图所示选择PyTorch–gt;2.0.0–gt;3.8(ubuntu20.04)–gt;11.8(11.3版本以上的都)接下来可以打开刚刚租用服务器的JupyterLab,图像打开其中的步骤环境配置、模型下载和开始运行演示。
pip换源和安装依赖包lt;pre class=quot;brush:php;toolbar:falsequot;gt;# 升级 pippython -m pip install --upgrade pip# 更换 pypi 源加速库的安装 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simplepip install modelscope==1.9.5pip install Transformers==4.35.2pip install streamlit==1.24.0pip install句子==0.1.99pip install Accelerator==0.24.1pip install Transformers_stream_generator==0.0.4登录后复制模型下载
使用modelscope中的snapshot_download函数下载模型,第一个参数为模型名称,参数cache_dir为模型的下载路径。
在 /root/autodl-tmp 路径下新建 download.py文件并在其中输入以下内容,粘贴代码后记得保存文件,如下图所示。并运行 python /root/autodl-tmp/download.py 执行下载,模型大小为15 GB,下载模型大概需要10~20分钟lt;pre class=quot;brush:php;toolbar:falsequot;gt;import torchfrom modelscope import snapshot_download, AutoModel, AutoTokenizerfrom modelscope import GenerationConfigmodel_dir = snapshot_download('deepseek-ai/deepseek-llm-7b-chat',cache_dir='/root/autodl-tmp', revision='master')登录后复制准备
在/root/autodl-tmp登录后复制路径下新建chatBot.py登录后复制文件并在其中输入以下内容,粘贴代码后记得保存文件。下面的代码有很详细的注释,如有不懂的地方,欢迎提出问题。
lt;pre class=quot;brush:php;toolbar:falsequot;gt;#导入需要的库from Transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfigimport torchimport streamlit as st# 在侧边创建一个标题和一个链接with st.sidebar: st.markdown(quot;## DeepSeek LLMquot;) quot;[开源大模型食用指南self-llm](https://github.com/datawhalechina/self-llm.git)quot;#一个创建,用于选择最大长度,范围在0到1024之间,默认值为512 max_length = st.slider(quot;max_lengthquot;, 0, 1024, 512,step=1)#创建一个标题和一个副st.title(quot;? DeepSeek Chatbotquot;)st.caption(quot;?由 Self-LLM 提供支持的流式聊天机器人quot;)# 定义模型路径mode_name_or_path = '/root/autodl-tmp/deepseek-ai/deepseek-llm-7b-chat'#定义一个函数,用于获取模型和tokenizer@st.cache_resourcedef get_model(): # 从预训练的模型中获取tokenizer tokenizer = AutoTokenizer.from_pretrained(mode_name_or_path, trust_remote_code=True) # 从预训练的模型中获取模型,并设置模型参数 model = AutoModelForCausalLM.from_pretrained(mode_name_or_path, trust_remote_code=True,torch_dtype=torch.bfloat16, device_map=quot;autoquot;) # 从预训练的模型中获取生成配置 model. Generation_config = GenerationConfig.from_pretrained(mode_name_or_path) # 设置生成配置的pad_token_id为生成配置的eos_token_id model. Generation_config.pad_token_id = model. Generation_config.eos_token_id # 设置模型为评估模式 model.eval() return tokenizer, model# 加载Chatglm3的model和tokenizertokenizer, model = get_model()#session_state中没有quot;messagesquot;,则创建一个包含默认消息的列表if quot;messagesquot; not in s
t.session_state: st.session_state[quot;messagesquot;] = [{quot;角色quot;:quot;助理quot;,quot;内容quot;:quot;有什么可以帮您的?quot;}]# 查看session_state中的所有消息,并在聊天界面上显示st.session_state.messages中的msg: st.chat_message(msg[quot;rolequot;]).write(msg[quot;contentquot;])# 如果用户在聊天输入框中输入了内容,则执行以下操作if提示:= st.chat_input(): # 将用户的输入添加到session_state中的消息列表中 st.session_state.messages.append({quot;rolequot;: quot;userquot;, quot;contentquot;: prompt}) # 在聊天界面上显示用户的输入 st.chat_message(quot;userquot;).write(prompt) # 构建输入 input_tensor = tokenizer.apply_chat_template(st.session_state.messages, add_ Generation_prompt=True, return_tensors=quot;ptquot;) # 通过模型获得输出outputs = model.generate(input_tensor.to(model.device), max_new_tokens=max_length) # 解码模型的输出,并去除特殊标记response = tokenizer.decode(outputs[0][input_tensor.shape[1]:],skip_special_tokens=True) # 将模型的输出添加到session_state中的消息列表中 st.session_state.messages.append({quot;rolequot;: quot;assistantquot;, quot;contentquot;: response}) # 在聊天界面上显示模型的输出st.chat_message(quot;assistantquot;).write(response)登录后复制运行demo
在终端中运行以下命令,启动streamlit服务,并按照 autodl登录后复制的指示将端口映射到本地,然后在浏览器中打开链接http://localhost:6006/,即可看到聊天界面。
lt;pre class=quot;brush:php;toolbar:falsequot;gt;streamlit run /root/autodl-tmp/chatBot.py --server.address 127.0.0.1 --server.port 6006登录后复制
如下图:
以上就是[大模型]DeepSeek-7B-chat WebDemo部署的详细内容,更多请关注乐哥常识网其他相关文章!