下载模型:
bash展开代码modelscope download Qwen/Qwen2.5-VL-7B-Instruct --local_dir ./Qwen/Qwen2.5-VL-7B-Instruct
模型位置:
bash展开代码/mnt/jfs/model/Qwen/Qwen2.5-VL-7B-Instruct
开启api:
bash展开代码python -m vllm.entrypoints.openai.api_server \ --model /mnt/jfs/model/Qwen/Qwen2.5-VL-7B-Instruct \ --gpu_memory_utilization 0.9 \ --tensor_parallel_size 2 \ --served-model-name gpt \ --port 8000
请求:
bash展开代码import base64
import requests
def analyze_custom_image():
# 配置服务器信息
server_ip = "10.130.18.35"
port = 8000
api_endpoint = f"http://{server_ip}:{port}/v1/chat/completions"
# 认证信息(需与服务器启动参数一致)
api_key = "token-abc123" # 替换为实际API密钥
# 图片URL
image_url = "https://img1.baidu.com/it/u=3132077889,2667724444&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1200"
# 提示文本
text_a = "请分析这张图片"
try:
# 1. 下载网络图片并编码为base64
response = requests.get(image_url)
response.raise_for_status()
base64_data = base64.b64encode(response.content).decode('utf-8')
# 2. 构建请求头
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
# 3. 组装请求体
payload = {
"model": "gpt", # 确认模型名称与服务器一致
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": text_a},
{
"type": "image_url",
"image_url": {
# 修改MIME类型为image/jpeg
"url": f"data:image/jpeg;base64,{base64_data}"
}
}
]
}
],
"max_tokens": 4096 # 增加token限额以获取更详细描述
}
# 4. 发送请求
response = requests.post(api_endpoint, headers=headers, json=payload)
response.raise_for_status() # 自动抛出HTTP错误
# 5. 解析结果
result = response.json()["choices"][0]["message"]["content"]
return f"图像分析结果:\n{result}"
except requests.exceptions.RequestException as e:
return f"网络请求失败:{str(e)}"
except KeyError:
return f"响应格式异常,原始响应:{response.text}"
except Exception as e:
return f"未知错误:{str(e)}"
# 执行分析
if __name__ == "__main__":
analysis_result = analyze_custom_image()
print(analysis_result)
本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!