1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

use std::fmt::Debug;
use std::fmt::Formatter;
use std::sync::Arc;

use bytes::Buf;
use http::Response;
use http::StatusCode;
use log::debug;

use super::core::HuggingfaceCore;
use super::core::HuggingfaceStatus;
use super::error::parse_error;
use super::lister::HuggingfaceLister;
use crate::raw::*;
use crate::services::HuggingfaceConfig;
use crate::*;

impl Configurator for HuggingfaceConfig {
    type Builder = HuggingfaceBuilder;
    fn into_builder(self) -> Self::Builder {
        HuggingfaceBuilder { config: self }
    }
}

/// [Huggingface](https://huggingface.co/docs/huggingface_hub/package_reference/hf_api)'s API support.
#[doc = include_str!("docs.md")]
#[derive(Default, Clone)]
pub struct HuggingfaceBuilder {
    config: HuggingfaceConfig,
}

impl Debug for HuggingfaceBuilder {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let mut ds = f.debug_struct("Builder");

        ds.field("config", &self.config);
        ds.finish()
    }
}

impl HuggingfaceBuilder {
    /// Set repo type of this backend. Default is model.
    ///
    /// Available values:
    /// - model
    /// - dataset
    ///
    /// Currently, only models and datasets are supported.
    /// [Reference](https://huggingface.co/docs/hub/repositories)
    pub fn repo_type(mut self, repo_type: &str) -> Self {
        if !repo_type.is_empty() {
            self.config.repo_type = Some(repo_type.to_string());
        }
        self
    }

    /// Set repo id of this backend. This is required.
    ///
    /// Repo id consists of the account name and the repository name.
    ///
    /// For example, model's repo id looks like:
    /// - meta-llama/Llama-2-7b
    ///
    /// Dataset's repo id looks like:
    /// - databricks/databricks-dolly-15k
    pub fn repo_id(mut self, repo_id: &str) -> Self {
        if !repo_id.is_empty() {
            self.config.repo_id = Some(repo_id.to_string());
        }
        self
    }

    /// Set revision of this backend. Default is main.
    ///
    /// Revision can be a branch name or a commit hash.
    ///
    /// For example, revision can be:
    /// - main
    /// - 1d0c4eb
    pub fn revision(mut self, revision: &str) -> Self {
        if !revision.is_empty() {
            self.config.revision = Some(revision.to_string());
        }
        self
    }

    /// Set root of this backend.
    ///
    /// All operations will happen under this root.
    pub fn root(mut self, root: &str) -> Self {
        self.config.root = if root.is_empty() {
            None
        } else {
            Some(root.to_string())
        };

        self
    }

    /// Set the token of this backend.
    ///
    /// This is optional.
    pub fn token(mut self, token: &str) -> Self {
        if !token.is_empty() {
            self.config.token = Some(token.to_string());
        }
        self
    }
}

impl Builder for HuggingfaceBuilder {
    const SCHEME: Scheme = Scheme::Huggingface;
    type Config = HuggingfaceConfig;

    /// Build a HuggingfaceBackend.
    fn build(self) -> Result<impl Access> {
        debug!("backend build started: {:?}", &self);

        let repo_type = match self.config.repo_type.as_deref() {
            Some("model") => Ok(RepoType::Model),
            Some("dataset") => Ok(RepoType::Dataset),
            Some("space") => Err(Error::new(
                ErrorKind::ConfigInvalid,
                "repo type \"space\" is unsupported",
            )),
            Some(repo_type) => Err(Error::new(
                ErrorKind::ConfigInvalid,
                format!("unknown repo_type: {}", repo_type).as_str(),
            )
            .with_operation("Builder::build")
            .with_context("service", Scheme::Huggingface)),
            None => Ok(RepoType::Model),
        }?;
        debug!("backend use repo_type: {:?}", &repo_type);

        let repo_id = match &self.config.repo_id {
            Some(repo_id) => Ok(repo_id.clone()),
            None => Err(Error::new(ErrorKind::ConfigInvalid, "repo_id is empty")
                .with_operation("Builder::build")
                .with_context("service", Scheme::Huggingface)),
        }?;
        debug!("backend use repo_id: {}", &repo_id);

        let revision = match &self.config.revision {
            Some(revision) => revision.clone(),
            None => "main".to_string(),
        };
        debug!("backend use revision: {}", &revision);

        let root = normalize_root(&self.config.root.unwrap_or_default());
        debug!("backend use root: {}", &root);

        let token = self.config.token.as_ref().cloned();

        let client = HttpClient::new()?;

        Ok(HuggingfaceBackend {
            core: Arc::new(HuggingfaceCore {
                repo_type,
                repo_id,
                revision,
                root,
                token,
                client,
            }),
        })
    }
}

