Skip to content

Commit

Permalink
Fix endpoint not working in Unity 4
Browse files Browse the repository at this point in the history
Added ctranslate autoconv switches
  • Loading branch information
Vin-meido committed Jul 17, 2022
1 parent 33356ee commit f842c2b
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions SugoiOfflineTranslatorEndpoint/SugoiOfflineTranslatorEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace SugoiOfflineTranslator
{
public class SugoiOfflineTranslatorEndpoint : HttpEndpoint, ITranslateEndpoint, IDisposable
public class SugoiOfflineTranslatorEndpoint : HttpEndpoint, ITranslateEndpoint, IDisposable, IMonoBehaviour_Update
{
public override string Id => "SugoiOfflineTranslator";

Expand Down Expand Up @@ -50,6 +50,7 @@ public class SugoiOfflineTranslatorEndpoint : HttpEndpoint, ITranslateEndpoint,
private bool LogServerMessages { get; set; }

private bool EnableCTranslate2 { get; set; }
private bool EnableCTranslateAutoConvert { get; set; }

private string PythonExePath { get; set; }

Expand All @@ -69,6 +70,7 @@ public override void Initialize(IInitializationContext context)
this.LogServerMessages = context.GetOrCreateSetting("SugoiOfflineTranslator", "LogServerMessages", false);

this.EnableCTranslate2 = context.GetOrCreateSetting("SugoiOfflineTranslator", "EnableCTranslate2", false);
this.EnableCTranslateAutoConvert = context.GetOrCreateSetting("SugoiOfflineTranslator", "EnableCTranslateAutoConvert", false);


if (string.IsNullOrEmpty(this.SugoiInstallPath))
Expand Down Expand Up @@ -141,14 +143,15 @@ private void StartProcess()
{
string cuda = this.EnableCuda ? "--cuda" : "";
string ctranslate = this.EnableCTranslate2 ? "--ctranslate2" : "";
string ctranslateconv = this.EnableCTranslateAutoConvert ? "--ctranslate2-auto-convert" : "";

XuaLogger.AutoTranslator.Info($"Running Sugoi Offline Translation server:\n\tExecPath: {this.ServerExecPath}\n\tPythonPath: {this.PythonExePath}\n\tScriptPath: {this.ServerScriptPath}");

this.process = new Process();
this.process.StartInfo = new ProcessStartInfo()
{
FileName = this.PythonExePath,
Arguments = $"\"{this.ServerScriptPath}\" {this.ServerPort} {cuda} {ctranslate}",
Arguments = $"\"{this.ServerScriptPath}\" {this.ServerPort} {cuda} {ctranslate} {ctranslateconv}",
WorkingDirectory = this.ServerExecPath,
UseShellExecute = false,
RedirectStandardError = true,
Expand Down Expand Up @@ -182,7 +185,10 @@ IEnumerator ITranslateEndpoint.Translate(ITranslationContext context)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
yield return base.Translate(context);
var iterator = base.Translate(context);

while (iterator.MoveNext()) yield return iterator.Current;

var elapsed = stopwatch.Elapsed.TotalSeconds;

if(LogServerMessages)
Expand Down Expand Up @@ -237,5 +243,11 @@ public override void OnExtractTranslation(IHttpTranslationExtractionContext cont
var result = JSON.Parse(data);
context.Complete(result.AsStringList.ToArray());
}


public void Update()
{
//XuaLogger.AutoTranslator.Info($"Sugoi update!");
}
}
}

0 comments on commit f842c2b

Please sign in to comment.