找别人做网站注意什么,如何通过网站做网上报名系统,wordpress 权限破解,手机设计图纸软件0#xff0c;背景
最近测试Ollama#xff0c;发现之前直接下载开源模型在我电脑上都跑不动的模型#xff0c;居然也能运行了#xff08;AMD 7840HS核显/32GB内存#xff09;#xff0c;突发奇想那些多模态大模型能不能基于Python接口使用#xff0c;所以决定尝试一下。…0背景
最近测试Ollama发现之前直接下载开源模型在我电脑上都跑不动的模型居然也能运行了AMD 7840HS核显/32GB内存突发奇想那些多模态大模型能不能基于Python接口使用所以决定尝试一下。
1安装环境与模型选择
安装过程略可以参考文章Ollama在Windows11部署与使用QWen2模型_ollama run qwen2 内容-CSDN博客
模型选择上选取多模态大模型BakLLaVA BakLLaVA 是一款由 SkunkworksAI 与 LAION、Ontocord 和 Skunkworks OSS AI 团队合作开发的多模态语言模型通过改进基础模型、调整训练过程、引入定制数据集及架构优化实现了接近 GPT-4 级别的多模态语言处理能力。它在图像描述生成、语音识别和理解、自然语言问答等应用中表现出色并且支持多种 GPU 配置具有较强的适应性。作为开源项目BakLLaVA 为研究人员和开发者提供了广阔的探索和改进空间。 ollama run bakllava
2Ollama的Python接口测试
使用指令安装库
pip install ollama
然后运行下面的程序测试
import ollamaresponse ollama.chat(modelbakllava, messages[{role: user,content: Why is the sky blue?,},
])print(response[message][content])
能够得到返回结果 3代码实现
1导入必要的库
首先我们需要导入处理图像和与 Ollama 模型交互所需的库。
import base64
from io import BytesIO
from PIL import Image
import ollama2定义图像转换函数
我们需要一个函数来将 PIL 图像转换为 Base64 编码字符串。这对于将图像数据发送给模型是必要的步骤。
# 将PIL图像转换为Base64编码字符串
def convert_to_base64(pil_image):buffered BytesIO()# 将图像转换为RGB模式pil_image pil_image.convert(RGB)pil_image.save(buffered, formatJPEG)img_str base64.b64encode(buffered.getvalue()).decode(utf-8)return img_str3定义图像加载函数
该函数用于从指定路径加载图像并将其转换为 Base64 编码字符串。
# 从指定路径加载图像并转换为Base64编码字符串
def load_image(file_path):pil_image Image.open(file_path)return convert_to_base64(pil_image)4定义与模型交互的函数
这个函数将图像和问题发送给 BakLLaVA 模型并获取模型的回答。
# 将图像和问题发送给Ollama的bakllava模型并获取回答
def chat_with_model(image_base64, question):response ollama.chat(modelbakllava, messages[{role: user,content: question,images: [image_base64]}])return response[message][content]5主程序逻辑
在主程序中我们加载图像将其转换为 Base64 编码然后向模型提问并打印模型的回答。
if __name__ __main__:# 图片所在地址file_path 2.jpg# 加载并转换图像image_b64 load_image(file_path)# 提问question What is written in the picture, and answer the question.# 与模型对话answer chat_with_model(image_b64, question)# 打印回答print(answer)上传的图片其实很简单如下 完整程序如下
import base64
from io import BytesIO
from PIL import Image
import ollama# 将PIL图像转换为Base64编码字符串
def convert_to_base64(pil_image):buffered BytesIO()# 将图像转换为RGB模式pil_image pil_image.convert(RGB)pil_image.save(buffered, formatJPEG)img_str base64.b64encode(buffered.getvalue()).decode(utf-8)return img_str# 从指定路径加载图像并转换为Base64编码字符串
def load_image(file_path):pil_image Image.open(file_path)return convert_to_base64(pil_image)# 将图像和问题发送给Ollama的phi3模型并获取回答
def chat_with_model(image_base64, question):response ollama.chat(modelbakllava, messages[{role: user,content: question,images: [image_base64]}])return response[message][content]if __name__ __main__:# 图片所在地址file_path 2.jpg# 加载并转换图像image_b64 load_image(file_path)# 提问question What is written in the pictureand answer the question.# 与模型对话answer chat_with_model(image_b64, question)# 打印回答print(answer)4运行得到结果