2021-06-06 14:01:33 +03:00
|
|
|
// Copyright 2019-present, the HuggingFace Inc. team, The Google AI Language Team and Facebook, Inc.
|
|
|
|
// Copyright 2019 Guillaume Becquin
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
extern crate anyhow;
|
|
|
|
|
|
|
|
use rust_bert::mbart::{
|
2021-07-11 12:13:00 +03:00
|
|
|
MBartConfigResources, MBartModelResources, MBartSourceLanguages, MBartTargetLanguages,
|
|
|
|
MBartVocabResources,
|
2021-06-06 14:01:33 +03:00
|
|
|
};
|
2023-05-30 09:20:25 +03:00
|
|
|
use rust_bert::pipelines::common::{ModelResource, ModelType};
|
2021-07-11 12:13:00 +03:00
|
|
|
use rust_bert::pipelines::translation::{Language, TranslationConfig, TranslationModel};
|
2022-02-26 00:24:03 +03:00
|
|
|
use rust_bert::resources::RemoteResource;
|
2021-07-11 12:13:00 +03:00
|
|
|
use tch::Device;
|
2021-06-06 14:01:33 +03:00
|
|
|
|
|
|
|
fn main() -> anyhow::Result<()> {
|
2022-02-26 00:24:03 +03:00
|
|
|
let model_resource = RemoteResource::from_pretrained(MBartModelResources::MBART50_MANY_TO_MANY);
|
|
|
|
let config_resource =
|
|
|
|
RemoteResource::from_pretrained(MBartConfigResources::MBART50_MANY_TO_MANY);
|
|
|
|
let vocab_resource = RemoteResource::from_pretrained(MBartVocabResources::MBART50_MANY_TO_MANY);
|
2021-07-11 12:13:00 +03:00
|
|
|
|
|
|
|
let source_languages = MBartSourceLanguages::MBART50_MANY_TO_MANY;
|
|
|
|
let target_languages = MBartTargetLanguages::MBART50_MANY_TO_MANY;
|
|
|
|
|
|
|
|
let translation_config = TranslationConfig::new(
|
|
|
|
ModelType::MBart,
|
2023-05-30 09:20:25 +03:00
|
|
|
ModelResource::Torch(Box::new(model_resource)),
|
2021-07-11 12:13:00 +03:00
|
|
|
config_resource,
|
|
|
|
vocab_resource,
|
2022-10-30 10:39:52 +03:00
|
|
|
None,
|
2021-07-11 12:13:00 +03:00
|
|
|
source_languages,
|
|
|
|
target_languages,
|
|
|
|
Device::cuda_if_available(),
|
2021-06-06 14:01:33 +03:00
|
|
|
);
|
2021-07-11 12:13:00 +03:00
|
|
|
let model = TranslationModel::new(translation_config)?;
|
|
|
|
|
|
|
|
let source_sentence = "This sentence will be translated in multiple languages.";
|
|
|
|
|
|
|
|
let mut outputs = Vec::new();
|
2021-11-07 11:42:56 +03:00
|
|
|
outputs.extend(model.translate(&[source_sentence], Language::English, Language::French)?);
|
|
|
|
outputs.extend(model.translate(&[source_sentence], Language::English, Language::Spanish)?);
|
|
|
|
outputs.extend(model.translate(&[source_sentence], Language::English, Language::Hindi)?);
|
2021-06-06 14:01:33 +03:00
|
|
|
|
2021-07-11 12:13:00 +03:00
|
|
|
for sentence in outputs {
|
2023-01-30 20:53:18 +03:00
|
|
|
println!("{sentence}");
|
2021-06-06 14:01:33 +03:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|