2121// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222// SOFTWARE.
2323
24- #include " huggingface_hub.h"
2524#include < algorithm>
2625#include < chrono>
2726#include < csignal>
28- #include < curl/curl.h>
2927#include < filesystem>
3028#include < fstream>
3129#include < iomanip>
30+ #include < regex>
3231#include < sstream>
32+
33+ #include < curl/curl.h>
3334#include < sys/stat.h>
3435#include < sys/types.h>
3536
37+ #include " huggingface_hub.h"
38+
3639namespace huggingface_hub {
40+
3741volatile sig_atomic_t stop_download = 0 ;
3842void handle_sigint (int ) { stop_download = 1 ; }
3943
@@ -369,4 +373,40 @@ struct DownloadResult hf_hub_download(const std::string &repo_id,
369373 result.success = res == CURLE_OK;
370374 return result;
371375}
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+
372412} // namespace huggingface_hub
0 commit comments