mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-22 19:22:53 +01:00
Test Papago
This commit is contained in:
parent
ae84ddfa0a
commit
64118bc0af
@ -17,6 +17,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||
public static string StaticName { get; set; } = "ChatGPT";
|
||||
public string Name => StaticName;
|
||||
public string Url => "https://chat.openai.com/";
|
||||
public string Error { get; set; }
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
@ -48,6 +49,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||
var json = Encoding.UTF8.GetString(bytes).Trim();
|
||||
if (!result.IsSuccessStatusCode)
|
||||
{
|
||||
Error = json;
|
||||
SeLogger.Error("ChatGptTranslate failed calling API: Status code=" + result.StatusCode + Environment.NewLine + json);
|
||||
}
|
||||
|
||||
|
@ -21,6 +21,8 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||
public static string StaticName { get; set; } = "DeepL V2 translate";
|
||||
public string Name => StaticName;
|
||||
public string Url => "https://www.deepl.com";
|
||||
public string Error { get; set; }
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_apiKey = Configuration.Settings.Tools.AutoTranslateDeepLApiKey;
|
||||
@ -102,6 +104,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||
var resultContent = result.Content.ReadAsStringAsync().Result;
|
||||
if (result.StatusCode == HttpStatusCode.Forbidden)
|
||||
{
|
||||
Error = resultContent;
|
||||
throw new Exception("Bad API key");
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||
public static string StaticName { get; set; } = "Google Translate V1 API";
|
||||
public string Name => StaticName;
|
||||
public string Url => "https://translate.google.com/";
|
||||
public string Error { get; set; }
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
|
@ -23,8 +23,9 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||
|
||||
public static string StaticName { get; set; } = "Google Translate V2 API";
|
||||
public string Name => StaticName;
|
||||
|
||||
public string Url => "https://translate.google.com/";
|
||||
public string Error { get; set; }
|
||||
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
|
@ -16,6 +16,8 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||
/// </summary>
|
||||
string Url { get; }
|
||||
|
||||
string Error { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initialization before calling translate.
|
||||
/// </summary>
|
||||
|
@ -18,6 +18,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||
public static string StaticName { get; set; } = "LibreTranslate";
|
||||
public string Name => StaticName;
|
||||
public string Url => "https://github.com/LibreTranslate/LibreTranslate";
|
||||
public string Error { get; set; }
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
@ -54,6 +55,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||
var json = Encoding.UTF8.GetString(bytes).Trim();
|
||||
if (!result.IsSuccessStatusCode)
|
||||
{
|
||||
Error = json;
|
||||
SeLogger.Error("LibreTranslate failed calling API: Status code=" + result.StatusCode + Environment.NewLine + json);
|
||||
}
|
||||
result.EnsureSuccessStatusCode();
|
||||
|
@ -31,6 +31,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||
public static string StaticName { get; set; } = "Bing Microsoft Translator";
|
||||
public string Name => StaticName;
|
||||
public string Url => "https://www.bing.com/translator";
|
||||
public string Error { get; set; }
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
@ -83,6 +84,8 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||
|
||||
if (!result.IsSuccessStatusCode)
|
||||
{
|
||||
Error = json;
|
||||
|
||||
if (result.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
throw new Exception("API key is not valid!" + Environment.NewLine + Environment.NewLine + jsonResult);
|
||||
|
@ -15,6 +15,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||
public static string StaticName { get; set; } = "MyMemory Translate";
|
||||
public string Name => StaticName;
|
||||
public string Url => "https://mymemory.translated.net/";
|
||||
public string Error { get; set; }
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
|
@ -17,6 +17,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||
public static string StaticName { get; set; } = "winstxnhdw-nllb-api";
|
||||
public string Name => StaticName;
|
||||
public string Url => "https://github.com/winstxnhdw/nllb-api";
|
||||
public string Error { get; set; }
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
|
@ -18,6 +18,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||
public static string StaticName { get; set; } = "thammegowda-nllb-serve";
|
||||
public string Name => StaticName;
|
||||
public string Url => "https://github.com/thammegowda/nllb-serve";
|
||||
public string Error { get; set; }
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
|
112
src/libse/AutoTranslate/PapagoTranslate.cs
Normal file
112
src/libse/AutoTranslate/PapagoTranslate.cs
Normal file
@ -0,0 +1,112 @@
|
||||
using Nikse.SubtitleEdit.Core.Common;
|
||||
using Nikse.SubtitleEdit.Core.SubtitleFormats;
|
||||
using Nikse.SubtitleEdit.Core.Translate;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||
{
|
||||
public class PapagoTranslate : IAutoTranslator
|
||||
{
|
||||
private HttpClient _httpClient;
|
||||
|
||||
public static string StaticName { get; set; } = "PapagoTranslate";
|
||||
public string Name => StaticName;
|
||||
public string Url => "https://papago.naver.com/";
|
||||
public string Error { get; set; }
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_httpClient?.Dispose();
|
||||
_httpClient = new HttpClient();
|
||||
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
|
||||
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
|
||||
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("X-NCP-APIGW-API-KEY-ID", Configuration.Settings.Tools.AutoTranslatePapagoApiKeyId);
|
||||
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("X-NCP-APIGW-API-KEY", Configuration.Settings.Tools.AutoTranslatePapagoApiKeyId);
|
||||
_httpClient.BaseAddress = new Uri("https://naveropenapi.apigw.ntruss.com/nmt/v1/");
|
||||
}
|
||||
|
||||
public List<TranslationPair> GetSupportedSourceLanguages()
|
||||
{
|
||||
return ListLanguages();
|
||||
}
|
||||
|
||||
public List<TranslationPair> GetSupportedTargetLanguages()
|
||||
{
|
||||
return ListLanguages();
|
||||
}
|
||||
|
||||
public async Task<string> Translate(string text, string sourceLanguageCode, string targetLanguageCode)
|
||||
{
|
||||
var input = "{\"text\": \"" + Json.EncodeJsonText(text.Trim()) + "\", \"source\": \"" + sourceLanguageCode + "\", \"target\": \"" + targetLanguageCode + "\"}";
|
||||
var content = new StringContent(input, Encoding.UTF8);
|
||||
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
|
||||
var result = _httpClient.PostAsync("translation", content).Result;
|
||||
var bytes = await result.Content.ReadAsByteArrayAsync();
|
||||
var json = Encoding.UTF8.GetString(bytes).Trim();
|
||||
if (!result.IsSuccessStatusCode)
|
||||
{
|
||||
Error = json;
|
||||
SeLogger.Error("PapagoTranslate failed calling API: Status code=" + result.StatusCode + Environment.NewLine + json);
|
||||
}
|
||||
result.EnsureSuccessStatusCode();
|
||||
var parser = new SeJsonParser();
|
||||
var resultText = parser.GetFirstObject(json, "translatedText");
|
||||
if (resultText == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return Json.DecodeJsonText(resultText).Trim();
|
||||
}
|
||||
|
||||
private static List<TranslationPair> ListLanguages()
|
||||
{
|
||||
var languageCodes = new List<string>
|
||||
{
|
||||
"ko",
|
||||
"en",
|
||||
"ja",
|
||||
"zh-CN",
|
||||
"zh-TW",
|
||||
"vi",
|
||||
"id",
|
||||
"fr",
|
||||
"es",
|
||||
"ru",
|
||||
"de",
|
||||
"it",
|
||||
};
|
||||
|
||||
var result = new List<TranslationPair>();
|
||||
var cultures = Utilities.GetSubtitleLanguageCultures(false).ToList();
|
||||
foreach (var code in languageCodes)
|
||||
{
|
||||
if (code == "zh-CN")
|
||||
{
|
||||
result.Add(new TranslationPair("Chinese (simplified)", code, code));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (code == "zh-TW")
|
||||
{
|
||||
result.Add(new TranslationPair("Chinese (traditional)", code, code));
|
||||
continue;
|
||||
}
|
||||
|
||||
var culture = cultures.FirstOrDefault(p=>p.TwoLetterISOLanguageName == code);
|
||||
if (culture != null)
|
||||
{
|
||||
result.Add(new TranslationPair(culture.EnglishName, code, code));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@ -18,11 +18,12 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||
public static string StaticName { get; set; } = "SeamlessM4T";
|
||||
public string Name => StaticName;
|
||||
public string Url => "https://replicate.com/cjwbw/seamless_communication/api";
|
||||
public string Error { get; set; }
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_httpClient?.Dispose();
|
||||
_httpClient = new HttpClient(); //DownloaderFactory.MakeHttpClient();
|
||||
_httpClient = new HttpClient();
|
||||
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
|
||||
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
|
||||
_httpClient.BaseAddress = new Uri(Configuration.Settings.Tools.AutoTranslateSeamlessM4TUrl.TrimEnd('/') + "/");
|
||||
|
@ -163,6 +163,8 @@ namespace Nikse.SubtitleEdit.Core.Common
|
||||
public string AutoTranslateSeamlessM4TUrl { get; set; }
|
||||
public string AutoTranslateDeepLApiKey { get; set; }
|
||||
public string AutoTranslateDeepLUrl { get; set; }
|
||||
public string AutoTranslatePapagoApiKeyId { get; set; }
|
||||
public string AutoTranslatePapagoApiKey { get; set; }
|
||||
public string AutoTranslateDeepLFormality { get; set; }
|
||||
public bool TranslateAllowSplit { get; set; }
|
||||
public string TranslateLastService { get; set; }
|
||||
@ -5189,6 +5191,18 @@ $HorzAlign = Center
|
||||
settings.Tools.AutoTranslateDeepLUrl = subNode.InnerText;
|
||||
}
|
||||
|
||||
subNode = node.SelectSingleNode("AutoTranslatePapagoApiKeyId");
|
||||
if (subNode != null)
|
||||
{
|
||||
settings.Tools.AutoTranslatePapagoApiKeyId = subNode.InnerText;
|
||||
}
|
||||
|
||||
subNode = node.SelectSingleNode("AutoTranslatePapagoApiKey");
|
||||
if (subNode != null)
|
||||
{
|
||||
settings.Tools.AutoTranslatePapagoApiKey = subNode.InnerText;
|
||||
}
|
||||
|
||||
subNode = node.SelectSingleNode("AutoTranslateDeepLFormality");
|
||||
if (subNode != null)
|
||||
{
|
||||
@ -11569,6 +11583,8 @@ $HorzAlign = Center
|
||||
textWriter.WriteElementString("AutoTranslateSeamlessM4TUrl", settings.Tools.AutoTranslateSeamlessM4TUrl);
|
||||
textWriter.WriteElementString("AutoTranslateDeepLApiKey", settings.Tools.AutoTranslateDeepLApiKey);
|
||||
textWriter.WriteElementString("AutoTranslateDeepLUrl", settings.Tools.AutoTranslateDeepLUrl);
|
||||
textWriter.WriteElementString("AutoTranslatePapagoApiKeyId", settings.Tools.AutoTranslatePapagoApiKeyId);
|
||||
textWriter.WriteElementString("AutoTranslatePapagoApiKey", settings.Tools.AutoTranslatePapagoApiKey);
|
||||
textWriter.WriteElementString("AutoTranslateDeepLFormality", settings.Tools.AutoTranslateDeepLFormality);
|
||||
textWriter.WriteElementString("TranslateAllowSplit", settings.Tools.TranslateAllowSplit.ToString(CultureInfo.InvariantCulture));
|
||||
textWriter.WriteElementString("TranslateLastService", settings.Tools.TranslateLastService);
|
||||
|
@ -114,6 +114,7 @@ namespace Nikse.SubtitleEdit.Forms.Translate
|
||||
new NoLanguageLeftBehindApi(),
|
||||
new MyMemoryApi(),
|
||||
new ChatGptTranslate(),
|
||||
new PapagoTranslate(),
|
||||
};
|
||||
|
||||
nikseComboBoxEngine.Items.Clear();
|
||||
@ -225,6 +226,14 @@ namespace Nikse.SubtitleEdit.Forms.Translate
|
||||
return;
|
||||
}
|
||||
|
||||
if (engineType == typeof(PapagoTranslate))
|
||||
{
|
||||
//TODO: Show settings
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (engineType == typeof(MyMemoryApi))
|
||||
{
|
||||
labelApiKey.Left = labelUrl.Left;
|
||||
@ -613,11 +622,32 @@ namespace Nikse.SubtitleEdit.Forms.Translate
|
||||
{
|
||||
var dr = MessageBox.Show(
|
||||
this, string.Format(LanguageSettings.Current.GoogleTranslate.XRequiresAnApiKey, nikseComboBoxUrl.Text) + Environment.NewLine +
|
||||
Environment.NewLine +
|
||||
LanguageSettings.Current.GoogleTranslate.ReadMore,
|
||||
Text,
|
||||
MessageBoxButtons.YesNoCancel,
|
||||
MessageBoxIcon.Error);
|
||||
Environment.NewLine +
|
||||
LanguageSettings.Current.GoogleTranslate.ReadMore + Environment.NewLine +
|
||||
Environment.NewLine +
|
||||
_autoTranslator.Error,
|
||||
Text,
|
||||
MessageBoxButtons.YesNoCancel,
|
||||
MessageBoxIcon.Error);
|
||||
|
||||
if (dr == DialogResult.Yes)
|
||||
{
|
||||
UiUtil.ShowHelp("#translation");
|
||||
}
|
||||
}
|
||||
else if (linesTranslate == 0 && engineType == typeof(PapagoTranslate))
|
||||
{
|
||||
var dr = MessageBox.Show(
|
||||
this, string.Format(LanguageSettings.Current.GoogleTranslate.XRequiresAnApiKey, PapagoTranslate.StaticName) + Environment.NewLine +
|
||||
Environment.NewLine +
|
||||
LanguageSettings.Current.GoogleTranslate.ReadMore + Environment.NewLine +
|
||||
Environment.NewLine +
|
||||
"AutoTranslatePapagoApiKeyId and AutoTranslatePapagoApiKey needs to be filled out in Settings.xml" + Environment.NewLine +
|
||||
Environment.NewLine +
|
||||
_autoTranslator.Error,
|
||||
Text,
|
||||
MessageBoxButtons.YesNoCancel,
|
||||
MessageBoxIcon.Error);
|
||||
|
||||
if (dr == DialogResult.Yes)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user