Skip to content

Commit b35a584

Browse files
authored
If metadata request fail, check if file exists (#6)
1 parent 34ae1d0 commit b35a584

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@
3636

3737
# CMake
3838
build/
39+
.cache/

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
cmake_minimum_required(VERSION 3.10)
22
project(HFHub CXX)
33

4+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
5+
46
# Find CURL package
57
find_package(CURL REQUIRED)
68

include/huggingface_hub.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#ifndef HUGGINGFACE_HUB_H
3333
#define HUGGINGFACE_HUB_H
3434

35+
#include <curl/curl.h>
3536
#include <stdint.h>
3637
#include <string>
3738
#include <variant>
@@ -72,9 +73,9 @@ struct DownloadResult {
7273
* @param repo The repository name or ID.
7374
* @param file The file name within the repository.
7475
* @return A variant containing either the FileMetadata structure or an error
75-
* message string.
76+
* code.
7677
*/
77-
std::variant<struct FileMetadata, std::string>
78+
std::variant<struct FileMetadata, CURLcode>
7879
get_model_metadata_from_hf(const std::string &repo, const std::string &file);
7980

8081
/**

src/huggingface_hub.cpp

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
// SOFTWARE.
2323

24-
#include <algorithm>
2524
#include <chrono>
2625
#include <csignal>
2726
#include <filesystem>
@@ -30,7 +29,6 @@
3029
#include <regex>
3130
#include <sstream>
3231

33-
#include <curl/curl.h>
3432
#include <sys/ioctl.h>
3533
#include <sys/stat.h>
3634
#include <sys/types.h>
@@ -173,11 +171,11 @@ FileMetadata extract_metadata(const std::string &json) {
173171
return metadata;
174172
}
175173

176-
std::variant<struct FileMetadata, std::string>
174+
std::variant<struct FileMetadata, CURLcode>
177175
get_model_metadata_from_hf(const std::string &repo, const std::string &file) {
178176
CURL *curl = curl_easy_init();
179177
if (!curl) {
180-
return "Failed to initialize CURL";
178+
return CURLE_FAILED_INIT;
181179
}
182180

183181
std::string response, headers;
@@ -204,7 +202,7 @@ get_model_metadata_from_hf(const std::string &repo, const std::string &file) {
204202
curl_easy_cleanup(curl);
205203

206204
if (res != CURLE_OK) {
207-
return "CURL request failed: " + std::string(curl_easy_strerror(res));
205+
return res;
208206
}
209207

210208
return extract_metadata(response);
@@ -332,8 +330,38 @@ struct DownloadResult hf_hub_download(const std::string &repo_id,
332330

333331
// 1. Check that model exists on Hugging Face
334332
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)));
337365
result.success = false;
338366
return result;
339367
}

0 commit comments

Comments
 (0)