@@ -179,17 +179,21 @@ void print_usage(int argc, const char* argv[], const std::vector<ArgOptions>& op
179179 options_list[0 ].print ();
180180 std::cout << " \n Context Options:\n " ;
181181 options_list[1 ].print ();
182+ std::cout << " \n Default Generation Options:\n " ;
183+ options_list[2 ].print ();
182184}
183185
184- void parse_args (int argc, const char ** argv, SDSvrParams& svr_params, SDContextParams& ctx_params) {
185- std::vector<ArgOptions> options_vec = {svr_params.get_options (), ctx_params.get_options ()};
186+ void parse_args (int argc, const char ** argv, SDSvrParams& svr_params, SDContextParams& ctx_params, SDGenerationParams& default_gen_params ) {
187+ std::vector<ArgOptions> options_vec = {svr_params.get_options (), ctx_params.get_options (), default_gen_params. get_options () };
186188
187189 if (!parse_options (argc, argv, options_vec)) {
188190 print_usage (argc, argv, options_vec);
189191 exit (svr_params.normal_exit ? 0 : 1 );
190192 }
191193
192- if (!svr_params.process_and_check () || !ctx_params.process_and_check (IMG_GEN)) {
194+ if (!svr_params.process_and_check () ||
195+ !ctx_params.process_and_check (IMG_GEN) ||
196+ !default_gen_params.process_and_check (IMG_GEN, ctx_params.lora_model_dir )) {
193197 print_usage (argc, argv, options_vec);
194198 exit (1 );
195199 }
@@ -298,14 +302,16 @@ void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) {
298302int main (int argc, const char ** argv) {
299303 SDSvrParams svr_params;
300304 SDContextParams ctx_params;
301- parse_args (argc, argv, svr_params, ctx_params);
305+ SDGenerationParams default_gen_params;
306+ parse_args (argc, argv, svr_params, ctx_params, default_gen_params);
302307
303308 sd_set_log_callback (sd_log_cb, (void *)&svr_params);
304309
305310 if (svr_params.verbose ) {
306311 printf (" %s" , sd_get_system_info ());
307312 printf (" %s\n " , svr_params.to_string ().c_str ());
308313 printf (" %s\n " , ctx_params.to_string ().c_str ());
314+ printf (" %s\n " , default_gen_params.to_string ().c_str ());
309315 }
310316
311317 sd_ctx_params_t sd_ctx_params = ctx_params.to_sd_ctx_params_t (false , false , false );
@@ -320,6 +326,23 @@ int main(int argc, const char** argv) {
320326
321327 httplib::Server svr;
322328
329+ svr.set_pre_routing_handler ([](const httplib::Request& req, httplib::Response& res) {
330+ std::string origin = req.get_header_value (" Origin" );
331+ if (origin.empty ()) {
332+ origin = " *" ;
333+ }
334+ res.set_header (" Access-Control-Allow-Origin" , origin);
335+ res.set_header (" Access-Control-Allow-Credentials" , " true" );
336+ res.set_header (" Access-Control-Allow-Methods" , " *" );
337+ res.set_header (" Access-Control-Allow-Headers" , " *" );
338+
339+ if (req.method == " OPTIONS" ) {
340+ res.status = 204 ;
341+ return httplib::Server::HandlerResponse::Handled;
342+ }
343+ return httplib::Server::HandlerResponse::Unhandled;
344+ });
345+
323346 // health
324347 svr.Get (" /" , [&](const httplib::Request&, httplib::Response& res) {
325348 res.set_content (R"( {"ok":true,"service":"sd-cpp-http"})" , " application/json" );
@@ -390,11 +413,11 @@ int main(int argc, const char** argv) {
390413 out[" data" ] = json::array ();
391414 out[" output_format" ] = output_format;
392415
393- SDGenerationParams gen_params;
394- gen_params.prompt = prompt;
395- gen_params.width = width;
396- gen_params.height = height;
397- gen_params.batch_count = n;
416+ SDGenerationParams gen_params = default_gen_params ;
417+ gen_params.prompt = prompt;
418+ gen_params.width = width;
419+ gen_params.height = height;
420+ gen_params.batch_count = n;
398421
399422 if (!sd_cpp_extra_args_str.empty () && !gen_params.from_json_str (sd_cpp_extra_args_str)) {
400423 res.status = 400 ;
@@ -570,18 +593,24 @@ int main(int argc, const char** argv) {
570593 output_compression = 0 ;
571594 }
572595
573- SDGenerationParams gen_params;
574- gen_params.prompt = prompt;
575- gen_params.width = width;
576- gen_params.height = height;
577- gen_params.batch_count = n;
596+ SDGenerationParams gen_params = default_gen_params ;
597+ gen_params.prompt = prompt;
598+ gen_params.width = width;
599+ gen_params.height = height;
600+ gen_params.batch_count = n;
578601
579602 if (!sd_cpp_extra_args_str.empty () && !gen_params.from_json_str (sd_cpp_extra_args_str)) {
580603 res.status = 400 ;
581604 res.set_content (R"( {"error":"invalid sd_cpp_extra_args"})" , " application/json" );
582605 return ;
583606 }
584607
608+ if (!gen_params.process_and_check (IMG_GEN, ctx_params.lora_model_dir )) {
609+ res.status = 400 ;
610+ res.set_content (R"( {"error":"invalid params"})" , " application/json" );
611+ return ;
612+ }
613+
585614 if (svr_params.verbose ) {
586615 printf (" %s\n " , gen_params.to_string ().c_str ());
587616 }
0 commit comments