You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "AnandInguva (via GitHub)" <gi...@apache.org> on 2024/01/27 04:52:59 UTC
[PR] [Testing]Add InferenceAPIEmbeddings to huggingface [beam]
AnandInguva opened a new pull request, #30138:
URL: https://github.com/apache/beam/pull/30138
**Please** add a meaningful description for your change here
------------------------
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
- [ ] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
- [ ] Update `CHANGES.md` with noteworthy changes.
- [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://github.com/apache/beam/blob/master/CONTRIBUTING.md#make-the-reviewers-job-easier).
To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
GitHub Actions Tests Status (on master branch)
------------------------------------------------------------------------------------------------
[](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI or the [workflows README](https://github.com/apache/beam/blob/master/.github/workflows/README.md) to see a list of phrases to trigger workflows.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add InferenceAPIEmbeddings to huggingface [beam]
Posted by "damccorm (via GitHub)" <gi...@apache.org>.
damccorm commented on PR #30138:
URL: https://github.com/apache/beam/pull/30138#issuecomment-1919642118
I confirmed that the secret is set, so that is at least not the problem
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add InferenceAPIEmbeddings to huggingface [beam]
Posted by "damccorm (via GitHub)" <gi...@apache.org>.
damccorm commented on code in PR #30138:
URL: https://github.com/apache/beam/pull/30138#discussion_r1470022638
##########
sdks/python/apache_beam/ml/transforms/embeddings/huggingface_test.py:
##########
@@ -44,6 +45,7 @@
except ImportError:
tft = None
+_HF_TOKEN = os.environ.get('HF_INFERENCE_TOKEN')
Review Comment:
Can we add this to our actions yaml file so that we verify this runs?
##########
sdks/python/apache_beam/ml/transforms/embeddings/huggingface.py:
##########
@@ -130,6 +142,88 @@ def get_model_handler(self):
def get_ptransform_for_processing(self, **kwargs) -> beam.PTransform:
# wrap the model handler in a _TextEmbeddingHandler since
# the SentenceTransformerEmbeddings works on text input data.
- return RunInference(
- model_handler=_TextEmbeddingHandler(self),
- inference_args=self.inference_args)
+ return (
+ RunInference(
+ model_handler=_TextEmbeddingHandler(self),
+ inference_args=self.inference_args,
+ ))
+
+
+class _InferenceAPIHandler(ModelHandler):
+ def __init__(self, config: 'InferenceAPIEmbeddings'):
+ super().__init__()
+ self._config = config
+
+ def load_model(self):
+ session = requests.Session()
+ session.headers.update(self._config.authorization_token)
+ return session
+
+ def run_inference(
+ self, batch, session: requests.Session, inference_args=None):
+ response = session.post(
+ self._config.api_url,
+ headers=self._config.authorization_token,
+ json={
+ "inputs": batch, "options": inference_args
+ })
+ return response.json()
+
+
+class InferenceAPIEmbeddings(EmbeddingsManager):
+ def __init__(
+ self,
+ hf_token: str,
+ columns: List[str],
+ model_name: Optional[str] = None, # example: "sentence-transformers/all-MiniLM-l6-v2" # pylint: disable=line-too-long
+ api_url: Optional[str] = None,
+ **kwargs,
+ ):
+ """
+ Feature extraction using HuggingFace's Inference API.
+ Intended to be used for feature-extraction. For other tasks, please
+ refer to https://huggingface.co/inference-api.
+ Args:
+ hf_token: HuggingFace token.
+ columns: List of columns to be embedded.
+ model_name: Model name used for feature extraction.
+ api_url: API url for feature extraction. If specified, model_name will be
+ ignored. If none, the default url
+ https://api-inference.huggingface.co/pipeline/feature-extraction/{model_name} # pylint: disable=line-too-long
+ will be used.
+
+ """
+ super().__init__(columns, **kwargs)
+ self._api_url = api_url
+ self._authorization_token = {"Authorization": f"Bearer {hf_token}"}
+ self._model_name = model_name
Review Comment:
We should probably do some validation that exactly one of model_name and api_url are set here instead of in api_url so that this throws at construction time instead of runtime
##########
sdks/python/apache_beam/ml/transforms/embeddings/huggingface.py:
##########
@@ -130,6 +142,88 @@ def get_model_handler(self):
def get_ptransform_for_processing(self, **kwargs) -> beam.PTransform:
# wrap the model handler in a _TextEmbeddingHandler since
# the SentenceTransformerEmbeddings works on text input data.
- return RunInference(
- model_handler=_TextEmbeddingHandler(self),
- inference_args=self.inference_args)
+ return (
+ RunInference(
+ model_handler=_TextEmbeddingHandler(self),
+ inference_args=self.inference_args,
+ ))
+
+
+class _InferenceAPIHandler(ModelHandler):
+ def __init__(self, config: 'InferenceAPIEmbeddings'):
+ super().__init__()
+ self._config = config
+
+ def load_model(self):
+ session = requests.Session()
+ session.headers.update(self._config.authorization_token)
+ return session
+
+ def run_inference(
+ self, batch, session: requests.Session, inference_args=None):
+ response = session.post(
+ self._config.api_url,
+ headers=self._config.authorization_token,
+ json={
+ "inputs": batch, "options": inference_args
+ })
+ return response.json()
+
+
+class InferenceAPIEmbeddings(EmbeddingsManager):
+ def __init__(
+ self,
+ hf_token: str,
Review Comment:
Should we make this optional? It looks like this kind of authorization can also be set in the environment - https://huggingface.co/docs/huggingface_hub/package_reference/environment_variables#hftoken - we could try to pull the token using that env variable if this isn't set. The downside is that it makes it a runtime error, but it would probably be a more robust/secure approach if users have custom containers.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add InferenceAPIEmbeddings to huggingface [beam]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #30138:
URL: https://github.com/apache/beam/pull/30138#issuecomment-1915657581
Assigning reviewers. If you would like to opt out of this review, comment `assign to next reviewer`:
R: @tvalentyn for label python.
R: @damccorm for label build.
Available commands:
- `stop reviewer notifications` - opt out of the automated review tooling
- `remind me after tests pass` - tag the comment author after tests pass
- `waiting on author` - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)
The PR bot will only process comments in the main thread (not review comments).
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add InferenceAPIEmbeddings to huggingface [beam]
Posted by "AnandInguva (via GitHub)" <gi...@apache.org>.
AnandInguva commented on PR #30138:
URL: https://github.com/apache/beam/pull/30138#issuecomment-1917427190
https://github.com/apache/beam/actions/runs/7714314294 - kicked off a test to verify the hf inference token
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add InferenceAPIEmbeddings to huggingface [beam]
Posted by "AnandInguva (via GitHub)" <gi...@apache.org>.
AnandInguva commented on code in PR #30138:
URL: https://github.com/apache/beam/pull/30138#discussion_r1471561180
##########
sdks/python/apache_beam/ml/transforms/embeddings/huggingface_test.py:
##########
@@ -44,6 +45,7 @@
except ImportError:
tft = None
+_HF_TOKEN = os.environ.get('HF_INFERENCE_TOKEN')
Review Comment:
I have added this to the python precommit Coverage actions file in the PR.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Testing]Add InferenceAPIEmbeddings to huggingface [beam]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #30138:
URL: https://github.com/apache/beam/pull/30138#issuecomment-1913010400
Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment `assign set of reviewers`
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add InferenceAPIEmbeddings to huggingface [beam]
Posted by "AnandInguva (via GitHub)" <gi...@apache.org>.
AnandInguva merged PR #30138:
URL: https://github.com/apache/beam/pull/30138
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add InferenceAPIEmbeddings to huggingface [beam]
Posted by "AnandInguva (via GitHub)" <gi...@apache.org>.
AnandInguva commented on PR #30138:
URL: https://github.com/apache/beam/pull/30138#issuecomment-1930221741
Actualluy, the tests are passing. I was looking at the wrong run all the 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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add InferenceAPIEmbeddings to huggingface [beam]
Posted by "AnandInguva (via GitHub)" <gi...@apache.org>.
AnandInguva commented on code in PR #30138:
URL: https://github.com/apache/beam/pull/30138#discussion_r1473230698
##########
sdks/python/apache_beam/ml/transforms/embeddings/huggingface.py:
##########
@@ -130,6 +142,88 @@ def get_model_handler(self):
def get_ptransform_for_processing(self, **kwargs) -> beam.PTransform:
# wrap the model handler in a _TextEmbeddingHandler since
# the SentenceTransformerEmbeddings works on text input data.
- return RunInference(
- model_handler=_TextEmbeddingHandler(self),
- inference_args=self.inference_args)
+ return (
+ RunInference(
+ model_handler=_TextEmbeddingHandler(self),
+ inference_args=self.inference_args,
+ ))
+
+
+class _InferenceAPIHandler(ModelHandler):
+ def __init__(self, config: 'InferenceAPIEmbeddings'):
+ super().__init__()
+ self._config = config
+
+ def load_model(self):
+ session = requests.Session()
+ session.headers.update(self._config.authorization_token)
+ return session
+
+ def run_inference(
+ self, batch, session: requests.Session, inference_args=None):
+ response = session.post(
+ self._config.api_url,
+ headers=self._config.authorization_token,
+ json={
+ "inputs": batch, "options": inference_args
+ })
+ return response.json()
+
+
+class InferenceAPIEmbeddings(EmbeddingsManager):
+ def __init__(
+ self,
+ hf_token: str,
+ columns: List[str],
+ model_name: Optional[str] = None, # example: "sentence-transformers/all-MiniLM-l6-v2" # pylint: disable=line-too-long
+ api_url: Optional[str] = None,
+ **kwargs,
+ ):
+ """
+ Feature extraction using HuggingFace's Inference API.
+ Intended to be used for feature-extraction. For other tasks, please
+ refer to https://huggingface.co/inference-api.
+ Args:
+ hf_token: HuggingFace token.
+ columns: List of columns to be embedded.
+ model_name: Model name used for feature extraction.
+ api_url: API url for feature extraction. If specified, model_name will be
+ ignored. If none, the default url
+ https://api-inference.huggingface.co/pipeline/feature-extraction/{model_name} # pylint: disable=line-too-long
+ will be used.
+
+ """
+ super().__init__(columns, **kwargs)
+ self._api_url = api_url
+ self._authorization_token = {"Authorization": f"Bearer {hf_token}"}
+ self._model_name = model_name
Review Comment:
Done
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add InferenceAPIEmbeddings to huggingface [beam]
Posted by "damccorm (via GitHub)" <gi...@apache.org>.
damccorm commented on PR #30138:
URL: https://github.com/apache/beam/pull/30138#issuecomment-1932570338
Oh, looks like docs precommit is failing
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add InferenceAPIEmbeddings to huggingface [beam]
Posted by "AnandInguva (via GitHub)" <gi...@apache.org>.
AnandInguva commented on code in PR #30138:
URL: https://github.com/apache/beam/pull/30138#discussion_r1473248495
##########
sdks/python/apache_beam/ml/transforms/embeddings/huggingface.py:
##########
@@ -130,6 +142,88 @@ def get_model_handler(self):
def get_ptransform_for_processing(self, **kwargs) -> beam.PTransform:
# wrap the model handler in a _TextEmbeddingHandler since
# the SentenceTransformerEmbeddings works on text input data.
- return RunInference(
- model_handler=_TextEmbeddingHandler(self),
- inference_args=self.inference_args)
+ return (
+ RunInference(
+ model_handler=_TextEmbeddingHandler(self),
+ inference_args=self.inference_args,
+ ))
+
+
+class _InferenceAPIHandler(ModelHandler):
+ def __init__(self, config: 'InferenceAPIEmbeddings'):
+ super().__init__()
+ self._config = config
+
+ def load_model(self):
+ session = requests.Session()
+ session.headers.update(self._config.authorization_token)
+ return session
+
+ def run_inference(
+ self, batch, session: requests.Session, inference_args=None):
+ response = session.post(
+ self._config.api_url,
+ headers=self._config.authorization_token,
+ json={
+ "inputs": batch, "options": inference_args
+ })
+ return response.json()
+
+
+class InferenceAPIEmbeddings(EmbeddingsManager):
+ def __init__(
+ self,
+ hf_token: str,
Review Comment:
Done
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add InferenceAPIEmbeddings to huggingface [beam]
Posted by "AnandInguva (via GitHub)" <gi...@apache.org>.
AnandInguva commented on PR #30138:
URL: https://github.com/apache/beam/pull/30138#issuecomment-1930277748
https://github.com/apache/beam/actions/runs/7802974816 - latest run. @damccorm this is ready for another round.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add InferenceAPIEmbeddings to huggingface [beam]
Posted by "damccorm (via GitHub)" <gi...@apache.org>.
damccorm commented on PR #30138:
URL: https://github.com/apache/beam/pull/30138#issuecomment-1919665194
I also confirmed that the token gets plumbed to at least the environment that we call the gradle command from - so I don't think this is an actions set up issue.
Have you tried running `:sdks:python:test-suites:tox:py38:preCommitPyCoverage` with the token set in the environment to see if it works locally? Or you could probably start with `testPy38CloudCoverage` instead
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add InferenceAPIEmbeddings to huggingface [beam]
Posted by "AnandInguva (via GitHub)" <gi...@apache.org>.
AnandInguva commented on PR #30138:
URL: https://github.com/apache/beam/pull/30138#issuecomment-1930133254
Building sdist on my cloudtop is taking longer than expected (it took 2 hours which was weird). I will try today and run the precommit locally.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add InferenceAPIEmbeddings to huggingface [beam]
Posted by "AnandInguva (via GitHub)" <gi...@apache.org>.
AnandInguva commented on PR #30138:
URL: https://github.com/apache/beam/pull/30138#issuecomment-1930205513
sdks:python:test-suites:tox:py38:testPy38CloudCoverage - this passes locally with inference api tests not getting skipped.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add InferenceAPIEmbeddings to huggingface [beam]
Posted by "AnandInguva (via GitHub)" <gi...@apache.org>.
AnandInguva commented on PR #30138:
URL: https://github.com/apache/beam/pull/30138#issuecomment-1932585823
> Oh, looks like docs precommit is failing
hm weird it worked locally. Let me see if i pushed the changes.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org