morristai commented on code in PR #3670: URL: https://github.com/apache/incubator-opendal/pull/3670#discussion_r1410109392
########## 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: Done, I will be more careful next time. 😂 ########## 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: Done, I will be more careful next time. 😂 -- 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]
