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
Add translation pipeline model #110
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3f551de
create pipeline model
2c8a5f8
update reqs
5f905ad
fix typo in setup
08bb39f
fix imports
96318ca
fix imports
f058549
fix bugs in summarize method
36088a5
add to docs and reformat
2c01eeb
Merge branch 'main' into nick/translationmodel
82f62b7
fix formatting
0627807
add supported langs for translation
83f0740
remove langs not supported by fasttext
36ef1a5
update mBART langs in README
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ mpi4py==3.0.3 | |
tqdm==4.49.0 | ||
tensorboard==2.4.1 | ||
fasttext==0.9.2 | ||
easynmt==2.0.1 | ||
black | ||
flake8 | ||
progressbar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .mbart_model import MBartModel | ||
from .mt5_model import MT5Model | ||
from .translation_pipeline_model import TranslationPipelineModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
summertime/model/single_doc/multilingual/translation_pipeline_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
from .base_multilingual_model import MultilingualSummModel, fasttext_predict | ||
from summertime.model.base_model import SummModel | ||
from summertime.model.single_doc import BartModel | ||
|
||
from easynmt import EasyNMT | ||
|
||
|
||
class TranslationPipelineModel(MultilingualSummModel): | ||
""" | ||
A class for multilingual summarization performed by first | ||
translating into English then performing summarization in English. | ||
""" | ||
|
||
model_name = "Translation Pipeline" | ||
is_multilingual = True | ||
# TODO: change to Pegasus as default? | ||
# language codes from https://github.com/UKPLab/EasyNMT#Opus-MT documentation | ||
# language codes not supported by https://fasttext.cc/docs/en/language-identification.html | ||
# are removed | ||
supported_langs = [ | ||
"aed", | ||
"af", | ||
"am", | ||
"ar", | ||
"az", | ||
"bat", | ||
"bcl", | ||
"be", | ||
"bg", | ||
"bn", | ||
"ca", | ||
"ceb", | ||
"cs", | ||
"cy", | ||
"da", | ||
"de", | ||
"el", | ||
"en", | ||
"eo", | ||
"es", | ||
"et", | ||
"eu", | ||
"fi", | ||
"fr", | ||
"ga", | ||
"gl", | ||
"gv", | ||
"he", | ||
"hi", | ||
"hr", | ||
"ht", | ||
"hu", | ||
"hy", | ||
"id", | ||
"ilo", | ||
"is", | ||
"it", | ||
"ja", | ||
"ka", | ||
"ko", | ||
"lt", | ||
"lv", | ||
"mg", | ||
"mk", | ||
"ml", | ||
"mr", | ||
"ms", | ||
"mt", | ||
"nl", | ||
"no", | ||
"pa", | ||
"pl", | ||
"pt", | ||
"ro", | ||
"ru", | ||
"run", | ||
"sh", | ||
"sk", | ||
"sl", | ||
"sq", | ||
"sv", | ||
"sw", | ||
"th", | ||
"tl", | ||
"tr", | ||
"uk", | ||
"ur", | ||
"vi", | ||
"wa", | ||
"war", | ||
"yo", | ||
"zh", | ||
] | ||
|
||
lang_tag_dict = {lang: lang for lang in supported_langs} | ||
|
||
def __init__(self, model_backend: SummModel = BartModel, **kwargs): | ||
|
||
model: SummModel = model_backend(**kwargs) | ||
self.model = model | ||
|
||
super(TranslationPipelineModel, self).__init__( | ||
trained_domain=self.model.trained_domain, | ||
max_input_length=self.model.max_input_length, | ||
max_output_length=self.model.max_output_length, | ||
) | ||
|
||
# translation module | ||
self.translator = EasyNMT("opus-mt") | ||
|
||
def summarize(self, corpus, queries=None): | ||
self.assert_summ_input_type(corpus, queries) | ||
|
||
src_lang = fasttext_predict(corpus) | ||
# translate to English | ||
corpus = self.translator.translate( | ||
corpus, source_lang=src_lang, target_lang="en", beam_size=4 | ||
) | ||
# TODO: translate each doc separately if provided multiple docs in corpus? | ||
if queries: | ||
queries = self.translator.translate(queries, target_lang="en", beam_size=4) | ||
|
||
# summarize in English | ||
english_summaries = self.model.summarize(corpus, queries) | ||
|
||
summaries = self.translator.translate( | ||
english_summaries, source_lang="en", target_lang=src_lang, beam_size=4 | ||
) | ||
|
||
return summaries | ||
|
||
@classmethod | ||
def show_capability(cls) -> None: | ||
basic_description = cls.generate_basic_description() | ||
more_details = ( | ||
"A simple pipeline model for multilingual translation. " | ||
"Uses machine translation to translate input into English, " | ||
"then performs summarization in English before translating results " | ||
"back to the original language.\n" | ||
"Strengths: \n - Massively multilingual: supports ~150 languages\n" | ||
"Weaknesses: \n - Information loss from translation to and from English" | ||
"Initialization arguments: \n " | ||
" - model_backend: the monolingual model to use for summarization. Defaults to BART" | ||
# TODO: if change to Pegasus, change this to reflect that!! | ||
"- `device = 'cpu'` specifies the device the model is stored on and uses for computation. " | ||
"Use `device='cuda'` to run on an Nvidia GPU." | ||
) | ||
print(f"{basic_description} \n {'#'*20} \n {more_details}") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when you specify the beam_size as 4, is the output one sentence or 4?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still just one sentence -- this is just the beam size for the translation model's generation.