首页 > 其他分享 >D2-InternLM-Chat-7B 智能对话 Demo

D2-InternLM-Chat-7B 智能对话 Demo

时间:2024-01-04 21:31:30浏览次数:34  
标签:code 7B demo InternLM Demo model root internlm

使用InternStudio中的 A100(1/4) 机器和InternLM-Chat-7B模型部署一个智能对话 Demo。

一、环境准备

D2-InternLM-Chat-7B 智能对话 Demo_开源

进入 conda 环境:

bash # 请每次使用 jupyter lab 打开终端时务必先执行 bash 命令进入 bash 中

使用以下命令从本地克隆一个已有的 pytorch 2.0.1 的环境

conda create --name internlm-demo --clone=/root/share/conda_envs/internlm-base

D2-InternLM-Chat-7B 智能对话 Demo_大模型_02

使用以下命令激活环境:

conda activate internlm-demo

D2-InternLM-Chat-7B 智能对话 Demo_部署_03

在环境中安装运行 demo 所需要的依赖:

# 升级pip

python -m pip install --upgrade pip
pip install modelscope==1.9.5
pip install transformers==4.35.2
pip install streamlit==1.24.0
pip install sentencepiece==0.1.99
pip install accelerate==0.24.1

D2-InternLM-Chat-7B 智能对话 Demo_部署_04

二、模型下载

InternStudio平台的 share 目录下已经为我们准备了全系列的 InternLM 模型,所以我们可以直接复制即可。使用如下命令复制:

mkdir -p /root/model/Shanghai_AI_Laboratory
cp -r /root/share/temp/model_repos/internlm-chat-7b /root/model/Shanghai_AI_Laboratory
  • -r 选项表示递归地复制目录及其内容

D2-InternLM-Chat-7B 智能对话 Demo_部署_05

------------------------------------------------------------------------------

也可以使用 modelscope 中的 snapshot_download 函数下载模型,第一个参数为 模型名称,参数 cache_dir 为模型的 下载路径

在 /root 路径下新建目录model,在目录下新建 download.py 文件并在其中输入以下内容,粘贴代码后记得保存文件,如下图所示。并运行 python /root/model/download.py 执行下载,模型大小为 14 GB,下载模型大概需要 10~20 分钟。

import torch
from modelscope import snapshot_download, AutoModel, AutoTokenizer
import os
model_dir = snapshot_download('Shanghai_AI_Laboratory/internlm-chat-7b', cache_dir='/root/model', revision='v1.0.3')

三、代码准备

先 clone 代码。在 /root 路径下新建 code 目录,然后切换路径, clone 代码:

cd /root/code
git clone https://gitee.com/internlm/InternLM.git

切换 commit 版本,与教程 commit 版本保持一致,可以让大家更好的复现。

cd InternLM
git checkout 3028f07cb79e5b1d7342f4ad8d11efad3fd13d17

D2-InternLM-Chat-7B 智能对话 Demo_bash_06

将 /root/code/InternLM/web_demo.py中 29 行和 33 行的模型更换为本地的 /root/model/Shanghai_AI_Laboratory/internlm-chat-7b

D2-InternLM-Chat-7B 智能对话 Demo_bash_07

D2-InternLM-Chat-7B 智能对话 Demo_bash_08

四、终端运行

在 /root/code/InternLM 目录下新建一个 cli_demo.py 文件,将以下代码填入其中:

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


model_name_or_path = "/root/model/Shanghai_AI_Laboratory/internlm-chat-7b"

tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_name_or_path, trust_remote_code=True, torch_dtype=torch.bfloat16, device_map='auto')
model = model.eval()

messages = []

print("=============Welcome to InternLM chatbot, type 'exit' to exit.=============")

while True:
    input_text = input("User  >>> ")
    if input_text == "exit":
        break
    response, history = model.chat(tokenizer, input_text, history=messages)
    messages.append((input_text, response))
    print(f"robot >>> {response}")

然后在终端运行以下命令,即可体验 InternLM-Chat-7B 模型的对话能力:

python /root/code/InternLM/cli_demo.py

对话效果如下所示:

D2-InternLM-Chat-7B 智能对话 Demo_开源_09

五、web demo 运行

本地配置端口配置:

在本地配置端口,可以通过 ssh 连接到服务器,然后将服务器的端口映射到本地,这样就可以在本地浏览器中访问服务器的端口了。

ssh-keygen -t rsa

D2-InternLM-Chat-7B 智能对话 Demo_部署_10

提示选择密钥文件的保存位置,默认情况下是在 ~/.ssh/ 目录中。按 Enter 键接受默认值或输入自定义路径。

公钥默认存储在 ~/.ssh/id_rsa.pub,可以通过系统自带的 cat 工具查看文件内容:

cat ~\.ssh\id_rsa.pub

将公钥复制到剪贴板中,然后回到 InternStudio 控制台,点击配置 SSH Key。如下图所示:

