大模型私有化部署实践(八):多模态大模型

340 阅读3分钟

专栏

简介

  • 多模态大模型(Multimodal Large Language Models, MLLMs)是人工智能领域的新星,它突破了传统单一模态模型的局限,能够像人类一样理解和处理多种形式的信息,包括文本、图像、音频、视频等。

  • 想象一下,一个模型不仅能读懂你的文字,还能看懂你分享的图片,听懂你哼唱的旋律,甚至理解视频中人物的情感。这就是多模态大模型的魅力所在!

MLLMs 的核心在于

  • 统一表示:  将不同模态的数据映射到同一语义空间,例如将“猫”的图片和“cat”的文本编码为相似的向量表示。
  • 跨模态理解:  学习不同模态数据之间的关联,例如理解图片中的物体与描述它的文本之间的关系。
  • 多模态生成:  根据一种模态的信息生成另一种模态的内容,例如根据文字描述生成图像,或根据图像生成文字描述

应用前景广阔

  • 更智能的人机交互:  打造能够理解用户意图、提供个性化服务的智能助手。

  • 更高效的内容创作:  辅助创作者进行图文、视频等多媒体内容的创作和编辑。

  • 更精准的信息检索:  实现跨模态的信息搜索,例如用图片搜索相关的文字内容。

  • 更深入的科学研究:  助力科学家分析海量多模态数据,例如医疗影像分析、天文数据分析等。

  • MLLMs 的发展仍处于早期阶段,面临着数据、算法、算力等方面的挑战。  但随着技术的不断进步,MLLMs 有望引领人工智能进入一个全新的时代,让机器真正理解并融入我们的世界。

Qwen的多模态

  • Qwn2-VL-7B-Instruct, 显存有限,拿这个举例,使用vllm部署, 这边提前用modelscope下载了qwen的视觉模型
python -m vllm.entrypoints.openai.api_server
--model /root/big_model/Qwen/Qwen2-VL-7B-Instruct 
--tensor-parallel-size 1 
--host 0.0.0.0
--port 5003
--served-model-name /Qwen/Qwen2-VL-7B-Instruct
--cpu-offload-gb 2.0 
--trust-remote-code 
--enforce-eager 
--max-model-len 6666
--gpu-memory-utilization 1.0
  • prompt调用的python代码, 其中image_url可以是阿里云的oss,也可以是自己搭建的图片存储服务器,多模态大模型也可以理解或者微调使其理解折线图的突增突降
import datetime
import json

import requests

if __name__ == '__main__':
    t1 = datetime.datetime.now()
    messages = [{
        'role':
            'user',
        'content': [{
            'type': 'text',
            'text': """描述下下面的图片, 300字作业必须带美中不足字样""",
        }, {
            'type': 'image_url',
            'image_url': {
                'url':
                    'http://xxx:5000/uploads/25fb7a6a304328e10222.jpeg',
            },
        }]
    }]
    headers = {
        'Content-Type': 'application/json'
    }
    data = {
        "model": "/Qwen/Qwen2-VL-7B-Instruct",
        "messages": messages
    }
    response = requests.post('http://xxxx:5003/v1/chat/completions', headers=headers, data=json.dumps(data),
                             timeout=(30.0, 360.0))
    print(json.loads(response.text)['choices'][0]['message']['content'])
    print('____________________________')
    end = datetime.datetime.now()
    print('res ,cost:+++++++++++++++++++++++++++++++', str(end - t1))

  • 简单实现的python服务端上传下载代码
from flask import Flask, request, send_file, jsonify
import os

app = Flask(__name__)

# 设置图片存储目录
UPLOAD_FOLDER = 'uploads'
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)

@app.route('/upload', methods=['POST'])
def upload_image():
    if 'file' not in request.files:
        return jsonify({"error": "No file part"}), 400

    file = request.files['file']

    if file.filename == '':
        return jsonify({"error": "No selected file"}), 400

    if file:
        filepath = os.path.join(UPLOAD_FOLDER, file.filename)
        file.save(filepath)
        return jsonify({"message": "File uploaded successfully", "filename": file.filename}), 200

@app.route('/download/<filename>', methods=['GET'])
def download_image(filename):
    filepath = os.path.join(UPLOAD_FOLDER, filename)
    if not os.path.exists(filepath):
        return jsonify({"error": "File not found"}), 404

    return send_file(filepath, as_attachment=True)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

stable-diffusion

  • 安装
cd /usr/local/src
conda create -n stable-diffusion python=3.11
conda activate stable-diffusion
cd /usr/local/src
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
cd stable-diffusion-webui
  • 安装必要的库(可选如果没安装的话)
sudo apt install wget git python3 python3-venv libgl1 libglib2.0-0
  • 继续安装
pip install -r requirements_versions.txt
pip install -r requirements.txt
python launch.py
  • 前端访问
开Web浏览器,输入http://127.0.0.1:7860来访问Web UI

在线stable-diffusion

  • liblib, 其中在线生图功能可以文生图,图生图等,也可以选择相应模型 1.png

2.png

3.png

LlmDeploy

  • 有一些多模态如,通过llmdeploy部署更方便,比如InternVL等

参考文章