/// Backend for Huggingface service
#[derive(Debug, Clone)]
pub struct HuggingfaceBackend {
    core: Arc<HuggingfaceCore>,
}

impl Access for HuggingfaceBackend {
    type Reader = HttpBody;
    type Writer = ();
    type Lister = oio::PageLister<HuggingfaceLister>;
    type BlockingReader = ();
    type BlockingWriter = ();
    type BlockingLister = ();

    fn info(&self) -> Arc<AccessorInfo> {
        let mut am = AccessorInfo::default();
        am.set_scheme(Scheme::Huggingface)
            .set_native_capability(Capability {
                stat: true,

                read: true,

                list: true,
                list_with_recursive: true,

                shared: true,

                ..Default::default()
            });
        am.into()
    }

    async fn stat(&self, path: &str, _: OpStat) -> Result<RpStat> {
        // Stat root always returns a DIR.
        if path == "/" {
            return Ok(RpStat::new(Metadata::new(EntryMode::DIR)));
        }

        let resp = self.core.hf_path_info(path).await?;

        let status = resp.status();

        match status {
            StatusCode::OK => {
                let mut meta = parse_into_metadata(path, resp.headers())?;
                let bs = resp.into_body();

                let decoded_response: Vec<HuggingfaceStatus> =
                    serde_json::from_reader(bs.reader()).map_err(new_json_deserialize_error)?;

                // NOTE: if the file is not found, the server will return 200 with an empty array
                if let Some(status) = decoded_response.first() {
                    if let Some(commit_info) = status.last_commit.as_ref() {
                        meta.set_last_modified(parse_datetime_from_rfc3339(
                            commit_info.date.as_str(),
                        )?);
                    }

                    meta.set_content_length(status.size);

                    match status.type_.as_str() {
                        "directory" => meta.set_mode(EntryMode::DIR),
                        "file" => meta.set_mode(EntryMode::FILE),
                        _ => return Err(Error::new(ErrorKind::Unexpected, "unknown status type")),
                    };
                } else {
                    return Err(Error::new(ErrorKind::NotFound, "path not found"));
                }

                Ok(RpStat::new(meta))
            }
            _ => Err(parse_error(resp)),
        }
    }

    async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
        let resp = self.core.hf_resolve(path, args.range(), &args).await?;

        let status = resp.status();

        match status {
            StatusCode::OK | StatusCode::PARTIAL_CONTENT => {
                Ok((RpRead::default(), resp.into_body()))
            }
            _ => {
                let (part, mut body) = resp.into_parts();
                let buf = body.to_buffer().await?;
                Err(parse_error(Response::from_parts(part, buf)))
            }
        }
    }

    async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
        let l = HuggingfaceLister::new(self.core.clone(), path.to_string(), args.recursive());

        Ok((RpList::default(), oio::PageLister::new(l)))
    }
}

/// Repository type of Huggingface. Currently, we only support `model` and `dataset`.
/// [Reference](https://huggingface.co/docs/hub/repositories)
#[derive(Debug, Clone, Copy)]
pub enum RepoType {
    Model,
    Dataset,
}