-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
147 lines (115 loc) · 4.37 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Windows;
using System.CommandLine;
using Microsoft.Extensions.DependencyInjection;
using Protonox.Labs.Api;
using Tesseract;
using Protonox.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Parsing;
using Serilog;
using Microsoft.Extensions.Logging;
namespace Protonox
{
public sealed partial class App : Application
{
public App()
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File("log_.txt",
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true)
.CreateLogger();
Log.Logger.Information("[Startup]");
}
private static void ConfigureServices(ServiceCollection services)
{
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
builder.ClearProviders().AddSerilog();
});
services.AddSingleton(loggerFactory.CreateLogger(nameof(App)));
services.AddSingleton<ElevenLabs>();
services.AddSingleton<VoiceSettings>();
services.AddSingleton<TesseractEngine>(x =>
CreateTesseractEngine(x.GetRequiredService<TesseractCLIArgs>()));
services.AddSingleton<AudioSettings>();
services.AddSingleton<Controller>();
services.AddSingleton<Window1>();
}
public static TesseractEngine CreateTesseractEngine(TesseractCLIArgs args)
{
Log.Logger.Information($"[Startup] Tesseract Path: `{args.Path}` | Lang: `{args.Lang}`");
return new TesseractEngine(args.Path, args.Lang, EngineMode.Default);
}
protected override void OnStartup(StartupEventArgs e)
{
ServiceCollection services = new();
int errorCount = ProcessCLI(services, e.Args);
if (errorCount > 0)
{
Current.Shutdown();
return;
}
ConfigureServices(services);
var serviceProvider = services.BuildServiceProvider();
var mainWindow = serviceProvider.GetRequiredService<Window1>();
mainWindow.Show();
}
private int ProcessCLI(ServiceCollection services, string[] args)
{
var apiKey = new Argument<string>("apikey", "ElevenLabs API key");
var userAgent = new Option<string>(
new[] { "--userAgent", "-ua" },
() => { return null; }, "Either: 'rand' or UserAgent");
var proxy = new Option<string>(
new[] { "--proxy", "-p" },
() => { return null; }, "Either: 'rand' or Proxy as format PROTOCOL://ADDRESS:PORT");
var lang = new Option<string>(
new[] { "--lang", "-l" },
() => { return "eng"; }, "Tesseract Language model file name without extension.");
var tessPath = new Option<string>(
new[] { "--tpath", "-t" },
() => { return @".\tessdata"; }, "Tesseract Language model path.");
var rootCommand = new RootCommand() {
apiKey,
userAgent,
proxy,
lang,
tessPath
};
rootCommand.SetHandler(
ProcessArgs,
apiKey,
proxy,
userAgent,
lang,
tessPath
);
var parser = new CommandLineBuilder(rootCommand)
.UseDefaults()
.Build();
var result = parser.Parse(args);
if (result.Errors.Count == 0)
parser.Invoke(args);
foreach (var error in result.Errors)
Log.Logger.Error(error.Message);
return result.Errors.Count;
void ProcessArgs(string apiKey, string proxy, string userAgent, string lang, string tPath)
{
services.AddScoped((x) => new ElevenLabsCLIArgs
{
ApiKey = apiKey,
Proxy = proxy,
UserAgent = userAgent
});
services.AddScoped((x) => new TesseractCLIArgs
{
Lang = lang,
Path = tPath
});
}
}
}
}