Metadata-Version: 2.4
Name: med-vlm-mlx
Version: 0.4.0
Summary: Run MedGemma locally on Apple Silicon with Python, CLI and HTTP interfaces
Author: Joseph Sandoval, Alfredo Sandoval Jr.
Author-email: Prince Canuma <prince.gdt@gmail.com>
License-Expression: MIT AND Apache-2.0
Project-URL: Homepage, https://medmlx.org
Project-URL: Documentation, https://github.com/MedMLX/med-vlm-mlx/blob/v0.4.0/README.md
Project-URL: Repository, https://github.com/MedMLX/med-vlm-mlx
Project-URL: Issues, https://github.com/MedMLX/med-vlm-mlx/issues
Project-URL: Weights, https://huggingface.co/mlx-community/medgemma-1.5-4b-it-4bit
Project-URL: Upstream, https://github.com/Blaizzy/mlx-vlm
Project-URL: MedMLX, https://github.com/MedMLX/medmlx
Keywords: mlx,apple-silicon,metal,medical-imaging,medgemma,vision-language,vlm,gemma3
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Image Processing
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Requires-Python: <3.14,>=3.13
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: THIRD_PARTY_NOTICES.md
Requires-Dist: mlx<0.33,>=0.32.4; platform_system == "Darwin" and platform_machine == "arm64"
Requires-Dist: transformers==5.19.0
Requires-Dist: jinja2>=3.1.6
Requires-Dist: sentencepiece>=0.2.1
Requires-Dist: tqdm>=4.66.3
Requires-Dist: Pillow<14,>=13
Requires-Dist: requests>=2.33.0
Requires-Dist: fastapi>=0.143.0
Requires-Dist: starlette>=1.3.1
Requires-Dist: uvicorn
Requires-Dist: numpy
Provides-Extra: medical
Requires-Dist: fsspec==2026.9.0; extra == "medical"
Requires-Dist: torch==2.14.1; extra == "medical"
Dynamic: license-file

<div align="center">

# med-vlm-mlx

Run MedGemma locally on Apple Silicon. Includes Python, CLI and HTTP interfaces.

