Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
namchuai committed Sep 9, 2024
1 parent 03822a4 commit 30d19fe
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 45 deletions.
17 changes: 4 additions & 13 deletions engine/commands/cortex_upd_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// clang-format off
#include "utils/cortex_utils.h"
// clang-format on
#include "cortex_upd_cmd.h"
#include "httplib.h"
#include "nlohmann/json.hpp"
Expand Down Expand Up @@ -66,23 +63,17 @@ void CortexUpdCmd::Exec(std::string v) {
for (auto& asset : assets) {
auto asset_name = asset["name"].get<std::string>();
if (asset_name == matched_variant) {
std::string host{"https://github.com"};

auto full_url = asset["browser_download_url"].get<std::string>();
std::string path = full_url.substr(host.length());

auto fileName = asset["name"].get<std::string>();
auto file_name = asset["name"].get<std::string>();
CTL_INF("URL: " << full_url);

auto download_task = DownloadTask{.id = "cortex",
.type = DownloadType::Cortex,
.error = std::nullopt,
.items = {DownloadItem{
.id = "cortex",
.host = host,
.fileName = fileName,
.fileName = file_name,
.type = DownloadType::Cortex,
.path = path,
.path = full_url,
}}};

DownloadService download_service;
Expand Down Expand Up @@ -168,4 +159,4 @@ void CortexUpdCmd::Exec(std::string v) {
#endif
CLI_LOG("Update cortex sucessfully");
}
} // namespace commands
} // namespace commands
4 changes: 2 additions & 2 deletions engine/controllers/command_line_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void CommandLineParser::EngineInstall(CLI::App* parent,
std::string& version) {
auto install_engine_cmd = parent->add_subcommand(engine_name, "");

install_engine_cmd->callback([&] {
install_engine_cmd->callback([=] {
commands::EngineInitCmd eic(engine_name, version);
eic.Exec();
});
Expand All @@ -251,7 +251,7 @@ void CommandLineParser::EngineUninstall(CLI::App* parent,
const std::string& engine_name) {
auto uninstall_engine_cmd = parent->add_subcommand(engine_name, "");

uninstall_engine_cmd->callback([&] {
uninstall_engine_cmd->callback([=] {
commands::EngineUninstallCmd cmd(engine_name);
cmd.Exec();
});
Expand Down
12 changes: 3 additions & 9 deletions engine/controllers/engines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,17 @@ void Engines::InstallEngine(
for (auto& asset : assets) {
auto assetName = asset["name"].get<std::string>();
if (assetName.find(os_arch) != std::string::npos) {
std::string host{"https://github.com"};

auto full_url = asset["browser_download_url"].get<std::string>();
std::string path = full_url.substr(host.length());

auto fileName = asset["name"].get<std::string>();
auto name = asset["name"].get<std::string>();
LOG_INFO << "URL: " << full_url;

auto downloadTask = DownloadTask{.id = engine,
.type = DownloadType::Engine,
.error = std::nullopt,
.items = {DownloadItem{
.id = engine,
.host = host,
.fileName = fileName,
.fileName = name,
.type = DownloadType::Engine,
.path = path,
.path = full_url,
}}};

DownloadService().AddAsyncDownloadTask(
Expand Down
8 changes: 2 additions & 6 deletions engine/services/download_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ void DownloadService::AddDownloadTask(const DownloadTask& task,

for (const auto& item : task.items) {
try {
auto download_url{item.host + "/" + item.path};
auto file_size = GetFileSize(download_url);

auto file_size = GetFileSize(item.path);
std::cout << "file name: " << item.fileName << std::endl;
std::cout << "file size: " << file_size << std::endl;
} catch (const std::runtime_error& e) {
Expand Down Expand Up @@ -114,8 +112,6 @@ void DownloadService::Download(
size_t (*progressCallbackPtr)(void*, curl_off_t, curl_off_t, curl_off_t,
curl_off_t) = &ProgressCallback;

auto downloadUrl{download_item.host + "/" + download_item.path};
CTL_INF("download ulr" << downloadUrl.c_str());
curl = curl_easy_init();
if (!curl) {
// TODO: throw a custom exception
Expand All @@ -127,7 +123,7 @@ void DownloadService::Download(
throw std::runtime_error("Failed to open output file");
}

curl_easy_setopt(curl, CURLOPT_URL, downloadUrl.c_str());
curl_easy_setopt(curl, CURLOPT_URL, download_item.path.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallbackPtr);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progressCallbackPtr);
Expand Down
7 changes: 1 addition & 6 deletions engine/services/download_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ enum class DownloadStatus {
struct DownloadItem {
std::string id;

// not needed anymore if we using libcurl to download
std::string host;

std::string fileName;

DownloadType type;
Expand All @@ -33,13 +30,11 @@ struct DownloadItem {
struct DownloadTask {
std::string id;
DownloadType type;
std::optional<std::string> error;
std::vector<DownloadItem> items;

std::string ToString() const {
return "DownloadTask{id: " + id +
", type: " + std::to_string(static_cast<int>(type)) +
", error: " + (error.has_value() ? error.value() : "nullopt") + "}";
", type: " + std::to_string(static_cast<int>(type)) + "}";
}
};

Expand Down
16 changes: 7 additions & 9 deletions engine/utils/cortexso_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,22 @@ inline std::optional<DownloadTask> getDownloadTask(
for (auto& [key, value] : jsonResponse.items()) {
std::ostringstream downloadUrlOutput;
auto path = value["path"].get<std::string>();
downloadUrlOutput << repoAndModelIdStr << "/resolve/" << branch << "/"
<< path;
downloadUrlOutput << kHuggingFaceHost << "/" << repoAndModelIdStr
<< "/resolve/" << branch << "/" << path;
const std::string downloadUrl = downloadUrlOutput.str();

DownloadItem downloadItem{};
downloadItem.id = path;
downloadItem.host = kHuggingFaceHost;
downloadItem.fileName = path;
downloadItem.type = DownloadType::Model;
downloadItem.path = downloadUrl;
downloadItems.push_back(downloadItem);
}

DownloadTask downloadTask{};
downloadTask.id = branch == "main" ? modelId : modelId + "-" + branch;
downloadTask.type = DownloadType::Model;
downloadTask.error = std::nullopt;
downloadTask.items = downloadItems;
DownloadTask downloadTask{
.id = branch == "main" ? modelId : modelId + "-" + branch,
.type = DownloadType::Model,
.items = downloadItems};

return downloadTask;
} catch (const json::parse_error& e) {
Expand All @@ -61,4 +59,4 @@ inline std::optional<DownloadTask> getDownloadTask(
}
return std::nullopt;
}
} // namespace cortexso_parser
} // namespace cortexso_parser

0 comments on commit 30d19fe

Please sign in to comment.