【模型】Qwen2-VL 服务端UI
1. 前言
最近在测试VLM模型,发现官方的网页demo,代码中视频与图片分辨率可能由于高并发设置的很小,导致达不到预期效果,于是自己研究了一下,搞了一个简单的前端部署,自己在服务器部署了下UI界面,方便在本地笔记本进行测试。
2.代码
import streamlit as st
from PIL import Image
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
from qwen_vl_utils import process_vision_info
import torch
import os
# 加载模型和处理器 (只加载一次)
@st.cache_resource # 这个装饰器会缓存模型和处理器
def load_model():
model = Qwen2VLForConditionalGeneration.from_pretrained(
"../qwen2_vl/model_7b/", torch_dtype=torch.float16, device_map="auto"
)
processor = AutoProcessor.from_pretrained("../qwen2_vl/model_7b/")
return model, processor
# 加载模型和处理器
model, processor = load_model()
def load_image(image_file):
img = Image.open(image_file)
return img
# Function to load and resize image to fixed height
def resize_image_to_height(image, height):
# Resize image keeping the aspect ratio
width = int(image.width * height / image.height)
return image.resize((width, height))
# 处理输入
def process_input(messages):
# Preparation for inference
t