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
55/// Global response format of Hugging Face Hub API
56#[derive(Debug, Clone, Deserialize)]
57#[serde(untagged)]
58pub enum HuggingFaceRes<T> {
59  Success(T),
60  Failure(FailureRes),
61}
62
63impl<T> HuggingFaceRes<T> {
64  /// Unwrap data from [`HuggingFaceRes::Success`]
65  pub fn unwrap_data(self) -> Result<T> {
66    match self {
67      HuggingFaceRes::Success(v) => Ok(v),
68      HuggingFaceRes::Failure(f) => Err(
69        HuggingFaceResponseSnafu {
70          message: f.error().to_string(),
71        }
72        .build(),
73      ),
74    }
75  }
76}
77
78/// Error response format of Hugging Face Hub API
79#[derive(Debug, Clone, Deserialize)]
80pub struct FailureRes {
81  error: String,
82}
83
84impl FailureRes {
85  /// Get error message
86  pub fn error(&self) -> &str {
87    &self.error
88  }
89}
90
91#[derive(Debug, Default, Serialize)]
92pub struct SearchReq<'a> {
93  #[serde(skip_serializing_if = "Option::is_none")]
94  search: Option<&'a str>,
95
96  #[serde(skip_serializing_if = "Option::is_none")]
97  author: Option<&'a str>,
98
99  #[serde(skip_serializing_if = "Option::is_none")]
100  filter: Option<&'a str>,
101
102  #[serde(skip_serializing_if = "Option::is_none")]
103  sort: Option<&'a str>,
104
105  #[serde(skip_serializing_if = "Option::is_none")]
106  direction: Option<i32>,
107
108  #[serde(skip_serializing_if = "Option::is_none")]
109  limit: Option<usize>,
110
111  #[serde(skip_serializing_if = "Option::is_none")]
112  full: Option<bool>,
113
114  #[serde(skip_serializing_if = "Option::is_none")]
115  config: Option<bool>,
116}
117
118impl<'a> SearchReq<'a> {
119  pub fn search(mut self, search: &'a str) -> Self {
120    self.search = Some(search);
121    self
122  }
123
124  pub fn author(mut self, author: &'a str) -> Self {
125    self.author = Some(author);
126    self
127  }
128
129  pub fn filter(mut self, filter: &'a str) -> Self {
130    self.filter = Some(filter);
131    self
132  }
133
134  pub fn sort(mut self, sort: &'a str) -> Self {
135    self.sort = Some(sort);
136    self
137  }
138
139  pub fn direction(mut self, direction: i32) -> Self {
140    self.direction = Some(direction);
141    self
142  }
143
144  pub fn limit(mut self, limit: usize) -> Self {
145    self.limit = Some(limit);
146    self
147  }
148
149  pub fn full(mut self, full: bool) -> Self {
150    self.full = Some(full);
151    self
152  }
153
154  pub fn config(mut self, config: bool) -> Self {
155    self.config = Some(config);
156    self
157  }
158}