Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redo packaging #12

Merged
merged 1 commit into from Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/python-publish.yml
@@ -0,0 +1,37 @@
name: Release

on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-ecosystem/action-regex-match@v2
id: regex-match
with:
text: ${{ github.event.head_commit.message }}
regex: '^Release ([^ ]+)'
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Release
if: ${{ steps.regex-match.outputs.match != '' }}
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ steps.regex-match.outputs.group1 }}
- name: Build and publish
if: ${{ steps.regex-match.outputs.match != '' }}
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
3 changes: 3 additions & 0 deletions Makefile
@@ -0,0 +1,3 @@
install: ## [Local development] Upgrade pip, install requirements, install package.
python -m pip install -U pip
python -m pip install -e .
2 changes: 1 addition & 1 deletion Model Cards/M-BERT Base 69/README.md
Expand Up @@ -13,7 +13,7 @@
To use this model along with the original CLIP vision encoder follow the [main page usage instructions](https://github.com/FreddeFrallan/Multilingual-CLIP) to download the additional linear weights.
Once this is done, you can load and use the model with the following code
```python
from src import multilingual_clip
from mclip import multilingual_clip

model = multilingual_clip.load_model('M-BERT-Base-69')
embeddings = model(['Älgen är skogens konung!', 'Wie leben Eisbären in der Antarktis?', 'Вы знали, что все белые медведи левши?'])
Expand Down
2 changes: 1 addition & 1 deletion Model Cards/M-BERT Base ViT-B/README.md
Expand Up @@ -13,7 +13,7 @@
To use this model along with the original CLIP vision encoder follow the [main page usage instructions](https://github.com/FreddeFrallan/Multilingual-CLIP) to download the additional linear weights.
Once this is done, you can load and use the model with the following code
```python
from src import multilingual_clip
from mclip import multilingual_clip

model = multilingual_clip.load_model('M-BERT-Base-ViT-B')
embeddings = model(['Älgen är skogens konung!', 'Wie leben Eisbären in der Antarktis?', 'Вы знали, что все белые медведи левши?'])
Expand Down
2 changes: 1 addition & 1 deletion Model Cards/M-BERT Distil 40/README.md
Expand Up @@ -13,7 +13,7 @@
To use this model along with the original CLIP vision encoder follow the [main page usage instructions](https://github.com/FreddeFrallan/Multilingual-CLIP) to download the additional linear weights.
Once this is done, you can load and use the model with the following code
```python
from src import multilingual_clip
from mclip import multilingual_clip

model = multilingual_clip.load_model('M-BERT-Distil-40')
embeddings = model(['Älgen är skogens konung!', 'Wie leben Eisbären in der Antarktis?', 'Вы знали, что все белые медведи левши?'])
Expand Down
2 changes: 1 addition & 1 deletion Model Cards/Swe-CLIP 2M/README.md
Expand Up @@ -13,7 +13,7 @@
To use this model along with the original CLIP vision encoder follow the [main page usage instructions](https://github.com/FreddeFrallan/Multilingual-CLIP) to download the additional linear weights.
Once this is done, you can load and use the model with the following code
```python
from src import multilingual_clip
from mclip import multilingual_clip

model = multilingual_clip.load_model('Swe-CLIP-2M')
embeddings = model(['Älgen är skogens konung!', 'Alla isbjörnar är vänsterhänta'])
Expand Down
2 changes: 1 addition & 1 deletion Model Cards/Swe-CLIP 500k/README.md
Expand Up @@ -13,7 +13,7 @@
To use this model along with the original CLIP vision encoder follow the [main page usage instructions](https://github.com/FreddeFrallan/Multilingual-CLIP) to download the additional linear weights.
Once this is done, you can load and use the model with the following code
```python
from src import multilingual_clip
from mclip import multilingual_clip

model = multilingual_clip.load_model('Swe-CLIP-500k')
embeddings = model(['Älgen är skogens konung!', 'Alla isbjörnar är vänsterhänta'])
Expand Down
10 changes: 5 additions & 5 deletions Multilingual_CLIP.ipynb

Large diffs are not rendered by default.

33 changes: 27 additions & 6 deletions README.md
Expand Up @@ -35,12 +35,22 @@ While it is possible that other versions works equally fine, we have worked with
* Python = 3.6.9
* Transformers = 4.8.1

## Inference Example
## Install

`pip install mclip torch`

You can also choose to `pip install tensorflow` instead of torch.


## Inference Usage

Inference code for Tensorflow is also available in [inference_example.py](https://github.com/FreddeFrallan/Multilingual-CLIP/blob/main/inference_example.py)

```python
from src import pt_multilingual_clip
from mclip import pt_multilingual_clip
import transformers

exampleTexts = [
texts = [
'Three blind horses listening to Mozart.',
'Älgen är skogens konung!',
'Wie leben Eisbären in der Antarktis?',
Expand All @@ -55,6 +65,17 @@ tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
embeddings = model.forward(texts, tokenizer)
print(embeddings.shape)
```

## Install for development

Setup a virtualenv:

```
python3 -m venv .env
source .env/bin/activate
pip install -e .
```

## Pre-trained Models
Every text encoder is a [Huggingface](https://huggingface.co/) available transformer, with an additional linear layer on top. For more information of a specific model, click the Model Name to see its model card.
<br>
Expand Down Expand Up @@ -101,7 +122,7 @@ $ bash legacy_get-weights.sh

### Inference
```python
from src import multilingual_clip
from mclip import multilingual_clip

print(multilingual_clip.AVAILABLE_MODELS.keys())

Expand Down Expand Up @@ -137,11 +158,11 @@ Every text encoder is a [Huggingface](https://huggingface.co/) available transfo
</details>

## Training a new model
[This folder](https://github.com/FreddeFrallan/Multilingual-CLIP/tree/main/src/TeacherLearning) contains the code used for training the above models. If you wsh to train your own model you must do the following things:
[This folder](https://github.com/FreddeFrallan/Multilingual-CLIP/tree/main/mclip/TeacherLearning) contains the code used for training the above models. If you wsh to train your own model you must do the following things:

* Prepare a set of translated sentence pairs from English -> Your Language(s)
* Compute regular CLIP-Text embeddings for the English sentences.
* Edit [Training.py](https://github.com/FreddeFrallan/Multilingual-CLIP/blob/main/src/TeacherLearning/Training.py) to load your data.
* Edit [Training.py](https://github.com/FreddeFrallan/Multilingual-CLIP/blob/main/mclip/TeacherLearning/Training.py) to load your data.
* Train a new CLIP-Text encoder via Teacher Learning

### Pre-computed CLIP Embeddings & Translaton Data
Expand Down
4 changes: 2 additions & 2 deletions inference_example.py
Expand Up @@ -2,7 +2,7 @@


def tf_example(texts, model_name='M-CLIP/XLM-Roberta-Large-Vit-L-14'):
import tf_multilingual_clip
from mclip import tf_multilingual_clip

model = tf_multilingual_clip.MultiLingualCLIP.from_pretrained(model_name)
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
Expand All @@ -13,7 +13,7 @@ def tf_example(texts, model_name='M-CLIP/XLM-Roberta-Large-Vit-L-14'):


def pt_example(texts, model_name='M-CLIP/XLM-Roberta-Large-Vit-L-14'):
import pt_multilingual_clip
from mclip import pt_multilingual_clip

model = pt_multilingual_clip.MultilingualCLIP.from_pretrained(model_name)
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
Expand Down
2 changes: 1 addition & 1 deletion legacy_inference.py
@@ -1,4 +1,4 @@
from src.legacy_multilingual_clip import MultilingualClip
from mclip.legacy_multilingual_clip import MultilingualClip

model_path = 'M-CLIP/Swedish-500k'
tok_path = 'M-CLIP/Swedish-500k'
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,4 +1,4 @@
from src import Config_MCLIP
from mclip import Config_MCLIP
import transformers
import torch

Expand Down
@@ -1,4 +1,4 @@
from src import Config_MCLIP
from mclip import Config_MCLIP
import tensorflow as tf
import transformers

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
@@ -0,0 +1 @@
transformers
38 changes: 38 additions & 0 deletions setup.py
@@ -0,0 +1,38 @@
from setuptools import setup, find_packages
from pathlib import Path
import os

if __name__ == "__main__":
with Path(Path(__file__).parent, "README.md").open(encoding="utf-8") as file:
long_description = file.read()

def _read_reqs(relpath):
fullpath = os.path.join(os.path.dirname(__file__), relpath)
with open(fullpath) as f:
return [s.strip() for s in f.readlines() if (s.strip() and not s.startswith("#"))]

REQUIREMENTS = _read_reqs("requirements.txt")

setup(
name="mclip",
packages=find_packages(),
include_package_data=True,
version="1.0.0",
license="MIT",
description="OpenAI CLIP text encoders for multiple languages!",
long_description=long_description,
long_description_content_type="text/markdown",
author="Fredrik Carlsson",
author_email="FreddeFc@gmail.com",
url="https://github.com/FreddeFrallan/Multilingual-CLIP",
data_files=[(".", ["README.md"])],
keywords=["machine learning"],
install_requires=REQUIREMENTS,
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3.6",
],
)