|
1 | 1 | #include "model_pull_cmd.h" |
| 2 | +#include <memory> |
| 3 | +#include "common/event.h" |
| 4 | +#include "database/models.h" |
| 5 | +#include "server_start_cmd.h" |
| 6 | +#include "utils/cli_selection_utils.h" |
| 7 | +#include "utils/download_progress.h" |
| 8 | +#include "utils/format_utils.h" |
| 9 | +#include "utils/huggingface_utils.h" |
| 10 | +#include "utils/json_helper.h" |
2 | 11 | #include "utils/logging_utils.h" |
| 12 | +#include "utils/scope_exit.h" |
| 13 | +#include "utils/string_utils.h" |
| 14 | +#if defined(_WIN32) |
| 15 | +#include <signal.h> |
| 16 | +#endif |
3 | 17 |
|
4 | 18 | namespace commands { |
5 | | -void ModelPullCmd::Exec(const std::string& input) { |
6 | | - auto result = model_service_.DownloadModel(input); |
7 | | - if (result.has_error()) { |
8 | | - CLI_LOG(result.error()); |
| 19 | +std::function<void(int)> shutdown_handler; |
| 20 | +inline void signal_handler(int signal) { |
| 21 | + if (shutdown_handler) { |
| 22 | + shutdown_handler(signal); |
| 23 | + } |
| 24 | +} |
| 25 | +std::optional<std::string> ModelPullCmd::Exec(const std::string& host, int port, |
| 26 | + const std::string& input) { |
| 27 | + |
| 28 | + // model_id: use to check the download progress |
| 29 | + // model: use as a parameter for pull API |
| 30 | + auto model_id = input; |
| 31 | + auto model = input; |
| 32 | + |
| 33 | + // Start server if server is not started yet |
| 34 | + if (!commands::IsServerAlive(host, port)) { |
| 35 | + CLI_LOG("Starting server ..."); |
| 36 | + commands::ServerStartCmd ssc; |
| 37 | + if (!ssc.Exec(host, port)) { |
| 38 | + return std::nullopt; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Get model info from Server |
| 43 | + httplib::Client cli(host + ":" + std::to_string(port)); |
| 44 | + cli.set_read_timeout(std::chrono::seconds(60)); |
| 45 | + Json::Value j_data; |
| 46 | + j_data["model"] = input; |
| 47 | + auto d_str = j_data.toStyledString(); |
| 48 | + auto res = cli.Post("/models/pull/info", httplib::Headers(), d_str.data(), |
| 49 | + d_str.size(), "application/json"); |
| 50 | + |
| 51 | + if (res) { |
| 52 | + if (res->status == httplib::StatusCode::OK_200) { |
| 53 | + // CLI_LOG(res->body); |
| 54 | + auto root = json_helper::ParseJsonString(res->body); |
| 55 | + auto id = root["id"].asString(); |
| 56 | + bool is_cortexso = root["modelSource"].asString() == "cortexso"; |
| 57 | + auto default_branch = root["defaultBranch"].asString(); |
| 58 | + std::vector<std::string> downloaded; |
| 59 | + for (auto const& v : root["downloadedModels"]) { |
| 60 | + downloaded.push_back(v.asString()); |
| 61 | + } |
| 62 | + std::vector<std::string> avails; |
| 63 | + for (auto const& v : root["availableModels"]) { |
| 64 | + avails.push_back(v.asString()); |
| 65 | + } |
| 66 | + auto download_url = root["downloadUrl"].asString(); |
| 67 | + |
| 68 | + if (downloaded.empty() && avails.empty()) { |
| 69 | + model_id = id; |
| 70 | + model = download_url; |
| 71 | + } else { |
| 72 | + if (is_cortexso) { |
| 73 | + auto selection = cli_selection_utils::PrintModelSelection( |
| 74 | + downloaded, avails, |
| 75 | + default_branch.empty() |
| 76 | + ? std::nullopt |
| 77 | + : std::optional<std::string>(default_branch)); |
| 78 | + |
| 79 | + if (!selection.has_value()) { |
| 80 | + CLI_LOG("Invalid selection"); |
| 81 | + return std::nullopt; |
| 82 | + } |
| 83 | + model_id = selection.value(); |
| 84 | + model = model_id; |
| 85 | + } else { |
| 86 | + auto selection = cli_selection_utils::PrintSelection(avails); |
| 87 | + CLI_LOG("Selected: " << selection.value()); |
| 88 | + model_id = id + ":" + selection.value(); |
| 89 | + model = download_url + selection.value(); |
| 90 | + } |
| 91 | + } |
| 92 | + } else { |
| 93 | + auto root = json_helper::ParseJsonString(res->body); |
| 94 | + CLI_LOG(root["message"].asString()); |
| 95 | + return std::nullopt; |
| 96 | + } |
| 97 | + } else { |
| 98 | + auto err = res.error(); |
| 99 | + CTL_ERR("HTTP error: " << httplib::to_string(err)); |
| 100 | + return std::nullopt; |
| 101 | + } |
| 102 | + |
| 103 | + // Send request download model to server |
| 104 | + Json::Value json_data; |
| 105 | + json_data["model"] = model; |
| 106 | + auto data_str = json_data.toStyledString(); |
| 107 | + cli.set_read_timeout(std::chrono::seconds(60)); |
| 108 | + res = cli.Post("/v1/models/pull", httplib::Headers(), data_str.data(), |
| 109 | + data_str.size(), "application/json"); |
| 110 | + |
| 111 | + if (res) { |
| 112 | + if (res->status != httplib::StatusCode::OK_200) { |
| 113 | + auto root = json_helper::ParseJsonString(res->body); |
| 114 | + CLI_LOG(root["message"].asString()); |
| 115 | + return std::nullopt; |
| 116 | + } |
| 117 | + } else { |
| 118 | + auto err = res.error(); |
| 119 | + CTL_ERR("HTTP error: " << httplib::to_string(err)); |
| 120 | + return std::nullopt; |
| 121 | + } |
| 122 | + |
| 123 | + CLI_LOG("Start downloading ...") |
| 124 | + DownloadProgress dp; |
| 125 | + bool force_stop = false; |
| 126 | + |
| 127 | + shutdown_handler = [this, &dp, &host, &port, &model_id, &force_stop](int) { |
| 128 | + force_stop = true; |
| 129 | + AbortModelPull(host, port, model_id); |
| 130 | + dp.ForceStop(); |
| 131 | + }; |
| 132 | + |
| 133 | + utils::ScopeExit se([]() { shutdown_handler = {}; }); |
| 134 | +#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 135 | + struct sigaction sigint_action; |
| 136 | + sigint_action.sa_handler = signal_handler; |
| 137 | + sigemptyset(&sigint_action.sa_mask); |
| 138 | + sigint_action.sa_flags = 0; |
| 139 | + sigaction(SIGINT, &sigint_action, NULL); |
| 140 | + sigaction(SIGTERM, &sigint_action, NULL); |
| 141 | +#elif defined(_WIN32) |
| 142 | + auto console_ctrl_handler = +[](DWORD ctrl_type) -> BOOL { |
| 143 | + return (ctrl_type == CTRL_C_EVENT) ? (signal_handler(SIGINT), true) : false; |
| 144 | + }; |
| 145 | + SetConsoleCtrlHandler( |
| 146 | + reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true); |
| 147 | +#endif |
| 148 | + dp.Connect(host, port); |
| 149 | + if (!dp.Handle(model_id)) |
| 150 | + return std::nullopt; |
| 151 | + if (force_stop) |
| 152 | + return std::nullopt; |
| 153 | + CLI_LOG("Model " << model_id << " downloaded successfully!") |
| 154 | + return model_id; |
| 155 | +} |
| 156 | + |
| 157 | +bool ModelPullCmd::AbortModelPull(const std::string& host, int port, |
| 158 | + const std::string& task_id) { |
| 159 | + Json::Value json_data; |
| 160 | + json_data["taskId"] = task_id; |
| 161 | + auto data_str = json_data.toStyledString(); |
| 162 | + httplib::Client cli(host + ":" + std::to_string(port)); |
| 163 | + cli.set_read_timeout(std::chrono::seconds(60)); |
| 164 | + auto res = cli.Delete("/v1/models/pull", httplib::Headers(), data_str.data(), |
| 165 | + data_str.size(), "application/json"); |
| 166 | + if (res) { |
| 167 | + if (res->status == httplib::StatusCode::OK_200) { |
| 168 | + CTL_INF("Abort model pull successfully: " << task_id); |
| 169 | + return true; |
| 170 | + } else { |
| 171 | + auto root = json_helper::ParseJsonString(res->body); |
| 172 | + CLI_LOG(root["message"].asString()); |
| 173 | + return false; |
| 174 | + } |
| 175 | + } else { |
| 176 | + auto err = res.error(); |
| 177 | + CTL_ERR("HTTP error: " << httplib::to_string(err)); |
| 178 | + return false; |
9 | 179 | } |
10 | 180 | } |
11 | 181 | }; // namespace commands |
0 commit comments