第二课主要以demo为主, 主要以作业为主(作业位置:http://t.csdnimg.cn/z8Xim)
第二课趣味Demo笔记
示例demo git文档地址:GitHub - InternLM/Tutorial 选择cap2分支
-
开发机内环境配置
# studio-conda -o internlm-base -t demo # conda create -n xxxx python==3.10 # conda activate demo # conda install pytorch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 pytorch-cuda=11.7 -c pytorch -c nvidia
-
安装其他依赖
pip install huggingface-hub==0.17.3 pip install transformers==4.34 pip install psutil==5.9.8 pip install accelerate==0.24.1 pip install streamlit==1.32.2 pip install matplotlib==3.8.3 pip install modelscope==1.9.5 pip install sentencepiece==0.1.99
第一个demo
-
下载 InternLM2-Chat-1.8B 模型
在开发机内/root/share目录下有已经准备好的模型。也可以通过huggingface或者modlescope进行下载。
model_scope下载示例:
模型repo_id可以从github中查看。InternLM · GitHub, 选择需要的模型,查看readme
import os from modelscope.hub.snapshot_download import snapshot_download # 创建保存模型目录 os.system("mkdir /root/models") # save_dir是模型保存到本地的目录 save_dir="/root/models" snapshot_download("Shanghai_AI_Laboratory/internlm2-chat-1_8b", cache_dir=save_dir, revision='v1.1.0')
-
第一个demo使用命令行模式:
# vim cli_demo.py import torch from transformers import AutoTokenizer, AutoModelForCausalLM model_name_or_path = "/root/models/Shanghai_AI_Laboratory/internlm2-chat-1_8b" tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, trust_remote_code=True, device_map='cuda:0') model = AutoModelForCausalLM.from_pretrained(model_name_or_path, trust_remote_code=True, torch_dtype=torch.bfloat16, device_map='cuda:0') model = model.eval() system_prompt = """You are an AI assistant whose name is InternLM (书生·浦语). - InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless. - InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文. """ messages = [(system_prompt, '')] print("=============Welcome to InternLM chatbot, type 'exit' to exit.=============") while True: input_text = input("\nUser >>> ") input_text = input_text.replace(' ', '') if input_text == "exit": break length = 0 for response, _ in model.stream_chat(tokenizer, input_text, messages): if response is not None: print(response[length:], flush=True, end="") length = len(response)
第二个demo
使用streamlit运行:(streamlit和gradio, Dash为几个主流的快速生成AI应用的框架)
# streamlit run /root/Tutorial/helloworld/bajie_chat.py --server.address 127.0.0.1 --server.port 6006
打开powershell映射本地端口: (-p 为开发机的ssh端口, 6006为服务器应用端口, 127.0.0.1:6006为映射为本机的端口)
ssh -CNg -L 6006:127.0.0.1:6006 root@ssh.intern-ai.org.cn -p 38374
第三个demo
加载internlm2-chat-7b模型需要30% A100的算力
升降配置需要先关闭开发机
具体操作内容参见作业
第四个demo
浦语*灵笔 internlm-xcompose2需要50%的A100算力
internlm-xcomposer2-7b 图文写作 internlm-xcomposer2-vl-7b 图文理解
浦语·灵笔2
是基于 书生·浦语2
大语言模型研发的突破性的图文多模态大模型,具有非凡的图文写作和图像理解能力,在多种应用场景表现出色,总结起来其具有:
-
自由指令输入的图文写作能力:
浦语·灵笔2
可以理解自由形式的图文指令输入,包括大纲、文章细节要求、参考图片等,为用户打造图文并貌的专属文章。生成的文章文采斐然,图文相得益彰,提供沉浸式的阅读体验。 -
准确的图文问题解答能力:
浦语·灵笔2
具有海量图文知识,可以准确的回复各种图文问答难题,在识别、感知、细节描述、视觉推理等能力上表现惊人。 -
杰出的综合能力:
浦语·灵笔2-7B
基于书生·浦语2-7B
模型,在13项多模态评测中大幅领先同量级多模态模型,在其中6项评测中超过GPT-4V
和Gemini Pro
。