ArgosTranslate.PyNet 2.0.3

dotnet add package ArgosTranslate.PyNet --version 2.0.3
NuGet\Install-Package ArgosTranslate.PyNet -Version 2.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ArgosTranslate.PyNet" Version="2.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ArgosTranslate.PyNet --version 2.0.3
#r "nuget: ArgosTranslate.PyNet, 2.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ArgosTranslate.PyNet as a Cake Addin
#addin nuget:?package=ArgosTranslate.PyNet&version=2.0.3

// Install ArgosTranslate.PyNet as a Cake Tool
#tool nuget:?package=ArgosTranslate.PyNet&version=2.0.3

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

  1. 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"]);
Console.InputEncoding = Encoding.UTF8; // additionally the input encoding might need to be adjusted
  1. 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");
  1. Initialize ArgosTranslate.PyNet environment:
 engine.Initialize(ArgosDeviceType.Cpu);
  1. Retrieve needed modules from service container directly or via constructor injection:
var packageModule = serviceProvider.GetRequiredService<IPackageModule>();
var translationModule = serviceProvider.GetRequiredService<ITranslationModule>();
  1. Prepare packages:
// downloads remote package index
await packageModule.UpdatePackageIndexAsync();

// searches for package
// if the direct package is not found, returns 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());
  1. Packages installation:
await packageModule.InstallPackageAsync(ukToPlPackageToInstall);
await packageModule.InstallPackageAsync(plToUkPackageToInstall);
  1. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.3 161 3/17/2024
2.0.2 116 3/17/2024
2.0.1 118 3/17/2024
2.0.0 136 3/17/2024
1.2.2 293 11/20/2023
1.0.30 118 11/16/2023
1.0.28 98 11/16/2023
1.0.23 106 11/15/2023
1.0.22 101 11/15/2023
1.0.21 101 11/15/2023
1.0.16 134 11/6/2023