-
Notifications
You must be signed in to change notification settings - Fork 688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[spark] Add text2text generation #2506
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
extensions/spark/setup/djl_spark/task/text/text2text_generator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python | ||
# | ||
# Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file | ||
# except in compliance with the License. A copy of the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" | ||
# BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the License for | ||
# the specific language governing permissions and limitations under the License. | ||
|
||
import pandas as pd | ||
from pyspark.sql.functions import pandas_udf | ||
from pyspark.sql.types import StringType | ||
from typing import Iterator | ||
from transformers import pipeline | ||
|
||
|
||
class Text2TextGenerator: | ||
|
||
def __init__(self, input_col, output_col, engine, model_url=None, model_name=None): | ||
""" | ||
Initializes the Text2TextGenerator. | ||
|
||
:param input_col: The input column | ||
:param output_col: The output column | ||
:param engine: The engine. Currently only PyTorch is supported. | ||
:param model_url: The model URL | ||
:param model_name: The model name | ||
""" | ||
self.input_col = input_col | ||
self.output_col = output_col | ||
self.engine = engine | ||
self.model_url = model_url | ||
self.model_name = model_name | ||
|
||
def generate(self, dataset, **kwargs): | ||
""" | ||
Performs text2text generation on the provided dataset. | ||
|
||
:param dataset: input dataset | ||
:return: output dataset | ||
""" | ||
if not self.model_url and not self.model_name: | ||
raise ValueError("Either model_url or model_name must be provided.") | ||
model_name_or_url = self.model_url if self.model_url else self.model_name | ||
|
||
@pandas_udf(StringType()) | ||
def predict_udf(iterator: Iterator[pd.Series]) -> Iterator[pd.Series]: | ||
generator = pipeline('text2text-generation', model=model_name_or_url, **kwargs) | ||
for s in iterator: | ||
output = generator(s.tolist()) | ||
text = [o["generated_text"] for o in output] | ||
yield pd.Series(text) | ||
|
||
return dataset.withColumn(self.output_col, predict_udf(self.input_col)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe check for not both
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like:
if not any([self.model_url, self.model_name])
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically xor. Must have one or the other but not both.
bool(self.model_url) ^ bool(self.model_name)
should work.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was assuming both should be fine. Just model_url has higher priority than model_name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is fine, but not great. For cases like this, I tend to ask whether a user would expect that this is the behavior of the API. If they did provide both, would they expect that priority? Instead, an error is clearer because having both properties when only a single one is used already means something is not right. The error will highlight that for the user and ensure they can clarify the situation rather then risk behaving in ways that they don't expect