Skip to content

Commit 4f36c15

Browse files
kz930claude
andauthored
feat(operator): export the estimators and the Hugging Face models (apache#8368)
### What changes were proposed in this PR? The scikit-learn estimators are fitted on one port and score on the other, so the script holds both frames and narrows each by the same rule: a fit and a score taken on different columns would compare two different models. The four Hugging Face models declare the column types they take, and the iris regression keeps the row when a petal measurement is empty rather than ending the run on it. Sklearn Prediction and Sklearn Testing each consume a fitted model on an input port. The verification fits one in Python and hands the same model to both paths, so they are run both ways like the rest. Review also turned up three defects that this PR fixes rather than mirrors. `SklearnTesting` squeezed the feature frame before predicting, so a single feature column, or the single row a drop left behind, reached scikit-learn as one dimension and was rejected. That was the executor's own behaviour and the export had copied it, so both paths pass the frame itself now. Separately, the three HuggingFace text operators guarded a missing cell with `is None`. Arrow normalizes an empty cell to null, so that held for the executor as long as the column really was a string, but `attributeTypeRules` is a warning the editor prints rather than a filter, and the exported script reads a frame where a column holding nothing else comes back as float64. Both paths ask pandas now. Third, the iris regression's engine code checked a missing measurement with `is None`, so a NaN went on to the model and got a prediction the export leaves empty. It now asks `pd.isna` too, as the export does. ### Any related issues, documentation, discussions? Part of apache#8325, 20 of 27; that issue lists the set in order. It needs apache#8327 for the trait, so it does not compile until that lands, and the rows these operators add to the verification runner follow with the harness rather than as whole new files here. Closes apache#8316, closes apache#8056. Closes apache#8422, the task this change is the whole of. ### How was this PR tested? Each operator asserts the block it emits in its own spec. Once the harness lands, each is also run through the engine and through its generated script, on every configuration its schema offers, and the two answers compared. ### Was this PR authored or co-authored using generative AI tooling? Generated-by: Claude Code (Opus 5) --------- Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
1 parent d7ce3a6 commit 4f36c15

19 files changed

Lines changed: 807 additions & 52 deletions

‎common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceIrisLogisticRegressionOpDesc.scala‎

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,40 @@
2020
package org.apache.texera.amber.operator.huggingFace
2121

2222
import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription}
23+
import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaInject
2324
import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
2425
import org.apache.texera.amber.pybuilder.PyStringTypes.EncodableString
2526
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.PythonTemplateBuilderStringContext
2627
import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity}
27-
import org.apache.texera.amber.operator.PythonOperatorDescriptor
28-
import org.apache.texera.amber.operator.metadata.annotations.AutofillAttributeName
28+
import org.apache.texera.amber.operator.{PythonOperatorDescriptor, StandaloneCodeGenerator}
29+
import org.apache.texera.amber.operator.metadata.annotations.{AutofillAttributeName, SampleColumn}
2930
import org.apache.texera.amber.operator.metadata.{OperatorGroupConstants, OperatorInfo}
31+
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.pyStringLiteral
3032

