# Prodigy Plugins

> Open-source plugins that add recipes to Prodigy for Hugging Face models, PDFs and OCR, Whisper transcription, image segmentation, search and evaluation.

Source: https://prodi.gy/docs/plugins

Some Prodigy recipes require a 3rd party library in order to work. To keep
Prodigy lightweight we've separated some of these recipes out into their own
packages so that you may install them as a plugin. These plugins always target
the most recent version of Prodigy with regards to compatibility.

This section of the docs showscases such plugins. Note that you can also explore
these recipes on Github to serve as a source of inspiration to customise
further.

|                                                                   |                                                                                                                                                                                                                                                                                                                                                       |                                                                 |
| ----------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------- |
| 🤗 **[Prodigy HF](https://prodi.gy/docs/plugins#hf)**             | Recipes that interact with the Huggingface stack.  **Recipes:** [`hf.train.ner`](https://prodi.gy/docs/plugins#hf-train.ner), [`hf.correct.ner`](https://prodi.gy/docs/plugins#hf-correct.ner), [`hf.upload`](https://prodi.gy/docs/plugins#hf-upload) and more                                                                                       | [Repo](https://github.com/explosion/prodigy-hf)                 |
| 📄 **[Prodigy PDF](https://prodi.gy/docs/plugins#pdf)**           | Recipes that help with the annotation of PDF files. **Recipes:** [`pdf.image.manual`](https://prodi.gy/docs/plugins#pdf-image.manual), [`pdf.ocr.correct`](https://prodi.gy/docs/plugins#pdf-ocr.correct), [`pdf.spans.manual`](https://prodi.gy/docs/plugins#pdf-spans.manual), [`pdf.layout.fetch`](https://prodi.gy/docs/plugins#pdf-layout.fetch) | [Repo](https://github.com/explosion/prodigy-pdf)                |
| 🤫 **[Prodigy Whisper](https://prodi.gy/docs/plugins#whisper)**   | Recipes that leverage OpenAI's Whisper model for audio transcription. **Recipes:** [`whisper.audio.annotate`](https://prodi.gy/docs/plugins#whisper-audio.annotate)                                                                                                                                                                                   | [Repo](https://github.com/explosion/prodigy-whisper)            |
| 🍰 **[Prodigy Segment](https://prodi.gy/docs/plugins#segment)**   | Recipes that leverage Meta's [Segment-Anything](https://segment-anything.com/) model for image segmentation. **Recipes:** [`segment.image.manual`](https://prodi.gy/docs/plugins#segment-image.manual) and more                                                                                                                                       | [Repo](https://github.com/explosion/prodigy-segment)            |
| 🏘 **[Prodigy ANN](https://prodi.gy/docs/plugins#ann)**           | Recipes that allow you to use approximate nearest neighbor techniques to help you annotate. **Recipes:** [`ann.text.index`](https://prodi.gy/docs/plugins#ann-text.index), [`ann.image.index`](https://prodi.gy/docs/plugins#ann-image.index), [`ann.text.fetch`](https://prodi.gy/docs/plugins#ann-text.fetch) and more                              | [Repo](https://github.com/explosion/prodigy-ann)                |
| 🌕 **[Prodigy Lunr](https://prodi.gy/docs/plugins#ann)**          | Recipes that allow you to use old-school string matching techniques to help you annotate. **Recipes:** [`lunr.text.index`](https://prodi.gy/docs/plugins#lunr-text.index), [`lunr.text.fetch`](https://prodi.gy/docs/plugins#lunr-text.fetch) and more                                                                                                | [Repo](https://github.com/explosion/prodigy-lunr)               |
| 🦆 **[sense2vec](https://prodi.gy/docs/plugins#sense2vec)**       | Recipes that allow to fetch terms using phrase embeddings trained on Reddit. **Recipes:** [`sense2vec.teach`](https://prodi.gy/docs/plugins#sense2vec-teach), [`sense2vec.to-patterns`](https://prodi.gy/docs/plugins#sense2vec-to-patterns) and more                                                                                                 | [Repo](https://github.com/explosion/sense2vec#-prodigy-recipes) |
| 🔎 **[Prodigy Evaluate](https://prodi.gy/docs/plugins#evaluate)** | Recipes that compute evaluation metrics for spaCy pipelines. **Recipes:**  [`evaluate.evaluate`](https://prodi.gy/docs/plugins#evaluate-evaluate), [`evaluate.evaluate-example`](https://prodi.gy/docs/plugins#evaluate-evaluate-example) and more                                                                                                    | [Repo](https://github.com/explosion/prodigy-evaluate)           |

## 🤗 Prodigy-HF

This plugin contains recipes that interact with the Hugging Face stack. Some
recipes will allow you to directly train transformer models on top of your
annotations while other recipes allow you to upload artifacts to HF cloud
environment.

To use these recipes, you'll first need to install the plugin.

```bash title="Install prodigy-hf"
$ pip install "prodigy-hf @ git+https://github.com/explosion/prodigy-hf"
```

Once it is installed you can explore some of the new recipes.

### Training Hugging Face models

The first recipe that you may enjoy from this plugin is the recipe to train
custom NER models.

```bash
prodigy hf.train.ner fashion,eval:fashion-eval hf-model-dir --epochs 10 --model-name distilbert-base-uncased
```

> **Warning: Warning: Model support**
>
> The `hf.train.ner` recipe is designed to work with Hugging Face's
> `AutoModelForTokenClassification` and `AutoTokenizer` classes. While these
> objects should cover most models out there, there may also be some edge cases.
> It could be that you'd like to use a pretrained model that is out of date or
> that you've found a model with very specific requirements that our recipe isn't
> aware of.
>
> If you have a very specific model in mind that requires more than what the
> auto-tools can immediately handle you can always take the recipe
> [on Github](https://github.com/explosion/prodigy-hf) and adapt it to your
> specific needs.

Once the model is done training you'll be able to inspect the `hf-model-dir`
folder to find all the trained state.

You can also choose to re-use this trained model to help you annotate data. The
plugin features a `hf.ner.correct` recipe that works similarily to
[`ner.correct`](https://prodi.gy/docs/recipes#ner-correct) except here we get to use a Hugging Face model. This means
that you can also use models from the
[Hugging Face Hub](https://huggingface.co/models?pipeline_tag=token-classification\&sort=trending).
This recipe will internally map the predictions from the transformer model to
spaCy tokens.

```bash
prodigy hf.ner.correct fashion hf-model-dir/checkpoint-20 examples.jsonl --lang en
```

> **Note**
>
> Note that if you're loading a model from your own local drive that you should
> point to the checkpoint folder. This will contain a full model, the folder that
> contains all these checkpoints does not.
>
> The `--lang` option allows you to configure a different spaCy model for
> tokenisation.

Note that this plugin also offers variants of these recipes for text
classification. Check out the API docs for `hf.train.textcat` and
`hf.correct.textcat` for more details.

> **Warning**
>
> ### Warning: Exclusive categories
>
> Note that `hf.correct.textcat` and `hf.train.textcat` assume mutually exclusive
> textcat labels. You cannot train a pipeline where multiple classes can be
> detected for a single example.
>
> This also has consequences with regards to annotation. Where possible you should
> add an `--exclusive` flag to your recipes to ensure that the annotations are in
> the right format. That also means that if you use [`textcat.manual`](https://prodi.gy/docs/recipes#textcat-manual) to
> annotate data for a transformer pipeline that you should also be mindful when
> you're doing binary annotations.
>
> ```bash
> prodigy textcat.manual dataset examples.jsonl --labels pos,not-pos --exclusive
> ```
>
> By using `--labels pos,not-pos` instead of `--labels pos` you are making sure
> that both classes are available to the pipeline after it is training.

### Interacting with Hugging Face Hub

Alternatively, you may also use these plugin to upload your annotated datasets
to Hugging Face Hub.

> **Note: Logging in**
>
> All the recipes below assume that you've authenticated beforehand via the
> following command:
>
> ```bash title="Logging into Huggingface from the CLI"
> $ huggingface-cli login
> ```

```text
$ prodigy hf.upload fashion,eval:fashion-eval username/reponame
✔ Upload completed! You should be able to view repo at
https://huggingface.co/datasets/username/reponame.
```

Internally this recipe will validate the dataset for consistency and will
attempt to anonymise the annotators before uploading. You can turn this behavior
off with flags and you can also specify that you want the dataset not to appear
publicly.

> **Note**
>
> ### Storing keyed datasets
>
> You can choose to prefix a dataset name, like `eval:fashion-eval` to have the
> examples appear in a different key on the `DataSetDict`. You can confirm this
> locally when downloading the dataset via the `datasets` library.
>
> ```python
> from datasets import load_dataset
>
> load_dataset("username/reponame")
> ```
>
> Notice how the `eval` key appears in the output.
>
> ```python
> DatasetDict({
>     train: Dataset({
>         features: ['text', 'meta', '_input_hash', '_task_hash', 'tokens', 'spans', '_session_id', '_view_id', 'answer'],
>         num_rows: 1235
>     })
>     eval: Dataset({
>         features: ['text', 'meta', '_input_hash', '_task_hash', 'tokens', 'spans', '_session_id', '_view_id', 'answer'],
>         num_rows: 500
>     })
> })
> ```

### API

### `hf.train.ner`

- **Type:** command
- **Interface:** terminal only
- **Use case:** train Hugging Face models directly

Trains a Hugging Face model for NER directly on your annotated datasets.

```bash
prodigy hf.train.ner datasets out_dir --model-name --batch-size --eval-split --learning-rate --verbose
```

| Argument                 | Type       | Description                                                                                                                                                                                                                     | Default                   |
| ------------------------ | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- |
| `datasets`               | positional | One or more (comma-separated) datasets for the named entity recognizer. Use the `eval:` prefix for evaluation                                                                                                                   |                           |
| `out_dir`                | positional | Folder to store trained model and checkpoints.                                                                                                                                                                                  |                           |
| `--model-name`, `-mn`    | option     | Pick the model you'd like to use as a starting point for training.                                                                                                                                                              | "distilbert-base-uncased" |
| `--batch-size`, `-bs`    | option     | Batch size for training.                                                                                                                                                                                                        | 8                         |
| `--eval-split`, `-es`    | option     | If no evaluation sets are provided for a component, this setting can be used to split off a a percentage of the training examples for evaluation. If no evaluation splits are given the train set performance will be reported. |                           |
| `--learning-rate`, `-lr` | option     | Learning rate.                                                                                                                                                                                                                  | 2e-5                      |
| `--verbose`, `-v`        | flag       | Output all the logs/warnings from Hugging Face libraries.                                                                                                                                                                       | False                     |

### `hf.correct.ner`

- **Type:** manual
- **Interface:** [`ner_manual`](https://prodi.gy/docs/api-interfaces#ner_manual)
- **Use case:** Annotate NER with a model in the loop

Annotate NER data with a transformer model in the loop.

```bash
prodigy hf.correct.ner dataset --model-name source --lang
```

| Argument       | Type       | Description                                                               | Default |
| -------------- | ---------- | ------------------------------------------------------------------------- | ------- |
| `dataset`      | positional | Dataset to save annotations into                                          |         |
| `out_dir`      | positional | Path to transformer model. Can also point to a model on Hugging Face Hub. |         |
| `source`       | positional | Source file to annotate                                                   |         |
| `--lang`, `-l` | option     | Language to assume for the spaCy tokeniser                                | "en"    |

### `hf.train.textcat`

- **Type:** command
- **Interface:** terminal only
- **Use case:** train Hugging Face models directly

Trains a Hugging Face model for text classification directly on your annotated
datasets.

> **Warning: Warning: Exclusive categories**
>
> Note that `hf.correct.textcat` and `hf.train.textcat` assume mutually exclusive
> textcat labels. You cannot train a pipeline where multiple classes can be
> detected for a single example.
>
> While you can use this recipe to train models on binary datasets, you should be
> aware that the resulting model will only be capable of emitting "accept" or
> "reject" as labels.
>
> If you use [`textcat.manual`](https://prodi.gy/docs/recipes#textcat-manual) to annotate data for a transformer pipeline
> that you should also be mindful when you're doing binary annotations that you
> may loose important information. So in general we recommend doing this:
>
> ```bash
> prodigy textcat.manual dataset examples.jsonl --labels pos,not-pos --exclusive
> ```
>
> By using `--labels pos,not-pos` instead of `--labels pos` you are making sure
> that both classes are available to the pipeline after it is training.

```bash
prodigy hf.train.textcat datasets out_dir --model-name --batch-size --eval-split --learning-rate --verbose
```

| Argument                 | Type       | Description                                                                                                                                                                                                                     | Default                   |
| ------------------------ | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- |
| `datasets`               | positional | One or more (comma-separated) datasets for the named entity recognizer. Use the `eval:` prefix for evaluation                                                                                                                   |                           |
| `out_dir`                | positional | Folder to store trained model and checkpoints.                                                                                                                                                                                  |                           |
| `--model-name`, `-mn`    | option     | The name of the model to be used as a starting point for training.                                                                                                                                                              | "distilbert-base-uncased" |
| `--batch-size`, `-bs`    | option     | Batch size for training.                                                                                                                                                                                                        | 8                         |
| `--eval-split`, `-es`    | option     | If no evaluation sets are provided for a component, this setting can be used to split off a a percentage of the training examples for evaluation. If no evaluation splits are given the train set performance will be reported. |                           |
| `--learning-rate`, `-lr` | option     | Learning rate.                                                                                                                                                                                                                  | 2e-5                      |
| `--verbose`, `-v`        | flag       | Output all the logs/warnings from Huggingface libraries.                                                                                                                                                                        | False                     |

### `hf.correct.textcat`

- **Type:** manual
- **Interface:** [`choice`](https://prodi.gy/docs/api-interfaces#choice)
- **Use case:** Annotate textcat data with a model in the loop

Annotate data for text classification with a transformer model in the loop.

```bash
prodigy hf.correct.textcat dataset --model-name source
```

| Argument  | Type       | Description                                                               | Default |
| --------- | ---------- | ------------------------------------------------------------------------- | ------- |
| `dataset` | positional | Dataset to save annotations into                                          |         |
| `out_dir` | positional | Path to transformer model. Can also point to a model on Hugging Face Hub. |         |
| `source`  | positional | Source file to annotate                                                   |         |

### `hf.upload`

- **Type:** command
- **Interface:** terminal only
- **Use case:** upload annotations to Hugginface Hub

Upload your annotations to Hugging Face Hub.

> **Note**
>
> This recipe assumes that you've authenticated beforehand via the following
> command:
>
> ```bash title="Logging into Hugging Face from the CLI"
> $ huggingface-cli login
> ```

You can use the same command multiple times to upload the most recent version of
your data to the hub.

```bash
prodigy datasets repo_id --keep-annotator-ids --patch_values --private
```

| Argument                     | Type       | Description                                                                                          | Default |
| ---------------------------- | ---------- | ---------------------------------------------------------------------------------------------------- | ------- |
| `datasets`                   | positional | One or more (comma-separated) datasets to upload. Use the `name:` prefix to add keys to the dataset. |         |
| `repo_id`                    | positional | Name of the repo to upload to. Should be formatted as `<username>/<reponame>`.                       |         |
| `--keep-annotator-ids`, `-k` | flag       | Don't anonymize the annotators.                                                                      | False   |
| `--patch_values`, `-nv`      | flag       | If keys are missing between datasets, patch them with `None` values.                                 | False   |
| `--private`, `-p`            | flag       | Upload dataset as a private repository.                                                              | False   |

## Prodigy-PDF

This plugin contains recipes for annotating PDF files using the familiar image-based [`image_manual`](https://prodi.gy/docs/api-interfaces#image_manual) interface, as well as recipes for OCR (Optical Character Recognition) to extract text-based content from documents.

[Video: https://www.youtube.com/watch?v=rwyze49ne8I](https://www.youtube.com/watch?v=rwyze49ne8I)

To use these recipes, you'll first need to install the plugin.  In order for the recipes to work, you may also need to install system dependencies
for `tesseract`.

```bash title="Install prodigy-pdf"
$ pip install "prodigy-pdf @ git+https://github.com/explosion/prodigy-pdf"

brew install tesseract  # macOs
sudo apt install tesseract-ocr  # Linux
```

Once it is installed, you can start annotating PDFs as images via
[`pdf.image.manual`](https://prodi.gy/docs/plugins#pdf-image.manual):

```bash title="Example"
prodigy pdf.image.manual papers ./pdfs --labels FIGURE,FOOTNOTE,PARAGRAPH
```

If you like, you can re-use the pdf annotations with the [`pdf.ocr.correct`](https://prodi.gy/docs/plugins#pdf-ocr.correct)
recipe to apply OCR to the annotated segments. This recipe uses
[pytessaract](https://pypi.org/project/pytesseract/) under the hood to give
suggestions that you can correct.

```bash title="Example"
prodigy pdf.ocr.correct ocr_images dataset:papers --labels PARAGRAPH --fold-dashes
```

> **Warning: Keep the .pdf files around! **
>
> This recipe re-uses the paths found in the annotations from the
> [`pdf.image.manual`](https://prodi.gy/docs/plugins#pdf-image.manual) recipe. If these files are removed from the filesystem the
> [`pdf.ocr.correct`](https://prodi.gy/docs/plugins#pdf-ocr.correct) recipe will not be able to find them.

### API

### `pdf.image.manual`

- **Type:** manual
- **Interface:** [`image_manual`](https://prodi.gy/docs/api-interfaces#image_manual)
- **Use case:** Add annotations to PDFs

Add layout annotations to a PDF.

```bash
prodigy pdf.image.manual dataset pdf_folder --labels --remove-base64 --split-pages
```

| Argument                | Type | Description                                                                                                                                                                      | Default |
| ----------------------- | ---- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| `dataset`               | str  | Prodigy dataset to save annotations to.                                                                                                                                          |         |
| `pdf_folder`            | str  | Folder that contains your PDF files.                                                                                                                                             |         |
| `--labels`, `-l`        | str  | Comma-separated labels to annotate.                                                                                                                                              |         |
| `--remove-base64`, `-R` | bool | Don't save the base64 images of the PDF.                                                                                                                                         | `False` |
| `--split-pages`, `-S`   | bool | (New in 0.3) View each page as a separate task. By default, multi-page documents are grouped together using the [`pages`](https://prodi.gy/docs/api-interfaces#pages) interface. | `False` |

### `pdf.ocr.correct`

- **Type:** manual
- **Interface:** [`image`](https://prodi.gy/docs/api-interfaces#image) / [`text_input`](https://prodi.gy/docs/api-interfaces#text_input)
- **Use case:** Add OCR annotations to PDF segments

Applies Optical Character Recognition (OCR) to annotated segments from [`pdf.image.manual`](https://prodi.gy/docs/plugins#pdf-image.manual) and gives a textbox
for corrections.
For language support please consult `pytesseract` [documentation](https://github.com/madmaze/pytesseract). Language codes supported by `pytesseract` can be passed
directly with Prodigy `lang` argument, including `pytesseract` convention for combining multiple lanaguages e.g. `eng+fra`.

```bash
prodigy pdf.ocr.correct dataset source --labels --scale --fold-dashes --remove-base64 --autofocus
```

| Argument                | Type | Description                                                                 | Default |
| ----------------------- | ---- | --------------------------------------------------------------------------- | ------- |
| `dataset`               | str  | Prodigy dataset to save annotations to.                                     |         |
| `source`                | str  | Source with PDF annotations.                                                |         |
| `--labels`, `-l`        | str  | Labels to consider.                                                         |         |
| `--lang`, `-la`         | str  | Language for OCR.                                                           | `eng`   |
| `--scale`, `-s`         | int  | Zoom scale. Increase above 3 to upscale the image for OCR.                  | 3       |
| `--remove-base64`, `-R` | bool | Don't save the base64 images of the PDFs.                                   | False   |
| `--fold-dashes`, `-f`   | bool | Removes dashes at the end of a text line and folds them with the next term. | False   |
| `--autofocus`, `-af`    | bool | Autofocus on the transcript UI.                                             | False   |

### `pdf.spans.manual` (new in 0.4.0)

- **Type:** manual
- **Interface:** [`spans_manual`](https://prodi.gy/docs/api-interfaces#spans_manual)
- **Use case:** Add span annotation to document contents

This recipe lets you apply span annotations to text-based document contents extracted with [`spacy-layout`](https://github.com/explosion/spacy-layout) and [Docling](https://ds4sd.github.io/docling/). For higher annotation speed and efficiency, you can set `--focus text,list_item` or similar to walk through individual text blocks of the given [layout labels](https://github.com/DS4SD/docling-core/blob/14cad33ae7f8dc011a79dd364361d2647c635466/docling_core/types/doc/labels.py), which are highlighted in a visual preview of the document page. When annotating in focus mode, the span of text you're working on is preserved as `"text_span"` in the JSON data, so you'll always be able to relate it back into the original full document.

To speed up loading times, you can optionally use [`pdf.layout.fetch`](https://prodi.gy/docs/plugins#pdf-layout.fetch) to pre-fetch the data extracted from the PDFs. You can then run this recipe with the name of a dataset using the `dataset:` prefix, or the path to the created JSONL file.

```bash
prodigy pdf.spans.manual dataset spacy_model source --label --add-ents --focus --disable --split-pages --hide-preview
```

| Argument                | Type | Description                                                                                                                                                                                                                                                                                                                                          | Default |
| ----------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| `dataset`               | str  | Prodigy dataset to save annotations to.                                                                                                                                                                                                                                                                                                              |         |
| `spacy_model`           | str  | Loadable spaCy model or `blank:en` or similar for tokenization, and optional named entities.                                                                                                                                                                                                                                                         |         |
| `source`                | str  | Path to directory of PDF files, or dataset or JSONL file created with [`pdf.layout.fetch`](https://prodi.gy/docs/plugins#pdf-layout.fetch).                                                                                                                                                                                                          |         |
| `--label`, `-l`         | str  | Comma-separated span labels to annotate. If named entities are added, those will be filtered by the provided labels.                                                                                                                                                                                                                                 | `None`  |
| `--add-ents`, `-E`      | bool | Add named entities predicted by the model.                                                                                                                                                                                                                                                                                                           | `False` |
| `--focus`, `-f`         | str  | Annotate in focus mode: comma-separated list of [layout span labels](https://github.com/DS4SD/docling-core/blob/14cad33ae7f8dc011a79dd364361d2647c635466/docling_core/types/doc/labels.py) to focus on and annotate section-by-section, e.g. `text`, `list_item` or `section_header`. The current section will be highlighted in the visual preview. | `None`  |
| `--disable`, `-d`       | str  | Comma-separated [layout span labels](https://github.com/DS4SD/docling-core/blob/14cad33ae7f8dc011a79dd364361d2647c635466/docling_core/types/doc/labels.py) to disable and make unselectable in the UI.                                                                                                                                               | `None`  |
| `--split-pages`, `-S`   | bool | View pages as separate tasks instead of grouped together using the [`pages`](https://prodi.gy/docs/api-interfaces#pages) interface.                                                                                                                                                                                                                  | `False` |
| `--hide-preview`, `-HP` | bool | Don't show side-by-side preview of document layout next to the extracted text.                                                                                                                                                                                                                                                                       | `False` |

```bash title="Example"
prodigy pdf.spans.manual papers blank:en ./pdfs --label EVENT,PLACE --focus text,list_item
```

### `pdf.layout.fetch` (new in 0.4.0)

- **Type:** command
- **Interface:** terminal only
- **Use case:** Pre-process PDFs

This recipe lets you preprocess your PDFs to speed up loading times during annotation with [`pdf.spans.manual`](https://prodi.gy/docs/plugins#pdf-spans.manual). The data can be saved to a JSONL file, or a Prodigy dataset using the `dataset:` prefix. The data includes everythig needed to render the examples with the different layout configurations. You only need to decide upfront if you want the data to be paginated or split by sections, since this impacts the JSON structure that will be created.

```bash
prodigy pdf.layout.fetch output spacy_model source --focus --split-pages
```

| Argument              | Type | Description                                                                                                                                                                                                                                                                                                                                          | Default |
| --------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| `output`              | str  | Path to JSONL file or name of dataset using the `dataset:` prefix.                                                                                                                                                                                                                                                                                   |         |
| `spacy_model`         | str  | Loadable spaCy model or `blank:en` or similar for tokenization.                                                                                                                                                                                                                                                                                      |         |
| `source`              | str  | Path to directory of PDF files.                                                                                                                                                                                                                                                                                                                      |         |
| `--focus`, `-f`       | str  | Annotate in focus mode: comma-separated list of [layout span labels](https://github.com/DS4SD/docling-core/blob/14cad33ae7f8dc011a79dd364361d2647c635466/docling_core/types/doc/labels.py) to focus on and annotate section-by-section, e.g. `text`, `list_item` or `section_header`. The current section will be highlighted in the visual preview. | `None`  |
| `--split-pages`, `-S` | bool | View pages as separate tasks instead of grouped together using the [`pages`](https://prodi.gy/docs/api-interfaces#pages) interface.                                                                                                                                                                                                                  | `False` |

```text title="Example"
$ prodigy pdf.layout.fetch dataset:papers_layout blank:en ./pdfs
ℹ Creating preprocessed PDFs
✔ Saved fetched data to dataset papers_layout
```

## 🤫 Prodigy-Whisper

OpenAI released an open model for audio annotation called
[Whisper](https://openai.com/research/whisper). It's a model that can be
downloaded locally, it has support for multiple languages and you're even able
to pick from a selection of models. The model isn't perfect, but when you're
transcribing text, it can really help to have such a model provide a starting
point. The goal of this plugin is to help you get started with this right away.

To use this plugin, you'll need to install it first.

```bash title="Install prodigy-whisper"
$ pip install "prodigy-whisper @ git+https://github.com/explosion/prodigy-whisper"
```

In order to use the plugin you'll also need to have `ffmpeg` installed. Most
package managers should have these available so you should be able to use one of
the following commands.

```bash title="Install ffmpeg"
# on Ubuntu or Debian
sudo apt update && sudo apt install ffmpeg
# on Arch Linux
sudo pacman -S ffmpeg
# on MacOS using Homebrew (https://brew.sh/)
brew install ffmpeg
# on Windows using Chocolatey (https://chocolatey.org/)
choco install ffmpeg
# on Windows using Scoop (https://scoop.sh/)
scoop install ffmpeg
```

Once the plugin is installed you can use the `whisper.audio.transcribe` recipe.
It is very similar to [`audio.transcribe`](https://prodi.gy/docs/recipes#audio-transcribe) recipe that Prodigy provides, but
this recipe uses Whisper to provide an initial transcription.

```bash title="Example"
prodigy whisper.audio.transcribe transcripts ./recordings --model base
```

> **Note**
>
> #### Model download
>
> The first time that you run this recipe it may take a while to start because
> Whisper will download a model on your behalf, which is indicated by a progess
> bar. These models are saved in the `~/.cache/` folder in the user home
> directory. After the model had downloaded you won't need to download it again.

In the base form you can already see that Whisper does a pretty good job at
transcription. But it may be easier to correct short pieces of audio instead of
a long one. This is where Wishper can help out as well. It is able to segment a
long audio clip into shorter segments and each of these segments can then be
annotated in Prodigy.

To use this feature, you can add the `--segment` flag to the recipe call.

```bash title="Example"
prodigy whisper.audio.transcribe transcripts ./recordings --model base --segment
```

Now, you can go through the segments one by one and each segment will have
metadata attached so that you can link it back to the timestamps in the original
file. This is what the first segment would look like.

This is what the second segment would look like.

> **Note**
>
> As a side-effect of segmenting the audio, Whisper will exclude "uhm"s that
> appear between these segments. It won't remove them when it is within a segment,
> but you may have bits of audio gone if they fall in between.

### API

### `whisper.audio.transcribe`

- **Type:** manual
- **Interface:** [`blocks`](https://prodi.gy/docs/api-interfaces#blocks) / [`audio`](https://prodi.gy/docs/api-interfaces#audio) / [`text_input`](https://prodi.gy/docs/api-interfaces#text_input)
- **Saves:** annotations to the database
- **Use case:** Manually create transcriptions for audio with a Whisper model in the loop

Manually transcribe audio files by typing the transcript into a text field with
the help of Whisper. The API is built on top of [`audio.transcribe`](https://prodi.gy/docs/recipes#audio-transcribe) and will
allow you to configure everything that the original recipe can. The only input
addition is that this recipe also allows you to select a Whisper model. The
recipe uses the `"base"` model by default, but you should be able to pick any of
the models shown on
[here](https://github.com/openai/whisper#available-models-and-languages).

```bash
prodigy whisper.audio.transcribe dataset source --loader --autoplay --keep-base64 --fetch-media --playpause-key --text-rows --text-rows --exclude
```

| Argument                 | Type | Description                                                                                                                                   | Default                                     |
| ------------------------ | ---- | --------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------- |
| `dataset`                | str  | Prodigy dataset to save annotations to.                                                                                                       |                                             |
| `source`                 | str  | Path to a directory containing audio files or pre-formatted JSONL file if `--loader jsonl` is set.                                            |                                             |
| `--model`, `-m`          | str  | Name of OpenAI Whisper model to use.                                                                                                          | `base`                                      |
| `--loader`, `-lo`        | str  | Optional ID of source loader, e.g. `audio` or `video`.                                                                                        | `audio`                                     |
| `--autoplay`, `-A`       | bool | Autoplay the audio when a new task loads.                                                                                                     | `False`                                     |
| `--keep-base64`, `-B`    | bool | If `audio` loader is used: _don't_ remove the base64-encoded audio data from the task before it's saved to the database.                      | `False`                                     |
| `--fetch-media`, `-FM`   | bool | Convert local paths and URLs to base64. Can be enabled if you're annotating a JSONL file with paths or for re-annotating an existing dataset. | `False`                                     |
| `--playpause-key`, `-pk` | str  | Alternative keyboard shortcuts to toggle play/pause so it doesn't conflict with text input field.                                             | `"command+enter, option+enter, ctrl+enter"` |
| `--text-rows`, `-tr`     | int  | Height of the text input field, in rows.                                                                                                      | `6`                                         |
| `--field-id`, `-fi`      | str  | Add the transcript text to the data using this key, e.g. `"transcript": "Text here"`.                                                         | `"transcript"`                              |
| `--exclude`, `-e`        | str  | Comma-separated list of dataset IDs containing annotations to exclude.                                                                        | `None`                                      |

## Prodigy-Segment

Sometimes you're interested in selecting pixels from an image, as opposed to
merely selecting a bounding box. Selecting the right pixels can be tedious work
so you may want to use a model in the loop to help you. A good choice for such a
model is Meta's [Segment Anything](https://segment-anything.com/) model, which
we've integrated into Prodigy via the
[`prodigy-segment`](https://github.com/explosion/prodigy-segment) plugin.

This model is able to take bounding box annotations from Prodigy to construct a
pixel segmentation map under the hood. From the UI, that might look like this:

[Video: /docs/prodigy-segment.mp4](https://prodi.gy/docs/prodigy-segment.mp4)

### Using Prodigy-Segment

For a quick overview of the features, you may also enjoy this Youtube tutorial.

[Video: https://www.youtube.com/watch?v=W-wYXumFJRE](https://www.youtube.com/watch?v=W-wYXumFJRE)

Before you'll be able to use recipes, you'll want to make sure you've downloaded
the appropriate model checkpoint beforehand. You can check the available models
[here](https://github.com/facebookresearch/segment-anything#model-checkpoints)
but this tutorial will assume the "default" model-type. The weights for this
model can be downloaded via:

```bash title="Download the weights for the &#x60;default&#x60; model-type"
$ wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth
```

Once the model is downloaded you can get started by running the
`segment.image.manual` recipe.

```bash
prodigy segment.image.manual segment-cat-dog images sam_vit_h_4b8939.pth --model-type default --labels cat,dog
```

When you run this model, you may notice that it's fairly slow. This isn't a big
suprise given the size of the model but it can be a serious burden, especially
if your machine does not have a GPU. For a better experience, you may want to
pre-compute the features ahead of annotation time and cache those results to
disk. It may take a while to precompute all the images, but once they are done
the annotation experience feels seamless and realtime again.

To precompute a cache, you can use the `segment.fill-cache` recipe.

```bash
prodigy segment.fill-cache images sam_vit_h_4b8939.pth --model-type default --cache segment-anything-cache
```

This will store all the features in a folder (configurable via the `--cache`
flag) which the `segment.image.manual` recipe can immediately pick up.

```bash
prodigy segment.image.manual segment-cat-dog images sam_vit_h_4b8939.pth --model-type default --label cat,dog --cache segment-anything-cache
```

The pixel maps, once annotated, are stored under the `spans` key in your
examples. You can explore these maps one by one in a Jupyter notebook using the
script shown below.

```python title="Script to loop over all annotated examples"
import base64
from io import BytesIO
from PIL import Image

from prodigy.components.db import connect

db = connect()
examples = db.get_dataset_examples("<dataset-name>")

def mask_to_pil(mask_str):
    indicator = "base64,"
    mask_str = mask_str[mask_str.find(indicator) + len(indicator):]
    bytes = BytesIO(base64.b64decode(mask_str))
    return Image.open(bytes)

# Loop over all the examples and display them.
for ex in examples:
    print(ex['path'])
    for span in ex.get("spans", []):
        # Use builtin `display` to view pixel map
        display(mask_to_pil(span['mask']))
```

From here you can re-use the
[Pillow library](https://pillow.readthedocs.io/en/stable/) to either store these
pixel maps into the required format for your pipeline or you can stream them
directly into a learning algorithm from Python.

### API

### `segment.image.manual`

- **Type:** manual
- **Interface:** [`blocks`](https://prodi.gy/docs/api-interfaces#blocks) / [`image_manual`](https://prodi.gy/docs/api-interfaces#image_manual)
- **Saves:** annotations to the database
- **Use case:** Annotate pixels by drawing bounding boxes

Manually transcribe pixels in images with Meta's
[segment anything](https://github.com/facebookresearch/segment-anything) model
under the hood.

```bash
prodigy segment.image.manual dataset source checkpoint --label --loader --exclude --width --darken --no-fetch --remove-base64 --model-type --cache
```

| Argument                | Type         | Description                                                                                                                                                                            | Default                  |
| ----------------------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------ |
| `dataset`               | str          | Prodigy dataset to save annotations to.                                                                                                                                                |                          |
| `source`                | str          | Path to a directory containing audio files or pre-formatted JSONL file if `--loader jsonl` is set.                                                                                     |                          |
| `checkpoint`            | Path         | Path to a model checkpoint.                                                                                                                                                            |                          |
| `--label`, `-l`         | str / `Path` | One or more labels to annotate. Supports a comma-separated list or a path to a file with one label per line.                                                                           |                          |
| `--loader`, `-lo`       | str          | Optional ID of source loader.                                                                                                                                                          | `images`                 |
| `--exclude`, `-e`       | str          | Comma-separated list of dataset IDs containing annotations to exclude.                                                                                                                 | `None`                   |
| `--width`, `-w`         | int          | Width of card and maximum image width in pixels.                                                                                                                                       | `675`                    |
| `--darken`, `-D`        | bool         | Darken image to make boxes stand out more.                                                                                                                                             | `False`                  |
| `--no-fetch`, `-NF`     | bool         | Don't fetch images as base64. Ideally requires a JSONL file as input, with `--loader jsonl` set and all images available as URLs.                                                      | `False`                  |
| `--remove-base64`, `-R` | bool         | Remove base64-encoded image data before storing example in the database and only keep the reference to the local file path. **Caution:** If enabled, make sure to keep original files! | `False`                  |
| `--model-type`, `-mt`   | str          | Type of model to use.                                                                                                                                                                  | `default`                |
| `--cache`, `-c`         | `Path`       | Path to feature cache to speed up inference.                                                                                                                                           | `segment-anything-cache` |

### `segment.fill-cache`

- **Type:** command
- **Interface:** terminal only
- **Saves:** inference features into disk cache
- **Use case:** Prepare images for segmented annotation

Prepares a local disk cache to speed up inference for `segment.image.manual`.
This can cause a _huge_ speedup if you're running on a non-GPU device.

```bash
prodigy segment.fill-cache source --loader checkpoint --model-type --cache
```

| Argument          | Type   | Description                                                                                        | Default                  |
| ----------------- | ------ | -------------------------------------------------------------------------------------------------- | ------------------------ |
| `source`          | str    | Path to a directory containing audio files or pre-formatted JSONL file if `--loader jsonl` is set. |                          |
| `checkpoint`      | Path   | Path to a model checkpoint.                                                                        |                          |
| `--loader`, `-lo` | str    | Optional ID of source loader.                                                                      | `images`                 |
| `--cache`, `-c`   | `Path` | Path to feature cache to speed up inference.                                                       | `segment-anything-cache` |

## Prodigy-ANN

Sometimes you may want to query your examples to find a relevant subset for
annotation. A modern method for doing this is to use numeric vectors to
represent text and you can use approximate neighest neighbor (ANN) techniques to
fetch relevant examples. The goal is to spend more time looking at examples that
matter, like examples similar to items that the model gets wrong. Curating these
examples first might be a pragmatic method to steer the model in the right
direction.

![](https://prodi.gy/docs/ann.png "This is the general approach for the ANN recipes")

If you're interested to see a quick demo for Prodigy-ANN applied to a text
dataset, you may appreciate this Prodigy short on Youtube.

[Video: https://www.youtube.com/watch?v=jyu2nbjwfXw](https://www.youtube.com/watch?v=jyu2nbjwfXw)

To use this plugin, you'll need to install it first.

```bash title="Install prodigy-ann"
$ pip install "prodigy-ann @ git+https://github.com/explosion/prodigy-ann"
```

As a first step for this approach you'll first need to generate an index with
vector representations of your text. To encode the text this library uses
[sentence-transformers](https://sbert.org) and it uses
[hnswlib](https://github.com/nmslib/hnswlib) as an index for these vectors.

> **Note**
>
> The approach in this library should be pragmatic for a lot of use-cases but it
> won't be perfect for everything . The sentence transformers library is great,
> but it typically works best on sentences and may have trouble encoding longer
> texts like paragraphs. Similarily, the `hnswlib` package is a great ANN tool but
> it will store all the vectors in memory. If you're dealing with huge datasets
> then you may want to adapt the recipe to work with an ANN database that can
> handle the scale.

To index your documents, you can run the `ann.text.index` recipe.

```text
$ prodigy ann.text.index examples.jsonl examples.index
indexing: 100%|███████████████████████████| 2210/2210 [00:09<00:00, 243.64it/s]
```

> **Warning: Correspondence between examples.jsonl and examples.index**
>
> This command will store an `examples.index` file on disk that corresponds to
> your `examples.jsonl` file. If you ever change the `examples.jsonl` file that
> means that you will have to re-index and create a new corresponding `.index`
> file because otherwise the index may point to examples that are out of order.

Once it is indexed you can use text queries find and curate interesting subsets.
A general method to prepare these subsets is to use `ann.text.fetch`. This will
fetch a subset of vectors that are close in vector space and save the associated
examples on disk. From there you can use any Prodigy recipe you like.

```bash
prodigy ann.text.fetch examples.jsonl examples.index subset.jsonl --query "this is an outrage!"
```

> **Note**
>
> It's good to write the `--query` in a similar style as the example. If, for
> example, you're looking for angry messages then you might get somewhat
> reasonable results by using "anger" as a query. But the results are typically
> better if you use a query that's similar to the data that you might actually
> expect.

### More interfaces

As a convenience this plugin also provides the `textcat.ann.manual`,
`ner.ann.manual` and `spans.ann.manual` so that you may query and annotate
directly. These recipes have the same arguments as their native Prodigy
[`textcat.manual`](https://prodi.gy/docs/recipes#textcat-manual), [`ner.manual`](https://prodi.gy/docs/recipes#ner-manual) and [`spans.manual`](https://prodi.gy/docs/recipes#spans-manual) counterparts
but add a `--query` parameter so that you may pass your query.

#### Interactive Queries

Sometimes you may want to update the stream while you're annotating. You can do
that without restarting the server by using the `--allow-reset` flag when you're
starting the `textcat.ann.manual`, `ner.ann.manual` or `spans.ann.manual`
recipes.

```bash
prodigy textcat.ann.manual examples.jsonl examples.index --query "new academic dataset" --allow-reset
```

Here's an example of what the experience might look like from the UI.

[Video: /docs/allow-reset-ann.mp4](https://prodi.gy/docs/allow-reset-ann.mp4)

> **Warning: Warning: multiple annotators **
>
> The ability to restart the stream from the UI is very powerful, but you should
> be mindful that it resets the stream of every annotator. Effectively that means
> that any user can change the stream of everyone else!

### Retreiving Images

You can use these embedding retreival techniques for images too. Models like
[CLIP](https://sbert.net/examples/applications/image-search/README.html#image-search)
allow you to embed images and text in the same space, which means that you can
query the images by using text.

The approach for images is very similar to the approach for text too. To get
started you'll first want to run an indexing recipe over a folder of images via
the `ann.image.index` recipe.

```text
$ prodigy ann.image.index path/to/image_folder image.index
indexing: 100%|███████████████████████████| 210/210 [01:49<00:00]
```

Once the index is built, you can query it. You can choose to query it to prepare
a `.jsonl` file to re-use later via the `ann.image.fetch` recipe.

```bash
prodigy ann.image.fetch path/to/image_folder examples.index out.jsonl --query "laptops" --remove-base64 --n 100
```

Alternatively the plugin also provides a wrapper around the familiar
[`image.manual`](https://prodi.gy/docs/recipes#image-manual) recipe. This will retreive the images before passing it on
to the [`image_manual`](https://prodi.gy/docs/api-interfaces#image_manual) interface. This interface also allows you to reset
the stream via the `--allow-reset` flag.

```bash
prodigy image.ann.manual annotated_laptops path/to/image_folder examples.index --query "laptops" --remove-base64 --n 100 --labels laptop,phone --allow-reset
```

Here's an example of what the experience might look like from the UI.

[Video: /docs/allow-reset-img.mp4](https://prodi.gy/docs/allow-reset-img.mp4)

### API

### `ann.text.index`

- **Type:** command
- **Interface:** terminal only
- **Use case:** Prepare an HNSWlib index

Builds an HSNWLIB index on example text data.

```bash
prodigy ann.text.index source examples.index
```

| Argument     | Type | Description              | Default |
| ------------ | ---- | ------------------------ | ------- |
| `source`     | Path | Path to source to index. |         |
| `index_path` | Path | Path of trained index    |         |

### `ann.text.fetch`

- **Type:** command
- **Interface:** terminal only
- **Use case:** Query to get a subset of interest.

Fetch a relevant subset using a HNSWlib index.

```bash
prodigy ann.text.fetch source index_path out_path --query --n
```

| Argument        | Type | Description                            | Default |
| --------------- | ---- | -------------------------------------- | ------- |
| `source`        | Path | Path to source to index.               |         |
| `index_path`    | Path | Path of trained index                  |         |
| `out_path`      | Path | Path to stored subset of interest      |         |
| `--query`, `-q` | str  | Query to encode and pass to index      |         |
| `--n`, `-n`     | str  | Number of results to return from index | 200     |

### `ann.image.index`

- **Type:** command
- **Interface:** terminal only
- **Use case:** Prepare an HNSWlib index.

Builds an HSNWLIB index on example image data.

```bash
prodigy ann.image.index source examples.index
```

| Argument     | Type | Description                               | Default |
| ------------ | ---- | ----------------------------------------- | ------- |
| `source`     | Path | Path to source folder of images to index. |         |
| `index_path` | Path | Path of trained index                     |         |

### `ann.image.fetch`

- **Type:** command
- **Interface:** terminal only
- **Use case:** Query to get a subset of interest

Fetch a relevant subset of images using a HNSWlib index.

```bash
prodigy ann.image.fetch source index_path out_path --query --query --remove-base64
```

| Argument              | Type | Description                                | Default |
| --------------------- | ---- | ------------------------------------------ | ------- |
| `source`              | Path | Path to source folder of images for index. |         |
| `index_path`          | Path | Path of trained index                      |         |
| `out_path`            | Path | Path to stored subset of interest          |         |
| `--query`, `-q`       | str  | Query to encode and pass to index          |         |
| `-n`                  | int  | Number of items to retreive                | 200     |
| `remove-base64`, `-R` | bool | Don't save the base64 images on disk       | False   |

## Prodigy-Lunr

Instead of using semantic vectors with approximate nearest neighbors to find
relevant subsets you can also resort to the "regular" search techniques. To
accomodate these techniques we've added support for recipes that use
[lunr](https://lunr.readthedocs.io/en/latest/). These recipes are very similar
to their `ann.*` counterparts but will rely on string matching techniques to
retreive relevant examples.

![](https://prodi.gy/docs/lunr.png)

To use this plugin, you'll need to install it first.

```bash title="Install prodigy-lunr"
$ pip install "prodigy-lunr @ git+https://github.com/explosion/prodigy-lunr"
```

> **Note**
>
> The approach in this library should work for a lot of use-cases but it won't be
> perfect for every use-case out there. The generated index needs to fit in memory
> and really requires strings to match. If there are plenty of typos or if the
> dataset is huge then you may want to implement a custom recipe that integrates
> with another search provider.

To index your documents, you can run the `ann.text.index` recipe. This will
generate an index and serialize it to disk by writing it into a gzipped json
file.

```text
$ prodigy lunr.text.index examples.jsonl index.gz.json
indexing: 100%|███████████████████████████| 2210/2210 [00:09<00:00, 243.64it/s]
```

> **Warning: Correspondence between examples.jsonl and index.gz.json**
>
> This command will store an `index.gz.json` file on disk that corresponds to your
> `examples.jsonl` file. If you ever change the `examples.jsonl` file that means
> that you will have to re-index and create a new corresponding `index.gz.json`
> file because otherwise the index may point to examples that are out of order.

Once it is indexed you can use text queries find and curate interesting subsets.
A general method to prepare these subsets is to use `lunr.text.fetch`. This will
fetch a subset of vectors that are close in vector space and save the associated
examples on disk. From there you can use any Prodigy recipe you like.

```bash
prodigy lunr.text.fetch examples.jsonl index.gz.json subset.jsonl --query "outrage better service unhappy"
```

> **Note**
>
> The `--query` that you pass to the recipe should be interpreted as a query like
> you might use it in a search engine. It will perform directly string matching
> though so you may also want to consider synonyms or alternative spellings in
> your query.

### More interfaces

As a convenience this plugin also provides the `textcat.lunr.manual`,
`ner.lunr.manual` and `spans.lunr.manual` so that you may query and annotate
directly. These recipes have the same arguments as their native Prodigy
[`textcat.manual`](https://prodi.gy/docs/recipes#textcat-manual), [`ner.manual`](https://prodi.gy/docs/recipes#ner-manual) and [`spans.manual`](https://prodi.gy/docs/recipes#spans-manual) counterparts
but add a `--query` parameter so that you may pass your query.

#### Interactive Queries

Sometimes you may want to update the stream while you're annotating. You can do
that without restarting the server by using the `--allow-reset` flag when you're
starting the `textcat.lunr.manual`, `ner.lunr.manual` or `spans.lunr.manual`
recipes.

```bash
prodigy textcat.lunr.manual examples.jsonl index.gz.json --query "outrage better service unhappy" --allow-reset
```

> **Warning: Warning: multiple annotators**
>
> The ability to restart the stream from the UI is very powerful, but you should
> be mindful that it resets the stream of every annotator. Effectively that means
> that any user can change the stream of everyone else!

### API

### `lunr.text.index`

- **Type:** command
- **Interface:** terminal only
- **Use case:** Prepare an HNSWlib index.

Builds an HSNWLIB index on example text data.

```bash
prodigy lunr.text.index source examples.index
```

| Argument     | Type | Description               | Default |
| ------------ | ---- | ------------------------- | ------- |
| `source`     | Path | Path to source to index.  |         |
| `index_path` | Path | Path to stored lunr index |         |

### `lunr.text.fetch`

- **Type:** command
- **Interface:** terminal only
- **Use case:** Query to get a subset of interest.

Fetch a relevant subset using a HNSWlib index.

```bash
prodigy lunr.text.fetch source index_path out_path --query
```

| Argument        | Type | Description                       | Default |
| --------------- | ---- | --------------------------------- | ------- |
| `source`        | Path | Path to source to index.          |         |
| `index_path`    | Path | Path to stored lunr index         |         |
| `out_path`      | Path | Path to stored subset of interest |         |
| `--query`, `-q` | str  | Query to encode and pass to index |         |

## Sense2vec

sense2vec ([Trask et. al](https://arxiv.org/abs/1511.06388), 2015) is a nice
twist on [word2vec](https://en.wikipedia.org/wiki/Word2vec) that lets you learn
more interesting and detailed word vectors. This library is a simple Python
implementation for loading, querying and training sense2vec models. To explore
the semantic similarities across all Reddit comments of 2015 and 2019, see the
[interactive demo](https://demos.explosion.ai/sense2vec). There are also more
details in [this blogpost](https://explosion.ai/blog/sense2vec-reloaded).

To see a demo on how to use this tool with Prodigy, you may enjoy this Youtube
video where we use it to detect video games in text.

[Video: https://www.youtube.com/watch?v=EoYHbUHr0fM](https://www.youtube.com/watch?v=EoYHbUHr0fM)

To use sense2vec, you'll first need to install it.

```bash
$ python -m pip install sense2vec
```

To use the pre-trained vectors in Prodigy you'll need to download the archive(s)
and extract them. Large files have been split into multi-part downloads. All the
available versions can be found below.

| Vectors              |   Size | Description                  | Download Link (zipped)                                                                                                                                                                                                                                                                                                    |
| -------------------- | -----: | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `s2v_reddit_2019_lg` |   4 GB | Reddit comments 2019 (01-07) | [part 1](https://github.com/explosion/sense2vec/releases/download/v1.0.0/s2v_reddit_2019_lg.tar.gz.001), [part 2](https://github.com/explosion/sense2vec/releases/download/v1.0.0/s2v_reddit_2019_lg.tar.gz.002), [part 3](https://github.com/explosion/sense2vec/releases/download/v1.0.0/s2v_reddit_2019_lg.tar.gz.003) |
| `s2v_reddit_2015_md` | 573 MB | Reddit comments 2015         | [part 1](https://github.com/explosion/sense2vec/releases/download/v1.0.0/s2v_reddit_2015_md.tar.gz)                                                                                                                                                                                                                       |

To merge the multi-part archives, you can run the following:

```bash
$ cat s2v_reddit_2019_lg.tar.gz.* > s2v_reddit_2019_lg.tar.gz
```

Once downloaded (and merged) you should be able to unarchive via:

```bash
$ tar -xvf s2v_reddit_lg.tar.gz
```

Now that the archive is extracted you can point the `sense2vec.teach` recipe to
it. This will allow Prodigy to suggest similar terms based on the most similar
phrases from sense2vec, and the suggestions will be adjusted as you annotate and
accept similar phrases. For each seed term, the best matching sense according to
the sense2vec vectors will be used.

```bash
prodigy sense2vec.teach video_game_yesno /path/to/s2v_reddit_2019_lg --seeds "mass effect,knights of the old republic,halo 3" --resume
```

After curating the generated examples you can choose to export the collected
phrases as pattern files which can be used with
[spaCy's `EntityRuler`](https://spacy.io/usage/rule-based-matching#entityruler)
or recipes like [`ner.manual`](https://prodi.gy/docs/recipes#ner-manual) by using the `sense2vec.to-patterns` recipe.

```bash
prodigy sense2vec.to-patterns video_game_yesno blank:en VIDEO_GAME patterns.jsonl
```

This will generate a `patterns.jsonl` file locally that has contents that may
look like:

```json
{"label": "VIDEO_GAME", "pattern": [{"LOWER": "mass"}, {"LOWER": "effect"}]}
{"label": "VIDEO_GAME", "pattern": [{"LOWER": "knights"}, {"LOWER": "of"}, {"LOWER": "the"}, {"LOWER": "old"}, {"LOWER": "republic"}]}
{"label": "VIDEO_GAME", "pattern": [{"LOWER": "halo"}, {"LOWER": "3"}]}
{"label": "VIDEO_GAME", "pattern": [{"LOWER": "jade"}, {"LOWER": "empire"}]}
```

### More recipes

Sense2vec also has the
[`sense2vec.eval`](https://github.com/explosion/sense2vec/tree/master#recipe-sense2veceval),
[`sense2vec.eval-most-similar`](https://github.com/explosion/sense2vec/tree/master#recipe-sense2veceval-most-similar)
and
[`sense2vec.eval-ab`](https://github.com/explosion/sense2vec/tree/master#recipe-sense2veceval-ab)
recipes. These may be interesting if you're interested in evaluating a sense2vec
model. For more information on those, you can check the
[README](https://github.com/explosion/sense2vec/tree/master#-prodigy-recipes) on
the Github repository.

### `sense2vec.teach`

- **Type:** binary
- **Interface:** [`html`](https://prodi.gy/docs/api-interfaces#html)
- **Saves:** annotations to the database
- **Use case:** curate terminology phrases via sense2vec

Bootstrap a terminology list using sense2vec.

```bash
prodigy sense2vec.teach dataset vectors_path --seeds --threshold --n-similar --batch-size --case-sensitive --resume
```

| Argument                  | Type       | Description                                | Default |
| ------------------------- | ---------- | ------------------------------------------ | ------- |
| `dataset`                 | positional | Dataset to save annotations to.            |         |
| `vectors_path`            | positional | Path to pretrained sense2vec vectors.      |         |
| `--seeds`, `-s`           | option     | One or more comma-separated seed phrases.  |         |
| `--threshold`, `-t`       | option     | Similarity threshold.                      | 0.85    |
| `--n-similar`, `-n`       | option     | Number of similar items to get at once.    | 100     |
| `--batch-size`, `-b`      | option     | Batch size for submitting annotations.     | 5       |
| `--case-sensitive`, `-CS` | option     | Show the same terms with different casing. | False   |
| `--resume`, `-R`          | flag       | Resume from an existing phrases dataset.   | False   |

### `sense2vec.to-patterns`

- **Type:** command
- **Interface:** terminal only
- **Use case:** generate pattern files

Convert a dataset of phrases collected with [sense2vec.teach](https://prodi.gy/docs/plugins#sensen2vec-teach)
to token-based match patterns.

```bash
prodigy sense2vec.to-patterns dataset spacy_model label --output-file --case-sensitive --dry
```

| Argument                  | Type       | Description                                  | Default |
| ------------------------- | ---------- | -------------------------------------------- | ------- |
| `dataset`                 | positional | Phrase dataset to convert.                   |         |
| `spacy_model`             | positional | spaCy model for tokenization.                |         |
| `label`                   | positional | Label to apply to all patterns.              |         |
| `--output-file`, `-o`     | option     | Optional output file. Defaults to stdout.    |         |
| `--case-sensitive`, `-CS` | flag       | Make patterns case-sensitive.                | False   |
| `--dry`, `-D`             | flag       | Perform a dry run and don't output anything. | False   |

## 🔎 Prodigy-evaluate

This Prodigy plugin allows you to evaluate your spaCy pipeline overall or on a
per-example basis. To use these recipes, you'll first need to install the
plugin.

```bash title="Install prodigy-evaluate"
$ pip install "prodigy-evaluate @ git+https://github.com/explosion/prodigy-evaluate"
```

Once installed, you can make use of the two main recipes in this plugin:
`evaluate.evaluate` and `evaluate.evaluate-example`.

### `evaluate.evaluate`

- **Type:** command
- **Interface:** terminal only
- **Use case:** evaluate a spaCy pipeline on one or more datasets

This recipe allows you to evaluate a spaCy pipeline on one or more datasets for
different components. Per-component datasets can be passed in the same way as in
the case of train recipe only all datasets will be considered evaluation
sets.

The `--label-stats` flag lets you investigate per-label scores like precision,
recall and F1 scores for `NER` and `textcat` components. The
`--confusion-matrix` flag will output a confusion matrix for the `NER` and
`textcat` components. If you'd like to customize how the confusion matrix is
rendered, you can save the an array of the confusion matrix by passing an output
path via the `--cf-path` argument and use it with your favourite data
visualization library. Please note that a separate inference is run to obtain the confusion matrix and as results are not deterministic, there may be slight variations in evaluation and confusion matrix results.

```text title="Example evaluate.evaluate output"
$ prodigy evaluate.evaluate my_custom_ner_model --ner ner_dataset --label-stats
ℹ Using CPU

================================= Results =================================

TOK     100.00
NER P   92.80
NER R   99.58
NER F   96.07
SPEED   26868

============================== NER (per type) ==============================

P        R       F
SKILL        92.53    99.55   95.91
EXPERIENCE   96.88   100.00   98.41
```

| Argument                    | Type | Description                                                                                                                                       | Default |
| --------------------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| `model`                     | str  | Name of spaCy pipeline to evaluate.                                                                                                               |         |
| `--ner`                     | str  | One or more (comma-separated) datasets for the named entity recognizer.                                                                           | `None`  |
| `--textcat`                 | str  | One or more (comma-separated) datasets for the text classifier (exclusive categories).                                                            | `None`  |
| `--textcat-multilabel`      | str  | One or more (comma-separated) datasets for the text classifier (non-exclusive categories).                                                        | `None`  |
| `--senter`                  | str  | One or more (comma-separated) datasets for the sentence recognizer.                                                                               | `None`  |
| `--parser`                  | str  | One or more (comma-separated) datasets for the dependency parser.                                                                                 | `None`  |
| `--tagger`                  | str  | One or more (comma-separated) datasets for the part-of-speech tagger.                                                                             | `None`  |
| `--spancat`                 | str  | One or more (comma-separated) datasets for the span categorizer.                                                                                  | `None`  |
| `--coref`                   | str  | One or more (comma-separated) datasets for the coreference model. Requires [spacy-experimental](https://github.com/explosion/spacy-experimental). | `None`  |
| `--label-stats`, `-LS`      | bool | Compute per-label statistics for NER and textcat components.                                                                                      | `False` |
| `--gpu_id`                  | int  | ID of the GPU to use.                                                                                                                             | `-1`    |
| `--verbose`                 | bool | Print detailed information about the evaluation.                                                                                                  | `False` |
| `--confusion-matrix`, `-CF` | bool | Compute confusion matrix for NER, textcat and textcat-multilabel components.                                                                      | `False` |
| `--cf-path`, `-CP`          | str  | Local path to save the confusion matrix to. Available for NER, textcat and textcat-multilabel components.                                         | `None`  |
| `--spans-key`               | str  | Key to use for spans in the evaluation data.                                                                                                      | `sc`    |

### `evaluate.evaluate-example`

- **Type:** command
- **Interface:** terminal only
- **Use case:** evaluate a spaCy pipeline on one or more datasets on a per-example basis

Evaluate a spaCy pipeline on one or more datasets for different components on a
_per-example_ basis. Datasets are provided in the same per-component format as
the prodigy evaluate command e.g. `--ner my_eval_dataset_1,my_eval_dataset_2`.
This command will run an evaluation on each example individually and then sort
by the desired `--metric` argument.

This is helpful for debugging and for understanding the hardest or easiest
examples for your model. The example below shows how to evaluate a model on a
dataset on a per-example basis and sort by the lowest NER F1 score.

If you would like to save the top examples sorted by your metric, you can use
the `--output-path` argument to save the examples in `.jsonl` format to file. If
you're evaluating NER, spancat or textcat pipeline, this `.jsonl` file could
then be used as input to Prodigy `correct` ([`ner.correct`](https://prodi.gy/docs/recipes#ner-correct),
[`spans.correct`](https://prodi.gy/docs/recipes#spans-correct), [`textcat.correct`](https://prodi.gy/docs/recipes#textcat-correct)) or `model-annotate`
([`ner.model-annotate`](https://prodi.gy/docs/recipes#ner-model-annotate), [`spans.model-annotate`](https://prodi.gy/docs/recipes#spans-model-annotate),
[`textcat.model-annotate`](https://prodi.gy/docs/recipes#textcat-model-annotate)) workflows to quickly inspect your model's
predictions on hardest examples.

```text title="Example evaluate.evaluate-example output"
$ prodigy evaluate.evaluate-example my_custom_ner_model --ner ner_dataset --metric ents_f --n-results 3
ℹ Using CPU

============================= Scored Examples =============================

Example                     ents_f
-----------------           ------
I live in london.           0.0
My name is Freya.           0.0
Where is Antonia?           0.0
```

| Argument               | Type | Description                                                                                                                                                                                                                                                                                                                                        | Default |   |
| ---------------------- | ---- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- | - |
| `model`                | str  | Name of spaCy pipeline to evaluate.                                                                                                                                                                                                                                                                                                                |         |   |
| `--ner`                | str  | One or more (comma-separated) datasets for the named entity recognizer.                                                                                                                                                                                                                                                                            | `None`  |   |
| `--textcat`            | str  | One or more (comma-separated) datasets for the text classifier (exclusive categories).                                                                                                                                                                                                                                                             | `None`  |   |
| `--textcat-multilabel` | str  | One or more (comma-separated) datasets for the text classifier (non-exclusive categories).                                                                                                                                                                                                                                                         | `None`  |   |
| `--senter`             | str  | One or more (comma-separated) datasets for the sentence recognizer.                                                                                                                                                                                                                                                                                | `None`  |   |
| `--parser`             | str  | One or more (comma-separated) datasets for the dependency parser.                                                                                                                                                                                                                                                                                  | `None`  |   |
| `--tagger`             | str  | One or more (comma-separated) datasets for the part-of-speech tagger.                                                                                                                                                                                                                                                                              | `None`  |   |
| `--spancat`            | str  | One or more (comma-separated) datasets for the span categorizer.                                                                                                                                                                                                                                                                                   | `None`  |   |
| `--coref`              | str  | One or more (comma-separated) datasets for the coreference model. Requires [`spacy-experimental`](https://github.com/explosion/spacy-experimental).                                                                                                                                                                                                | `None`  |   |
| `--metric`             | str  | The metric to sort the examples by. The following metrics are supported: `token_acc`, `tag_acc`, `pos_acc`, `morph_acc`, `lemma_acc`, `dep_uas`, `dep_las`, `ents_p`, `ents_r`, `ents_f`, `cats_score`, `sents_p`, `sents_r`, `sents_f`, `spans_sc_p`, `spans_sc_r`, `spans_sc_f`, `speed`. Please choose a metric most appropriate to your model. | `None`  |   |
| `--n-results`, `-NR`   | int  | Number of top examples to display.                                                                                                                                                                                                                                                                                                                 | `10`    |   |
| `--gpu_id`             | int  | ID of the GPU to use.                                                                                                                                                                                                                                                                                                                              | `-1`    |   |
| `--verbose`            | bool | Print detailed information about the evaluation.                                                                                                                                                                                                                                                                                                   | `False` |   |
| `--output-path`, `-OP` | str  | Path to a `jsonl` file to save the scored examples to.                                                                                                                                                                                                                                                                                             | `None`  |   |

### `evaluate.nervaluate`

- **Type:** command
- **Interface:** terminal only
- **Use case:** evaluate a spaCy NER component using full named-entity evaluation metrics based on SemEval '13

Evaluate a spaCy NER component using full named-entity evaluation metrics based
on SemEval '13. Datasets are provided in the same per-component format as the
prodigy evaluate command e.g. `--ner my_eval_dataset_1,my_eval_dataset_2`.

This command leverages the `nervaluate`
[Python library](https://github.com/MantisAI/nervaluate) to "go beyond a simple
token/tag based schema, and consider different scenarios based on weather all
the tokens that belong to a named entity were classified or not, and also
whether the correct entity type was assigned."

This is helpful if you are interested in **partial matches** as part of your NER
evaluation use-case. If you are interested in per-label evaluation metrics, you
can pass the `--per-label` flag to the command.

```bash
prodigy evaluate.nervaluate my_custom_ner_model --ner ner_dataset --per-label
```

| Argument      | Type | Description                                                                                                       | Default |
| ------------- | ---- | ----------------------------------------------------------------------------------------------------------------- | ------- |
| `model`       | str  | Name of spaCy pipeline to evaluate. Must have a trained NER model to evaluate.                                    |         |
| `--ner`       | str  | One or more (comma-separated) datasets for the named entity recognizer. Use the eval: prefix for evaluation sets. | `None`  |
| `--gpu_id`    | int  | ID of the GPU to use.                                                                                             | `-1`    |
| `--verbose`   | bool | Print detailed information about the evaluation.                                                                  | `False` |
| `--per-label` | bool | print per-label NER metrics to the terminal.                                                                      | `False` |
