@@ -18,20 +18,8 @@ void Models::list(std::string project_token, std::map<std::string, std::string>
1818 console::info (" Your models: " );
1919 auto models = json.array_items ();
2020 for (int i = 0 ; i < models.size (); i++) {
21- auto tags = models[i][" tags" ].array_items ();
22- std::string model_tags;
23- if (tags.size () == 0 ){
24- model_tags = " nil" ;
25- } else {
26- for (int j = 0 ; j < tags.size (); j++) {
27- if (j == tags.size () - 1 ){
28- model_tags = model_tags + " " + tags[j].string_value ();
29- } else {
30- model_tags = model_tags + " " + tags[j].string_value () + " ," ;
31- }
32- }
33-
34- }
21+ Json tags = models[i][" tags" ];
22+ std::string model_tags = tags_to_string (tags);
3523
3624 console::info (" Name: " + models[i][" display_name" ].string_value () +
3725 " \n Version: " + models[i][" version" ].string_value () +
@@ -40,4 +28,133 @@ void Models::list(std::string project_token, std::map<std::string, std::string>
4028 } else {
4129 console::error (" There was an error listing your models \n " );
4230 }
43- }
31+ }
32+
33+ void Models::download (std::string project_token, std::map<std::string, std::string> params, std::string output_path){
34+ if (project_token.compare (" ." ) == 0 ){
35+ project_token = PROJECT_TOKEN;
36+ }
37+
38+ RestClient::Response resp = Request::list_models (project_token, params);
39+ std::string err;
40+ Json json = Json::parse (resp.body , err);
41+
42+ int status_code = resp.code ;
43+ std::string error_message = json[" message" ].string_value ();
44+
45+ Json selected_model;
46+
47+ if (not (Helpers::success (status_code))) {
48+ console::error (" There was an error retreiving your model for download: " + std::to_string (status_code) + " " + error_message + " \n " );
49+ } else if (json.is_array ()) {
50+ auto models = json.array_items ();
51+ string list_size = std::to_string (models.size ());
52+
53+ if (stoi (list_size) == 1 ){
54+ selected_model = models[0 ];
55+ } else {
56+ cout << " Please select the model you would like to download." << endl;
57+ int opt_iter = 1 ;
58+ for (int i = 0 ; i < models.size (); i++){
59+ Json tags = models[i][" tags" ];
60+ std::string model_tags = tags_to_string (tags);
61+
62+ cout << std::to_string (opt_iter) << " . " << models[i][" display_name" ].string_value () << endl;
63+ cout << " Version: " + models[i][" version" ].string_value () << endl;
64+ cout << " Tags: " + model_tags << endl;
65+ cout << endl;
66+ opt_iter++;
67+ }
68+ cout << " Enter your choice and press return: " ;
69+ string opt_select;
70+ cin >> opt_select;
71+ auto idx = stoi (opt_select) - 1 ;
72+ if (idx >= models.size ()) {
73+ cin.clear ();
74+ cin.ignore (1 , ' \n ' );
75+ cout << " Invalid choice. Valid numbers are [1-" << list_size << " ]." ;
76+ return ;
77+ } else {
78+ selected_model = models[idx];
79+ }
80+ }
81+ std::string display_name = selected_model[" display_name" ].string_value ();
82+ std::string version = selected_model[" version" ].string_value ();
83+
84+ Models::download_to_file (project_token, display_name, version, output_path);
85+
86+ } else {
87+ console::error (" There was an error downloading your model \n " );
88+ }
89+ }
90+
91+ std::string Models::tags_to_string (Json tags){
92+ auto tags_list = tags.array_items ();
93+ std::string model_tags;
94+ if (tags_list.size () == 0 ){
95+ model_tags = " nil" ;
96+ } else {
97+ for (int j = 0 ; j < tags_list.size (); j++) {
98+ if (j == tags_list.size () - 1 ){
99+ model_tags = model_tags + " " + tags_list[j].string_value ();
100+ } else {
101+ model_tags = model_tags + " " + tags_list[j].string_value () + " ," ;
102+ }
103+ }
104+ }
105+ return model_tags;
106+ }
107+
108+ void Models::download_to_file (std::string project_token, std::string display_name, std::string version, std::string output_path){
109+
110+ std::string model_url = DOWNLOAD_URL + " /projects/" + project_token + " /models?name=" + display_name + " &version=" + version;
111+
112+ std::string output_file = display_name;
113+ std::string download_path;
114+
115+ try {
116+ download_path = resolve_download_path (output_path, output_file);
117+ console::debug (" Download path: " + download_path);
118+ Request::download (model_url, download_path);
119+
120+ YAML::Node model_error = YAML::LoadFile (download_path);
121+ if (model_error[" Error" ]){
122+ console::error (" There was an error downloading your model to " + download_path + " : " + model_error[" Error" ].as <std::string>());
123+ } else {
124+ console::error (" There was an error downloading your model to " + download_path);
125+ }
126+ FileManager::delete_file (download_path);
127+ } catch (YAML::ParserException& e){
128+ console::info (" Model successfully downloaded to " + download_path);
129+ } catch (std::string &error) {
130+ console::error (" Error: " + error);
131+ } catch (...) {
132+ console::error (" There was an error downloading your model to " + download_path);
133+ }
134+ }
135+
136+ std::string Models::resolve_download_path (std::string output_path, std::string output_file) {
137+ std::string download_path;
138+
139+ if (FileManager::is_dir (output_path)){
140+ char ch = output_path.back ();
141+ if (ch == ' /' ) {
142+ download_path = output_path + output_file;
143+ } else {
144+ download_path = output_path + " /" + output_file;
145+ }
146+ } else {
147+ std::size_t found = output_path.rfind (" /" );
148+ if (found == std::string::npos) {
149+ download_path = FileManager::resolve_path (output_path);
150+ } else {
151+ string dir = output_path.substr (0 , found);
152+ if (FileManager::is_dir (dir)){
153+ download_path = output_path;
154+ } else {
155+ throw " Directory does not exist: " + dir;
156+ }
157+ }
158+ }
159+ return download_path;
160+ }
0 commit comments