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 " huggingface_hub.h"
25
24
#include < algorithm>
26
25
#include < chrono>
27
26
#include < csignal>
28
- #include < curl/curl.h>
29
27
#include < filesystem>
30
28
#include < fstream>
31
29
#include < iomanip>
30
+ #include < regex>
32
31
#include < sstream>
32
+
33
+ #include < curl/curl.h>
33
34
#include < sys/stat.h>
34
35
#include < sys/types.h>
35
36
37
+ #include " huggingface_hub.h"
38
+
36
39
namespace huggingface_hub {
40
+
37
41
volatile sig_atomic_t stop_download = 0 ;
38
42
void handle_sigint (int ) { stop_download = 1 ; }
39
43
@@ -369,4 +373,40 @@ struct DownloadResult hf_hub_download(const std::string &repo_id,
369
373
result.success = res == CURLE_OK;
370
374
return result;
371
375
}
376
+
377
+ struct DownloadResult hf_hub_download_with_shards (const std::string &repo_id,
378
+ const std::string &filename,
379
+ const std::string &cache_dir,
380
+ bool force_download) {
381
+
382
+ std::regex pattern (R"( -([0-9]+)-of-([0-9]+)\.gguf)" );
383
+ std::smatch match;
384
+
385
+ if (std::regex_search (filename, match, pattern)) {
386
+ int total_shards = std::stoi (match[2 ]);
387
+ std::string base_name = filename.substr (0 , match.position (0 ));
388
+
389
+ // Download shards
390
+ for (int i = 1 ; i <= total_shards; ++i) {
391
+ char shard_file[512 ];
392
+ snprintf (shard_file, sizeof (shard_file), " %s-%05d-of-%05d.gguf" ,
393
+ base_name.c_str (), i, total_shards);
394
+ auto aux_res =
395
+ hf_hub_download (repo_id, shard_file, cache_dir, force_download);
396
+
397
+ if (!aux_res.success ) {
398
+ return aux_res;
399
+ }
400
+ }
401
+
402
+ // Return first shard
403
+ char first_shard[512 ];
404
+ snprintf (first_shard, sizeof (first_shard), " %s-00001-of-%05d.gguf" ,
405
+ base_name.c_str (), total_shards);
406
+ return hf_hub_download (repo_id, first_shard, cache_dir, false );
407
+ }
408
+
409
+ return hf_hub_download (repo_id, filename, cache_dir, force_download);
410
+ }
411
+
372
412
} // namespace huggingface_hub
0 commit comments