Skip to content

DeepL MT performance improvements #1013

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Net;
using System.Net.Http;

namespace Sdl.Community.DeepLMTProvider.Helpers
{
public static class ExceptionExtensions
{
private const string HttpStatusCodeDataKey = "StatusCode";

public static void SetHttpStatusCode(this HttpRequestException ex, HttpStatusCode statusCode)
{
ex.Data[HttpStatusCodeDataKey] = statusCode;
}

public static HttpStatusCode? GetHttpStatusCode(this HttpRequestException ex)
{
if (!ex.Data.Contains(HttpStatusCodeDataKey))
return null;

return (HttpStatusCode)ex.Data[HttpStatusCodeDataKey];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
<Reference Include="WindowsFormsIntegration" />
</ItemGroup>
<ItemGroup>
<Compile Include="Helpers\ExceptionExtensions.cs" />
<Compile Include="Studio\DeepLMtTranslationProvider.cs" />
<Compile Include="Studio\DeepLMtTranslationProviderFactory.cs" />
<Compile Include="Studio\DeepLMtTranslationProviderLanguageDirection.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using NLog;
using Sdl.Community.DeepLMTProvider.Helpers;
Expand Down Expand Up @@ -167,7 +168,7 @@ public SearchResults[] SearchTranslationUnitsMasked(SearchSettings settings, Tra
if (preTranslateList.Count > 0)
{
//Create temp file with translations
var translatedSegments = PrepareTempData(preTranslateList).Result;
var translatedSegments = PrepareTempData(preTranslateList);
var preTranslateSearchResults = GetPreTranslationSearchResults(translatedSegments);

foreach (var result in preTranslateSearchResults)
Expand Down Expand Up @@ -282,7 +283,7 @@ private string LookupDeepL(string sourceText)
return _connecter.Translate(_languageDirection, sourceText);
}

private async Task<List<PreTranslateSegment>> PrepareTempData(List<PreTranslateSegment> preTranslateSegments)
private List<PreTranslateSegment> PrepareTempData(List<PreTranslateSegment> preTranslateSegments)
{
try
{
Expand All @@ -307,16 +308,21 @@ private async Task<List<PreTranslateSegment>> PrepareTempData(List<PreTranslateS
}
}

await Task.Run(() => Parallel.ForEach(preTranslateSegments, segment =>
{
if (segment != null)
{
segment.PlainTranslation = _connecter.Translate(_languageDirection, segment.SourceText);
}
})).ConfigureAwait(true);

return preTranslateSegments;
}
string[] segmentTranslation = _connecter.Translate(
_languageDirection,
preTranslateSegments.Where(ps => ps != null).Select(ps => ps.SourceText));

int translatedSegmentIndex = 0;
for (var i = 0; i < preTranslateSegments.Count; i++)
{
if (preTranslateSegments[i] != null)
{
preTranslateSegments[i].PlainTranslation = segmentTranslation[translatedSegmentIndex++];
}
}

return preTranslateSegments;
}
catch (Exception e)
{
_logger.Error($"{e.Message}\n {e.StackTrace}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ public class DeepLTranslationProviderConnecter
private static string _apiKey;
private List<string> _supportedSourceLanguages;

/// <summary>
/// Maximum count of translation retries
/// </summary>
private const int TranslateMaxRetryCount = 3;
/// <summary>
/// Too Many Requests status code as it does not exists in <see cref="System.Net.HttpStatusCode"/>
/// </summary>
private const int HttpStatusCodeTooManyRequests = 429;
/// <summary>
/// The total count of translation retry attempts in a row.
/// </summary>
private ulong _totalTranslationRetryAttemptInARowCount = 0;
/// <summary>
/// The maximum count of translation retry attempts in a row after which we give up executing retry routine.
/// </summary>
private const ulong MaxTranslationRetryAttemptInARowCount = TranslateMaxRetryCount * 5L;

public DeepLTranslationProviderConnecter(string key, Formality formality = Formality.Default)
{
ApiKey = key;
Expand Down Expand Up @@ -91,53 +108,155 @@ public bool IsLanguagePairSupported(CultureInfo sourceCulture, CultureInfo targe
}

public string Translate(LanguagePair languageDirection, string sourceText)
{
return TranslateWithRetry(languageDirection, AsEnumerable(sourceText)).First();
}

private static IEnumerable<TObject> AsEnumerable<TObject>(TObject value)
{
yield return value;
}

/// <summary>
/// Translate <paramref name="sourceTexts"/> in batch
/// </summary>
/// <param name="languageDirection"></param>
/// <param name="sourceTexts"></param>
/// <returns>Translated source texts in the same order</returns>
public string[] Translate(LanguagePair languageDirection, IEnumerable<string> sourceTexts)
{
return TranslateWithRetry(languageDirection, sourceTexts);
}

/// <summary>
/// Translate <paramref name="sourceTexts"/> in batch with retrying routine in case of errors
/// that might occur temporary, eg. request timeout, bad gateway
/// </summary>
/// <param name="languageDirection"></param>
/// <param name="sourceTexts"></param>
/// <param name="retryAttemptCount"></param>
/// <returns></returns>
private string[] TranslateWithRetry(LanguagePair languageDirection, IEnumerable<string> sourceTexts, int retryAttemptCount = 0)
{
string[] translatedTexts = null;
try
{
translatedTexts = BatchTranslate(languageDirection, sourceTexts);
}
catch (HttpRequestException ex)
{
if (!ex.GetHttpStatusCode().HasValue || retryAttemptCount < TranslateMaxRetryCount || DoesDeepLServiceLooksLikeIsUnavailable())
throw;

switch (ex.GetHttpStatusCode().Value)
{
case System.Net.HttpStatusCode.RequestTimeout:
case System.Net.HttpStatusCode.GatewayTimeout:
case System.Net.HttpStatusCode.BadGateway:
case System.Net.HttpStatusCode.ServiceUnavailable:
case (System.Net.HttpStatusCode)HttpStatusCodeTooManyRequests:
++retryAttemptCount;
++_totalTranslationRetryAttemptInARowCount;
TimeSpan retryDelay = CalculateRetryDelayTimeSpan(retryAttemptCount);
_logger.Info($"Retrying translation ({retryAttemptCount}) in {retryDelay} because of: {ex.Message} ({ex.GetHttpStatusCode().Value}).");
translatedTexts = TranslateWithRetry(languageDirection, sourceTexts, retryAttemptCount);
break;
default:
throw;
}
}

_totalTranslationRetryAttemptInARowCount = 0;
return translatedTexts;
}

private TimeSpan CalculateRetryDelayTimeSpan(int retryAttemptCount)
{
return TimeSpan.FromSeconds(retryAttemptCount * 5);
}

/// <summary>
/// Does DeepL service looks like is unavailable for a while? We would like to prevent continuous and time consuming
/// retrying routine in case of some serious problem on DeepL service side. If the service is unavailable for lets say few hours,
/// we would end up executing pre-translation task for very long.
/// </summary>
/// <returns></returns>
private bool DoesDeepLServiceLooksLikeIsUnavailable()
{
return _totalTranslationRetryAttemptInARowCount > MaxTranslationRetryAttemptInARowCount;
}

private string[] BatchTranslate(LanguagePair languageDirection, IEnumerable<string> sourceTexts)
{
var formality = GetFormality(languageDirection);

var targetLanguage = GetLanguage(languageDirection.TargetCulture, SupportedTargetLanguages);
var sourceLanguage = GetLanguage(languageDirection.SourceCulture, SupportedSourceLanguages);
var translatedText = string.Empty;

var translatedTexts = new string[sourceTexts.Count()];
var normalizeHelper = new NormalizeSourceTextHelper();
string stringContent = null;
HttpResponseMessage response = null;

try
{
sourceText = normalizeHelper.NormalizeText(sourceText);

var content = new StringContent($"text={sourceText}" +
$"&source_lang={sourceLanguage}" +
$"&target_lang={targetLanguage}" +
$"&formality={formality.ToString().ToLower()}" +
"&preserve_formatting=1" +
"&tag_handling=xml" +
$"&auth_key={ApiKey}",
Encoding.UTF8, "application/x-www-form-urlencoded");

var response = AppInitializer.Client.PostAsync("https://api.deepl.com/v1/translate", content).Result;
stringContent = $"source_lang={sourceLanguage}" +
$"&target_lang={targetLanguage}" +
$"&formality={formality.ToString().ToLower()}" +
"&preserve_formatting=1" +
"&tag_handling=xml" +
$"&auth_key={ApiKey}";


foreach (string sourceText in sourceTexts)
{
string normalizedSourceText = normalizeHelper.NormalizeText(sourceText);
stringContent += $"&text={normalizedSourceText}";
}

var content = new StringContent(stringContent, Encoding.UTF8, "application/x-www-form-urlencoded");
response = AppInitializer.Client.PostAsync("https://api.deepl.com/v1/translate", content).Result;
response.EnsureSuccessStatusCode();

var translationResponse = response.Content?.ReadAsStringAsync().Result;
var translatedObject = JsonConvert.DeserializeObject<TranslationResponse>(translationResponse);

if (translatedObject != null && translatedObject.Translations.Any())
if (translatedObject != null)
{
translatedText = translatedObject.Translations[0].Text;
translatedText = DecodeWhenNeeded(translatedText);
for (int i = 0; i < translatedObject.Translations.Count; i++)
{
translatedTexts[i] = translatedObject.Translations[i].Text;
translatedTexts[i] = DecodeWhenNeeded(translatedTexts[i]);

}
}

}
catch (AggregateException aEx)
{
foreach (var innerEx in aEx.InnerExceptions)
{
_logger.Error(innerEx);
}
throw;
}
catch (HttpRequestException ex)
{
if (response != null)
{
ex.SetHttpStatusCode(response.StatusCode);
}

_logger.Error(ex);
throw;
}
catch (Exception ex)
{
_logger.Error(ex);
throw;
}

return translatedText;
return translatedTexts;
}

private static string GetSupportedLanguages(string type, string apiKey)
Expand Down