| 754 | }; |
| 755 | |
| 756 | static handle_model_result common_params_handle_model( |
| 757 | struct common_params_model & model, |
| 758 | const std::string & bearer_token, |
| 759 | const std::string & model_path_default, |
| 760 | bool offline) { |
| 761 | handle_model_result result; |
| 762 | // handle pre-fill default model path and url based on hf_repo and hf_file |
| 763 | { |
| 764 | if (!model.hf_repo.empty()) { |
| 765 | // short-hand to avoid specifying --hf-file -> default it to --model |
| 766 | if (model.hf_file.empty()) { |
| 767 | if (model.path.empty()) { |
| 768 | auto auto_detected = common_get_hf_file(model.hf_repo, bearer_token, offline); |
| 769 | if (auto_detected.repo.empty() || auto_detected.ggufFile.empty()) { |
| 770 | exit(1); // built without CURL, error message already printed |
| 771 | } |
| 772 | model.hf_repo = auto_detected.repo; |
| 773 | model.hf_file = auto_detected.ggufFile; |
| 774 | if (!auto_detected.mmprojFile.empty()) { |
| 775 | result.found_mmproj = true; |
| 776 | result.mmproj.hf_repo = model.hf_repo; |
| 777 | result.mmproj.hf_file = auto_detected.mmprojFile; |
| 778 | } |
| 779 | } else { |
| 780 | model.hf_file = model.path; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | std::string model_endpoint = get_model_endpoint(); |
| 785 | model.url = model_endpoint + model.hf_repo + "/resolve/main/" + model.hf_file; |
| 786 | // make sure model path is present (for caching purposes) |
| 787 | if (model.path.empty()) { |
| 788 | // this is to avoid different repo having same file name, or same file name in different subdirs |
| 789 | std::string filename = model.hf_repo + "_" + model.hf_file; |
| 790 | // to make sure we don't have any slashes in the filename |
| 791 | string_replace_all(filename, "/", "_"); |
| 792 | model.path = fs_get_cache_file(filename); |
| 793 | } |
| 794 | |
| 795 | } else if (!model.url.empty()) { |
| 796 | if (model.path.empty()) { |
| 797 | auto f = string_split<std::string>(model.url, '#').front(); |
| 798 | f = string_split<std::string>(f, '?').front(); |
| 799 | model.path = fs_get_cache_file(string_split<std::string>(f, '/').back()); |
| 800 | } |
| 801 | |
| 802 | } else if (model.path.empty()) { |
| 803 | model.path = model_path_default; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | // then, download it if needed |
| 808 | if (!model.url.empty()) { |
| 809 | bool ok = common_download_model(model, bearer_token, offline); |
| 810 | if (!ok) { |
| 811 | LOG_ERR("error: failed to download model from %s\n", model.url.c_str()); |
| 812 | exit(1); |
| 813 | } |
no test coverage detected