Class: DurableHuggingfaceHub::HfApi
- Inherits:
-
Object
- Object
- DurableHuggingfaceHub::HfApi
- Defined in:
- lib/durable_huggingface_hub/hf_api.rb
Overview
Main API client for interacting with the HuggingFace Hub
This class provides methods for accessing and managing repositories, models, datasets, and spaces on the HuggingFace Hub. It handles authentication, request management, and response parsing.
Instance Attribute Summary collapse
-
#endpoint ⇒ String
readonly
Base endpoint URL for the HuggingFace Hub.
-
#http_client ⇒ DurableHuggingfaceHub::Utils::HttpClient
readonly
HTTP client instance.
-
#token ⇒ String?
readonly
Authentication token for API requests.
Instance Method Summary collapse
-
#build_tree_structure(items) ⇒ Hash
Build a tree structure from the API response.
-
#create_branch(repo_id:, branch_name:, repo_type: "model", revision: nil, timeout: nil) ⇒ String
Create a new branch in a repository.
-
#create_commit(repo_id:, operations:, repo_type: "model", revision: nil, commit_message: nil, commit_description: nil, parent_commit: nil, timeout: nil) ⇒ String
Create a new commit with multiple file operations.
-
#create_repo(repo_id:, repo_type: "model", private: false, organization: nil, timeout: nil) ⇒ String
Create a new repository on the HuggingFace Hub.
-
#create_tag(repo_id:, tag_name:, repo_type: "model", revision: nil, message: nil, timeout: nil) ⇒ String
Create a new tag in a repository.
-
#dataset_info(repo_id, revision: nil, timeout: nil) ⇒ DurableHuggingfaceHub::Types::DatasetInfo
Get information about a specific dataset.
-
#delete_branch(repo_id:, branch_name:, repo_type: "model", timeout: nil) ⇒ Boolean
Delete a branch from a repository.
-
#delete_file(repo_id:, path_in_repo:, repo_type: "model", revision: nil, commit_message: nil, commit_description: nil, timeout: nil) ⇒ Boolean
Delete a file from a repository on the HuggingFace Hub.
-
#delete_folder(repo_id:, folder_path_in_repo:, repo_type: "model", revision: nil, commit_message: nil, commit_description: nil, timeout: nil) ⇒ Boolean
Delete an entire folder from a repository on the HuggingFace Hub.
-
#delete_repo(repo_id:, repo_type: "model", token: nil, timeout: nil) ⇒ Boolean
Delete a repository from the HuggingFace Hub.
-
#delete_tag(repo_id:, tag_name:, repo_type: "model", timeout: nil) ⇒ Boolean
Delete a tag from a repository.
-
#duplicate_space(from_repo_id:, to_repo_id:, private: nil, organization: nil, timeout: nil) ⇒ String
Duplicate a Space repository.
-
#file_exists(repo_id:, path_in_repo:, repo_type: "model", revision: nil, timeout: nil) ⇒ Boolean
Check if a file exists in a repository on the HuggingFace Hub.
-
#get_hf_file_metadata(repo_id:, filename:, repo_type: "model", revision: nil, timeout: nil) ⇒ Hash
Get metadata about a file in a repository without downloading it.
-
#get_paths_info(repo_id:, paths:, repo_type: "model", revision: nil, timeout: nil) ⇒ Array<Hash>
Get metadata for multiple paths in a repository.
-
#initialize(token: nil, endpoint: nil) ⇒ HfApi
constructor
Initialize a new HfApi client.
-
#list_datasets(filter: nil, author: nil, search: nil, sort: nil, direction: nil, limit: nil, full: false, timeout: nil) ⇒ Array<DurableHuggingfaceHub::Types::DatasetInfo>
List datasets from the HuggingFace Hub with optional filtering.
-
#list_lfs_files(repo_id:, repo_type: "model", revision: nil, timeout: nil) ⇒ Array<Hash>
List Git LFS files in a repository.
-
#list_models(filter: nil, author: nil, search: nil, sort: nil, direction: nil, limit: nil, full: false, timeout: nil) ⇒ Array<DurableHuggingfaceHub::Types::ModelInfo>
List models from the HuggingFace Hub with optional filtering.
-
#list_repo_commits(repo_id:, repo_type: "model", revision: nil, limit: nil, timeout: nil) ⇒ Array<DurableHuggingfaceHub::Types::CommitInfo>
List commit history for a repository.
-
#list_repo_files(repo_id:, repo_type: "model", revision: nil, timeout: nil) ⇒ Array<String>
List files in a repository.
-
#list_repo_refs(repo_id:, repo_type: "model", timeout: nil) ⇒ Hash
List branches and tags (refs) for a repository.
-
#list_repo_tree(repo_id:, repo_type: "model", revision: nil, path: nil, recursive: false, timeout: nil) ⇒ Hash
List repository contents in a hierarchical tree structure.
-
#list_spaces(filter: nil, author: nil, search: nil, sort: nil, direction: nil, limit: nil, full: false, timeout: nil) ⇒ Array<DurableHuggingfaceHub::Types::SpaceInfo>
List spaces from the HuggingFace Hub with optional filtering.
-
#model_info(repo_id, revision: nil, timeout: nil) ⇒ DurableHuggingfaceHub::Types::ModelInfo
Get information about a specific model.
-
#move_repo(from_repo_id:, to_repo_id:, repo_type: "model", timeout: nil) ⇒ Boolean
Move or rename a repository.
-
#permanently_delete_lfs_files(repo_id:, lfs_oids:, repo_type: "model", timeout: nil) ⇒ Boolean
Permanently delete LFS files from a repository.
-
#repo_exists(repo_id, repo_type: "model", timeout: nil) ⇒ Boolean
Check if a repository exists on the HuggingFace Hub.
-
#repo_info(repo_id, repo_type: "model", revision: nil, timeout: nil) ⇒ DurableHuggingfaceHub::Types::ModelInfo, ...
Get comprehensive information about a repository.
-
#space_info(repo_id, revision: nil, timeout: nil) ⇒ DurableHuggingfaceHub::Types::SpaceInfo
Get information about a specific space.
-
#update_repo_settings(repo_id:, repo_type: "model", git_lfs_enabled: nil, protected: nil, unlisted: nil, tags: nil, default_branch: nil, timeout: nil) ⇒ Boolean
Update various settings for a repository.
-
#update_repo_visibility(repo_id:, repo_type: "model", private:, timeout: nil) ⇒ Boolean
Update the visibility of a repository (public/private).
-
#upload_file(repo_id:, path_or_fileobj:, path_in_repo:, repo_type: "model", revision: nil, commit_message: nil, commit_description: nil, timeout: nil) ⇒ String
Upload a file to a repository on the HuggingFace Hub.
-
#upload_folder(repo_id:, folder_path:, repo_type: "model", revision: nil, commit_message: nil, commit_description: nil, timeout: nil) ⇒ Array<String>
Upload an entire folder to a repository on the HuggingFace Hub.
-
#whoami(timeout: nil) ⇒ DurableHuggingfaceHub::Types::User
Get current user information (requires authentication).
Constructor Details
#initialize(token: nil, endpoint: nil) ⇒ HfApi
Initialize a new HfApi client
63 64 65 66 67 68 69 70 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 63 def initialize(token: nil, endpoint: nil) @token = token || DurableHuggingfaceHub::Utils::Auth.get_token @endpoint = endpoint || DurableHuggingfaceHub.configuration.endpoint @http_client = DurableHuggingfaceHub::Utils::HttpClient.new( endpoint: @endpoint, token: @token ) end |
Instance Attribute Details
#endpoint ⇒ String (readonly)
Returns Base endpoint URL for the HuggingFace Hub.
41 42 43 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 41 def endpoint @endpoint end |
#http_client ⇒ DurableHuggingfaceHub::Utils::HttpClient (readonly)
Returns HTTP client instance.
44 45 46 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 44 def http_client @http_client end |
#token ⇒ String? (readonly)
Returns Authentication token for API requests.
38 39 40 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 38 def token @token end |
Instance Method Details
#build_tree_structure(items) ⇒ Hash
Build a tree structure from the API response.
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 586 def build_tree_structure(items) tree = {} items.each do |item| path = item["path"] path_parts = path.split("/") # Navigate/create nested structure current = tree path_parts.each_with_index do |part, index| is_last = index == path_parts.length - 1 if is_last # This is a file current[part] = { type: "file", size: item["size"], oid: item["oid"], # SHA for Git LFS files lfs: item["lfs"] # LFS information if applicable }.compact else # This is a directory current[part] ||= { type: "directory", children: {} } current = current[part][:children] end end end tree end |
#create_branch(repo_id:, branch_name:, repo_type: "model", revision: nil, timeout: nil) ⇒ String
Create a new branch in a repository.
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 1055 def create_branch(repo_id:, branch_name:, repo_type: "model", revision: nil, timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) revision = DurableHuggingfaceHub::Utils::Validators.validate_revision(revision) if revision revision ||= "main" path = "/api/#{repo_type}s/#{repo_id}/branches" payload = { name: branch_name, revision: revision }.compact response = http_client.post(path, body: payload, timeout: timeout) body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body body["name"] end |
#create_commit(repo_id:, operations:, repo_type: "model", revision: nil, commit_message: nil, commit_description: nil, parent_commit: nil, timeout: nil) ⇒ String
Create a new commit with multiple file operations.
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 1016 def create_commit( repo_id:, operations:, repo_type: "model", revision: nil, commit_message: nil, commit_description: nil, parent_commit: nil, timeout: nil ) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) revision = DurableHuggingfaceHub::Utils::Validators.validate_revision(revision) if revision revision ||= "main" path = "/api/#{repo_type}s/#{repo_id}/commits/#{revision}" payload = { operations: operations, commit_message: || "Commit from Ruby client", commit_description: commit_description, parent_commit: parent_commit }.compact response = http_client.post(path, body: payload, timeout: timeout) body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body body["commit_id"] end |
#create_repo(repo_id:, repo_type: "model", private: false, organization: nil, timeout: nil) ⇒ String
Create a new repository on the HuggingFace Hub.
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 628 def create_repo(repo_id:, repo_type: "model", private: false, organization: nil, timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) # Extract organization from repo_id if not provided repo_parts = repo_id.split("/") if organization.nil? && repo_parts.length == 2 # Get current username to check if it's a personal repo current_user = whoami potential_org = repo_parts[0] # Only set organization if it's not the current user organization = potential_org unless potential_org == current_user.name end repo_name = repo_parts.last path = "/api/repos/create" payload = { name: repo_name, private: private } payload[:type] = repo_type if repo_type != "model" payload[:organization] = organization if organization response = http_client.post(path, body: payload, timeout: timeout) body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body body["url"] end |
#create_tag(repo_id:, tag_name:, repo_type: "model", revision: nil, message: nil, timeout: nil) ⇒ String
Create a new tag in a repository.
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 1104 def create_tag(repo_id:, tag_name:, repo_type: "model", revision: nil, message: nil, timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) revision = DurableHuggingfaceHub::Utils::Validators.validate_revision(revision) if revision revision ||= "main" path = "/api/#{repo_type}s/#{repo_id}/tags" payload = { name: tag_name, revision: revision, message: }.compact response = http_client.post(path, body: payload, timeout: timeout) body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body body["name"] end |
#dataset_info(repo_id, revision: nil, timeout: nil) ⇒ DurableHuggingfaceHub::Types::DatasetInfo
Get information about a specific dataset
This is a convenience method that calls #repo_info with repo_type: “dataset”.
193 194 195 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 193 def dataset_info(repo_id, revision: nil, timeout: nil) repo_info(repo_id, repo_type: "dataset", revision: revision, timeout: timeout) end |
#delete_branch(repo_id:, branch_name:, repo_type: "model", timeout: nil) ⇒ Boolean
Delete a branch from a repository.
1083 1084 1085 1086 1087 1088 1089 1090 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 1083 def delete_branch(repo_id:, branch_name:, repo_type: "model", timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) path = "/api/#{repo_type}s/#{repo_id}/branches/#{branch_name}" http_client.delete(path) true end |
#delete_file(repo_id:, path_in_repo:, repo_type: "model", revision: nil, commit_message: nil, commit_description: nil, timeout: nil) ⇒ Boolean
Delete a file from a repository on the HuggingFace Hub.
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 896 def delete_file( repo_id:, path_in_repo:, repo_type: "model", revision: nil, commit_message: nil, commit_description: nil, timeout: nil ) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) revision = DurableHuggingfaceHub::Utils::Validators.validate_revision(revision) if revision revision ||= "main" path = "/api/#{repo_type}s/#{repo_id}/upload/#{path_in_repo}" params = { commit_message: || "Delete #{path_in_repo}", commit_description: commit_description, revision: revision }.compact http_client.delete(path, params: params) true end |
#delete_folder(repo_id:, folder_path_in_repo:, repo_type: "model", revision: nil, commit_message: nil, commit_description: nil, timeout: nil) ⇒ Boolean
Delete an entire folder from a repository on the HuggingFace Hub.
This method lists all files within the specified folder in the repository and deletes them one by one.
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 937 def delete_folder( repo_id:, folder_path_in_repo:, repo_type: "model", revision: nil, commit_message: nil, commit_description: nil, timeout: nil ) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) revision = DurableHuggingfaceHub::Utils::Validators.validate_revision(revision) if revision revision ||= "main" # List all files in the folder files_to_delete = list_repo_files( repo_id: repo_id, repo_type: repo_type, revision: revision, timeout: timeout ).select { |file_path| file_path.start_with?(folder_path_in_repo) } # Delete each file files_to_delete.each do |file_path| delete_file( repo_id: repo_id, path_in_repo: file_path, repo_type: repo_type, revision: revision, commit_message: || "Delete #{file_path} from #{folder_path_in_repo}", commit_description: commit_description, timeout: timeout ) end true end |
#delete_repo(repo_id:, repo_type: "model", token: nil, timeout: nil) ⇒ Boolean
Delete a repository from the HuggingFace Hub.
666 667 668 669 670 671 672 673 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 666 def delete_repo(repo_id:, repo_type: "model", token: nil, timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) path = "/api/#{repo_type}s/#{repo_id}" http_client.delete(path) true end |
#delete_tag(repo_id:, tag_name:, repo_type: "model", timeout: nil) ⇒ Boolean
Delete a tag from a repository.
1133 1134 1135 1136 1137 1138 1139 1140 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 1133 def delete_tag(repo_id:, tag_name:, repo_type: "model", timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) path = "/api/#{repo_type}s/#{repo_id}/tags/#{tag_name}" http_client.delete(path) true end |
#duplicate_space(from_repo_id:, to_repo_id:, private: nil, organization: nil, timeout: nil) ⇒ String
Duplicate a Space repository.
768 769 770 771 772 773 774 775 776 777 778 779 780 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 768 def duplicate_space(from_repo_id:, to_repo_id:, private: nil, organization: nil, timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(from_repo_id) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(to_repo_id) path = "/api/spaces/#{from_repo_id}/duplicate" payload = { newPath: to_repo_id } payload[:private] = private unless private.nil? payload[:organization] = organization unless organization.nil? response = http_client.post(path, body: payload, timeout: timeout) body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body body["url"] end |
#file_exists(repo_id:, path_in_repo:, repo_type: "model", revision: nil, timeout: nil) ⇒ Boolean
Check if a file exists in a repository on the HuggingFace Hub.
985 986 987 988 989 990 991 992 993 994 995 996 997 998 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 985 def file_exists(repo_id:, path_in_repo:, repo_type: "model", revision: nil, timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) revision = DurableHuggingfaceHub::Utils::Validators.validate_revision(revision) if revision revision ||= "main" path = "/api/#{repo_type}s/#{repo_id}/resolve/#{revision}/#{path_in_repo}" begin http_client.head(path, timeout: timeout) true rescue DurableHuggingfaceHub::RepositoryNotFoundError, DurableHuggingfaceHub::EntryNotFoundError false end end |
#get_hf_file_metadata(repo_id:, filename:, repo_type: "model", revision: nil, timeout: nil) ⇒ Hash
Get metadata about a file in a repository without downloading it.
This method retrieves file metadata including size, ETag, and other information from the HuggingFace Hub API without downloading the actual file content.
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 490 def (repo_id:, filename:, repo_type: "model", revision: nil, timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) DurableHuggingfaceHub::Utils::Validators.validate_filename(filename) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) revision = DurableHuggingfaceHub::Utils::Validators.validate_revision(revision) if revision revision ||= "main" path = "/api/#{repo_type}s/#{repo_id}/resolve/#{revision}/#{filename}" begin response = http_client.head(path, timeout: timeout) # Extract metadata from response headers headers = response.headers { size: headers["x-linked-size"]&.to_i, etag: DurableHuggingfaceHub::FileDownload.extract_etag(headers["etag"] || headers["x-linked-etag"]), commit_hash: headers["x-repo-commit"], last_modified: headers["last-modified"] ? Time.parse(headers["last-modified"]) : nil, content_type: headers["content-type"], filename: filename, repo_id: repo_id, repo_type: repo_type, revision: revision }.compact rescue DurableHuggingfaceHub::EntryNotFoundError raise rescue DurableHuggingfaceHub::HfHubHTTPError => e # Convert 404 to EntryNotFoundError for consistency if e.status_code == 404 raise DurableHuggingfaceHub::EntryNotFoundError.new( "File #{filename} not found in #{repo_id}@#{revision}" ) end raise end end |
#get_paths_info(repo_id:, paths:, repo_type: "model", revision: nil, timeout: nil) ⇒ Array<Hash>
Get metadata for multiple paths in a repository.
This method efficiently retrieves metadata for multiple files or paths in a repository using batch requests where possible.
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 557 def get_paths_info(repo_id:, paths:, repo_type: "model", revision: nil, timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) raise ArgumentError, "paths must be an array" unless paths.is_a?(Array) paths.each { |path| DurableHuggingfaceHub::Utils::Validators.validate_filename(path) } repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) revision = DurableHuggingfaceHub::Utils::Validators.validate_revision(revision) if revision revision ||= "main" # For now, implement sequentially. In the future, this could be optimized # with concurrent requests or batch API calls if available. paths.map do |path| begin ( repo_id: repo_id, filename: path, repo_type: repo_type, revision: revision, timeout: timeout ) rescue DurableHuggingfaceHub::EntryNotFoundError nil # Return nil for missing files end end end |
#list_datasets(filter: nil, author: nil, search: nil, sort: nil, direction: nil, limit: nil, full: false, timeout: nil) ⇒ Array<DurableHuggingfaceHub::Types::DatasetInfo>
List datasets from the HuggingFace Hub with optional filtering
Returns a list of datasets matching the specified criteria.
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 305 def list_datasets(filter: nil, author: nil, search: nil, sort: nil, direction: nil, limit: nil, full: false, timeout: nil) path = "/api/datasets" params = build_list_params( filter: filter, author: , search: search, sort: sort, direction: direction, limit: limit, full: full ) response = http_client.get(path, params: params, timeout: timeout) body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body body.map { |dataset_data| DurableHuggingfaceHub::Types::DatasetInfo.from_hash(dataset_data) } end |
#list_lfs_files(repo_id:, repo_type: "model", revision: nil, timeout: nil) ⇒ Array<Hash>
List Git LFS files in a repository.
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 1216 def list_lfs_files(repo_id:, repo_type: "model", revision: nil, timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) revision = DurableHuggingfaceHub::Utils::Validators.validate_revision(revision) if revision revision ||= "main" path = "/api/#{repo_type}s/#{repo_id}/lfs/objects" params = { revision: revision }.compact response = http_client.get(path, params: params, timeout: timeout) response.body.is_a?(String) ? JSON.parse(response.body) : response.body end |
#list_models(filter: nil, author: nil, search: nil, sort: nil, direction: nil, limit: nil, full: false, timeout: nil) ⇒ Array<DurableHuggingfaceHub::Types::ModelInfo>
List models from the HuggingFace Hub with optional filtering
Returns a list of models matching the specified criteria. Results can be filtered by tags, author, search query, and sorted by various metrics.
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 268 def list_models(filter: nil, author: nil, search: nil, sort: nil, direction: nil, limit: nil, full: false, timeout: nil) path = "/api/models" params = build_list_params( filter: filter, author: , search: search, sort: sort, direction: direction, limit: limit, full: full ) response = http_client.get(path, params: params, timeout: timeout) # Response is an array of model objects body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body body.map { |model_data| DurableHuggingfaceHub::Types::ModelInfo.from_hash(model_data) } end |
#list_repo_commits(repo_id:, repo_type: "model", revision: nil, limit: nil, timeout: nil) ⇒ Array<DurableHuggingfaceHub::Types::CommitInfo>
List commit history for a repository.
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 1177 def list_repo_commits(repo_id:, repo_type: "model", revision: nil, limit: nil, timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) revision = DurableHuggingfaceHub::Utils::Validators.validate_revision(revision) if revision revision ||= "main" path = "/api/#{repo_type}s/#{repo_id}/commits" params = { revision: revision, limit: limit }.compact response = http_client.get(path, params: params, timeout: timeout) body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body body.map do |commit_data| # Flatten the nested commit structure commit = commit_data["commit"] = commit["author"] { oid: commit["id"], title: commit["message"]&.split("\n")&.first, # First line as title message: commit["message"], date: ? Time.parse(["date"]) : nil, authors: ? [["name"]] : nil }.compact end.map { |data| DurableHuggingfaceHub::Types::CommitInfo.from_hash(data) } end |
#list_repo_files(repo_id:, repo_type: "model", revision: nil, timeout: nil) ⇒ Array<String>
List files in a repository.
408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 408 def list_repo_files(repo_id:, repo_type: "model", revision: nil, timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) revision = DurableHuggingfaceHub::Utils::Validators.validate_revision(revision) if revision path = "/api/#{repo_type}s/#{repo_id}/tree" params = { recursive: true } params[:revision] = revision if revision response = http_client.get(path, params: params, timeout: timeout) body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body body.map { |file_data| file_data["path"] } end |
#list_repo_refs(repo_id:, repo_type: "model", timeout: nil) ⇒ Hash
List branches and tags (refs) for a repository.
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 1151 def list_repo_refs(repo_id:, repo_type: "model", timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) path = "/api/#{repo_type}s/#{repo_id}/refs" response = http_client.get(path, timeout: timeout) body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body branches = body["branches"].map { |branch_data| DurableHuggingfaceHub::Types::GitRefInfo.from_hash(branch_data) } = body["tags"].map { |tag_data| DurableHuggingfaceHub::Types::GitRefInfo.from_hash(tag_data) } { branches: branches, tags: } end |
#list_repo_tree(repo_id:, repo_type: "model", revision: nil, path: nil, recursive: false, timeout: nil) ⇒ Hash
List repository contents in a hierarchical tree structure.
This method provides a tree-like view of the repository contents, organized by directories and files with their metadata.
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 446 def list_repo_tree(repo_id:, repo_type: "model", revision: nil, path: nil, recursive: false, timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) revision = DurableHuggingfaceHub::Utils::Validators.validate_revision(revision) if revision revision ||= "main" # Build the API path api_path = "/api/#{repo_type}s/#{repo_id}/tree" api_path += "/#{path}" if path params = { recursive: recursive } params[:revision] = revision if revision response = http_client.get(api_path, params: params, timeout: timeout) # Organize the response into a tree structure body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body build_tree_structure(body) end |
#list_spaces(filter: nil, author: nil, search: nil, sort: nil, direction: nil, limit: nil, full: false, timeout: nil) ⇒ Array<DurableHuggingfaceHub::Types::SpaceInfo>
List spaces from the HuggingFace Hub with optional filtering
Returns a list of spaces matching the specified criteria.
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 340 def list_spaces(filter: nil, author: nil, search: nil, sort: nil, direction: nil, limit: nil, full: false, timeout: nil) path = "/api/spaces" params = build_list_params( filter: filter, author: , search: search, sort: sort, direction: direction, limit: limit, full: full ) response = http_client.get(path, params: params, timeout: timeout) body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body body.map { |space_data| DurableHuggingfaceHub::Types::SpaceInfo.from_hash(space_data) } end |
#model_info(repo_id, revision: nil, timeout: nil) ⇒ DurableHuggingfaceHub::Types::ModelInfo
Get information about a specific model
This is a convenience method that calls #repo_info with repo_type: “model”.
169 170 171 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 169 def model_info(repo_id, revision: nil, timeout: nil) repo_info(repo_id, repo_type: "model", revision: revision, timeout: timeout) end |
#move_repo(from_repo_id:, to_repo_id:, repo_type: "model", timeout: nil) ⇒ Boolean
Move or rename a repository.
745 746 747 748 749 750 751 752 753 754 755 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 745 def move_repo(from_repo_id:, to_repo_id:, repo_type: "model", timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(from_repo_id) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(to_repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) path = "/api/#{repo_type}s/#{from_repo_id}/move" payload = { newPath: to_repo_id } http_client.post(path, body: payload, timeout: timeout) true end |
#permanently_delete_lfs_files(repo_id:, lfs_oids:, repo_type: "model", timeout: nil) ⇒ Boolean
Permanently delete LFS files from a repository.
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 1239 def permanently_delete_lfs_files(repo_id:, lfs_oids:, repo_type: "model", timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) path = "/api/#{repo_type}s/#{repo_id}/lfs/delete" payload = { oids: lfs_oids } http_client.post(path, body: payload, timeout: timeout) true end |
#repo_exists(repo_id, repo_type: "model", timeout: nil) ⇒ Boolean
Check if a repository exists on the HuggingFace Hub
370 371 372 373 374 375 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 370 def repo_exists(repo_id, repo_type: "model", timeout: nil) repo_info(repo_id, repo_type: repo_type, timeout: timeout) true rescue DurableHuggingfaceHub::RepositoryNotFoundError false end |
#repo_info(repo_id, repo_type: "model", revision: nil, timeout: nil) ⇒ DurableHuggingfaceHub::Types::ModelInfo, ...
Get comprehensive information about a repository
This is a generic method that works for any repository type (model, dataset, or space). For type-specific methods, see #model_info, #dataset_info, or #space_info.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 105 def repo_info(repo_id, repo_type: "model", revision: nil, timeout: nil) # Validate inputs DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) if revision DurableHuggingfaceHub::Utils::Validators.validate_revision(revision) end # Build API path path = case repo_type.to_s when "model" "/api/models/#{repo_id}" when "dataset" "/api/datasets/#{repo_id}" when "space" "/api/spaces/#{repo_id}" else raise ArgumentError, "Invalid repo_type: #{repo_type}" end # Add revision if specified params = {} params[:revision] = revision if revision # Make request response = http_client.get(path, params: params, timeout: timeout) # Parse response based on repo_type body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body case repo_type.to_s when "model" DurableHuggingfaceHub::Types::ModelInfo.from_hash(body) when "dataset" DurableHuggingfaceHub::Types::DatasetInfo.from_hash(body) when "space" DurableHuggingfaceHub::Types::SpaceInfo.from_hash(body) end end |
#space_info(repo_id, revision: nil, timeout: nil) ⇒ DurableHuggingfaceHub::Types::SpaceInfo
Get information about a specific space
This is a convenience method that calls #repo_info with repo_type: “space”.
217 218 219 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 217 def space_info(repo_id, revision: nil, timeout: nil) repo_info(repo_id, repo_type: "space", revision: revision, timeout: timeout) end |
#update_repo_settings(repo_id:, repo_type: "model", git_lfs_enabled: nil, protected: nil, unlisted: nil, tags: nil, default_branch: nil, timeout: nil) ⇒ Boolean
Update various settings for a repository.
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 710 def update_repo_settings( repo_id:, repo_type: "model", git_lfs_enabled: nil, protected: nil, unlisted: nil, tags: nil, default_branch: nil, timeout: nil ) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) path = "/api/#{repo_type}s/#{repo_id}/settings" payload = {} payload[:git_lfs_enabled] = git_lfs_enabled unless git_lfs_enabled.nil? payload[:protected] = protected unless protected.nil? payload[:unlisted] = unlisted unless unlisted.nil? payload[:tags] = unless .nil? payload[:default_branch] = default_branch unless default_branch.nil? http_client.post(path, body: payload, timeout: timeout) true end |
#update_repo_visibility(repo_id:, repo_type: "model", private:, timeout: nil) ⇒ Boolean
Update the visibility of a repository (public/private).
685 686 687 688 689 690 691 692 693 694 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 685 def update_repo_visibility(repo_id:, repo_type: "model", private:, timeout: nil) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) path = "/api/#{repo_type}s/#{repo_id}/settings" payload = { private: private } http_client.post(path, body: payload, timeout: timeout) true end |
#upload_file(repo_id:, path_or_fileobj:, path_in_repo:, repo_type: "model", revision: nil, commit_message: nil, commit_description: nil, timeout: nil) ⇒ String
Upload a file to a repository on the HuggingFace Hub.
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 796 def upload_file( repo_id:, path_or_fileobj:, path_in_repo:, repo_type: "model", revision: nil, commit_message: nil, commit_description: nil, timeout: nil ) DurableHuggingfaceHub::Utils::Validators.validate_repo_id(repo_id) repo_type = DurableHuggingfaceHub::Utils::Validators.validate_repo_type(repo_type) revision = DurableHuggingfaceHub::Utils::Validators.validate_revision(revision) if revision revision ||= "main" path = "/api/#{repo_type}s/#{repo_id}/upload/#{path_in_repo}" params = { commit_message: || "Upload #{path_in_repo}", commit_description: commit_description, revision: revision }.compact file_content = if path_or_fileobj.is_a?(String) || path_or_fileobj.is_a?(Pathname) Faraday::Multipart::FilePart.new(path_or_fileobj, "application/octet-stream", Pathname(path_or_fileobj).basename.to_s) else # Assume IO object Faraday::Multipart::FilePart.new(path_or_fileobj, "application/octet-stream") end payload = { file: file_content } response = http_client.post(path, params: params, body: payload, timeout: timeout) body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body body["url"] end |
#upload_folder(repo_id:, folder_path:, repo_type: "model", revision: nil, commit_message: nil, commit_description: nil, timeout: nil) ⇒ Array<String>
Upload an entire folder to a repository on the HuggingFace Hub.
This method iterates through all files in a local folder and uploads them to the specified repository, maintaining the folder structure.
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 849 def upload_folder( repo_id:, folder_path:, repo_type: "model", revision: nil, commit_message: nil, commit_description: nil, timeout: nil ) folder_path = Pathname(folder_path) raise ArgumentError, "Folder not found: #{folder_path}" unless folder_path.directory? uploaded_urls = [] Dir.glob(File.join(folder_path, "**", "*")).each do |file_path_str| file_path = Pathname(file_path_str) next if file_path.directory? relative_path = file_path.relative_path_from(folder_path).to_s uploaded_urls << upload_file( repo_id: repo_id, path_or_fileobj: file_path, path_in_repo: relative_path, repo_type: repo_type, revision: revision, commit_message: , commit_description: commit_description, timeout: timeout ) end uploaded_urls end |
#whoami(timeout: nil) ⇒ DurableHuggingfaceHub::Types::User
Get current user information (requires authentication)
Returns information about the authenticated user. Requires a valid authentication token.
392 393 394 395 396 397 |
# File 'lib/durable_huggingface_hub/hf_api.rb', line 392 def whoami(timeout: nil) path = "/api/whoami-v2" response = http_client.get(path, timeout: timeout) body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body DurableHuggingfaceHub::Types::User.from_hash(body) end |