ArgosTranslate.PyNet
2.0.1
See the version list below for details.
dotnet add package ArgosTranslate.PyNet --version 2.0.1
NuGet\Install-Package ArgosTranslate.PyNet -Version 2.0.1
<PackageReference Include="ArgosTranslate.PyNet" Version="2.0.1" />
paket add ArgosTranslate.PyNet --version 2.0.1
#r "nuget: ArgosTranslate.PyNet, 2.0.1"
// Install ArgosTranslate.PyNet as a Cake Addin #addin nuget:?package=ArgosTranslate.PyNet&version=2.0.1 // Install ArgosTranslate.PyNet as a Cake Tool #tool nuget:?package=ArgosTranslate.PyNet&version=2.0.1
ArgosTranslate.PyNet
.NET Wrapper for the Argos Translate Python Library
Description
ArgosTranslate.PyNet
is a .NET library that provides a convenient interface for integrating the functionality of the open-source Python library, Argos Translate
, into .NET applications. Argos Translate is an open-source offline translation library that uses OpenNMT for translations.
This wrapper is built on top of pythonnet, which enables calling Python libraries from .NET. The library takes care of setting up the Python environment, managing dependencies, and exposing functionalities seamlessly to the .NET environment.
Features
- Easy Python environment setup and management.
- Configuration flexibility with support for both embedded Python or custom installations.
- Out-of-the-box support for Argos Translate functionalities including translation and language model management.
- Integrate Argos Translate APIs into .NET. using pythonnet library to call Python functions and return .NET types.
- Integrate Argos Translate APIs into .NET. using CLI wrapper.
- Allows to perform batch translations.
Getting Started
Nuget package
Usage
- Register ArgosTranslate.PyNet services in service container:
// To use pythonenet bindings
serviceCollection.AddArgosTranslatePyNetBindings();
// To use CLI wrapper instead
serviceCollection.AddArgosTranslateCliBindings();
// or with additional optional configuration
serviceCollection.AddArgosTranslateCliBindings(pythonCliOutputEncoding: Encoding.UTF8, useSingleSharedProcessPerModule: false, filterContainingError: ["WARNING"]);
- Retrieve an instance of the IArgosTranslateEngineFactory from the IoC container and get the necessary engine directly via the service provider of from the constructor injection:
var argosTranslateEngineFactory = serviceProvider.GetRequiredService<IArgosTranslateEngineFactory>();
// uses embended python environment, currently embeded only supported on Windows OS
var engine = argosTranslateEngineFactory.GetEngineWithBuiltInWindowsPython();
var engine = argosTranslateEngineFactory.GetEngineWithCustomPythonPath(@"https://www.python.org/ftp/python/3.11.5/python-3.11.5-embed-amd64.zip");
// or depending on the OS by providing the python executable file path and other optional arguments
var engine = argosTranslateEngineFactory.GetEngineWithCustomPythonPath(@"/usr/bin/python3");
var engine = argosTranslateEngineFactory.GetEngineWithCustomPythonPath(@"C:\Users\user\AppData\Local\Programs\Python\Python311\python.exe");
- Initialize ArgosTranslate.PyNet environment:
engine.Initialize(ArgosDeviceType.Cpu);
- Retrieve needed modules from service container directly or via constructor injection:
var packageModule = serviceProvider.GetRequiredService<IPackageModule>();
var translationModule = serviceProvider.GetRequiredService<ITranslationModule>();
- Prepare packages:
// downloads remote package index
await packageModule.UpdatePackageIndexAsync();
// searches for package
// if the direct package is not found, searches return the ephemeral package that allows translation with an intermediate language
var enToUkPackageToInstall = await packageModule.GetPackageAsync("en", "uk");
var ukToEnPackageToInstall = await packageModule.GetPackageAsync("uk", "en");
var enToPlPackageToInstall = await packageModule.GetPackageAsync("en", "pl");
var plToEnPackageToInstall = await packageModule.GetPackageAsync("pl", "en");
// language enum usage
var enToEsPackageToInstall = await packageModule.GetPackageAsync(Language.English.GetCode(), Language.Spanish.GetCode());
var esToEnPackageToInstall = await packageModule.GetPackageAsync(Language.Spanish.GetCode(), Language.English.GetCode());
// packages with an intermediate language, English is the default
var ukToPlPackageToInstall = await packageModule.GetPackageAsync("uk", "pl", intermediateCode: Language.English.GetCode());
var plToUkPackageToInstall = await packageModule.GetPackageAsync("pl", "uk", intermediateCode: Language.English.GetCode());
- Packages installation:
await packageModule.InstallPackageAsync(ukToPlPackageToInstall);
await packageModule.InstallPackageAsync(plToUkPackageToInstall);
- Translation:
string[] ukrainianTexts =
[
"Привіт!",
"Це чудовий день."
];
var polishTexts = await translationModule.TranslateBatchAsync(ukrainianTexts, Language.Ukrainian.GetCode(), Language.Polish.GetCode());
// or
var singleTranslation = await translationModule.TranslateAsync("world", "en", "uk")
Contributing
Contributions are welcome!
License
This project is open-source, licensed under MIT License. See MIT LICENSE for more details.
Acknowledgments
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- pythonnet (>= 3.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.