21
21
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
22
// SOFTWARE.
23
23
24
- #include < algorithm>
25
24
#include < chrono>
26
25
#include < csignal>
27
26
#include < filesystem>
30
29
#include < regex>
31
30
#include < sstream>
32
31
33
- #include < curl/curl.h>
34
32
#include < sys/ioctl.h>
35
33
#include < sys/stat.h>
36
34
#include < sys/types.h>
@@ -173,11 +171,11 @@ FileMetadata extract_metadata(const std::string &json) {
173
171
return metadata;
174
172
}
175
173
176
- std::variant<struct FileMetadata , std::string >
174
+ std::variant<struct FileMetadata , CURLcode >
177
175
get_model_metadata_from_hf (const std::string &repo, const std::string &file) {
178
176
CURL *curl = curl_easy_init ();
179
177
if (!curl) {
180
- return " Failed to initialize CURL " ;
178
+ return CURLE_FAILED_INIT ;
181
179
}
182
180
183
181
std::string response, headers;
@@ -204,7 +202,7 @@ get_model_metadata_from_hf(const std::string &repo, const std::string &file) {
204
202
curl_easy_cleanup (curl);
205
203
206
204
if (res != CURLE_OK) {
207
- return " CURL request failed: " + std::string ( curl_easy_strerror ( res)) ;
205
+ return res;
208
206
}
209
207
210
208
return extract_metadata (response);
@@ -332,8 +330,38 @@ struct DownloadResult hf_hub_download(const std::string &repo_id,
332
330
333
331
// 1. Check that model exists on Hugging Face
334
332
auto metadata_result = get_model_metadata_from_hf (repo_id, filename);
335
- if (std::holds_alternative<std::string>(metadata_result)) {
336
- log_error (std::get<std::string>(metadata_result));
333
+ if (std::holds_alternative<CURLcode>(metadata_result)) {
334
+ CURLcode err = std::get<CURLcode>(metadata_result);
335
+
336
+ std::string refs_main_path = " models/" + repo_id;
337
+ size_t pos = 0 ;
338
+ while ((pos = refs_main_path.find (" /" , pos)) != std::string::npos) {
339
+ refs_main_path.replace (pos, 1 , " --" );
340
+ pos += 2 ;
341
+ }
342
+
343
+ std::filesystem::path cache_model_dir =
344
+ expand_user_home (" ~/.cache/huggingface/hub/" + refs_main_path + " /" );
345
+ std::filesystem::path refs_file_path = cache_model_dir / " refs/main" ;
346
+
347
+ if (std::filesystem::exists (refs_file_path)) {
348
+ std::ifstream refs_file (refs_file_path);
349
+ std::string commit;
350
+ refs_file >> commit;
351
+ refs_file.close ();
352
+
353
+ std::filesystem::path snapshot_file_path =
354
+ cache_model_dir / " snapshots" / commit / filename;
355
+ if (std::filesystem::exists (snapshot_file_path)) {
356
+ log_info (" Snapshot file exists. Skipping download..." );
357
+ result.success = true ;
358
+ result.path = snapshot_file_path;
359
+ return result;
360
+ }
361
+ }
362
+
363
+ log_error (" CURL metadata request failed: " +
364
+ std::string (curl_easy_strerror (err)));
337
365
result.success = false ;
338
366
return result;
339
367
}
0 commit comments