Skip to content

Commit

Permalink
Merge pull request #1199 from luxonis/feature/graphql2restapi
Browse files Browse the repository at this point in the history
Switch from GraphQL to RestAPI
  • Loading branch information
lnotspotl authored Dec 19, 2024
2 parents bee44fa + d26fccd commit f5bd0dd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
2 changes: 1 addition & 1 deletion include/depthai/modelzoo/NNModelDescription.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct NNModelDescription {
/** SNPE version = OPTIONAL parameter */
std::string snpeVersion;

/** modelPrecisionType */
/** modelPrecisionType = OPTIONAL parameter */
std::string modelPrecisionType;
};

Expand Down
2 changes: 1 addition & 1 deletion include/depthai/modelzoo/Zoo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "depthai/modelzoo/NNModelDescription.hpp"

namespace dai {
constexpr const char* MODEL_ZOO_URL = "https://api.cloud.luxonis.com/graphql";
constexpr const char* MODEL_ZOO_URL = "https://easyml.cloud.luxonis.com/models/api/v1/models/download";
constexpr const char* MODEL_ZOO_DEFAULT_CACHE_DIRECTORY = ".depthai_cached_models";

/**
Expand Down
68 changes: 38 additions & 30 deletions src/modelzoo/Zoo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include "utility/Logging.hpp"

#ifdef DEPTHAI_ENABLE_CURL
#include <cpr/cpr.h>
#include <cpr/api.h>
#include <cpr/parameters.h>
#include <cpr/status_codes.h>
namespace dai {
class ZooManager {
public:
Expand Down Expand Up @@ -139,10 +141,10 @@ bool checkIsErrorHub(const cpr::Response& response) {
// Check if response is an HTTP error
if(response.status_code != cpr::status::HTTP_OK) return true;

// If there was no HTTP error, check response content for errors
// If there was no HTTP error, check presence of required fields
nlohmann::json responseJson = nlohmann::json::parse(response.text);
if(responseJson.contains("errors")) return true;
if(responseJson["data"]["ml"]["modelDownloads"].is_null()) return true;
if(!responseJson.contains("hash")) return true;
if(!responseJson.contains("download_links")) return true;

// All checks passed - no errors yay
return false;
Expand Down Expand Up @@ -206,47 +208,53 @@ bool ZooManager::isModelCached() const {
}

void ZooManager::downloadModel() {
// graphql query to send to Hub - always the same
constexpr std::string_view MODEL_ZOO_QUERY = "query MlDownloads($input: MlModelDownloadsInput!) {ml { modelDownloads(input : $input) { data }}}";

// Setup request body
nlohmann::json requestBody;
requestBody["query"] = MODEL_ZOO_QUERY;

// Add REQUIRED parameters
requestBody["variables"]["input"]["platform"] = modelDescription.platform;
requestBody["variables"]["input"]["slug"] = modelDescription.model;

// Add OPTIONAL parameters
if(!modelDescription.optimizationLevel.empty()) {
requestBody["variables"]["input"]["optimizationLevel"] = modelDescription.optimizationLevel;
}
if(!modelDescription.compressionLevel.empty()) {
requestBody["variables"]["input"]["compressionLevel"] = modelDescription.compressionLevel;
}
if(!modelDescription.snpeVersion.empty()) {
requestBody["variables"]["input"]["snpeVersion"] = modelDescription.snpeVersion;
// Add request parameters
cpr::Parameters params;

// Required parameters
// clang-format off
std::vector<std::pair<std::string, std::string>> requiredParams = {
{"slug", modelDescription.model},
{"platform", modelDescription.platform}
};
// clang-format on
for(const auto& param : requiredParams) {
params.Add({param.first, param.second});
}
if(!modelDescription.modelPrecisionType.empty()) {
requestBody["variables"]["input"]["modelPrecisionType"] = modelDescription.modelPrecisionType;

// Optional parameters
// clang-format off
std::vector<std::pair<std::string, std::string>> optionalParams = {
{"optimizationLevel", modelDescription.optimizationLevel},
{"compressionLevel", modelDescription.compressionLevel},
{"snpeVersion", modelDescription.snpeVersion},
{"modelPrecisionType", modelDescription.modelPrecisionType}
};
// clang-format on
for(const auto& param : optionalParams) {
if(!param.second.empty()) {
params.Add({param.first, param.second});
}
}

// Set the Authorization headers
cpr::Header headers = {
{"Content-Type", "application/json"},
};
if(!apiKey.empty()) {
headers["Authorization"] = "Bearer " + apiKey;
}
// Send HTTP request to Hub
cpr::Response response = cpr::Post(cpr::Url{MODEL_ZOO_URL}, headers, cpr::Body{requestBody.dump()});

// Send HTTP GET request to REST endpoint
cpr::Response response = cpr::Get(cpr::Url{MODEL_ZOO_URL}, headers, params);
if(checkIsErrorHub(response)) {
removeModelCacheFolder();
throw std::runtime_error(generateErrorMessageHub(response));
}

// Extract download link from response
// Extract download links from response
nlohmann::json responseJson = nlohmann::json::parse(response.text);
auto downloadLinks = responseJson["data"]["ml"]["modelDownloads"]["data"].get<std::vector<std::string>>();
auto downloadLinks = responseJson["download_links"].get<std::vector<std::string>>();

// Download all files and store them in cache folder
for(const auto& downloadLink : downloadLinks) {
Expand Down

0 comments on commit f5bd0dd

Please sign in to comment.