Skip to content

Commit

Permalink
Add OpenAI translate engine (#146)
Browse files Browse the repository at this point in the history
* Feature: add OpenAI translate engine

* OpenAI -> OpenAi

* fix: txtOpenAIApiKey -> txtOpenAiApiKey

---------

Co-authored-by: root <root@Gaming>
Co-authored-by: root <root>
  • Loading branch information
fanza1 and root authored Mar 16, 2023
1 parent aba8d75 commit 78457a9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Jellyfin.Plugin.MetaTube/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class PluginConfiguration : BasePluginConfiguration

public string DeepLApiKey { get; set; } = string.Empty;

public string OpenAiApiKey { get; set; } = string.Empty;

#endregion

#region Provider
Expand Down
21 changes: 21 additions & 0 deletions Jellyfin.Plugin.MetaTube/Configuration/configPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ <h2 class="sectionTitle">Translation</h2>
<option value="Google">Google</option>
<option value="GoogleFree">Google (Free)</option>
<option value="DeepL">DeepL (Free)</option>
<option value="OpenAI">OpenAI</option>
</select>
</div>

Expand Down Expand Up @@ -204,6 +205,14 @@ <h2 class="sectionTitle">Translation</h2>
<div class="fieldDescription"></div>
</div>
</div>

<div class="selectTranslationEngineOpenAI">
<div class="inputContainer">
<label class="inputLabel inputLabelUnfocused" for="txtOpenAiApiKey">OpenAI api key:</label>
<input id="txtOpenAiApiKey" is="emby-input" name="txtOpenAiApiKey" type="text"/>
<div class="fieldDescription"></div>
</div>
</div>
</div>
</div>

Expand Down Expand Up @@ -279,22 +288,32 @@ <h2 class="sectionTitle">Substitution</h2>
$('.selectTranslationEngineBaidu').css('display', 'inherit');
$('.selectTranslationEngineGoogle').css('display', 'none');
$('.selectTranslationEngineDeepL').css('display', 'none');
$('.selectTranslationEngineOpenAI').css('display', 'none');
} else if (this.value === "Google") {
$('.selectTranslationEngineBaidu').css('display', 'none');
$('.selectTranslationEngineGoogle').css('display', 'inherit');
$('.selectTranslationEngineDeepL').css('display', 'none');
$('.selectTranslationEngineOpenAI').css('display', 'none');
} else if (this.value === "GoogleFree") {
$('.selectTranslationEngineBaidu').css('display', 'none');
$('.selectTranslationEngineGoogle').css('display', 'none');
$('.selectTranslationEngineDeepL').css('display', 'none');
$('.selectTranslationEngineOpenAI').css('display', 'none');
} else if (this.value === "DeepL") {
$('.selectTranslationEngineBaidu').css('display', 'none');
$('.selectTranslationEngineGoogle').css('display', 'none');
$('.selectTranslationEngineDeepL').css('display', 'inherit');
$('.selectTranslationEngineOpenAI').css('display', 'none');
} else if (this.value === "OpenAI") {
$('.selectTranslationEngineBaidu').css('display', 'none');
$('.selectTranslationEngineGoogle').css('display', 'none');
$('.selectTranslationEngineDeepL').css('display', 'none');
$('.selectTranslationEngineOpenAI').css('display', 'inherit');
} else {
$('.selectTranslationEngineBaidu').css('display', 'none');
$('.selectTranslationEngineGoogle').css('display', 'none');
$('.selectTranslationEngineDeepL').css('display', 'none');
$('.selectTranslationEngineOpenAI').css('display', 'none');
}
});

Expand Down Expand Up @@ -324,6 +343,7 @@ <h2 class="sectionTitle">Substitution</h2>
$('#txtBaiduAppKey', page).val(config.BaiduAppKey).change();
$('#txtGoogleApiKey', page).val(config.GoogleApiKey).change();
$('#txtDeepLApiKey', page).val(config.DeepLApiKey).change();
$('#txtOpenAiApiKey', page).val(config.OpenAiApiKey).change();
page.querySelector('#chkEnableTitleSubstitution').checked = config.EnableTitleSubstitution;
$('#txtTitleRawSubstitutionTable', page).val(config.TitleRawSubstitutionTable).change();
page.querySelector('#chkEnableActorSubstitution').checked = config.EnableActorSubstitution;
Expand Down Expand Up @@ -360,6 +380,7 @@ <h2 class="sectionTitle">Substitution</h2>
config.BaiduAppKey = $('#txtBaiduAppKey', form).val();
config.GoogleApiKey = $('#txtGoogleApiKey', form).val();
config.DeepLApiKey = $('#txtDeepLApiKey', form).val();
config.OpenAiApiKey = $('#txtOpenAiApiKey', form).val();
config.EnableTitleSubstitution = $('#chkEnableTitleSubstitution', form).prop('checked');
config.TitleRawSubstitutionTable = $('#txtTitleRawSubstitutionTable', form).val();
config.EnableActorSubstitution = $('#chkEnableActorSubstitution', form).prop('checked');
Expand Down
3 changes: 2 additions & 1 deletion Jellyfin.Plugin.MetaTube/Translation/TranslationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public enum TranslationEngine
Baidu,
Google,
GoogleFree,
DeepL
DeepL,
OpenAI
}
7 changes: 7 additions & 0 deletions Jellyfin.Plugin.MetaTube/Translation/TranslationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ private static async Task<string> TranslateAsync(string q, string from, string t
{ "deepl-api-key", Configuration.DeepLApiKey }
});
break;
case TranslationEngine.OpenAI:
millisecondsDelay = 1000;
nv.Add(new NameValueCollection
{
{ "openai-api-key", Configuration.OpenAiApiKey }
});
break;
default:
throw new ArgumentException($"Invalid translation engine: {Configuration.TranslationEngine}");
}
Expand Down

0 comments on commit 78457a9

Please sign in to comment.