ParagEkbote commented on code in PR #76317: URL: https://github.com/apache/airflow/pull/76317#discussion_r4496103092
########## providers/huggingface/src/airflow/providers/huggingface/hooks/huggingface.py: ########## @@ -0,0 +1,156 @@ +# 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. +"""Connection and client helpers for Hugging Face services.""" +from __future__ import annotations + +from typing import Any + +from datasets import load_dataset +from huggingface_hub import HfApi +from huggingface_hub.utils import RepositoryNotFoundError, RevisionNotFoundError + +from airflow.exceptions import AirflowNotFoundException +from airflow.providers.common.compat.sdk import BaseHook + + +class HuggingFaceHook(BaseHook): + """Access Hugging Face Datasets and Hub APIs using an Airflow connection. + + If the connection is absent, huggingface_hub may use ``HF_TOKEN`` or a + cached worker login. + """ + + conn_name_attr = "huggingface_conn_id" + default_conn_name = "huggingface_default" + conn_type = "huggingface" + hook_name = "Hugging Face" + + def __init__(self, huggingface_conn_id: str | None = default_conn_name) -> None: + super().__init__() + self.huggingface_conn_id = huggingface_conn_id + self._allow_ambient_fallback = huggingface_conn_id in (None, self.default_conn_name) + self._client: HfApi | None = None + + def get_token(self) -> str | None: + if not self.huggingface_conn_id: + return None + try: + connection = self.get_connection(self.huggingface_conn_id) + except AirflowNotFoundException: + if self._allow_ambient_fallback: + return None + raise + return connection.password or None Review Comment: Fixed this edge issue and updated the docs to mention `token=False` and added a note about passing a token as hf hub for better rate limit. ########## providers/huggingface/src/airflow/providers/huggingface/hooks/huggingface.py: ########## @@ -0,0 +1,156 @@ +# 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. +"""Connection and client helpers for Hugging Face services.""" +from __future__ import annotations + +from typing import Any + +from datasets import load_dataset +from huggingface_hub import HfApi +from huggingface_hub.utils import RepositoryNotFoundError, RevisionNotFoundError + +from airflow.exceptions import AirflowNotFoundException +from airflow.providers.common.compat.sdk import BaseHook + + +class HuggingFaceHook(BaseHook): + """Access Hugging Face Datasets and Hub APIs using an Airflow connection. + + If the connection is absent, huggingface_hub may use ``HF_TOKEN`` or a + cached worker login. + """ + + conn_name_attr = "huggingface_conn_id" + default_conn_name = "huggingface_default" + conn_type = "huggingface" + hook_name = "Hugging Face" + + def __init__(self, huggingface_conn_id: str | None = default_conn_name) -> None: + super().__init__() + self.huggingface_conn_id = huggingface_conn_id + self._allow_ambient_fallback = huggingface_conn_id in (None, self.default_conn_name) + self._client: HfApi | None = None + + def get_token(self) -> str | None: + if not self.huggingface_conn_id: + return None + try: + connection = self.get_connection(self.huggingface_conn_id) + except AirflowNotFoundException: + if self._allow_ambient_fallback: + return None + raise + return connection.password or None Review Comment: Fixed this edge issue and updated the docs to mention `token=False` and added a note about passing a token as hf hub for better rate limits. -- 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]
