MCPcopy Create free account
hub / github.com/PABannier/bark.cpp / main

Function main

examples/server/server.cpp:96–188  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

94}
95
96int main(int argc, char **argv) {
97 ggml_time_init();
98 const int64_t t_main_start_us = ggml_time_us();
99
100 bark_params params;
101 server_params server_params;
102 bark_verbosity_level verbosity = bark_verbosity_level::LOW;
103
104 bark_params_parse(argc, argv, params, server_params);
105
106 struct bark_context_params ctx_params = bark_context_default_params();
107 ctx_params.verbosity = verbosity;
108
109 struct bark_context *bctx = bark_load_model(params.model_path.c_str(), ctx_params, params.seed);
110 if (!bctx) {
111 fprintf(stderr, "%s: Could not load model\n", __func__);
112 return 1;
113 }
114
115 // bark_seed_rng(bctx, params.seed);
116
117 std::mutex bark_mutex;
118
119 Server svr;
120
121 std::string default_content = "<html>hello</html>";
122
123 // this is only called if no index.html is found in the public --path
124 svr.Get("/", [&default_content](const Request &, Response &res) {
125 res.set_content(default_content.c_str(), default_content.size(), "text/html");
126 return false; });
127
128 svr.Post("/bark", [&](const Request &req, Response &res) {
129 // aquire bark model mutex lock
130 bark_mutex.lock();
131
132 json jreq = json::parse(req.body);
133 std::string text = jreq.at("text");
134
135 std::string dest_wav_path = "/tmp/bark_tmp.wav";
136
137 // generate audio
138 bool generated = generate_audio(params.n_threads, bctx, text, dest_wav_path);
139
140 // read audio as binary
141 std::ifstream wav_file(dest_wav_path, std::ios::binary);
142
143 if (generated && wav_file.is_open()) {
144 // Read the contents of the WAV file
145 std::string wav_contents((std::istreambuf_iterator<char>(wav_file)),
146 std::istreambuf_iterator<char>());
147
148 // Set the response content type to audio/wav
149 res.set_header("Content-Type", "audio/wav");
150
151 // Set the response body to the WAV file contents
152 res.set_content(wav_contents, "audio/wav");
153 } else {

Callers

nothing calls this directly

Calls 15

parseFunction · 0.85
generate_audioFunction · 0.85
GetMethod · 0.80
set_contentMethod · 0.80
PostMethod · 0.80
set_headerMethod · 0.80
set_read_timeoutMethod · 0.80
set_write_timeoutMethod · 0.80
bind_to_portMethod · 0.80
set_base_dirMethod · 0.80
listen_after_bindMethod · 0.80
bark_params_parseFunction · 0.70

Tested by

no test coverage detected