Skip to content

Commit

Permalink
MLH fellowship contribution: adding the laser_encoders module (face…
Browse files Browse the repository at this point in the history
…bookresearch#249)

* feat: converted SPMapply function to use python script

* modified laserTokenizer class to have a seperate function for tokenizing a file

* modified tokenize_file function

* removed instances of Path

* created new function for opening files

* test for LaserTokenizer.tokenize

* tests for normalisation, descape and lower_case

* deleted test dir because of relative import error

* modified test tokenizer function to use the downloaded model before exiting the context manager

* test for tokenize_file

* added test for is_printable

* test for over_write when equal to True and False

* added some type hints for tests

* added type hint for log function

* added header comment

* feat: make LASER pip installable (facebookresearch#239)

* feat: make LASER pip installable

* Added GitHub Actions workflow for tests and linting

* upgraded python version due to node depreciation error

* removed updated python version

* removed poetry

* bug fixes

* removed dependencies install

* updated pyproject and made lint_and_test to install dev and mono dependencies

* removed isort and black

* removed mono dependencies

* removed version from pyproject

* removed duplicate of classifiers

* removed description

* removed dynamic

* added src-layout to discover only laser_encoder

* added build backend

* updated project name

* changed license to BSD

* removed src-layout to test

* added linting to actions

* updated linting to only check the laser_encoders folder

* fixed linting issues

* fixed black linting issues

* added white-space

* Refactor embedder (facebookresearch#241)

* feat: make LASER pip installable

* Added GitHub Actions workflow for tests and linting

* upgraded python version due to node depreciation error

* removed updated python version

* removed poetry

* bug fixes

* removed dependencies install

* updated pyproject and made lint_and_test to install dev and mono dependencies

* removed isort and black

* removed mono dependencies

* removed version from pyproject

* removed duplicate of classifiers

* removed description

* removed dynamic

* added src-layout to discover only laser_encoder

* added build backend

* updated project name

* changed license to BSD

* removed src-layout to test

* added linting to actions

* updated linting to only check the laser_encoders folder

* fixed linting issues

* fixed black linting issues

* added white-space

* refactored emmbeder to work in the laser tokenizer package

* downgraded numpy version to suit the installled python version

* added test for sentence encoder

* added whitespace to test workflow

* restructured test for sentence encoder

* restructured test for sentence encoder

* fixed black issues

* restructured test for sentence encoder

* changed python version because of workflow error

* updated dependencies requirements version

* removed unneccessary print statement

* updated python version

* restructured test_sentence_encoder

* restructured test_sentence encoder

* black linting fixes

* restructure calling of tempile module

* updated workflow to remove pip cache

* removed commented code

* refactored code and added type hints

* fixed black issues

* fixed no module found error by adding Laser environment

* feat: Add Python function to download LASER models (facebookresearch#244)

* feat: make LASER pip installable

* Added GitHub Actions workflow for tests and linting

* upgraded python version due to node depreciation error

* removed updated python version

* removed poetry

* bug fixes

* removed dependencies install

* updated pyproject and made lint_and_test to install dev and mono dependencies

* removed isort and black

* removed mono dependencies

* removed version from pyproject

* removed duplicate of classifiers

* removed description

* removed dynamic

* added src-layout to discover only laser_encoder

* added build backend

* updated project name

* changed license to BSD

* removed src-layout to test

* added linting to actions

* updated linting to only check the laser_encoders folder

* fixed linting issues

* fixed black linting issues

* added white-space

* refactored emmbeder to work in the laser tokenizer package

* downgraded numpy version to suit the installled python version

* added test for sentence encoder

* added whitespace to test workflow

* restructured test for sentence encoder

* restructured test for sentence encoder

* fixed black issues

* restructured test for sentence encoder

* changed python version because of workflow error

* updated dependencies requirements version

* removed unneccessary print statement

* updated python version

* restructured test_sentence_encoder

* restructured test_sentence encoder

* black linting fixes

* restructure calling of tempile module

* updated workflow to remove pip cache

* removed commented code

* refactored code and added type hints

* fixed black issues

* fixed no module found error by adding Laser environment

* feat:created download function for downloading laser models in python

* added language list and made some changes to the download models

* fixed linting issues

* added type hints

* fixed linting issues

* added progress bar for downloading of models

* fixed black issues

* updated code to download laser model based on where the language is found

* fixed black and linting issues

* fixed black issues

* fixed bug in sentence encoder

* black issues and relative import issues

* removed addition of laser path

* fixed isort issues

* refactored the python entrypoint functions

* fixed black issues

* updated laguage list with some laser2 and laser3 languages

* refactor: added option for laser

* added laser2 language list

* added laser3 language list

* fixed black issues

* updated language list

* refactoed download function to display total filesize in MB and also made some changes to raise an error when laser is not passed

* fixed black issues

* refactored download models to move model_dir to the class

* fixed black issues

* refactored laser tokenizer test to use the laser downloader class methods

* documentation for the laser_encoder

* added tokenizer part

* added some docs for tokenize file and download models

* updated readme to include supported flore200 langs

* corrected readme path and license

* added requirements for laser_encoder

* added __main__.py file for running download command easily

* black and isort fixes, updated docs to effect changes due to creation of __main__.py file

* added contributors section

* Revert "added requirements for laser_encoder"

This reverts commit 431780e.

reverting back

* reverting creation of main.py

* fixed isort and black issues

* removed irrelevant comment

* moved pyproject to laser direcory and adjust contributors name

* workflow issues due to removal of pyproject

* pointed workflow to laser_encoders dir

* fixed EOF error

* fixed EOF error

* debuging

* debuging

* debuging

* debuging

* debuging

* debuging

* debuging

* debuging

* debuging

* debuging

* debuging

* debuging

* bug fixes and new implementation of convert_tokens_to_id function

* bug fix

* bug fix

* bug fix

* bug fix

* bug fix

* bug fix

* bug fix

* bug fix

* bug fix

* reverting back because of workflow error

* reverting back because of workflow error

* some extra adjustment

* changed ibo to igbo

* updated doc to effect the ibo to igbo change

* refactore: modified the sentence encoder to tokenize a text before encodingit

* debugging failed test

* added a call method to seperately handle the tokenization before encodding

* added value error for when there is no spm_model

* documentation for the new __call__ method for tokenization with encoder

* docs: Update docs to include reference to laserembeddings (facebookresearch#254)

* Handle Interrupted Model Weight Downloads (facebookresearch#253)

* fix: Fix interrupted downloads issue

* style: Format code using black

* Update download method to use tempfile

* style: Remove unnecessary space

* Fix OSError by using shutil.move for cross-filesystem moves

Using os.rename caused an OSError when trying to move files across different filesystems (e.g., from /tmp to another directory).
By using shutil.move, we gracefully handle such situations,
ensuring files are moved correctly regardless of the source and destination filesystems.

* Refactor `initialize_encoder` to `LaserEncoderPipeline` (facebookresearch#256)

* Remove 'tokenize' argument from initialize_encoder function

* Add LaserEncoderPipeline for streamlined tokenization and encoding

* docs: Update README to show use of LaserEncoderPipeline

* style: Reformat code using black

* refactor: move encoder and tokenizer initialization into repective files

* style: run black

* test: Add test for LaserEncoderPipeline

* test to validate languages

* test to validate languages

* Delete flores directory

* Update validate_models.py

* Update validate_models.py

* Update validate_models.py

* Update validate_models.py

* Update .gitignore

* added pytest to validate_models.py

* Update validate_models.py

* Update validate_models.py

* Update validate_models.py using mock downloader

* Update validate_models.py

* Update validate_models.py

* Update validate_models.py

* Update validate_models.py

* Extend Tokenizer to Support Single Strings and Lists of Strings (facebookresearch#258)

* Handle case for both str and list in tokenizer

* test: Add test for tokenizer call method

* Rename 'sentences' argument to 'text_or_batch' for clarity

* Handle string input in call method

* Update validate_models.py

* Update download_models.py according to 1.

* Update download_models.py

* Update download_models.py

* Update download_models.py

* Enhance LaserTokenizer with Perl Parity, Optional Punctuation Normalization, and Embedding Normalization (facebookresearch#262)

* Introduce pearl compability flag

* Add argument `normalize_punct` to `LaserTokenizer`

* Add normalize_embeddings option to encode_sentences

* Update README on normalize_embeddings option

* style: Run black and isort

* test: Add tests for normalize_embeddings flag in sentence encoder

* style: Run black

* Update validate_models.py

* Update models.py

* Update laser_tokenizer.py

* Update download_models.py

* Update validate_models.py

* Update validate_models.py

* Added slow and fast tests to validate_models.py

* Update validate_models.py

* Update validate_models.py

* Create test_validate_models.py

* Rename test_validate_models.py to test_models_initialization.py

* Update test_models_initialization.py

* Update test_models_initialization.py

* Update download_models.py

* Update test_models_initialization.py

* Update test_models_initialization.py

* Update download_models.py

* Update validate_models.py

* Update validate_models.py

* Update validate_models.py

* Update validate_models.py

* Update validate_models.py

* Update validate_models.py

* Update validate_models.py

* Update validate_models.py

* Update README.md

* Update README.md

* Decrease versions of numpy and torch required by laser-encoders (facebookresearch#264)

* Update requirements to follow fairseq

* Update README

* Update dependencies in toml file

* Remove requirements.txt

* Update laser_encoders README

* resolve parity with MOSES-4.0 release

* update test

* Update the main README file with a mention of `laser_encoders` (facebookresearch#266)

* update the main readme file

* wording changes

* update the example in the readme

* fix readme text

* Update language_list.py (facebookresearch#269)

* Update language_list.py

* Update language_list.py

* Update language_list.py

* Updated laser encoder pipeline

* Update models.py

* Update models.py

* Added warning for using laser2 with a language

* add tests to test_laser_tokenizer.py

* Update test_laser_tokenizer.py

* Update models.py

* Update test_laser_tokenizer.py

* Update test_laser_tokenizer.py

* Update language_list.py

* Update language_list.py

* Update language_list.py

---------

Co-authored-by: CaptainVee <[email protected]>
Co-authored-by: Victor Joseph <[email protected]>
Co-authored-by: Kevin Heffernan <[email protected]>
Co-authored-by: Okewunmi Paul <[email protected]>
Co-authored-by: NIXBLACK11 <[email protected]>
Co-authored-by: Siddharth Singh Rana <[email protected]>
Co-authored-by: Kevin Heffernan <[email protected]>
  • Loading branch information
8 people committed Nov 21, 2023
1 parent dd4e527 commit 91ca272
Show file tree
Hide file tree
Showing 18 changed files with 2,126 additions and 305 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/lint_and_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: lint_and_tests

on: [push, pull_request]

jobs:
build:
strategy:
max-parallel: 1
matrix:
platform: [ubuntu-latest]
python-version: [3.8]

runs-on: ${{ matrix.platform }}

steps:
- uses: actions/checkout@v2

- name: Install dependencies
run: |
python --version
python -m pip install --upgrade 'pip>=23.2.1'
python -m pip show pip
python -m pip install -e '.[dev]'
- name: isort
run: cd laser_encoders && isort --check --diff .

- name: black
run: cd laser_encoders && black --check --diff .

- name: pytest
run: pytest laser_encoders
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ tasks/xnli/XNLI-1.0*
tasks/xnli/multinli_1.0*
.??*swp
.idea
__pycache__
nllb
dist
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
LASER is a library to calculate and use multilingual sentence embeddings.

**NEWS**
* 2023/11/16 Released [**laser_encoders**](laser_encoders), a pip-installable package supporting LASER-2 and LASER-3 models
* 2023/06/26 [**xSIM++**](https://arxiv.org/abs/2306.12907) evaluation pipeline and data [**released**](tasks/xsimplusplus/README.md)
* 2022/07/06 Updated LASER models with support for over 200 languages are [**now available**](nllb/README.md)
* 2022/07/06 Multilingual similarity search (**xsim**) evaluation pipeline [**released**](tasks/xsim/README.md)
Expand All @@ -26,7 +27,27 @@ a language family which is covered by other languages.
A detailed description of how the multilingual sentence embeddings are trained can
be found [here](https://arxiv.org/abs/2205.12654), together with an experimental evaluation.

## Dependencies
## The core sentence embedding package: `laser_encoders`
We provide a package `laser_encoders` with minimal dependencies.
It supports LASER-2 (a single encoder for the languages listed [below](#supported-languages))
and LASER-3 (147 language-specific encoders described [here](nllb/README.md)).

The package can be installed simply with `pip install laser_encoders` and used as below:

```python
from laser_encoders import LaserEncoderPipeline
encoder = LaserEncoderPipeline(lang="eng_Latn")
embeddings = encoder.encode_sentences(["Hi!", "This is a sentence encoder."])
print(embeddings.shape) # (2, 1024)
```

The laser_encoders [readme file](laser_encoders) provides more examples of its installation and usage.

## The full LASER kit
Apart from the `laser_encoders`, we provide support for LASER-1 (the original multilingual encoder)
and for various LASER applications listed below.

### Dependencies
* Python >= 3.7
* [PyTorch 1.0](http://pytorch.org/)
* [NumPy](http://www.numpy.org/), tested with 1.15.4
Expand All @@ -42,7 +63,8 @@ be found [here](https://arxiv.org/abs/2205.12654), together with an experimental
* [pandas](https://pypi.org/project/pandas), data analysis toolkit (`pip install pandas`)
* [Sentencepiece](https://github.com/google/sentencepiece), subword tokenization (installed automatically)

## Installation
### Installation
* install the `laser_encoders` package by e.g. `pip install -e .` for installing it in the editable mode
* set the environment variable 'LASER' to the root of the installation, e.g.
`export LASER="${HOME}/projects/laser"`
* download encoders from Amazon s3 by e.g. `bash ./nllb/download_models.sh`
Expand Down
4 changes: 4 additions & 0 deletions install_external_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ InstallMecab () {
#
###################################################################

echo "Installing the laser_encoders package in editable mode"

pip install -e .

echo "Installing external tools"

InstallMosesTools
Expand Down
149 changes: 149 additions & 0 deletions laser_encoders/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# LASER encoders

LASER Language-Agnostic SEntence Representations Toolkit

laser_encoders is the official Python package for the Facebook [LASER](https://github.com/facebookresearch/LASER) library. It provides a simple and convenient way to use LASER embeddings in Python. It allows you to calculate multilingual sentence embeddings using the LASER toolkit. These embeddings can be utilized for various natural language processing tasks, including document classification, bitext filtering, and mining.

## Dependencies

- Python `>= 3.8`
- [PyTorch `>= 1.10.0`](http://pytorch.org/)
- sacremoses `>=0.1.0`
- sentencepiece `>=0.1.99`
- numpy `>=1.21.3`
- fairseq `>=0.12.2`

You can find a full list of requirements [here](https://github.com/facebookresearch/LASER/blob/main/pyproject.toml)

## Installation

You can install `laser_encoders` package from PyPI:

```sh
pip install laser_encoders
```

Alternatively, you can install it from a local clone of this repository, in editable mode:
```sh
pip install . -e
```

## Usage

Here's a simple example on how to obtain embeddings for sentences using the `LaserEncoderPipeline`:

>**Note:** By default, the models will be downloaded to the `~/.cache/laser_encoders` directory. To specify a different download location, you can provide the argument `model_dir=path/to/model/directory`
```py
from laser_encoders import LaserEncoderPipeline

# Initialize the LASER encoder pipeline
encoder = LaserEncoderPipeline(lang="igbo")

# Encode sentences into embeddings
embeddings = encoder.encode_sentences(["nnọọ, kedu ka ị mere"])
# If you want the output embeddings to be L2-normalized, set normalize_embeddings to True
normalized_embeddings = encoder.encode_sentences(["nnọọ, kedu ka ị mere"], normalize_embeddings=True)

```

If you prefer more control over the tokenization and encoding process, you can initialize the tokenizer and encoder separately:
```py
from laser_encoders import initialize_encoder, initialize_tokenizer

# Initialize the LASER tokenizer
tokenizer = initialize_tokenizer(lang="igbo")
tokenized_sentence = tokenizer.tokenize("nnọọ, kedu ka ị mere")

# Initialize the LASER sentence encoder
encoder = initialize_encoder(lang="igbo")

# Encode tokenized sentences into embeddings
embeddings = encoder.encode_sentences([tokenized_sentence])
```
>By default, the `spm` flag is set to `True` when initializing the encoder, ensuring the accompanying spm model is downloaded.
**Supported Languages:** You can specify any language from the [FLORES200](https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200) dataset. This includes both languages identified by their full codes (like "ibo_Latn") and simpler alternatives (like "igbo").

## Downloading the pre-trained models

If you prefer to download the models individually, you can use the following command:

```sh
python -m laser_encoders.download_models --lang=your_prefered_language # e.g., --lang="igbo""
```

By default, the downloaded models will be stored in the `~/.cache/laser_encoders` directory. To specify a different download location, utilize the following command:

```sh
python -m laser_encoders.download_models --model-dir=path/to/model/directory
```

> For a comprehensive list of available arguments, you can use the `--help` command with the download_models script.
Once you have successfully downloaded the models, you can utilize the `SentenceEncoder` to tokenize and encode your text in your desired language. Here's an example of how you can achieve this:

```py
from laser_encoders.models import SentenceEncoder
from pathlib import Path

encoder = SentenceEncoder(model_path=path/to/downloaded/model, spm_model=Path(path/to/spm_model), spm_vocab=path/to/cvocab)
embeddings = encoder("This is a test sentence.")
```
If you want to perform tokenization seperately, you can do this below:
```py
from laser_encoders.laser_tokenizer import LaserTokenizer

tokenizer = LaserTokenizer(spm_model=Path(path/to/spm_model))

tokenized_sentence = tokenizer.tokenize("This is a test sentence.")

encoder = SentenceEncoder(model_path=path/to/downloaded/model, spm_vocab=path/to/cvocab)
embeddings = encoder.encode_sentences([tokenized_sentence])
```

For tokenizing a file instead of a string, you can use the following:

```py
tokenized_sentence = tokenizer.tokenize_file(inp_fname=Path(path/to/input_file.txt), out_fname=Path(path/to/output_file.txt))
```

### Now you can use these embeddings for downstream tasks

For more advanced usage and options, please refer to the official LASER repository documentation.

## LASER Versions and Associated Packages

For users familiar with the earlier version of LASER, you might have encountered the [`laserembeddings`](https://pypi.org/project/laserembeddings/) package. This package primarily dealt with LASER-1 model embeddings.

For the latest LASER-2,3 models, use the newly introduced `laser_encoders` package, which offers better performance and support for a wider range of languages.


## Contributing

We welcome contributions from the developer community to enhance and improve laser_encoders. If you'd like to contribute, you can:

1. Submit bug reports or feature requests through GitHub issues.
1. Fork the repository, make changes, and submit pull requests for review.

Please follow our [Contribution Guidelines](https://github.com/facebookresearch/LASER/blob/main/CONTRIBUTING.md) to ensure a smooth process.

### Code of Conduct

We expect all contributors to adhere to our [Code of Conduct](https://github.com/facebookresearch/LASER/blob/main/CODE_OF_CONDUCT.md).

### Contributors

The following people have contributed to this project:

- [Victor Joseph](https://github.com/CaptainVee)
- [Paul Okewunmi](https://github.com/Paulooh007)
- [Siddharth Singh Rana](https://github.com/NIXBLACK11)
- [David Dale](https://github.com/avidale/)
- [Holger Schwenk](https://github.com/hoschwenk)
- [Kevin Heffernan](https://github.com/heffernankevin)

### License

This package is released under the [LASER](https://github.com/facebookresearch/LASER/blob/main/LICENSE) BSD License.

16 changes: 16 additions & 0 deletions laser_encoders/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
#
# LASER Language-Agnostic SEntence Representations
# is a toolkit to calculate multilingual sentence embeddings
# and to use them for document classification, bitext filtering
# and mining
#
# -------------------------------------------------------

from laser_encoders.laser_tokenizer import initialize_tokenizer
from laser_encoders.models import LaserEncoderPipeline, initialize_encoder
Loading

0 comments on commit 91ca272

Please sign in to comment.