Xuanwo commented on code in PR #3670: URL: https://github.com/apache/incubator-opendal/pull/3670#discussion_r1405774415
########## core/src/services/huggingface/backend.rs: ########## @@ -0,0 +1,313 @@ +// 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::collections::HashMap; +use std::fmt::Debug; +use std::fmt::Formatter; +use std::sync::Arc; + +use async_trait::async_trait; +use http::StatusCode; +use log::debug; + +use super::core::HuggingFaceCore; +use super::error::parse_error; +use super::lister::HuggingFaceLister; +use super::message::HuggingFaceStatus; +use crate::raw::*; +use crate::*; + +/// [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 { + repo_type: Option<String>, + repo_id: Option<String>, + revision: Option<String>, + root: Option<String>, + read_token: Option<String>, +} + +impl Debug for HuggingFaceBuilder { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("Builder"); + + ds.field("repo_type", &self.repo_type); + ds.field("repo_id", &self.repo_id); + ds.field("revision", &self.revision); + ds.field("root", &self.root); + + if self.read_token.is_some() { + ds.field("read_token", &"<redacted>"); + } + + 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) -> &mut Self { + if !repo_type.is_empty() { + self.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) -> &mut Self { + if !repo_id.is_empty() { + self.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) -> &mut Self { + if !revision.is_empty() { + self.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) -> &mut Self { + if !root.is_empty() { + self.root = Some(root.to_string()); + } + self + } + + /// Set the read token of this backend. + /// + /// This is optional. + pub fn read_token(&mut self, read_token: &str) -> &mut Self { + if !read_token.is_empty() { + self.read_token = Some(read_token.to_string()); + } + self + } +} + +impl Builder for HuggingFaceBuilder { + const SCHEME: Scheme = Scheme::HuggingFace; + type Accessor = HuggingFaceBackend; + + fn from_map(map: HashMap<String, String>) -> Self { + let mut builder = HuggingFaceBuilder::default(); + + map.get("repo_type").map(|v| builder.repo_type(v)); + map.get("repo_id").map(|v| builder.repo_id(v)); + map.get("revision").map(|v| builder.revision(v)); + map.get("root").map(|v| builder.root(v)); + map.get("read_token").map(|v| builder.read_token(v)); + + builder + } + + /// Build a HuggingFaceBackend. + fn build(&mut self) -> Result<Self::Accessor> { + debug!("backend build started: {:?}", &self); + + let repo_type = match &self.repo_type { Review Comment: How about writing in this way: ```rust match self.repo_type.as_deref() { Some("model") => .., Some("dataset") => .., Some("space") => .., _ => ... } ``` ########## core/src/services/huggingface/backend.rs: ########## @@ -0,0 +1,313 @@ +// 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::collections::HashMap; +use std::fmt::Debug; +use std::fmt::Formatter; +use std::sync::Arc; + +use async_trait::async_trait; +use http::StatusCode; +use log::debug; + +use super::core::HuggingFaceCore; +use super::error::parse_error; +use super::lister::HuggingFaceLister; +use super::message::HuggingFaceStatus; +use crate::raw::*; +use crate::*; + +/// [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 { + repo_type: Option<String>, + repo_id: Option<String>, + revision: Option<String>, + root: Option<String>, + read_token: Option<String>, +} + +impl Debug for HuggingFaceBuilder { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("Builder"); + + ds.field("repo_type", &self.repo_type); + ds.field("repo_id", &self.repo_id); + ds.field("revision", &self.revision); + ds.field("root", &self.root); + + if self.read_token.is_some() { + ds.field("read_token", &"<redacted>"); + } + + 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) -> &mut Self { + if !repo_type.is_empty() { + self.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) -> &mut Self { + if !repo_id.is_empty() { + self.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) -> &mut Self { + if !revision.is_empty() { + self.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) -> &mut Self { + if !root.is_empty() { + self.root = Some(root.to_string()); + } + self + } + + /// Set the read token of this backend. + /// + /// This is optional. Review Comment: How about naming it `token`? Will hf have different token types? And maybe we should provide more comment for this, like what's the benefits to fill if token? ########## core/src/services/huggingface/core.rs: ########## @@ -0,0 +1,182 @@ +// 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 bytes::Bytes; +use http::Request; +use http::Response; +use http::{header, StatusCode}; +use serde_json::json; + +use super::backend::RepoType; +use super::error::parse_error; +use crate::raw::*; +use crate::*; + +pub struct HuggingFaceCore { + pub repo_type: RepoType, + pub repo_id: String, + pub revision: String, + pub root: String, + pub read_token: Option<String>, + + pub client: HttpClient, +} + +impl Debug for HuggingFaceCore { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("HuggingFaceCore") + .field("repo_type", &self.repo_type) + .field("repo_id", &self.repo_id) + .field("revision", &self.revision) + .field("root", &self.root) + .finish_non_exhaustive() + } +} + +impl HuggingFaceCore { + pub async fn hf_path_info(&self, path: &str) -> Result<Response<IncomingAsyncBody>> { + let p = build_abs_path(&self.root, path) + .trim_end_matches('/') + .to_string(); + + let url = match self.repo_type { + RepoType::Model => format!( + "https://huggingface.co/api/models/{}/paths-info/{}", Review Comment: Do we really need to parse `repo_type` to `RepoType`? How about using it in url directly? ########## core/src/services/huggingface/mod.rs: ########## @@ -0,0 +1,24 @@ +// 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. + +mod backend; +pub use backend::HuggingFaceBuilder as HuggingFace; Review Comment: If we want to make it name after `HuggingFace`, we should use `hugging_face` as it scheme. Please make sure they are consistent: - `HuggingFace`, `hugging_face`, `hugging-face` - `Huggingface`, `huggingface` I prefer to use the second one. ########## core/src/services/huggingface/backend.rs: ########## @@ -0,0 +1,313 @@ +// 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::collections::HashMap; +use std::fmt::Debug; +use std::fmt::Formatter; +use std::sync::Arc; + +use async_trait::async_trait; +use http::StatusCode; +use log::debug; + +use super::core::HuggingFaceCore; +use super::error::parse_error; +use super::lister::HuggingFaceLister; +use super::message::HuggingFaceStatus; +use crate::raw::*; +use crate::*; + +/// [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 { + repo_type: Option<String>, + repo_id: Option<String>, + revision: Option<String>, + root: Option<String>, + read_token: Option<String>, +} + +impl Debug for HuggingFaceBuilder { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let mut ds = f.debug_struct("Builder"); + + ds.field("repo_type", &self.repo_type); + ds.field("repo_id", &self.repo_id); + ds.field("revision", &self.revision); + ds.field("root", &self.root); + + if self.read_token.is_some() { + ds.field("read_token", &"<redacted>"); + } + + 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) -> &mut Self { + if !repo_type.is_empty() { + self.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) -> &mut Self { + if !repo_id.is_empty() { + self.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) -> &mut Self { + if !revision.is_empty() { + self.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) -> &mut Self { + if !root.is_empty() { + self.root = Some(root.to_string()); + } + self + } + + /// Set the read token of this backend. + /// + /// This is optional. + pub fn read_token(&mut self, read_token: &str) -> &mut Self { + if !read_token.is_empty() { + self.read_token = Some(read_token.to_string()); + } + self + } +} + +impl Builder for HuggingFaceBuilder { + const SCHEME: Scheme = Scheme::HuggingFace; + type Accessor = HuggingFaceBackend; + + fn from_map(map: HashMap<String, String>) -> Self { Review Comment: Please use `ConfigDeserializer`, take s3 as an example. ########## core/src/services/huggingface/core.rs: ########## @@ -0,0 +1,182 @@ +// 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 bytes::Bytes; +use http::Request; +use http::Response; +use http::{header, StatusCode}; +use serde_json::json; + +use super::backend::RepoType; +use super::error::parse_error; +use crate::raw::*; +use crate::*; + +pub struct HuggingFaceCore { + pub repo_type: RepoType, + pub repo_id: String, + pub revision: String, + pub root: String, + pub read_token: Option<String>, + + pub client: HttpClient, +} + +impl Debug for HuggingFaceCore { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("HuggingFaceCore") + .field("repo_type", &self.repo_type) + .field("repo_id", &self.repo_id) + .field("revision", &self.revision) + .field("root", &self.root) + .finish_non_exhaustive() + } +} + +impl HuggingFaceCore { + pub async fn hf_path_info(&self, path: &str) -> Result<Response<IncomingAsyncBody>> { + let p = build_abs_path(&self.root, path) + .trim_end_matches('/') + .to_string(); + + let url = match self.repo_type { + RepoType::Model => format!( + "https://huggingface.co/api/models/{}/paths-info/{}", + &self.repo_id, &self.revision + ), + RepoType::Dataset => format!( + "https://huggingface.co/api/datasets/{}/paths-info/{}", + &self.repo_id, &self.revision + ), + }; + + let mut req = Request::post(&url); + + if let Some(token) = &self.read_token { + let auth_header_content = format!("Bearer {}", token); + req = req.header(header::AUTHORIZATION, auth_header_content); + } + + req = req.header(header::CONTENT_TYPE, "application/x-www-form-urlencoded"); + + let path = percent_encode_path(&p); + let req_body = &json!({ + "paths": path, + "expand": "True", + }); + + // NOTE: We need to transfer body to x-www-form-urlencoded format. + let req_body = + serde_urlencoded::to_string(&req_body).expect("failed to encode request body"); Review Comment: Can we avoid introducing `serde_urlencoded`? ########## core/src/services/huggingface/message.rs: ########## Review Comment: We can move all those types into `core.rs` ########## core/src/services/huggingface/message.rs: ########## @@ -0,0 +1,401 @@ +// 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. + +//! HuggingFace response messages + +use serde::Deserialize; + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +#[allow(dead_code)] +pub(super) struct HuggingFaceStatus { + #[serde(rename = "type")] + pub type_: String, + pub oid: String, + pub size: u64, + pub lfs: Option<HuggingFaceLfs>, + pub path: String, + pub last_commit: Option<HuggingFaceLastCommit>, + pub security: Option<HuggingFaceSecurity>, +} + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +#[allow(dead_code)] +pub(super) struct HuggingFaceLfs { + pub oid: String, + pub size: u64, + pub pointer_size: u64, +} + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +#[allow(dead_code)] +pub(super) struct HuggingFaceLastCommit { + pub id: String, + pub title: String, + pub date: String, +} + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +#[allow(dead_code)] +pub(super) struct HuggingFaceSecurity { + pub blob_id: String, + pub name: String, + pub safe: bool, + pub av_scan: Option<HuggingFaceAvScan>, + pub pickle_import_scan: Option<HuggingFacePickleImportScan>, +} + +#[derive(Deserialize)] +#[allow(dead_code)] +#[serde(rename_all = "camelCase")] +pub(super) struct HuggingFaceAvScan { + pub virus_found: bool, + pub virus_names: Option<Vec<String>>, +} + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +#[allow(dead_code)] +pub(super) struct HuggingFacePickleImportScan { + pub highest_safety_level: String, + pub imports: Vec<HuggingFaceImport>, +} + +#[derive(Deserialize)] +#[allow(dead_code)] +pub(super) struct HuggingFaceImport { + pub module: String, + pub name: String, + pub safety: String, +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::raw::new_json_deserialize_error; + use crate::types::Result; + use bytes::Bytes; + + #[test] + fn parse_list_response_test() -> Result<()> { + let resp = Bytes::from( + r#" + [ + { + "type": "file", + "oid": "45fa7c3d85ee7dd4139adbc056da25ae136a65f2", + "size": 69512435, + "lfs": { + "oid": "b43f4c2ea569da1d66ca74e26ca8ea4430dfc29195e97144b2d0b4f3f6cafa1c", + "size": 69512435, + "pointerSize": 133 + }, + "path": "maelstrom/lib/maelstrom.jar" + }, + { + "type": "directory", + "oid": "b43f4c2ea569da1d66ca74e26ca8ea4430dfc29195e97144b2d0b4f3f6cafa1c", + "size": 69512435, + "path": "maelstrom/lib/plugins" + } + ] + "#, + ); + + let decoded_response = serde_json::from_slice::<Vec<HuggingFaceStatus>>(&resp) + .map_err(new_json_deserialize_error)?; + + assert_eq!(decoded_response.len(), 2); + + assert_eq!(decoded_response[0].type_, "file"); + assert_eq!( + decoded_response[0].oid, + "45fa7c3d85ee7dd4139adbc056da25ae136a65f2" + ); + assert_eq!(decoded_response[0].size, 69512435); + assert_eq!(decoded_response[0].path, "maelstrom/lib/maelstrom.jar"); + + assert_eq!( + decoded_response[0].lfs.as_ref().unwrap().oid, + "b43f4c2ea569da1d66ca74e26ca8ea4430dfc29195e97144b2d0b4f3f6cafa1c" + ); + assert_eq!(decoded_response[0].lfs.as_ref().unwrap().size, 69512435); + assert_eq!(decoded_response[0].lfs.as_ref().unwrap().pointer_size, 133); + + assert_eq!(decoded_response[1].type_, "directory"); + assert_eq!( + decoded_response[1].oid, + "b43f4c2ea569da1d66ca74e26ca8ea4430dfc29195e97144b2d0b4f3f6cafa1c" + ); + assert_eq!(decoded_response[1].size, 69512435); + assert_eq!(decoded_response[1].path, "maelstrom/lib/plugins"); + + Ok(()) + } + + #[test] + fn parse_files_info_test() -> Result<()> { + let resp = Bytes::from( + r#" + [ + { + "type": "file", + "oid": "45fa7c3d85ee7dd4139adbc056da25ae136a65f2", + "size": 69512435, + "lfs": { + "oid": "b43f4c2ea569da1d66ca74e26ca8ea4430dfc29195e97144b2d0b4f3f6cafa1c", + "size": 69512435, + "pointerSize": 133 + }, + "path": "maelstrom/lib/maelstrom.jar", + "lastCommit": { + "id": "bc1ef030bf3743290d5e190695ab94582e51ae2f", + "title": "Upload 141 files", + "date": "2023-11-17T23:50:28.000Z" + }, + "security": { + "blobId": "45fa7c3d85ee7dd4139adbc056da25ae136a65f2", + "name": "maelstrom/lib/maelstrom.jar", + "safe": true, + "avScan": { + "virusFound": false, + "virusNames": null + }, + "pickleImportScan": { + "highestSafetyLevel": "innocuous", + "imports": [ + {"module": "torch", "name": "FloatStorage", "safety": "innocuous"}, + {"module": "collections", "name": "OrderedDict", "safety": "innocuous"}, + {"module": "torch", "name": "LongStorage", "safety": "innocuous"}, + {"module": "torch._utils", "name": "_rebuild_tensor_v2", "safety": "innocuous"} + ] + } + } + } + ] + "#, + ); + + let decoded_response = serde_json::from_slice::<Vec<HuggingFaceStatus>>(&resp) + .map_err(new_json_deserialize_error)?; + + assert_eq!(decoded_response.len(), 1); + + assert_eq!(decoded_response[0].type_, "file"); + assert_eq!( + decoded_response[0].oid, + "45fa7c3d85ee7dd4139adbc056da25ae136a65f2" + ); + assert_eq!(decoded_response[0].size, 69512435); + assert_eq!(decoded_response[0].path, "maelstrom/lib/maelstrom.jar"); + + assert_eq!( + decoded_response[0].lfs.as_ref().unwrap().oid, + "b43f4c2ea569da1d66ca74e26ca8ea4430dfc29195e97144b2d0b4f3f6cafa1c" + ); + + assert_eq!(decoded_response[0].lfs.as_ref().unwrap().size, 69512435); + assert_eq!(decoded_response[0].lfs.as_ref().unwrap().pointer_size, 133); + + assert_eq!( + decoded_response[0].last_commit.as_ref().unwrap().id, + "bc1ef030bf3743290d5e190695ab94582e51ae2f" + ); + assert_eq!( + decoded_response[0].last_commit.as_ref().unwrap().title, + "Upload 141 files" + ); + assert_eq!( + decoded_response[0].last_commit.as_ref().unwrap().date, + "2023-11-17T23:50:28.000Z" + ); + + assert_eq!( + decoded_response[0].security.as_ref().unwrap().blob_id, + "45fa7c3d85ee7dd4139adbc056da25ae136a65f2" + ); + assert_eq!( + decoded_response[0].security.as_ref().unwrap().name, + "maelstrom/lib/maelstrom.jar" + ); + assert_eq!(decoded_response[0].security.as_ref().unwrap().safe, true); + + assert_eq!( Review Comment: We can implement `Eq` for those types, so we can `assert_eq!(decoded_response[0], TypeAbc)` directly. ########## core/src/services/huggingface/core.rs: ########## @@ -0,0 +1,182 @@ +// 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 bytes::Bytes; +use http::Request; +use http::Response; +use http::{header, StatusCode}; +use serde_json::json; + +use super::backend::RepoType; +use super::error::parse_error; +use crate::raw::*; +use crate::*; + +pub struct HuggingFaceCore { + pub repo_type: RepoType, + pub repo_id: String, + pub revision: String, + pub root: String, + pub read_token: Option<String>, + + pub client: HttpClient, +} + +impl Debug for HuggingFaceCore { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("HuggingFaceCore") + .field("repo_type", &self.repo_type) + .field("repo_id", &self.repo_id) + .field("revision", &self.revision) + .field("root", &self.root) + .finish_non_exhaustive() + } +} + +impl HuggingFaceCore { + pub async fn hf_path_info(&self, path: &str) -> Result<Response<IncomingAsyncBody>> { + let p = build_abs_path(&self.root, path) + .trim_end_matches('/') + .to_string(); + + let url = match self.repo_type { + RepoType::Model => format!( + "https://huggingface.co/api/models/{}/paths-info/{}", + &self.repo_id, &self.revision + ), + RepoType::Dataset => format!( + "https://huggingface.co/api/datasets/{}/paths-info/{}", + &self.repo_id, &self.revision + ), + }; + + let mut req = Request::post(&url); + + if let Some(token) = &self.read_token { + let auth_header_content = format!("Bearer {}", token); Review Comment: Please use `format_authorization_by_bearer` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
