Metadata-Version: 2.4
Name: moonlabel
Version: 0.2.0
Summary: Create object-detection (YOLO, COCO, VOC) and image-caption datasets using Multimodal LLMs and Moondream
Author: Muratcan Laloğlu
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/muratcanlaloglu/moonlabel
Project-URL: Source, https://github.com/muratcanlaloglu/moonlabel
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: moondream
Requires-Dist: Pillow
Requires-Dist: tqdm>=4.66.0
Provides-Extra: local
Requires-Dist: transformers>=4.40.0; extra == "local"
Requires-Dist: torch>=2.2.0; extra == "local"
Requires-Dist: accelerate; extra == "local"
Requires-Dist: einops; extra == "local"
Requires-Dist: pyvips-binary; extra == "local"
Requires-Dist: pyvips; extra == "local"
Provides-Extra: ui
Requires-Dist: fastapi[standard]; extra == "ui"
Dynamic: license-file

<div align="center">
  <h1>MoonLabel</h1>
  <img src="https://raw.githubusercontent.com/muratcanlaloglu/moonlabel/main/ui/src/assets/moonlabellogo.svg" alt="MoonLabel Logo" width="200" />
  <p>An object-detection and image-caption dataset tool.</p>
  <p><em>Powered by Multimodal LLMs (OpenAI, Gemini, Claude, Ollama, OpenRouter, vLLM) &amp; <a href="https://moondream.ai/">Moondream VLM</a></em></p>
  <p>
    <a href="https://pypi.org/project/moonlabel/"><img src="https://img.shields.io/pypi/v/moonlabel.svg?logo=pypi" alt="PyPI version"></a>
    <a href="https://pepy.tech/projects/moonlabel"><img src="https://static.pepy.tech/personalized-badge/moonlabel?period=total&units=INTERNATIONAL_SYSTEM&left_color=BLACK&right_color=GREEN&left_text=downloads" alt="PyPI Downloads"></a>
  </p>
</div>

---

## Overview

MoonLabel is both a Python library and a lightweight web UI to generate object-detection and image-caption datasets quickly.

1. Use the library to auto-label folders of images and export YOLO, COCO, VOC, or Captions.
2. Or launch the UI, switch between Detection/Caption, and export with one click.

Supported backends:
- **Multimodal LLMs** (zero extra dependencies): **OpenAI** (GPT-4o / GPT-4.1), **Google Gemini** (native `box_2d` 0–1000 grounding), **Anthropic Claude**, **Ollama** (local VLMs like `qwen2.5vl`, `llama3.2-vision`, `gemma3`), **OpenRouter**, and any **OpenAI-compatible** endpoint (vLLM, LM Studio, Groq, Together, etc.).
- **Moondream VLM**: Moondream Cloud, Moondream Station, or fully local (Hugging Face).
- **Custom backends**: Implement `VisionBackend` to plug in any custom vision model.

## Demo


https://github.com/user-attachments/assets/a2dfc6b6-c83d-4296-986b-ac221e10fc3b


---

## Features

* 🤖 **Multimodal LLM support** — OpenAI, Google Gemini, Anthropic Claude, Ollama, OpenRouter, and any OpenAI-compatible vision API.
* 📦 **Library + UI** — `moonlabel` package with an optional web UI.
* 🌐 **FastAPI server** — Served by a single `moonlabel-ui` command.
* ⚛️ **Modern frontend** — React, TypeScript, TailwindCSS, Vite.
* 🖼️ **Object detection** — Single-pass multi-class detection with Multimodal LLMs, plus Moondream Cloud / Station / Local HF.
* 📝 **Image caption datasets** — Export captions as `captions.jsonl` alongside images, with length presets or custom prompts.
* ⚡ **GPU-accelerated & offline** — Local Ollama, vLLM, Moondream Station, and Hugging Face modes run fully offline.


## Install

