人工智能之基于阿里云快速搭建Llama-3.2-11B-Vision-Instruct

人工智能之基于阿里云快速搭建Llama-3.2-11B-Vision-Instruct

需求描述

  1. 基于阿里云搭建图片生成文字模型,模型名称:LLM-Research/Llama-3.2-11B-Vision-Instruct
  2. 使用上述模型输入图片生成文字,模型路径

业务实现

阿里云配置

阿里云配置如下:

代码验证

python 复制代码
import requests
import torch
from PIL import Image
from transformers import MllamaForConditionalGeneration, AutoProcessor
from modelscope import snapshot_download
model_id = "LLM-Research/Llama-3.2-11B-Vision-Instruct"
model_dir = snapshot_download(model_id, ignore_file_pattern=['*.pth'])

model = MllamaForConditionalGeneration.from_pretrained(
    model_dir,
    torch_dtype=torch.bfloat16,
    device_map="auto",
)
processor = AutoProcessor.from_pretrained(model_dir)

url = "https://vcg01.cfp.cn/creative/vcg/800/new/VCG41519623066.jpg"
image = Image.open(requests.get(url, stream=True).raw)

messages = [
    {"role": "user", "content": [
        {"type": "image"},
        {"type": "text", "text": "如果你需要对图片描述,你会怎么描述?"}
    ]}
]
input_text \
       = processor.apply_chat_template(messages, add_generation_prompt=True)
inputs = processor(image, input_text, return_tensors="pt").to(model.device)

output = model.generate(**inputs, max_new_tokens=30)
print(processor.decode(output[0]))

结果展示如下,生成的文本信息如下:

The image shows a baby in a white outfit being held by an adult, with the baby looking at the camera and smiling.

从多次搭建的经验来看,建议在搭建模型相关的内容的时候,优先选择阿里云的相关服务,很多东西阿里云的容器云已经提供底层技术,可以很快的使用modelscope提供的操作内容进行快速的模型搭建。

相关推荐
小阿鑫10 分钟前
不要太信任Cursor,这位网友被删库了。。。
人工智能·aigc·cursor·部署mcp
说私域1 小时前
基于定制开发开源 AI 智能名片 S2B2C 商城小程序的热点与人工下发策略研究
人工智能·小程序
GoGeekBaird2 小时前
GoHumanLoopHub开源上线,开启Agent人际协作新方式
人工智能·后端·github
Jinkxs2 小时前
测试工程师的AI转型指南:从工具使用到测试策略重构
人工智能·重构
别惹CC2 小时前
Spring AI 进阶之路01:三步将 AI 整合进 Spring Boot
人工智能·spring boot·spring
stbomei4 小时前
当 AI 开始 “理解” 情感:情感计算技术正在改写人机交互规则
人工智能·人机交互
Moshow郑锴9 小时前
人工智能中的(特征选择)数据过滤方法和包裹方法
人工智能
TY-20259 小时前
【CV 目标检测】Fast RCNN模型①——与R-CNN区别
人工智能·目标检测·目标跟踪·cnn
CareyWYR10 小时前
苹果芯片Mac使用Docker部署MinerU api服务
人工智能
失散1311 小时前
自然语言处理——02 文本预处理(下)
人工智能·自然语言处理