mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-25 12:44:46 +01:00
testing Gemini
This commit is contained in:
parent
17522168c2
commit
c961c0bd3b
186
src/libse/AutoTranslate/GeminiTranslate.cs
Normal file
186
src/libse/AutoTranslate/GeminiTranslate.cs
Normal file
@ -0,0 +1,186 @@
|
|||||||
|
using Nikse.SubtitleEdit.Core.Common;
|
||||||
|
using Nikse.SubtitleEdit.Core.SubtitleFormats;
|
||||||
|
using Nikse.SubtitleEdit.Core.Translate;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Nikse.SubtitleEdit.Core.AutoTranslate
|
||||||
|
{
|
||||||
|
public class GeminiTranslate : IAutoTranslator
|
||||||
|
{
|
||||||
|
private HttpClient _httpClient;
|
||||||
|
|
||||||
|
public static string StaticName { get; set; } = "Google Gemini";
|
||||||
|
public string Name => StaticName;
|
||||||
|
public string Url => "https://deepmind.google/technologies/gemini/";
|
||||||
|
public string Error { get; set; }
|
||||||
|
public int MaxCharacters => 1500;
|
||||||
|
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_httpClient?.Dispose();
|
||||||
|
_httpClient = new HttpClient();
|
||||||
|
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
|
||||||
|
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
|
||||||
|
_httpClient.BaseAddress = new Uri("https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent");
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(Configuration.Settings.Tools.GeminiProApiKey))
|
||||||
|
{
|
||||||
|
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("x-goog-api-key", Configuration.Settings.Tools.GeminiProApiKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<TranslationPair> GetSupportedSourceLanguages()
|
||||||
|
{
|
||||||
|
return ListLanguages();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<TranslationPair> GetSupportedTargetLanguages()
|
||||||
|
{
|
||||||
|
return ListLanguages();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> Translate(string text, string sourceLanguageCode, string targetLanguageCode, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var input = "{ \"contents\": [ { \"role\": \"user\", \"parts\": [{ \"text\": \"Please translate the following text from " + sourceLanguageCode + " to " + targetLanguageCode + ", only write the result: \\n\\n" + Json.EncodeJsonText(text.Trim()) + "\" }]}]}";
|
||||||
|
var content = new StringContent(input, Encoding.UTF8);
|
||||||
|
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
|
||||||
|
|
||||||
|
SeLogger.Error("GeminiTranslate calling with: " + input);
|
||||||
|
|
||||||
|
var result = await _httpClient.PostAsync(string.Empty, content, cancellationToken);
|
||||||
|
var bytes = await result.Content.ReadAsByteArrayAsync();
|
||||||
|
var json = Encoding.UTF8.GetString(bytes).Trim();
|
||||||
|
if (!result.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
Error = json;
|
||||||
|
SeLogger.Error("GeminiTranslate failed calling API: Status code=" + result.StatusCode + Environment.NewLine + json);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
SeLogger.Error("GeminiTranslate response: (Status code=" + result.StatusCode + ")" + Environment.NewLine + json);
|
||||||
|
}
|
||||||
|
|
||||||
|
result.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var parser = new SeJsonParser();
|
||||||
|
var resultText = parser.GetFirstObject(json, "content");
|
||||||
|
if (resultText == null)
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var outputText = Json.DecodeJsonText(resultText).Trim();
|
||||||
|
if (outputText.StartsWith('"') && outputText.EndsWith('"') && !text.StartsWith('"'))
|
||||||
|
{
|
||||||
|
outputText = outputText.Trim('"').Trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
return outputText;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<TranslationPair> ListLanguages()
|
||||||
|
{
|
||||||
|
return new List<TranslationPair>
|
||||||
|
{
|
||||||
|
MakePair("Albanian","sq"),
|
||||||
|
MakePair("Arabic","ar"),
|
||||||
|
MakePair("Armenian","hy"),
|
||||||
|
MakePair("Awadhi","ay"),
|
||||||
|
MakePair("Azerbaijani","az"),
|
||||||
|
MakePair("Bashkir","ba"),
|
||||||
|
MakePair("Basque","eu"),
|
||||||
|
MakePair("Belarusian","be"),
|
||||||
|
MakePair("Bengali","bn"),
|
||||||
|
MakePair("Bhojpuri",""),
|
||||||
|
MakePair("Bosnian","bs"),
|
||||||
|
MakePair("Brazilian Portuguese","br"),
|
||||||
|
MakePair("Bulgarian","bg"),
|
||||||
|
MakePair("Cantonese","zh"),
|
||||||
|
MakePair("Catalan","ca"),
|
||||||
|
MakePair("Chhattisgarhi",""),
|
||||||
|
MakePair("Chinese","zh"),
|
||||||
|
MakePair("Croatian","hr"),
|
||||||
|
MakePair("Czech","cs"),
|
||||||
|
MakePair("Danish","da"),
|
||||||
|
MakePair("Dogri",""),
|
||||||
|
MakePair("Dutch","nl"),
|
||||||
|
MakePair("English","en"),
|
||||||
|
MakePair("Estonian","et"),
|
||||||
|
MakePair("Faroese","fo"),
|
||||||
|
MakePair("Finnish","fi"),
|
||||||
|
MakePair("French","fr"),
|
||||||
|
MakePair("Galician","gl"),
|
||||||
|
MakePair("Georgian","ka"),
|
||||||
|
MakePair("German","de"),
|
||||||
|
MakePair("Greek","el"),
|
||||||
|
MakePair("Gujarati","gu"),
|
||||||
|
MakePair("Haryanvi",""),
|
||||||
|
MakePair("Hindi","hi"),
|
||||||
|
MakePair("Hungarian","hu"),
|
||||||
|
MakePair("Indonesian","id"),
|
||||||
|
MakePair("Irish","ga"),
|
||||||
|
MakePair("Italian","it"),
|
||||||
|
MakePair("Japanese","ja"),
|
||||||
|
MakePair("Javanese","jv"),
|
||||||
|
MakePair("Kannada","kn"),
|
||||||
|
MakePair("Kashmiri","ks"),
|
||||||
|
MakePair("Kazakh","kk"),
|
||||||
|
MakePair("Konkani",""),
|
||||||
|
MakePair("Korean","ko"),
|
||||||
|
MakePair("Kyrgyz","ky"),
|
||||||
|
MakePair("Latvian","lv"),
|
||||||
|
MakePair("Lithuanian","lt"),
|
||||||
|
MakePair("Macedonian","mk"),
|
||||||
|
MakePair("Maithili",""),
|
||||||
|
MakePair("Malay","ms"),
|
||||||
|
MakePair("Maltese","mt"),
|
||||||
|
MakePair("Mandarin","zh"),
|
||||||
|
MakePair("Mandarin Chinese","zh"),
|
||||||
|
MakePair("Marathi","mr"),
|
||||||
|
MakePair("Marwari",""),
|
||||||
|
MakePair("Min Nan",""),
|
||||||
|
MakePair("Moldovan","ro"),
|
||||||
|
MakePair("Mongolian","mn"),
|
||||||
|
MakePair("Montenegrin",""),
|
||||||
|
MakePair("Nepali","ne"),
|
||||||
|
MakePair("Norwegian","no"),
|
||||||
|
MakePair("Oriya","or"),
|
||||||
|
MakePair("Pashto","ps"),
|
||||||
|
MakePair("Persian","fa"),
|
||||||
|
MakePair("Polish","pl"),
|
||||||
|
MakePair("Portuguese","pt"),
|
||||||
|
MakePair("Punjabi","pa"),
|
||||||
|
MakePair("Rajasthani",""),
|
||||||
|
MakePair("Romanian","ro"),
|
||||||
|
MakePair("Russian","ru"),
|
||||||
|
MakePair("Sanskrit","sa"),
|
||||||
|
MakePair("Santali",""),
|
||||||
|
MakePair("Serbian","sr"),
|
||||||
|
MakePair("Sindhi","sd"),
|
||||||
|
MakePair("Sinhala","si"),
|
||||||
|
MakePair("Slovak","sk"),
|
||||||
|
MakePair("Slovene","sl"),
|
||||||
|
MakePair("Slovenian","sl"),
|
||||||
|
MakePair("Spanish","es"),
|
||||||
|
MakePair("Turkish","tr"),
|
||||||
|
MakePair("Ukrainian","uk"),
|
||||||
|
MakePair("Urdu","ur"),
|
||||||
|
MakePair("Uzbek","uz"),
|
||||||
|
MakePair("Vietnamese","vi"),
|
||||||
|
MakePair("Welsh","cy"),
|
||||||
|
MakePair("Wu",""),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static TranslationPair MakePair(string nameCode, string twoLetter)
|
||||||
|
{
|
||||||
|
return new TranslationPair(nameCode, nameCode, twoLetter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -176,6 +176,7 @@ namespace Nikse.SubtitleEdit.Core.Common
|
|||||||
public string ChatGptUrl { get; set; }
|
public string ChatGptUrl { get; set; }
|
||||||
public string ChatGptApiKey { get; set; }
|
public string ChatGptApiKey { get; set; }
|
||||||
public int ChatGptDelaySeconds { get; set; }
|
public int ChatGptDelaySeconds { get; set; }
|
||||||
|
public string GeminiProApiKey { get; set; }
|
||||||
public bool ListViewSyntaxColorDurationSmall { get; set; }
|
public bool ListViewSyntaxColorDurationSmall { get; set; }
|
||||||
public bool ListViewSyntaxColorDurationBig { get; set; }
|
public bool ListViewSyntaxColorDurationBig { get; set; }
|
||||||
public bool ListViewSyntaxColorOverlap { get; set; }
|
public bool ListViewSyntaxColorOverlap { get; set; }
|
||||||
@ -5282,6 +5283,12 @@ $HorzAlign = Center
|
|||||||
settings.Tools.ChatGptDelaySeconds = Convert.ToInt32(subNode.InnerText, CultureInfo.InvariantCulture);
|
settings.Tools.ChatGptDelaySeconds = Convert.ToInt32(subNode.InnerText, CultureInfo.InvariantCulture);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
subNode = node.SelectSingleNode("GeminiProApiKey");
|
||||||
|
if (subNode != null)
|
||||||
|
{
|
||||||
|
settings.Tools.GeminiProApiKey = subNode.InnerText;
|
||||||
|
}
|
||||||
|
|
||||||
subNode = node.SelectSingleNode("TranslateViaCopyPasteAutoCopyToClipboard");
|
subNode = node.SelectSingleNode("TranslateViaCopyPasteAutoCopyToClipboard");
|
||||||
if (subNode != null)
|
if (subNode != null)
|
||||||
{
|
{
|
||||||
@ -11687,6 +11694,7 @@ $HorzAlign = Center
|
|||||||
textWriter.WriteElementString("ChatGptUrl", settings.Tools.ChatGptUrl);
|
textWriter.WriteElementString("ChatGptUrl", settings.Tools.ChatGptUrl);
|
||||||
textWriter.WriteElementString("ChatGptApiKey", settings.Tools.ChatGptApiKey);
|
textWriter.WriteElementString("ChatGptApiKey", settings.Tools.ChatGptApiKey);
|
||||||
textWriter.WriteElementString("ChatGptDelaySeconds", settings.Tools.ChatGptDelaySeconds.ToString(CultureInfo.InvariantCulture));
|
textWriter.WriteElementString("ChatGptDelaySeconds", settings.Tools.ChatGptDelaySeconds.ToString(CultureInfo.InvariantCulture));
|
||||||
|
textWriter.WriteElementString("GeminiProApiKey", settings.Tools.GeminiProApiKey);
|
||||||
textWriter.WriteElementString("ListViewSyntaxColorDurationSmall", settings.Tools.ListViewSyntaxColorDurationSmall.ToString(CultureInfo.InvariantCulture));
|
textWriter.WriteElementString("ListViewSyntaxColorDurationSmall", settings.Tools.ListViewSyntaxColorDurationSmall.ToString(CultureInfo.InvariantCulture));
|
||||||
textWriter.WriteElementString("ListViewSyntaxColorDurationBig", settings.Tools.ListViewSyntaxColorDurationBig.ToString(CultureInfo.InvariantCulture));
|
textWriter.WriteElementString("ListViewSyntaxColorDurationBig", settings.Tools.ListViewSyntaxColorDurationBig.ToString(CultureInfo.InvariantCulture));
|
||||||
textWriter.WriteElementString("ListViewSyntaxColorLongLines", settings.Tools.ListViewSyntaxColorLongLines.ToString(CultureInfo.InvariantCulture));
|
textWriter.WriteElementString("ListViewSyntaxColorLongLines", settings.Tools.ListViewSyntaxColorLongLines.ToString(CultureInfo.InvariantCulture));
|
||||||
|
@ -121,6 +121,7 @@ namespace Nikse.SubtitleEdit.Forms.Translate
|
|||||||
new LibreTranslate(),
|
new LibreTranslate(),
|
||||||
new MyMemoryApi(),
|
new MyMemoryApi(),
|
||||||
new ChatGptTranslate(),
|
new ChatGptTranslate(),
|
||||||
|
new GeminiTranslate(),
|
||||||
new PapagoTranslate(),
|
new PapagoTranslate(),
|
||||||
new NoLanguageLeftBehindServe(),
|
new NoLanguageLeftBehindServe(),
|
||||||
new NoLanguageLeftBehindApi(),
|
new NoLanguageLeftBehindApi(),
|
||||||
@ -298,6 +299,19 @@ namespace Nikse.SubtitleEdit.Forms.Translate
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (engineType == typeof(GeminiTranslate))
|
||||||
|
{
|
||||||
|
nikseComboBoxUrl.Visible = false;
|
||||||
|
labelUrl.Visible = false;
|
||||||
|
|
||||||
|
labelApiKey.Left = labelUrl.Left;
|
||||||
|
nikseTextBoxApiKey.Text = Configuration.Settings.Tools.GeminiProApiKey;
|
||||||
|
nikseTextBoxApiKey.Left = labelApiKey.Right + 3;
|
||||||
|
labelApiKey.Visible = true;
|
||||||
|
nikseTextBoxApiKey.Visible = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
throw new Exception($"Engine {_autoTranslator.Name} not handled!");
|
throw new Exception($"Engine {_autoTranslator.Name} not handled!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -880,6 +894,11 @@ namespace Nikse.SubtitleEdit.Forms.Translate
|
|||||||
Configuration.Settings.Tools.ChatGptApiKey = nikseTextBoxApiKey.Text.Trim();
|
Configuration.Settings.Tools.ChatGptApiKey = nikseTextBoxApiKey.Text.Trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (engineType == typeof(GeminiTranslate) && !string.IsNullOrWhiteSpace(nikseTextBoxApiKey.Text))
|
||||||
|
{
|
||||||
|
Configuration.Settings.Tools.GeminiProApiKey = nikseTextBoxApiKey.Text.Trim();
|
||||||
|
}
|
||||||
|
|
||||||
if (engineType == typeof(PapagoTranslate) && !string.IsNullOrWhiteSpace(nikseTextBoxApiKey.Text))
|
if (engineType == typeof(PapagoTranslate) && !string.IsNullOrWhiteSpace(nikseTextBoxApiKey.Text))
|
||||||
{
|
{
|
||||||
Configuration.Settings.Tools.AutoTranslatePapagoApiKeyId = nikseComboBoxUrl.Text.Trim();
|
Configuration.Settings.Tools.AutoTranslatePapagoApiKeyId = nikseComboBoxUrl.Text.Trim();
|
||||||
|
Loading…
Reference in New Issue
Block a user