hugging_face_client/api/
mod.rs

1//! Request and response
2//!
3//! Hugging Face has open endpoints that you can use to retrieve information from the Hub as well as perform
4//! certain actions such as creating model, dataset or Space repos.
5
6use serde::{Deserialize, Serialize};
7
8use crate::errors::{HuggingFaceResponseSnafu, Result};
9
10mod search_models;
11pub use search_models::{SearchModelReq, SearchModelRes};
12
13mod get_model;
14pub use get_model::{GetModelReq, GetModelRes};
15
16mod create_repo;
17pub use create_repo::{CreateRepoReq, CreateRepoRes};
18
19mod delete_repo;
20pub use delete_repo::DeleteRepoReq;
21
22mod get_model_tags;
23pub use get_model_tags::GetModelTagsRes;
24
25mod search_datasets;
26pub use search_datasets::{SearchDatasetReq, SearchDatasetRes};
27
28mod get_dataset;
29pub use get_dataset::{GetDatasetReq, GetDatasetRes};
30
31mod get_dataset_tags;
32pub use get_dataset_tags::GetDatasetTagRes;
33
34mod get_space;
35pub use get_space::{GetSpaceReq, GetSpaceRes};
36
37mod search_spaces;
38pub use search_spaces::{SearchSpaceReq, SearchSpaceRes};
39
40mod move_repo;
41pub use move_repo::MoveRepoReq;
42
43mod get_metrics;
44pub use get_metrics::GetMetricsRes;
45
46mod arxiv_paper;
47pub use arxiv_paper::{ArxivPaperReq, ArxivPaperRes};
48
49mod arxiv_repos;
50pub use arxiv_repos::{ArxivReposReq, ArxivReposRes};
51
52mod arxiv_daily;
53pub use arxiv_daily::ArxivDailyRes;
54
55mod get_members;
56pub use get_members::{GetMembersReq, GetMembersRes};
57
58mod get_collections;
59pub use get_collections::{GetCollectionsReq, GetCollectionsRes};
60
61mod get_collection;
62pub use get_collection::{GetCollectionReq, GetCollectionRes};
63
64mod create_collection;
65pub use create_collection::CreateCollectionReq;
66
67mod create_collection_item;
68pub use create_collection_item::{CreateCollectionItemReq, CreateCollectionItemRes};
69
70mod delete_collection;
71pub use delete_collection::{DeleteCollectionReq, DeleteCollectionRes};
72
73mod modify_collection;
74pub use modify_collection::{ModifyCollectionReq, ModifyCollectionRes};
75
76mod modify_collection_item;
77pub use modify_collection_item::{ModifyCollectionItemReq, ModifyCollectionItemRes};
78
79mod delete_collection_item;
80pub use delete_collection_item::{DeleteCollectionItemReq, DeleteCollectionItemRes};
81
82mod get_userinfo;
83pub use get_userinfo::GetUserInfoRes;
84
85mod get_parquet;
86pub use get_parquet::{GetParquetReq, GetParquetRes};
87
88mod download_parquet;
89pub use download_parquet::{DownloadParquetReq, DownloadParquetRes};
90
91/// Global response format of Hugging Face Hub API
92#[derive(Debug, Clone, Deserialize)]
93#[serde(untagged)]
94pub enum HuggingFaceRes<T> {
95  Success(T),
96  Failure(FailureRes),
97}
98
99impl<T> HuggingFaceRes<T> {
100  /// Unwrap data from [`HuggingFaceRes::Success`]
101  pub fn unwrap_data(self) -> Result<T> {
102    match self {
103      HuggingFaceRes::Success(v) => Ok(v),
104      HuggingFaceRes::Failure(f) => Err(
105        HuggingFaceResponseSnafu {
106          message: f.error().to_string(),
107        }
108        .build(),
109      ),
110    }
111  }
112}
113
114/// Error response format of Hugging Face Hub API
115#[derive(Debug, Clone, Deserialize)]
116pub struct FailureRes {
117  error: String,
118}
119
120impl FailureRes {
121  /// Get error message
122  pub fn error(&self) -> &str {
123    &self.error
124  }
125}
126
127#[derive(Debug, Default, Serialize)]
128pub struct SearchReq<'a> {
129  #[serde(skip_serializing_if = "Option::is_none")]
130  search: Option<&'a str>,
131
132  #[serde(skip_serializing_if = "Option::is_none")]
133  author: Option<&'a str>,
134
135  #[serde(skip_serializing_if = "Option::is_none")]
136  filter: Option<&'a str>,
137
138  #[serde(skip_serializing_if = "Option::is_none")]
139  sort: Option<&'a str>,
140
141  #[serde(skip_serializing_if = "Option::is_none")]
142  direction: Option<i32>,
143
144  #[serde(skip_serializing_if = "Option::is_none")]
145  limit: Option<usize>,
146
147  #[serde(skip_serializing_if = "Option::is_none")]
148  full: Option<bool>,
149
150  #[serde(skip_serializing_if = "Option::is_none")]
151  config: Option<bool>,
152}
153
154impl<'a> SearchReq<'a> {
155  pub fn search(mut self, search: &'a str) -> Self {
156    self.search = Some(search);
157    self
158  }
159
160  pub fn author(mut self, author: &'a str) -> Self {
161    self.author = Some(author);
162    self
163  }
164
165  pub fn filter(mut self, filter: &'a str) -> Self {
166    self.filter = Some(filter);
167    self
168  }
169
170  pub fn sort(mut self, sort: &'a str) -> Self {
171    self.sort = Some(sort);
172    self
173  }
174
175  pub fn direction(mut self, direction: i32) -> Self {
176    self.direction = Some(direction);
177    self
178  }
179
180  pub fn limit(mut self, limit: usize) -> Self {
181    self.limit = Some(limit);
182    self
183  }
184
185  pub fn full(mut self, full: bool) -> Self {
186    self.full = Some(full);
187    self
188  }
189
190  pub fn config(mut self, config: bool) -> Self {
191    self.config = Some(config);
192    self
193  }
194}