D2-InternLM-Chat-7B 智能对话 Demo_开源_11

D2-InternLM-Chat-7B 智能对话 Demo_部署_12

在本地终端输入以下指令 .6006 是在服务器中打开的端口,而 33799 是根据开发机的端口进行更改:

ssh -CNg -L 6006:127.0.0.1:6006 [email protected] -p 33799

切换到开发机 VScode 中,运行 /root/code/InternLM 目录下的 web_demo.py 文件,输入以下命令:

bash
conda activate internlm-demo  # 首次进入 vscode 会默认是 base 环境,所以首先切换环境
cd /root/code/InternLM
streamlit run web_demo.py --server.address 127.0.0.1 --server.port 6006

D2-InternLM-Chat-7B 智能对话 Demo_bash_13

在本地浏览器输入 http://127.0.0.1:6006 ,下面是加载界面:

D2-InternLM-Chat-7B 智能对话 Demo_部署_14

在加载完模型之后,就可以与 InternLM-Chat-7B 进行对话:

D2-InternLM-Chat-7B 智能对话 Demo_部署_15

D2-InternLM-Chat-7B 智能对话 Demo_大模型_16


标签:code,7B,demo,InternLM,Demo,model,root,internlm
From: https://blog.51cto.com/morcake/9105322

相关文章

  • Linux驱动开发笔记(六):用户层与内核层进行数据传递的原理和Demo
    前言  驱动作为桥梁,用户层调用预定义名称的系统函数与系统内核交互,而用户层与系统层不能直接进行数据传递,进行本篇主要就是理解清楚驱动如何让用户编程来实现与内核的数据交互传递。<br>温故知新设备节点是应用层(用户层)与内核层交互;使用预先的结构体进行操作,如系统open函数......
  • 使用CMakeLists.txt创建一个动态库工程Demo给main程序使用
    主要需求是把hello程序编译动态库,再main程序或者第三方程序执行的时候动态加载。工程目录如下:$ls-al*-rw-r--r--1neuti197609352Oct2019:30CMakeLists.txtinclude:total1drwxr-xr-x1neuti1976090Oct2019:30./drwxr-xr-x1neuti1976090Oct201......
  • YCOJ227B 摆放鞋子
    题意给定一个由\(['L','R']\)组成的网格图。每个点有一个方向,用\(['U','D','L','R']\)表示。每次操作可以选择两个相邻的点,使其中一个顺时针旋转另一个逆时针旋转。称一个匹配为站在两个相邻点所朝的方向上使得左边是\(L\)右边是\(R\)。Sol考虑操作前后不变的东......
  • LLaVA-v1.5-7B:实现先进多模态学习的开源AI
    引言LLaVA-v1.5-7B是一个开源大型多模态模型(LMM),它通过结合视觉指令调整(VisualInstructionTuning)技术,展示了在多模态理解和生成任务上的卓越性能。该模型特别注重简洁性和数据效率,利用CLIP-ViT-L-336px与多层感知器(MLP)投影以及包含学术任务导向的视觉问答(VQA)数据,来建立更强的基准......
  • svelte的一些基础demo
    脚手架Vite:vite是集成了svelte,初始化的时候选择svelte就行了。npminitviteSvelteKit:底层基于vite的更上层框架,类似于nextjs。[email protected]文件结构和vue类似svelte文件是.svelte结尾的文件,比如demo.svelte......
  • codeforces刷题(1100):1917B_div2
    模板B、EraseFirstorSecondLetter跳转原题点击此:该题地址1、题目大意  给你一个字符串,可以执行任意次以下操作,生成最终的字符串(不可为空),问你能生成的不重复字符串数为多少。操作一:删除字符串第一个字符;操作二:删除字符串第二个字符。2、题目解析  发现,操作一:即选......
  • 随机点名demo+文字特效
    上代码:一、HTML部分<div><h2>随机点名</h2><spanid="name"></span></div><buttonid="button-text">点一下不要钱</button>二、CSS部分*{margin:0;padd......
  • 初中英语优秀范文100篇-037Books or TV?-书还是电视
    PDF格式公众号回复关键字:SHCZFW037记忆树1BooksorTV?IpreferbooksbecausebookshavemanyadvantagesoverTV.翻译书籍还是电视?我更喜欢书籍,因为相比电视,书籍有许多优势简化记忆喜欢句子结构1"BooksorTV?":这是一个选择疑问句,用来询问对方对某事物的偏好。......
  • demo
    importReact,{useState,useEffect}from'react';import{Modal,Button,Form,Checkbox}from'antd';import{useForm}from'antd/lib/form/Form';interfaceResourceSelectProps{form:ReturnType<typeofuseForm>......
  • 003元素定位方式与项目demo创建
    一、环境搭建1、创建项目,添加java-client依赖包             新建maven项目,引入java-client依赖包       2、创建并编写代码 测试运行以上代码,运行前需打开Appnium.Appnium没有打开时,运行会报错:Connectionrefused:connect 二、......