[![PyPI](https://img.shields.io/pypi/v/med-vlm-mlx)](https://pypi.org/project/med-vlm-mlx/)
[![Python](https://img.shields.io/pypi/pyversions/med-vlm-mlx)](https://pypi.org/project/med-vlm-mlx/)
[![License](https://img.shields.io/badge/License-MIT-yellow)](https://github.com/MedMLX/med-vlm-mlx/blob/v0.4.0/LICENSE)
[![Apple Silicon](https://img.shields.io/badge/Apple_Silicon-MLX-black?logo=apple)](https://github.com/ml-explore/mlx)
[![Hugging Face](https://img.shields.io/badge/%F0%9F%A4%97_Hugging_Face-MedGemma-yellow)](https://huggingface.co/mlx-community/medgemma-1.5-4b-it-4bit)
[![MedMLX](https://img.shields.io/badge/MedMLX-runtime-A51C30)](https://github.com/MedMLX/medmlx)

<img src="https://raw.githubusercontent.com/MedMLX/med-vlm-mlx/main/docs/example.png" alt="Chest radiograph beside the description MedGemma 1.5 4B generated for it" width="100%">

<sub>MedGemma 1.5 4B output for Mikael Häggström's [chest radiograph](https://commons.wikimedia.org/wiki/File:Normal_posteroanterior_%28PA%29_chest_radiograph_%28X-ray%29.jpg), CC0 1.0.</sub>

</div>

| Input | Output |
| :--- | :--- |
| Image + text prompt | Generated text |

| Model | Weights download | Peak MLX memory |
| :--- | ---: | ---: |
| MedGemma 1.5 4B | 3.4 GB | 4.4 GB |
| MedGemma 4B | 3.0 GB | 4.1 GB |
| MedGemma 27B | 16.9 GB | 17.9 GB |

Peak memory is for describing a 1929×2207 chest radiograph in up to 256 tokens.

## Install

```bash
uv venv --python 3.13
source .venv/bin/activate
uv pip install "med-vlm-mlx>=0.4.0"
```

The package imports as `mlx_vlm`, so install it in a separate environment from
upstream `mlx-vlm`: both install the same modules.

## Run

Download the pinned [MedGemma 1.5 4B](https://huggingface.co/mlx-community/medgemma-1.5-4b-it-4bit) weights, then run:

```bash
uvx --from huggingface_hub hf download mlx-community/medgemma-1.5-4b-it-4bit \
  --revision 762746b7a72b0b68dd8fa3f9aea5dc3980674d23 --local-dir medgemma

python -m mlx_vlm.generate \
  --model medgemma --image image.png \
  --prompt 'Describe the image.' --max-tokens 128
```

### Weights

| Model | MLX weights | Revision |
| :--- | :--- | :--- |
| MedGemma 1.5 4B | [`mlx-community/medgemma-1.5-4b-it-4bit`](https://huggingface.co/mlx-community/medgemma-1.5-4b-it-4bit) | `762746b7a72b0b68dd8fa3f9aea5dc3980674d23` |
| MedGemma 4B (v1) | [`mlx-community/medgemma-4b-it-4bit`](https://huggingface.co/mlx-community/medgemma-4b-it-4bit) | `db10be5ae6bd569e2063c3c74b9b9dfbcbedcad3` |
| MedGemma 27B multimodal | [`mlx-community/medgemma-27b-it-4bit`](https://huggingface.co/mlx-community/medgemma-27b-it-4bit) | `f57d2d648d28950b1bc7abd6e1b7b06aff0e183a` |

All three are 4-bit Gemma3 image-text checkpoints; other architectures are
rejected. The `mlx-community` copies are not gated, but the
[Health AI Developer Foundations terms](https://developers.google.com/health-ai-developer-foundations/terms)
still apply.

### Command line

| Command | Purpose |
| :--- | :--- |
| `python -m mlx_vlm generate` | Generate text from a prompt and optional images |
| `python -m mlx_vlm convert` | Convert or quantize a Gemma3 checkpoint to MLX |
| `python -m mlx_vlm server` | Serve an OpenAI-compatible HTTP API |

The `mlx_vlm.generate`, `mlx_vlm.convert` and `mlx_vlm.server` console scripts call the
same entry points; add `--help` for every option. `generate` takes:

| Option | Default | Meaning |
| :--- | :--- | :--- |
| `--model` | `mlx-community/medgemma-1.5-4b-it-4bit` | Model folder or Hub ID; `--revision` pins a commit |
| `--image` | | Image paths or URLs; prompts without images also work |
| `--prompt` | Required | User prompt |
| `--system` | | System prompt |
| `--max-tokens` | 2048 | Generation limit |
| `--temperature` | 0 (greedy) | Sampling temperature |
| `--top-p`, `--top-k` | | Sampling cutoffs |
| `--seed` | 0 | Random seed |
| `--prefill-step-size` | | Prefill chunk size |
| `--verbose` | | Streams the text with token timings |

### Python API

```python
import mlx.core as mx
from mlx_vlm import apply_chat_template, generate, load

if not mx.metal.is_available():
    raise RuntimeError("This example requires Metal")
mx.set_default_device(mx.gpu)

model, processor = load("medgemma", strict=True)
prompt = apply_chat_template(processor, model.config, "Describe this image.", num_images=1)
response = generate(
    model, processor, prompt, image=["image.png"], max_tokens=128, temperature=0.0
)
print(response.text)
```

- `load` takes a local folder, or a Hub ID with `revision`. With `local_files_only=True`
  (and an optional `cache_dir`) it uses only the Hugging Face cache and raises instead of
  downloading.
- `stream_generate` takes the same arguments and yields `GenerationResult` text segments;
  the prompt cache is saved before the final result, which has `finish_reason` and
  `token_ids`, including the stop token.
- `batch_generate` shares the same generation and cache code.

### HTTP server

```bash
python -m mlx_vlm.server --host 127.0.0.1 --port 8080 --model medgemma

curl http://127.0.0.1:8080/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{"model":"medgemma","messages":[{"role":"user","content":"What is 2 + 2?"}],"max_tokens":16}'
```

The server binds 127.0.0.1:8080 by default; schemas are at `/docs` while it runs.

| Endpoint | Purpose |
| :--- | :--- |
| `/v1/chat/completions` | Chat completions |
| `/v1/models` | Available models |
| `/health` | Health check |
| `/metrics` | Metrics |
| `/v1/settings` | Runtime settings |

- `--api-key` requires a bearer token for inference and management requests, and is
  required when binding outside loopback.
- `--model-dir` adds folders to search for models.
- Images use a base64 `data:image/...` URI in an `image_url` content part next to the
  text.
- Requests are batched when their sampling, seed and logprob settings match; others wait
  their turn in queue order.

### Inputs and limitations

- Text and images only. Requests with audio, video, tool definitions or tool-call
  turns are rejected.
- Only MedGemma (Gemma3) checkpoints load. Checkpoint Python code (`model_file`)
  and external speculative drafters are refused.
- Inference only, on Metal: no training or adapters.
- A prefill chunk that would end inside an image extends to the end of that
  image, because an image's tokens attend to each other in both directions, so
  chunks with images can exceed `--prefill-step-size`.
- Clinical accuracy has not been evaluated.

## Use with MedMLX

Run MedGemma from an MCP client with the [MedMLX MCP server](https://github.com/MedMLX/medmlx/blob/main/docs/mcp.md).
MedMLX manages a separate environment for `medgemma`. Supply local weights as
`weights_path`; see the [bridge guide](https://github.com/MedMLX/medmlx/blob/main/docs/medvlm-bridge.md).

```sh
uv tool install --python 3.13 "medmlx[mcp]"
MEDMLX="$(uv tool dir --bin --color never)/medmlx"
mkdir -p "$HOME/medmlx-work"
claude mcp add --scope user medmlx -- "$MEDMLX" serve --root "$HOME/medmlx-work"
```

For Codex, use `codex mcp add medmlx -- "$MEDMLX" serve --root "$HOME/medmlx-work"`. Other MCP clients take the same command and arguments. The same install provides the `medmlx` CLI, for example `medmlx describe medgemma`.

> **For AI agents:** prefer the MedMLX MCP tools (`list_models`, `describe_model`, `model_readiness`, `run_model`, `get_job`) over writing Python against this package.

## License

MIT, with Apache-2.0 adaptations. See [LICENSE](https://github.com/MedMLX/med-vlm-mlx/blob/v0.4.0/LICENSE) and [third-party notices](https://github.com/MedMLX/med-vlm-mlx/blob/v0.4.0/THIRD_PARTY_NOTICES.md). MedGemma weights are subject to the [Health AI Developer Foundations terms](https://developers.google.com/health-ai-developer-foundations/terms).