- Library only (Multimodal LLMs + Moondream Cloud/Station):
```bash
pip install moonlabel
```
- Library + UI server:
```bash
pip install "moonlabel[ui]"
```
- Local Moondream (Hugging Face) extras:
```bash
pip install "moonlabel[local]"
```
- Both UI and local Hugging Face inference:
```bash
pip install "moonlabel[ui,local]"
```

## Quick Start (UI)

```bash
pip install "moonlabel[ui]"
moonlabel-ui    # opens http://localhost:8342
```

Choose a provider in **Settings**:
- **Multimodal LLMs**: OpenAI, Google Gemini, Anthropic Claude, Ollama (Local), OpenRouter, or OpenAI-Compatible (vLLM / LM Studio / custom `base_url`)
- **Moondream**: Cloud (API key), Station (`http://localhost:2020/v1`), or Local (Hugging Face)

In the Home page:
- Use the top toggle to select **Detection** or **Caption** dataset.
- For Caption, choose length (`short`/`normal`/`long`) or enter an optional custom prompt, then generate and export.

## Quick Start (Library)

### Using Multimodal LLMs

API keys can be passed via `api_key=...` or read automatically from environment variables (`OPENAI_API_KEY`, `GEMINI_API_KEY` / `GOOGLE_API_KEY`, `ANTHROPIC_API_KEY`, `OPENROUTER_API_KEY`).

```python
from moonlabel import create_dataset

# Google Gemini (uses native 0-1000 box_2d grounding)
create_dataset(
    "/path/to/images",
    objects=["person", "car"],
    provider="gemini",
    model="gemini-2.5-flash",
    api_key="YOUR_GEMINI_API_KEY",  # or GEMINI_API_KEY env var
)

# OpenAI (GPT-4o / GPT-4o-mini / GPT-4.1)
create_dataset(
    "/path/to/images",
    objects=["person", "dog"],
    provider="openai",
    model="gpt-4o-mini",
    api_key="YOUR_OPENAI_API_KEY",  # or OPENAI_API_KEY env var
)

# Anthropic Claude
create_dataset(
    "/path/to/images",
    objects=["bicycle", "helmet"],
    provider="anthropic",
    model="claude-sonnet-4-20250514",
    api_key="YOUR_ANTHROPIC_API_KEY",  # or ANTHROPIC_API_KEY env var
)

# Ollama (local multimodal LLMs: qwen2.5vl, llama3.2-vision, gemma3, llava, ...)
create_dataset(
    "/path/to/images",
    objects=["laptop", "cup"],
    provider="ollama",
    model="qwen2.5vl",
    base_url="http://localhost:11434/v1",  # default
)

# OpenRouter
create_dataset(
    "/path/to/images",
    objects=["car", "traffic light"],
    provider="openrouter",
    model="qwen/qwen2.5-vl-72b-instruct",
    api_key="YOUR_OPENROUTER_API_KEY",  # or OPENROUTER_API_KEY env var
)

# Any OpenAI-compatible server (vLLM, LM Studio, Groq, Together, etc.)
create_dataset(
    "/path/to/images",
    objects=["person"],
    provider="openai-compatible",
    model="Qwen/Qwen2.5-VL-7B-Instruct",
    base_url="http://localhost:8000/v1",
)
```

### Using Moondream (Cloud / Station / Local)

```python
from moonlabel import create_dataset

# Cloud
create_dataset("/path/to/images", objects=["person"], api_key="YOUR_API_KEY")

# Station
create_dataset("/path/to/images", objects=["car"], station_endpoint="http://localhost:2020/v1")

# Local (after: pip install "moonlabel[local]")
create_dataset("/path/to/images", objects=["bottle"])  # no key needed
```

### Export Formats

By default `create_dataset` exports YOLO. Choose formats via `export_format`:

