hugging_face_client/client/
repo.rs

1use bytes::Bytes;
2use futures_core::Stream;
3use reqwest::Method;
4
5use crate::{
6  api::{
7    CreateRepoReq, CreateRepoRes, DeleteRepoReq, DownloadParquetReq, DownloadParquetRes,
8    GetDatasetReq, GetDatasetRes, GetDatasetTagRes, GetMetricsRes, GetModelReq, GetModelRes,
9    GetModelTagsRes, GetParquetReq, GetParquetRes, GetSpaceReq, GetSpaceRes, MoveRepoReq,
10    SearchDatasetReq, SearchDatasetRes, SearchModelReq, SearchModelRes, SearchSpaceReq,
11    SearchSpaceRes,
12  },
13  client::Client,
14  errors::Result,
15};
16
17impl Client {
18  /// Get information from all models in the Hub
19  ///
20  /// Endpoint: `GET /api/models`
21  pub async fn search_model(&self, req: SearchModelReq<'_>) -> Result<SearchModelRes> {
22    let url = format!("{}/api/models", &self.api_endpoint);
23    self.get_request(&url, Some(&req), true).await
24  }
25
26  /// Get all information for a specific model
27  ///
28  /// Endpoint: `GET /api/models/{repo_id}` or
29  ///
30  /// Endpoint: `GET /api/models/{repo_id}/revision/{revision}`
31  pub async fn get_model(&self, req: GetModelReq<'_>) -> Result<GetModelRes> {
32    let url = if let Some(revision) = req.revision {
33      format!(
34        "{}/api/models/{}/revision/{}",
35        &self.api_endpoint, req.repo_name, revision
36      )
37    } else {
38      format!("{}/api/models/{}", &self.api_endpoint, req.repo_name)
39    };
40    self.get_request(&url, self.empty_req(), true).await
41  }
42
43  /// Gets all the available model tags hosted in the Hub
44  ///
45  /// Endpoint: `GET /api/models-tags-by-type`
46  pub async fn get_model_tags(&self) -> Result<GetModelTagsRes> {
47    let url = format!("{}/api/models-tags-by-type", &self.api_endpoint);
48    self.get_request(&url, self.empty_req(), false).await
49  }
50
51  /// Get information from all datasets in the Hub
52  ///
53  /// Endpoint: ` GET /api/datasets`
54  pub async fn search_dataset(&self, req: SearchDatasetReq<'_>) -> Result<SearchDatasetRes> {
55    let url = format!("{}/api/datasets", &self.api_endpoint);
56    self.get_request(&url, Some(&req), true).await
57  }
58
59  /// Get all information for a specific dataset
60  ///
61  /// Endpoint: `GET /api/datasets/{repo_id}` or
62  ///
63  /// Endpoint: `GET /api/datasets/{repo_id}/revision/{revision}`
64  pub async fn get_dataset(&self, req: GetDatasetReq<'_>) -> Result<GetDatasetRes> {
65    let url = if let Some(revision) = req.revision {
66      format!(
67        "{}/api/datasets/{}/revision/{}",
68        &self.api_endpoint, req.repo_name, revision
69      )
70    } else {
71      format!("{}/api/datasets/{}", &self.api_endpoint, req.repo_name)
72    };
73    self.get_request(&url, self.empty_req(), true).await
74  }
75
76  /// Gets all the available dataset tags hosted in the Hub
77  ///
78  /// Endpoint: `GET /api/datasets-tags-by-type`
79  pub async fn get_dataset_tags(&self) -> Result<GetDatasetTagRes> {
80    let url = format!("{}/api/models-tags-by-type", &self.api_endpoint);
81    self.get_request(&url, self.empty_req(), false).await
82  }
83
84  /// Get information from all spaces in the Hub
85  ///
86  /// Endpoint: ` GET /api/spaces`
87  pub async fn search_space(&self, req: SearchSpaceReq<'_>) -> Result<SearchSpaceRes> {
88    let url = format!("{}/api/spaces", &self.api_endpoint);
89    self.get_request(&url, Some(&req), true).await
90  }
91
92  /// Get all information for a specific space
93  ///
94  /// Endpoint: `GET /api/spaces/{repo_id}` or
95  ///
96  /// Endpoint: `GET /api/spaces/{repo_id}/revision/{revision}`
97  pub async fn get_space(&self, req: GetSpaceReq<'_>) -> Result<GetSpaceRes> {
98    let url = if let Some(revision) = req.revision {
99      format!(
100        "{}/api/spaces/{}/revision/{}",
101        &self.api_endpoint, req.repo_name, revision
102      )
103    } else {
104      format!("{}/api/spaces/{}", &self.api_endpoint, req.repo_name)
105    };
106    self.get_request(&url, self.empty_req(), true).await
107  }
108
109  /// Create a repository, model repo by default
110  ///
111  /// Endpoint:  POST /api/repos/create
112  pub async fn create_repo(&self, req: CreateRepoReq<'_>) -> Result<CreateRepoRes> {
113    let url = format!("{}/api/repos/create", &self.api_endpoint);
114    self.exec_request(&url, Method::POST, Some(&req)).await
115  }
116
117  /// Delete a repository, model repo by default
118  ///
119  /// Endpoint: `DELETE /api/repos/delete`
120  pub async fn delete_repo(&self, req: DeleteRepoReq<'_>) -> Result<()> {
121    let url = format!("{}/api/repos/delete", &self.api_endpoint);
122    self
123      .exec_request_without_response(&url, Method::DELETE, Some(&req))
124      .await
125  }
126
127  /// Move a repository (rename within the same namespace or transfer from user to organization).
128  ///
129  /// Endpoint: ` POST /api/repos/move`
130  pub async fn move_repo(&self, req: MoveRepoReq<'_>) -> Result<()> {
131    let url = format!("{}/api/repos/move", &self.api_endpoint);
132    self
133      .exec_request_without_response(&url, Method::POST, Some(&req))
134      .await
135  }
136
137  /// Gets all the available metrics in the Hub
138  ///
139  /// Endpoint: `GET /api/metrics`
140  pub async fn get_metrics(&self) -> Result<GetMetricsRes> {
141    let url = format!("{}/api/metrics", &self.api_endpoint);
142    self.get_request(&url, self.empty_req(), false).await
143  }
144
145  /// Get the list of auto-converted parquet files
146  ///
147  /// Endpoint: `GET /api/datasets/{repo_id}/parquet`
148  ///
149  /// Endpoint: `GET /api/datasets/{repo_id}/parquet/{subset}`
150  ///
151  /// Endpoint: `GET /api/datasets/{repo_id}/parquet/{subset}/{split}`
152  pub async fn get_parquet(&self, req: GetParquetReq<'_>) -> Result<GetParquetRes> {
153    let url = match (req.subset, req.split) {
154      (None, None) | (None, Some(_)) => format!(
155        "{}/api/datasets/{}/parquet",
156        &self.api_endpoint, req.repo_name
157      ),
158      (Some(subset), None) => format!(
159        "{}/api/datasets/{}/parquet/{}",
160        &self.api_endpoint, req.repo_name, subset
161      ),
162      (Some(subset), Some(split)) => format!(
163        "{}/api/datasets/{}/parquet/{}/{}",
164        &self.api_endpoint, req.repo_name, subset, split
165      ),
166    };
167    self.get_request(&url, self.empty_req(), false).await
168  }
169
170  /// Get the nth shard of the auto-converted parquet files, for a specific subset (also called
171  /// “config”) and split.
172  ///
173  /// Endpoint: ` GET /api/datasets/{repo_id}/parquet/{subset}/{split}/{n}.parquet`
174  pub async fn download_parquet(
175    &self,
176    req: DownloadParquetReq<'_>,
177  ) -> impl Stream<Item = Result<Bytes>> {
178    let url = format!(
179      "{}/api/datasets/{}/parquet/{}/{}/{}.parquet",
180      &self.api_endpoint, req.repo_name, req.subset, req.split, req.nth
181    );
182    let stream = self
183      .http_client
184      .get(url)
185      .bearer_auth(&self.access_token)
186      .send()
187      .await
188      .unwrap()
189      .bytes_stream();
190    DownloadParquetRes::new(stream)
191  }
192
193  ///  Get the nth shard of the auto-converted parquet files, same as [`Client::download_parquet`]
194  ///
195  /// `url`: full url of parquet file, you can get this from [`Client::get_parquet`],
196  /// this is an example: `https://huggingface.co/api/datasets/DMindAI/DMind_Benchmark/parquet/objective_infrastructure/Infrastructrue/0.parquet`
197  pub async fn download_parquet_by_url(
198    &self,
199    url: impl AsRef<str>,
200  ) -> impl Stream<Item = Result<Bytes>> {
201    let stream = self
202      .http_client
203      .get(url.as_ref())
204      .bearer_auth(&self.access_token)
205      .send()
206      .await
207      .unwrap()
208      .bytes_stream();
209    DownloadParquetRes::new(stream)
210  }
211}