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>
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 >
177175get_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