```python
# YOLO (default)
create_dataset("/path/to/images", objects=["person"], export_format="yolo", provider="gemini")

# COCO
create_dataset("/path/to/images", objects=["person", "car"], export_format="coco", provider="openai")

# Pascal VOC
create_dataset("/path/to/images", objects=["cat", "dog"], export_format="voc", provider="anthropic")

# Image Captioning (short | normal | long)
create_dataset("/path/to/images", export_format="caption", caption_length="normal", provider="openai")

# Image Captioning with a custom prompt
create_dataset(
    "/path/to/images",
    export_format="caption",
    caption_prompt="Describe the product and any visible surface defects in one sentence.",
    provider="gemini",
)
```

Output layouts:
- YOLO: `images/`, `labels/`, `classes.txt`
- COCO: `images/`, `annotations/instances.json`, `classes.txt`
- VOC: `images/`, `annotations/*.xml`, `classes.txt`
- Caption: `images/`, `annotations/captions.jsonl`

### Direct Inference & Custom Backends

You can also run inference directly on single images or implement a custom `VisionBackend`:

```python
from moonlabel import MultimodalInference, VisionBackend, create_dataset

client = MultimodalInference(provider="gemini", model="gemini-2.5-flash")
image, detections = client.detect("photo.jpg", ["person", "car"])
image, caption = client.caption("photo.jpg", length="normal")

# Or pass a pre-configured / custom VisionBackend directly
create_dataset("/path/to/images", objects=["person"], backend=client)
```

## Moondream Station Mode

The backend can connect to a running [Moondream Station](https://moondream.ai/station) instance for fast, native, on-device inference.

1. Download, install, and run Moondream Station.
2. Ensure the endpoint matches your Station configuration (default: `http://localhost:2020/v1`).

## Local Mode (Hugging Face)

The backend can run fully offline using the open-source [vikhyatk/moondream2](https://huggingface.co/vikhyatk/moondream2) checkpoint.

1. `pip install "moonlabel[local]"`
2. In the UI, select **Moondream Local (HF)** (no API key required).

The first detection will trigger a one-off model download to `~/.cache/huggingface/`; subsequent runs reuse the cached weights.

### GPU / Device selection

The backend chooses the best device automatically in the following order: CUDA → Apple Silicon (MPS) → CPU.

Override via environment variable before launching the backend:

```bash
# Force GPU
export MOONDREAM_DEVICE=cuda

# Force Apple Silicon
export MOONDREAM_DEVICE=mps

# CPU only
export MOONDREAM_DEVICE=cpu
```

## Project Structure

```
moonlabel/
├── src/moonlabel/             # Python package (library + server)
│   ├── backends.py            # Multimodal LLM backends (OpenAI, Gemini, Anthropic, Ollama, OpenRouter, Custom)
│   ├── dataset.py             # create_dataset API
│   ├── infer.py               # Moondream wrapper (cloud/station/local) + re-exports
│   ├── types.py               # shared types
│   ├── utils.py               # helpers
│   ├── yolo.py                # YOLO label writer
│   ├── coco.py                # COCO writer
│   ├── voc.py                 # Pascal VOC XML writer
│   └── server/                # FastAPI app + static assets
│       ├── api.py
│       ├── cli.py             # moonlabel-ui entrypoint (port 8342)
│       └── static/            # embedded UI build (no npm for users)
├── ui/                        # Frontend source (for maintainers)
│   └── dist/                  # Built files to embed
├── scripts/embed_ui.py        # Copies ui/dist → src/moonlabel/server/static
├── Makefile                   # make ui-build, ui-embed, release
└── pyproject.toml
```

---

## Roadmap / TODOs

Below are planned enhancements and upcoming features. Contributions welcome!

- [x] **Multimodal LLM support** – OpenAI, Google Gemini, Anthropic Claude, Ollama, OpenRouter, and OpenAI-compatible APIs.
- [x] **Local Hugging Face model support** – Offline inference with optional GPU acceleration.
- [x] **Moondream Station integration** – Native Mac/Linux app support for on-device inference.
- [x] **Batch uploads** – Label multiple images in one go, with progress tracking.
- [x] **Additional export formats** – COCO JSON and Pascal VOC alongside YOLO.

---

## License

This project is licensed under the terms of the Apache License 2.0. See [LICENSE](LICENSE) for details.
