lhoestq commented on code in PR #6668:
URL: https://github.com/apache/opendal/pull/6668#discussion_r2511123595


##########
core/src/services/huggingface/config.rs:
##########
@@ -77,7 +77,122 @@ impl Debug for HuggingfaceConfig {
 
 impl crate::Configurator for HuggingfaceConfig {
     type Builder = HuggingfaceBuilder;
+
+    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
+        let mut map = uri.options().clone();
+
+        if let Some(repo_type) = uri.name() {
+            if !repo_type.is_empty() {
+                map.insert("repo_type".to_string(), repo_type.to_string());
+            }
+        }
+
+        let raw_path = uri.root().ok_or_else(|| {
+            crate::Error::new(
+                crate::ErrorKind::ConfigInvalid,
+                "uri path must include owner and repo",
+            )
+            .with_context("service", crate::Scheme::Huggingface)
+        })?;
+
+        let mut segments = raw_path.splitn(4, '/');
+        let owner = segments.next().filter(|s| !s.is_empty()).ok_or_else(|| {
+            crate::Error::new(
+                crate::ErrorKind::ConfigInvalid,
+                "repository owner is required in uri path",
+            )
+            .with_context("service", crate::Scheme::Huggingface)
+        })?;
+        let repo = segments.next().filter(|s| !s.is_empty()).ok_or_else(|| {
+            crate::Error::new(
+                crate::ErrorKind::ConfigInvalid,
+                "repository name is required in uri path",
+            )
+            .with_context("service", crate::Scheme::Huggingface)
+        })?;
+
+        map.insert("repo_id".to_string(), format!("{owner}/{repo}"));
+
+        if let Some(segment) = segments.next() {
+            if map.contains_key("revision") {
+                let mut root_value = segment.to_string();
+                if let Some(rest) = segments.next() {
+                    if !rest.is_empty() {
+                        if !root_value.is_empty() {
+                            root_value.push('/');
+                            root_value.push_str(rest);
+                        } else {
+                            root_value = rest.to_string();
+                        }
+                    }
+                }
+                if !root_value.is_empty() {
+                    map.insert("root".to_string(), root_value);
+                }
+            } else {
+                if !segment.is_empty() {
+                    map.insert("revision".to_string(), segment.to_string());
+                }
+                if let Some(rest) = segments.next() {
+                    if !rest.is_empty() {
+                        map.insert("root".to_string(), rest.to_string());
+                    }
+                }
+            }
+        }
+
+        Self::from_iter(map)
+    }
+
     fn into_builder(self) -> Self::Builder {
         HuggingfaceBuilder { config: self }
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use crate::Configurator;
+    use crate::types::OperatorUri;
+
+    #[test]
+    fn from_uri_sets_repo_type_id_and_revision() {
+        let uri = OperatorUri::new(
+            "huggingface://model/opendal/sample/main/dataset",

Review Comment:
   Hi ! Just a heads up that HF uses `hf://{owner}/{repo}/{path_in_repo}`, it 
would be cool to follow this :)
   
   As a reminder, if there is a revision it should be 
`hf://{owner}/{repo}@{revision}/{path_in_repo}`
   
   and for datasets and spaces it's respectively 
`hf://datasets/{owner}/{repo}/{path_in_repo}` and 
`hf://spaces/{owner}/{repo}/{path_in_repo}`



-- 
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]

Reply via email to