comfyui-lmstudio-image-to-text-node:集成LM Studio本地API,支持图像描述与文本生成

A custom node for ComfyUI that integrates LM Studio's vision models to generate text descriptions of images. It provides a flexible and customizable way to add image-to-text capabilities to your ComfyUI workflows, working with LM Studio's local API.

Branch1Tags0
FilesLast commitLast update
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago

LM Studio 节点兼容 ComfyUI

此扩展为 ComfyUI 提供了自定义节点,集成了 LM Studio 的功能。它主要包含以下两大功能:

  1. 图像转文本:使用视觉模型生成图像的文本描述。
  2. 文本生成:根据给定的提示使用语言模型生成文本。

这两个节点均设计为与 LM Studio 的本地 API 配合使用,为您的 ComfyUI 工作流提供灵活且可定制的增强方式。

工作流示例

以下是一个使用 LM Studio 节点的 ComfyUI 工作流示例:

LM Studio 节点工作流

功能

  • 使用 LM Studio 的视觉模型生成图像的文本描述
  • 使用 LM Studio 的语言模型根据提示生成文本
  • 可定制的系统提示
  • 灵活的模型选择
  • 可配置的服务器地址和端口
  • 调试模式用于故障排除

安装

  1. 将此存储库克隆到您的 ComfyUI custom_nodes 目录下:
    cd /path/to/ComfyUI/custom_nodes
    git clone https://github.com/mattjohnpowell/comfyui-lmstudio-nodes.git
    
  2. 重新启动 ComfyUI 或重新加载自定义节点。

使用

LM Studio 图像转文本节点

将 "LM Studio 图像转文本" 节点添加到您的 ComfyUI 工作流中。将图像输出连接到节点的 "image" 输入。

输入

  • image(必填):待描述的输入图像。
  • text_input(必填):图像描述的提示。默认:"这幅图像里有什么?"
  • model(必填):要使用的 LM Studio 视觉模型名称。默认:"billborkowski/llava-NousResearch_Nous-Hermes-2-Vision-GGUF"
  • system_prompt(必填):设置 AI 上下文的系统提示。默认:"这是一个用户和助手之间的对话。助手正在帮助用户描述一幅图像。"
  • ip_address(必填):您的 LM Studio 服务器的 IP 地址。默认:"localhost"
  • port(必填):您的 LM Studio 服务器的端口号。默认:1234
  • debug(可选):设置为 True 以进行详细日志记录。默认:False

输出

  • 描述:输入图像生成的文本描述。

LM Studio 文本生成节点

将 "LM Studio 文本生成" 节点添加到您的 ComfyUI 工作流中。

输入

  • prompt(必填):文本生成的输入提示。
  • model(必填):要使用的 LM Studio 语言模型名称。默认:"TheBloke/Llama-2-13B-chat-GGUF"
  • system_prompt(必填):设置 AI 上下文的系统提示。默认:"你是一个有帮助的人工智能助手。"
  • ip_address(必填):您的 LM Studio 服务器的 IP 地址。默认:"localhost"
  • port(必填):您的 LM Studio 服务器的端口号。默认:1234
  • max_tokens(可选):生成的最大标记数。默认:1000
  • temperature(可选):控制生成的随机性。默认:0.7
  • debug(可选):设置为 True 以进行详细日志记录。默认:False

输出

  • 生成文本:根据输入提示生成的文本。

LM Studio 设置

  1. 在您的本地机器上安装并运行 LM Studio。
  2. 在 LM Studio 中加载适当的模型(图像转文本的视觉模型,文本生成的语言模型)。
  3. 确保 LM Studio API 正在运行且可访问(默认:http://localhost:1234)。

注意事项

  • 此扩展是基于 LM Studio 的图像转文本和文本生成代码示例改编而成。
  • model 参数无需从默认值更改,但在共享工作流时明确设置有助于确保一致性。
  • 如果您的 LM Studio 实例运行在不同的机器或端口上,请调整 ip_addressport

原始 LM Studio 代码

此扩展改编自 LM Studio 的代码示例。以下是原始的图像转文本示例:

# Adapted from OpenAI's Vision example
from openai import OpenAI
import base64
import requests

# Point to the local server
client = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")

# Ask the user for a path on the filesystem:
path = input("Enter a local filepath to an image: ")

# Read the image and encode it to base64:
base64_image = ""
try:
    image = open(path.replace("'", ""), "rb").read()
    base64_image = base64.b64encode(image).decode("utf-8")
except:
    print("Couldn't read the image. Make sure the path is correct and the file exists.")
    exit()

completion = client.chat.completions.create(
    model="moondream/moondream2-gguf",
    messages=[
        {
            "role": "system",
            "content": "This is a chat between a user and an assistant. The assistant is helping the user to describe an image.",
        },
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "What's in this image?"},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/jpeg;base64,{base64_image}"
                    },
                },
            ],
        }
    ],
    max_tokens=1000,
    stream=True
)

for chunk in completion:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

故障排除

如果您遇到任何问题:

  1. 通过将 debug 输入设置为 True 启用调试模式。
  2. 检查 ComfyUI 控制台以获取错误消息和调试输出。
  3. 确认 LM Studio 在指定的 IP 地址和端口上运行且可访问。
  4. 确保您在 LM Studio 中使用的是兼容的模型(图像到文本任务使用视觉模型,文本生成任务使用语言模型)。

如需进一步帮助,请在 GitHub 仓库中创建一个 issue。

许可

本项目遵循 MIT 许可 - 请参阅 LICENSE 文件以获取详细信息。

鸣谢

  • 本项目基于 ComfyUI 框架构建。
  • 受到 LM Studio 的图像到文本和文本生成示例代码的启发并进行了改编。

Introduction

A custom node for ComfyUI that integrates LM Studio's vision models to generate text descriptions of images. It provides a flexible and customizable way to add image-to-text capabilities to your ComfyUI workflows, working with LM Studio's local API.

Customize your domain