31-
class HuggingFaceIrisLogisticRegressionOpDesc extends PythonOperatorDescriptor {
33+
// type constraint: both measurements are standardized against the training means
34+
// and handed to the model as floats, so each column can only be numeric.
35+
@JsonSchemaInject(json = """
36+
{
37+
"attributeTypeRules": {
38+
"petalLengthCmAttribute": { "enum": ["integer", "long", "double"] },
39+
"petalWidthCmAttribute": { "enum": ["integer", "long", "double"] }
40+
}
41+
}
42+
""")
43+
class HuggingFaceIrisLogisticRegressionOpDesc
44+
extends PythonOperatorDescriptor
45+
with StandaloneCodeGenerator {
3246

3347
@JsonProperty(value = "petalLengthCmAttribute", required = true)
3448
@JsonPropertyDescription("attribute in your dataset corresponding to PetalLengthCm")
3549
@AutofillAttributeName
50+
@SampleColumn("petal_length")
3651
var petalLengthCmAttribute: EncodableString = _
3752

3853
@JsonProperty(value = "petalWidthCmAttribute", required = true)
3954
@JsonPropertyDescription("attribute in your dataset corresponding to PetalWidthCm")
4055
@AutofillAttributeName
56+
@SampleColumn("petal_width")
4157
var petalWidthCmAttribute: EncodableString = _
4258

4359
@JsonProperty(
@@ -67,6 +83,7 @@ class HuggingFaceIrisLogisticRegressionOpDesc extends PythonOperatorDescriptor {
6783
override def generatePythonCode(): String = {
6884
pyb"""from pytexera import *
6985
|import numpy as np
86+
|import pandas as pd
7087
|import torch
7188
|import torch.nn as nn
7289
|from huggingface_hub import PyTorchModelHubMixin
@@ -91,6 +108,16 @@ class HuggingFaceIrisLogisticRegressionOpDesc extends PythonOperatorDescriptor {
91108
| training_features_stds = [1.72528903, 0.73788937]
92109
| length = tuple_[$petalLengthCmAttribute]
93110
| width = tuple_[$petalWidthCmAttribute]
111+
| # An empty cell arrives as None, which numpy carries as an object the
112+
| # standardization cannot subtract from, and a NaN is no measurement
113+
| # either. Keep the row and leave the prediction empty rather than
114+
| # ending the run or predicting from a measurement the model was
115+
| # never given, as the exported script does.
116+
| if pd.isna(length) or pd.isna(width):
117+
| tuple_[$predictionClassName] = None
118+
| tuple_[$predictionProbabilityName] = None
119+
| yield tuple_
120+
| return
94121
| features = np.array([[length, width]])
95122
| features = ((features - training_features_means) / training_features_stds)
96123
| features = torch.from_numpy(features).float()
@@ -103,6 +130,54 @@ class HuggingFaceIrisLogisticRegressionOpDesc extends PythonOperatorDescriptor {
103130
| yield tuple_""".encode
104131
}
105132

133+
// Standalone mirror of generatePythonCode: rebuild+load the pretrained linear
134+
// model once, then apply the same per-row standardize→sigmoid→threshold logic,
135+
// adding the STRING predicted class and DOUBLE probability columns (in
136+
// getOutputSchemas order) to produce out1df.
137+
override def generateStandaloneCode(): String = {
138+
val lengthLit = pyStringLiteral(petalLengthCmAttribute)
139+
val widthLit = pyStringLiteral(petalWidthCmAttribute)
140+
s"""import numpy as np
141+
|import torch
142+
|import torch.nn as nn
143+
|from huggingface_hub import PyTorchModelHubMixin
144+
|
145+
|class LinearModel(nn.Module, PyTorchModelHubMixin):
146+
| def __init__(self):
147+
| super().__init__()
148+
| self.fc = nn.Linear(2, 1)
149+
|
150+
| def forward(self, x):
151+
| return self.fc(x)
152+
|
153+
|model = LinearModel.from_pretrained("sadhaklal/logistic-regression-iris")
154+
|model.eval()
155+
|
156+
|training_features_means = [3.72666667, 1.17619048]
157+
|training_features_stds = [1.72528903, 0.73788937]
158+
|out1df = in1df.copy()
159+
|_classes = []
160+
|_probs = []
161+
|for _length, _width in zip(out1df[$lengthLit], out1df[$widthLit]):
162+
| # The operator's guard: pandas answers for the NaN a frame read from
163+
| # JSON holds and for the None a Tuple hands over.
164+
| if pd.isna(_length) or pd.isna(_width):
165+
| _classes.append(None)
166+
| _probs.append(None)
167+
| continue
168+
| features = np.array([[_length, _width]])
169+
| features = ((features - training_features_means) / training_features_stds)
170+
| features = torch.from_numpy(features).float()
171+
| with torch.no_grad():
172+
| logits = model(features)
173+
| proba = torch.sigmoid(logits.squeeze())
174+
| preds = (proba > 0.5).long()
175+
| _probs.append(float(proba))
176+
| _classes.append("Iris-setosa" if preds == 1 else "Not Iris-setosa")
177+
|out1df[${pyStringLiteral(predictionClassName)}] = _classes
178+
|out1df[${pyStringLiteral(predictionProbabilityName)}] = _probs""".stripMargin
179+
}
180+
106181
override def operatorInfo: OperatorInfo =
107182
OperatorInfo(
108183
"Hugging Face Iris Logistic Regression",

‎common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSentimentAnalysisOpDesc.scala‎

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,33 @@
2020
package org.apache.texera.amber.operator.huggingFace
2121

2222
import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription}
23+
import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaInject
2324
import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
2425
import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity}
25-
import org.apache.texera.amber.operator.PythonOperatorDescriptor
26-
import org.apache.texera.amber.operator.metadata.annotations.AutofillAttributeName
26+
import org.apache.texera.amber.operator.{PythonOperatorDescriptor, StandaloneCodeGenerator}
27+
import org.apache.texera.amber.operator.metadata.annotations.{AutofillAttributeName, SampleColumn}
2728
import org.apache.texera.amber.operator.metadata.{OperatorGroupConstants, OperatorInfo}
2829
import org.apache.texera.amber.pybuilder.PyStringTypes.EncodableString
29-
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.PythonTemplateBuilderStringContext
30-
class HuggingFaceSentimentAnalysisOpDesc extends PythonOperatorDescriptor {
30+
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.{
31+
PythonTemplateBuilderStringContext,
32+
pyStringLiteral
33+
}
34+
// type constraint: the tokenizer scores text and refuses anything that is not a
35+
// string, so the column can only be a string.
36+
@JsonSchemaInject(json = """
37+
{
38+
"attributeTypeRules": {
39+
"attribute": { "enum": ["string"] }
40+
}
41+
}
42+
""")
43+
class HuggingFaceSentimentAnalysisOpDesc
44+
extends PythonOperatorDescriptor
45+
with StandaloneCodeGenerator {
3146
@JsonProperty(value = "attribute", required = true)
3247
@JsonPropertyDescription("column to perform sentiment analysis on")
3348
@AutofillAttributeName
49+
@SampleColumn("short_text")
3450
var attribute: EncodableString = _
3551

3652
@JsonProperty(
@@ -63,6 +79,7 @@ class HuggingFaceSentimentAnalysisOpDesc extends PythonOperatorDescriptor {
6379
|from transformers import AutoModelForSequenceClassification
6480
|from transformers import AutoTokenizer, AutoConfig
6581
|import numpy as np
82+
|import pandas as pd
6683
|from scipy.special import softmax
6784
|
6885
|class ProcessTupleOperator(UDFOperatorV2):
@@ -76,10 +93,11 @@ class HuggingFaceSentimentAnalysisOpDesc extends PythonOperatorDescriptor {
7693
| @overrides
7794
| def process_tuple(self, tuple_: Tuple, port: int) -> Iterator[Optional[TupleLike]]:
7895
| text = tuple_[$attribute]
79-
| # An empty cell arrives as None, which the tokenizer rejects. Keep the row
80-
| # and leave the scores empty rather than ending the run over a value the
81-
| # model has nothing to say about.
82-
| if text is None or (isinstance(text, str) and not text.strip()):
96+
| # An empty cell arrives as None, and a column the type rule was meant to
97+
| # keep out can carry a NaN of its own. The tokenizer rejects both. Keep
98+
| # the row and leave the scores empty rather than ending the run over a
99+
| # value the model has nothing to say about.
100+
| if pd.isna(text) or (isinstance(text, str) and not text.strip()):
83101
| for label in ($resultAttributePositive, $resultAttributeNeutral, $resultAttributeNegative):
84102
| tuple_[label] = None
85103
| yield tuple_
@@ -96,6 +114,46 @@ class HuggingFaceSentimentAnalysisOpDesc extends PythonOperatorDescriptor {
96114
| yield tuple_""".encode
97115
}
98116

117+
// Standalone mirror of generatePythonCode: load the model once, then apply the
118+
// same per-row softmax-over-3-labels logic to in1df, adding the three DOUBLE
119+
// result columns (in the same order as getOutputSchemas) to produce out1df.
120+
override def generateStandaloneCode(): String = {
121+
val positiveLit = pyStringLiteral(resultAttributePositive)
122+
val neutralLit = pyStringLiteral(resultAttributeNeutral)
123+
val negativeLit = pyStringLiteral(resultAttributeNegative)
124+
s"""from transformers import AutoModelForSequenceClassification
125+
|from transformers import AutoTokenizer, AutoConfig
126+
|import numpy as np
127+
|import pandas as pd
128+
|from scipy.special import softmax
129+
|
130+
|model_name = "cardiffnlp/twitter-roberta-base-sentiment-latest"
131+
|tokenizer = AutoTokenizer.from_pretrained(model_name)
132+
|config = AutoConfig.from_pretrained(model_name)
133+
|model = AutoModelForSequenceClassification.from_pretrained(model_name)
134+
|
135+
|out1df = in1df.copy()
136+
|labels = {"positive": $positiveLit, "neutral": $neutralLit, "negative": $negativeLit}
137+
|for _col in ($positiveLit, $neutralLit, $negativeLit):
138+
| out1df[_col] = 0.0
139+
|for _idx, _text in out1df[${pyStringLiteral(attribute)}].items():
140+
| # An empty cell reaches the frame as None, or as NaN once a column holds
141+
| # nothing else, and the tokenizer rejects both. Keep the row and leave the
142+
| # scores empty rather than ending the run over a value the model has
143+
| # nothing to say about.
144+
| if pd.isna(_text) or (isinstance(_text, str) and not _text.strip()):
145+
| for _col in ($positiveLit, $neutralLit, $negativeLit):
146+
| out1df.at[_idx, _col] = None
147+
| continue
148+
| encoded_input = tokenizer(_text, return_tensors='pt')
149+
| output = model(**encoded_input)
150+
| scores = softmax(output[0][0].detach().numpy())
151+
| ranking = np.argsort(scores)[::-1]
152+
| for i in range(scores.shape[0]):
153+
| label = labels[config.id2label[ranking[i]]]
154+
| out1df.at[_idx, label] = np.round(float(scores[ranking[i]]), 4)""".stripMargin
155+
}
156+
99157
override def operatorInfo: OperatorInfo =
100158
OperatorInfo(
101159
"Hugging Face Sentiment Analysis",

‎common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSpamSMSDetectionOpDesc.scala‎

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,33 @@
2020
package org.apache.texera.amber.operator.huggingFace
2121

2222
import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription}
23+
import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaInject
2324
import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
2425
import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity}
25-
import org.apache.texera.amber.operator.PythonOperatorDescriptor
26-
import org.apache.texera.amber.operator.metadata.annotations.AutofillAttributeName
26+
import org.apache.texera.amber.operator.{PythonOperatorDescriptor, StandaloneCodeGenerator}
27+
import org.apache.texera.amber.operator.metadata.annotations.{AutofillAttributeName, SampleColumn}
2728
import org.apache.texera.amber.operator.metadata.{OperatorGroupConstants, OperatorInfo}
2829
import org.apache.texera.amber.pybuilder.PyStringTypes.EncodableString
29-
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.PythonTemplateBuilderStringContext
30-
class HuggingFaceSpamSMSDetectionOpDesc extends PythonOperatorDescriptor {
30+
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.{
31+
PythonTemplateBuilderStringContext,
32+
pyStringLiteral
33+
}
34+
// type constraint: the classification pipeline reads text and refuses anything
35+
// that is not a string, so the column can only be a string.
36+
@JsonSchemaInject(json = """
37+
{
38+
"attributeTypeRules": {
39+
"attribute": { "enum": ["string"] }
40+
}
41+
}
42+
""")
43+
class HuggingFaceSpamSMSDetectionOpDesc
44+
extends PythonOperatorDescriptor
45+
with StandaloneCodeGenerator {
3146
@JsonProperty(value = "attribute", required = true)
3247
@JsonPropertyDescription("column to perform spam detection on")
3348
@AutofillAttributeName
49+
@SampleColumn("short_text")
3450
var attribute: EncodableString = _
3551

3652
@JsonProperty(
@@ -51,6 +67,7 @@ class HuggingFaceSpamSMSDetectionOpDesc extends PythonOperatorDescriptor {
5167

5268
override def generatePythonCode(): String = {
5369
pyb"""from transformers import pipeline
70+
|import pandas as pd
5471
|from pytexera import *
5572
|
5673
|class ProcessTupleOperator(UDFOperatorV2):
@@ -61,10 +78,11 @@ class HuggingFaceSpamSMSDetectionOpDesc extends PythonOperatorDescriptor {
6178
| @overrides
6279
| def process_tuple(self, tuple_: Tuple, port: int) -> Iterator[Optional[TupleLike]]:
6380
| text = tuple_[$attribute]
64-
| # An empty cell arrives as None, which the pipeline rejects. Keep the row
65-
| # and leave the results empty rather than ending the run over a value the
66-
| # model has nothing to say about.
67-
| if text is None or (isinstance(text, str) and not text.strip()):
81+
| # An empty cell arrives as None, and a column the type rule was meant to
82+
| # keep out can carry a NaN of its own. The pipeline rejects both. Keep
83+
| # the row and leave the results empty rather than ending the run over a
84+
| # value the model has nothing to say about.
85+
| if pd.isna(text) or (isinstance(text, str) and not text.strip()):
6886
| tuple_[$resultAttributeSpam] = None
6987
| tuple_[$resultAttributeProbability] = None
7088
| yield tuple_
@@ -75,6 +93,33 @@ class HuggingFaceSpamSMSDetectionOpDesc extends PythonOperatorDescriptor {
7593
| yield tuple_""".encode
7694
}
7795

96+
// Standalone mirror of generatePythonCode: build the text-classification
97+
// pipeline once, run it per row, and add the BOOLEAN spam flag (LABEL_1) and
98+
// the DOUBLE score columns (in getOutputSchemas order) to produce out1df.
99+
override def generateStandaloneCode(): String = {
100+
val attributeLit = pyStringLiteral(attribute)
101+
val spamLit = pyStringLiteral(resultAttributeSpam)
102+
val probabilityLit = pyStringLiteral(resultAttributeProbability)
103+
s"""from transformers import pipeline
104+
|import pandas as pd
105+
|
106+
|_pipeline = pipeline("text-classification", model="mrm8488/bert-tiny-finetuned-sms-spam-detection")
107+
|out1df = in1df.copy()
108+
|
109+
|def _classify(_t):
110+
| # An empty cell reaches the frame as None, or as NaN once a column holds
111+
| # nothing else, and the pipeline rejects both. Keep the row and leave the
112+
| # results empty rather than ending the run over a value the model has
113+
| # nothing to say about.
114+
| if pd.isna(_t) or (isinstance(_t, str) and not _t.strip()):
115+
| return None
116+
| return _pipeline(_t)[0]
117+
|
118+
|_results = [_classify(_t) for _t in out1df[$attributeLit]]
119+
|out1df[$spamLit] = [None if _r is None else _r["label"] == "LABEL_1" for _r in _results]
120+
|out1df[$probabilityLit] = [None if _r is None else _r["score"] for _r in _results]""".stripMargin
121+
}
122+
78123
override def operatorInfo: OperatorInfo =
79124
OperatorInfo(
80125
"Hugging Face Spam Detection",

0 commit comments

Comments
 (0)