Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
300 lines (283 loc) · 13.5 KB
/
Copy pathmain.cpp
File metadata and controls
300 lines (283 loc) · 13.5 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "config.h"
#include "http.h"
#include "runtime.h"
#include "../common/build_info.h"
#include "engine/framework/core/backend.h"
#include "engine/framework/debug/trace.h"
#include <csignal>
#include <filesystem>
#include <iostream>
#include <optional>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
namespace {
volatile std::sig_atomic_t g_shutdown_requested = 0;
void request_shutdown(int) {
g_shutdown_requested = 1;
}
bool shutdown_requested() {
return g_shutdown_requested != 0;
}
std::optional<std::string> arg_value(int argc, char ** argv, const std::string & name) {
for (int i = 1; i + 1 < argc; ++i) {
if (argv[i] == name) {
return std::string(argv[i + 1]);
}
}
return std::nullopt;
}
bool has_arg(int argc, char ** argv, const std::string & name) {
for (int i = 1; i < argc; ++i) {
if (argv[i] == name) {
return true;
}
}
return false;
}
std::vector<std::string> arg_values(int argc, char ** argv, const std::string & name) {
std::vector<std::string> out;
for (int i = 1; i + 1 < argc; ++i) {
if (argv[i] == name) {
out.emplace_back(argv[i + 1]);
++i;
}
}
return out;
}
std::pair<std::string, std::string> parse_key_value_arg(const std::string & text, const std::string & name) {
const auto eq = text.find('=');
if (eq == std::string::npos || eq == 0) {
throw std::runtime_error(name + " must be key=value");
}
return {text.substr(0, eq), text.substr(eq + 1)};
}
std::filesystem::path executable_directory(const char * argv0) {
if (argv0 == nullptr || *argv0 == '\0') {
return std::filesystem::current_path();
}
std::error_code ec;
auto path = std::filesystem::absolute(std::filesystem::path(argv0), ec);
if (ec) {
return std::filesystem::current_path();
}
path = path.lexically_normal();
if (std::filesystem::is_regular_file(path, ec)) {
return path.parent_path();
}
return std::filesystem::current_path();
}
void print_help() {
std::cout
<< "audiocpp_server [--config <server.json>] [--ui] [--host <ip>] [--port <port>] [--backend <backend>]\n"
<< " [--device <id>] [--list-devices] [--threads <n>] [--busy-timeout-ms <ms>]\n"
<< " [--max-loaded-models <n>] [--idle-unload-ms <ms>] [--min-free-memory-mb <mb>]\n"
<< " [--model-spec-override <json-or-directory>] [--voice-dir <directory>]\n"
<< " [--frontend-listener <name>] [--frontend-option key=value]\n"
<< " [--log] [--log-file <path>]\n"
<< " [--cors-origins <origins>]\n"
<< " --version print build version, commit, compiler, platform, and enabled backends\n"
<< " --ui serve the embedded WebUI\n"
<< " --no-ui disable the embedded WebUI\n"
<< " --ui-management allow WebUI model management and downloads; requires\n"
<< " AUDIOCPP_BUILD_NATIVE_MODEL_MANAGER=ON at build time\n"
<< " --host <ip> server bind address; default 127.0.0.1\n"
<< " --port <port> server listening port; default 8080\n"
<< " --backend cpu|cuda|hip|rocm|vulkan|metal default cuda (rocm is an alias for hip)\n"
<< " --list-devices list available backend devices and exit\n"
<< " --busy-timeout-ms <ms> fail a request with 503 when the model has been\n"
<< " busy this long; default 300000, 0 disables\n"
<< " --max-loaded-models <n> keep at most n models resident in memory, unloading\n"
<< " the least recently used idle model first; 1 enforces\n"
<< " a single loaded model, default 0 (no limit)\n"
<< " --idle-unload-ms <ms> unload all resident models after this many ms without\n"
<< " any model load/run; default 0 (disabled), next request\n"
<< " reloads lazily\n"
<< " --min-free-memory-mb <mb> refuse a model load unless host and GPU each keep at\n"
<< " least this many MiB free after the load; default 0\n"
<< " (guard disabled)\n"
<< " --voice-dir <directory> override the shared reference voice library directory\n"
<< " --frontend-listener <name> opt-in frontend transport listener, e.g. https or websocket\n"
<< " --frontend-option key=value listener-specific option; may be repeated\n"
<< " --cors-origins \"*\" experimental; disabled by default. Allows browser\n"
<< " requests from any origin for trusted local demos only\n"
<< "\n"
<< "Endpoints:\n"
<< " GET / embedded WebUI (enabled by default with a config)\n"
<< " GET /health\n"
<< " GET /v1/models\n"
<< " POST /v1/models/load available with --ui-management\n"
<< " POST /v1/models/unload available with --ui-management\n"
<< " POST /v1/ui/upload temporary browser upload for WebUI requests\n"
<< " POST /v1/ui/models/install background package download/preparation\n"
<< " POST /v1/ui/models/install/stop stop one active package download\n"
<< " POST /v1/ui/models/clean-partial remove abandoned package staging files\n"
<< " POST /v1/ui/models/delete remove one installed package precision\n"
<< " GET /v1/ui/models-root current and binary-local default models folders\n"
<< " POST /v1/ui/models-root select a models folder (empty path restores default)\n"
<< " POST /v1/ui/browse-directories list local folders for the native folder picker\n"
<< " GET /v1/ui/models/install-status[?id=<package>]\n"
<< " GET /v1/ui/models/package-sizes package sizes from metadata-only checks\n"
<< " GET /v1/audio/voices?model=<id>\n"
<< " POST /v1/audio/speech\n"
<< " POST /v1/audio/speech/live?model=<id>\n"
<< " raw PCM in a chunked body, speech audio deltas as SSE on the same connection\n"
<< " POST /v1/audio/transcriptions\n"
<< " fields: file, model, language, prompt, stream\n"
<< " POST /v1/audio/alignments\n"
<< " fields: file, model, text, language\n"
<< " OpenAI-style streaming: speech stream_format=sse|audio, transcription stream=true\n"
<< " POST /v1/audio/transcriptions/live?model=<id>\n"
<< " raw PCM in a chunked body, transcript deltas as SSE on the same connection\n"
<< " POST /v1/tasks/run\n";
}
} // namespace
int main(int argc, char ** argv) {
try {
if (has_arg(argc, argv, "--list-devices")) {
engine::core::print_backend_devices(std::cout);
return 0;
}
if (has_arg(argc, argv, "--version")) {
minitts::app::print_build_info(std::cout);
return 0;
}
if (has_arg(argc, argv, "--help") || has_arg(argc, argv, "-h")) {
print_help();
return 0;
}
const auto config_path = arg_value(argc, argv, "--config");
const bool ui_requested = has_arg(argc, argv, "--ui");
if (!config_path.has_value() && !ui_requested) {
throw std::runtime_error("missing required --config argument (or use --ui for the native WebUI)");
}
const auto log_file = arg_value(argc, argv, "--log-file");
engine::debug::configure_logging(engine::debug::LoggingConfig{
has_arg(argc, argv, "--log") || log_file.has_value(),
log_file,
});
std::signal(SIGINT, request_shutdown);
std::signal(SIGTERM, request_shutdown);
#ifdef SIGPIPE
// Writing to a socket whose peer has already disconnected (for example a
// client that closed an SSE/chunked stream early) would otherwise deliver
// SIGPIPE and terminate the whole server. Ignore it so the failed send
// surfaces as an EPIPE error on that single request thread, which
// handle_client already unwinds cleanly, instead of taking the process down.
std::signal(SIGPIPE, SIG_IGN);
#endif
auto config = config_path.has_value()
? minitts::server::load_server_config(*config_path)
: minitts::server::ServerConfig{};
if (!config_path.has_value()) {
config.lazy_load = true;
}
if (ui_requested) {
config.ui_enabled = true;
}
if (has_arg(argc, argv, "--no-ui")) {
config.ui_enabled = false;
}
if (has_arg(argc, argv, "--ui-management")) {
config.ui_management = true;
}
#if !defined(AUDIOCPP_HAS_NATIVE_MODEL_MANAGER)
if (config.ui_management) {
throw std::runtime_error(
"UI model management is not available in this build; reconfigure with "
"-DAUDIOCPP_BUILD_NATIVE_MODEL_MANAGER=ON");
}
#endif
if (const auto host = arg_value(argc, argv, "--host")) {
config.host = *host;
}
if (const auto port = arg_value(argc, argv, "--port")) {
config.port = std::stoi(*port);
}
if (const auto cors_origins = arg_value(argc, argv, "--cors-origins")) {
config.cors_origins = *cors_origins;
}
if (const auto backend = arg_value(argc, argv, "--backend")) {
config.backend = minitts::server::parse_server_backend(*backend);
}
if (const auto device = arg_value(argc, argv, "--device")) {
config.device = std::stoi(*device);
}
if (const auto threads = arg_value(argc, argv, "--threads")) {
config.threads = std::stoi(*threads);
}
if (const auto busy_timeout = arg_value(argc, argv, "--busy-timeout-ms")) {
config.busy_timeout_ms = std::stoi(*busy_timeout);
}
if (const auto max_loaded_models = arg_value(argc, argv, "--max-loaded-models")) {
config.max_loaded_models = std::stoi(*max_loaded_models);
}
if (const auto idle_unload_ms = arg_value(argc, argv, "--idle-unload-ms")) {
config.idle_unload_ms = std::stoi(*idle_unload_ms);
}
if (const auto min_free_memory_mb = arg_value(argc, argv, "--min-free-memory-mb")) {
config.min_free_memory_mb = std::stoi(*min_free_memory_mb);
}
if (const auto model_spec = arg_value(argc, argv, "--model-spec-override")) {
config.model_spec_override = std::filesystem::path(*model_spec);
}
if (const auto voice_dir = arg_value(argc, argv, "--voice-dir")) {
config.voice_dir = std::filesystem::path(*voice_dir);
}
if (const auto frontend_listener = arg_value(argc, argv, "--frontend-listener")) {
config.frontend_listener = *frontend_listener;
}
for (const auto & option : arg_values(argc, argv, "--frontend-option")) {
auto [key, value] = parse_key_value_arg(option, "--frontend-option");
config.frontend_options[std::move(key)] = std::move(value);
}
if (config.frontend_listener.empty() && !config.frontend_options.empty()) {
throw std::runtime_error("--frontend-option requires --frontend-listener");
}
if (!(config.cors_origins == "*" || config.cors_origins == "")) {
throw std::runtime_error("--cors-origins must be '*' (allow all origins) or '' (disabled)");
}
if (config.threads <= 0) {
throw std::runtime_error("--threads must be positive");
}
if (config.busy_timeout_ms < 0) {
throw std::runtime_error("--busy-timeout-ms must be >= 0 (0 disables the guard)");
}
if (config.max_loaded_models < 0) {
throw std::runtime_error("--max-loaded-models must be >= 0 (0 disables the limit)");
}
if (config.idle_unload_ms < 0) {
throw std::runtime_error("--idle-unload-ms must be >= 0 (0 disables idle unload)");
}
if (config.min_free_memory_mb < 0) {
throw std::runtime_error("--min-free-memory-mb must be >= 0 (0 disables the memory guard)");
}
const auto ui_resource_anchor = executable_directory(argc > 0 ? argv[0] : nullptr);
minitts::server::ServerState state(
config,
std::filesystem::current_path(),
ui_resource_anchor);
if (!config.frontend_listener.empty()) {
auto listener = state.make_frontend_listener(config.frontend_listener);
listener->serve(
config.host,
config.port,
state,
shutdown_requested,
config.max_request_body_bytes,
config.frontend_options);
} else {
minitts::server::serve_http(
config.host,
config.port,
state,
shutdown_requested,
config.max_request_body_bytes);
}
return 0;
} catch (const std::exception & ex) {
std::cerr << "audiocpp_server failed: " << ex.what() << "\n";
return 1;
}
}