Giter Club home page Giter Club logo

kani-vision's Introduction

kani

Test Package Documentation Status PyPI Quickstart in Colab Discord

kani-vision

Installation

To install kani-vision, you must have at least Python 3.10. kani-vision uses extras to provide support for specific models - see below for model-specific instructions and other extras.

You can combine multiple extras into a single command, like pip install "kani-vision[openai,ascii]".

OpenAI (GPT-4V)

$ pip install "kani-vision[openai]"

LLaVA v1.5

Note: to install dependencies for LLaVA, you will have to run the following two commands as the LLaVA package installs some outdated incompatible dependencies by default:

$ pip install "kani-vision[llava]"
$ pip install --no-deps "llava @ git+https://github.com/haotian-liu/[email protected]"

Other Extras

  • pip install "kani-vision[ascii]": When using chat_in_terminal_vision(), this will display any images you provide to the model as ASCII art in your terminal :).

Quickstart

from kani import Kani
from kani.ext.vision import chat_in_terminal_vision
from kani.ext.vision.engines.openai import OpenAIVisionEngine

# add your OpenAI API key here
api_key = "sk-..."
engine = OpenAIVisionEngine(api_key, model="gpt-4-vision-preview", max_tokens=512)
ai = Kani(engine)

# use `!path/to/file.png` to provide an image to the engine, e.g. `Please describe this image: !kani-logo.png`
# or use a URL: `Please describe this image: !https://example.com/image.png`
chat_in_terminal_vision(ai)

Usage

This section assumes that you're already familiar with the basic usage of kani. If not, go check out the kani docs first!

kani-vision provides two main features to extend kani with vision using the message parts API.

Engines

The first are the vision engines, which are the underlying vision-language models (VLMs). kani-vision comes with support for two VLM engines, GPT-4V (OpenAI's hosted model) and LLaVA v1.5 (an open-source extension of Vicuna):

Model Name Extra Capabilities Engine
GPT-4V openai ๐Ÿ›  ๐Ÿ“ก kani.ext.vision.engines.openai.OpenAIVisionEngine
LLaVA v1.5 llava 1 ๐Ÿ”“ ๐Ÿ–ฅ ๐Ÿš€ kani.ext.vision.engines.llava.LlavaEngine

Legend

  • ๐Ÿ› : Supports function calling.
  • ๐Ÿ”“: Open source model.
  • ๐Ÿ–ฅ: Runs locally on CPU.
  • ๐Ÿš€: Runs locally on GPU.
  • ๐Ÿ“ก: Hosted API.

To initialize an engine, you use it the same way as in normal kani! All vision engines are interchangeable with normal kani engines.

Message Part

The second feature you need to be familiar with is the ImagePart, the core way of sending messages to the engine. To do this, when you call the kani round methods (i.e. Kani.chat_round or Kani.full_round or their str variants), pass a list rather than a string:

from kani import Kani
from kani.ext.vision import ImagePart
from kani.ext.vision.engines.llava import LlavaEngine

engine = LlavaEngine("liuhaotian/llava-v1.5-7b")
ai = Kani(engine)

# notice how the arg is a list of parts rather than a single str!
msg = await ai.chat_round_str([
    "Please describe this image:",
    ImagePart.from_path("path/to/image.png")
])
print(msg)

You can also define images from a URL, raw PNG binary or a Pillow Image, using ImagePart.from_url, ImagePart.from_bytes, or ImagePart.from_image, respectively.

See the examples for more.

Terminal Utility

Finally, kani-vision comes with an additional utility to chat with a VLM in your terminal, chat_in_terminal_vision.

This utility allows you to provide images on your disk or on the internet inline by prepending it with an exclamation point:

>>> from kani.ext.vision import chat_in_terminal_vision
>>> chat_in_terminal_vision(ai)
USER: Please describe this image: !path/to/image.png and also this one: !https://example.com/image.png

Footnotes

  1. See the installation instructions. You may also need to install PyTorch manually. โ†ฉ

kani-vision's People

Contributors

zhudotexe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.