#使用mindie部署qwen-vl系列模型,在python脚本中对图片进行编码,然后使用openai格式的接口发送图片推理请求
import requests
import base64
import json

def query_vl_model(image_path, prompt="描述这张图", api_url="http://127.0.0.1:8025/v1/chat/completions", model="qwen-vl", max_tokens=1024):
    """
    使用Base64编码的图片查询视觉语言模型

    Args:
        image_path (str): 图片文件路径
        prompt (str): 文本提示
        api_url (str): API端点URL
        model (str): 模型名称
        max_tokens (int): 最大输出token数

    Returns:
        dict: API响应数据
    """

    # 读取图片并转换为base64编码
    try:
        with open(image_path, "rb") as image_file:
            base64_image = base64.b64encode(image_file.read()).decode('utf-8')
    except Exception as e:
        raise Exception(f"Error reading image file: {e}")

    # 根据图片扩展名确定MIME类型
    if image_path.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
        mime_type = f"image/{'jpeg' if image_path.lower().endswith(('.jpg', '.jpeg')) else image_path.split('.')[-1].lower()}"
        if mime_type == "image/jpg":
            mime_type = "image/jpeg"
    else:
        mime_type = "image/jpeg"  # 默认使用jpeg

    # 构建请求数据
    payload = {
        "model": model,
        "messages": [
            {
                "role": "user",
                "content": [
                    {"type": "text", "text": prompt},
                    {
                        "type": "image_url",
                        "image_url": f"data:image/jpeg;base64,{base64_image}"

                    }
                ]
            }
        ],
        "max_tokens": max_tokens
    }

    # 设置请求头
    headers = {
        "Content-Type": "application/json"
    }

    # 发送请求
    try:
        response = requests.post(api_url, headers=headers, data=json.dumps(payload))
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        raise Exception(f"API request failed: {e}")

# 使用示例
if __name__ == "__main__":
    image_path = "/data/sh/VL/bus.jpg"  # 替换为你的图片路径

    try:
        result = query_vl_model(image_path, "描述这张图")
        print("Response:")
        print(json.dumps(result, indent=2, ensure_ascii=False))
    except Exception as e:
        print(f"Error: {e}")
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