Elffuss BraidHub

kikocis/mlx-vlm public

main · 3.2 KiBRenderedRawPermalink
1# MLX-VLM
2
3MLX-VLM is a package for inference and fine-tuning of Vision Language Models (VLMs) on your Mac using MLX.
4
5## Table of Contents
6- [Installation](#installation)
7- [Usage](#usage)
8 - [Command Line Interface (CLI)](#command-line-interface-cli)
9 - [Chat UI with Gradio](#chat-ui-with-gradio)
10 - [Python Script](#python-script)
11- [Multi-Image Chat Support](#multi-image-chat-support)
12 - [Supported Models](#supported-models)
13 - [Usage Examples](#usage-examples)
14- [Fine-tuning](#fine-tuning)
15
16## Installation
17
18The easiest way to get started is to install the `mlx-vlm` package using pip:
19
20```sh
21pip install mlx-vlm
22```
23
24## Usage
25
26### Command Line Interface (CLI)
27
28Generate output from a model using the CLI:
29
30```sh
31python -m mlx_vlm.generate --model mlx-community/Qwen2-VL-2B-Instruct-4bit --max-tokens 100 --temp 0.0 --image http://images.cocodataset.org/val2017/000000039769.jpg
32```
33
34### Chat UI with Gradio
35
36Launch a chat interface using Gradio:
37
38```sh
39python -m mlx_vlm.chat_ui --model mlx-community/Qwen2-VL-2B-Instruct-4bit
40```
41
42### Python Script
43
44Here's an example of how to use MLX-VLM in a Python script:
45
46```python
47import mlx.core as mx
48from mlx_vlm import load, generate
49from mlx_vlm.prompt_utils import apply_chat_template
50from mlx_vlm.utils import load_config
51
52# Load the model
53model_path = "mlx-community/Qwen2-VL-2B-Instruct-4bit"
54model, processor = load(model_path)
55config = load_config(model_path)
56
57# Prepare input
58image = ["http://images.cocodataset.org/val2017/000000039769.jpg"]
59prompt = "Describe this image."
60
61# Apply chat template
62formatted_prompt = apply_chat_template(
63 processor, config, prompt, num_images=len(image)
64)
65
66# Generate output
67output = generate(model, processor, image, formatted_prompt, verbose=False)
68print(output)
69```
70
71## Multi-Image Chat Support
72
73MLX-VLM supports analyzing multiple images simultaneously with select models. This feature enables more complex visual reasoning tasks and comprehensive analysis across multiple images in a single conversation.
74
75### Supported Models
76
77The following models support multi-image chat:
78
791. Idefics 2
802. LLaVA (Interleave)
813. Qwen2-VL
824. Phi3-Vision
835. Pixtral
84
85### Usage Examples
86
87#### Python Script
88
89```python
90from mlx_vlm import load, generate
91from mlx_vlm.prompt_utils import apply_chat_template
92from mlx_vlm.utils import load_config
93
94model_path = "mlx-community/Qwen2-VL-2B-Instruct-4bit"
95model, processor = load(model_path)
96config = load_config(model_path)
97
98images = ["path/to/image1.jpg", "path/to/image2.jpg"]
99prompt = "Compare these two images."
100
101formatted_prompt = apply_chat_template(
102 processor, config, prompt, num_images=len(images)
103)
104
105output = generate(model, processor, images, formatted_prompt, verbose=False)
106print(output)
107```
108
109#### Command Line
110
111```sh
112python -m mlx_vlm.generate --model mlx-community/Qwen2-VL-2B-Instruct-4bit --max-tokens 100 --prompt "Compare these images" --image path/to/image1.jpg path/to/image2.jpg
113```
114
115These examples demonstrate how to use multiple images with MLX-VLM for more complex visual reasoning tasks.
116
117# Fine-tuning
118
119MLX-VLM supports fine-tuning models with LoRA and QLoRA.
120
121## LoRA & QLoRA
122
123To learn more about LoRA, please refer to the [LoRA.md](./mlx_vlm/LORA.MD) file.