Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,20 @@ class AggregationOperation {
res
}

private def sumAgg(attributeType: AttributeType): DistributedAggregation[Object] = {
private def requireNumericOrTimestamp(opName: String, attributeType: AttributeType): Unit =
if (
attributeType != AttributeType.INTEGER &&
attributeType != AttributeType.DOUBLE &&
attributeType != AttributeType.LONG &&
attributeType != AttributeType.TIMESTAMP
) {
throw new UnsupportedOperationException(
"Unsupported attribute type for sum aggregation: " + attributeType
s"Unsupported attribute type for $opName aggregation: $attributeType"
)
}

private def sumAgg(attributeType: AttributeType): DistributedAggregation[Object] = {
requireNumericOrTimestamp("sum", attributeType)
new DistributedAggregation[Object](
() => AttributeTypeUtils.zeroValue(attributeType),
(partial, tuple) => {
Expand Down Expand Up @@ -191,55 +194,32 @@ class AggregationOperation {
)
}

private def minAgg(attributeType: AttributeType): DistributedAggregation[Object] = {
if (
attributeType != AttributeType.INTEGER &&
attributeType != AttributeType.DOUBLE &&
attributeType != AttributeType.LONG &&
attributeType != AttributeType.TIMESTAMP
) {
throw new UnsupportedOperationException(
"Unsupported attribute type for min aggregation: " + attributeType
)
}
private def extremumAgg(
opName: String,
sentinel: AttributeType => Object,
isBetter: Int => Boolean,
attributeType: AttributeType
): DistributedAggregation[Object] = {
requireNumericOrTimestamp(opName, attributeType)
new DistributedAggregation[Object](
() => AttributeTypeUtils.maxValue(attributeType),
() => sentinel(attributeType),
(partial, tuple) => {
val value = tuple.getField[Object](attribute)
val comp = AttributeTypeUtils.compare(value, partial, attributeType)
if (value != null && comp < 0) value else partial
if (value != null && isBetter(comp)) value else partial
},
(partial1, partial2) =>
if (AttributeTypeUtils.compare(partial1, partial2, attributeType) < 0) partial1
if (isBetter(AttributeTypeUtils.compare(partial1, partial2, attributeType))) partial1
else partial2,
partial => if (partial == AttributeTypeUtils.maxValue(attributeType)) null else partial
partial => if (partial == sentinel(attributeType)) null else partial
)
}

private def maxAgg(attributeType: AttributeType): DistributedAggregation[Object] = {
if (
attributeType != AttributeType.INTEGER &&
attributeType != AttributeType.DOUBLE &&
attributeType != AttributeType.LONG &&
attributeType != AttributeType.TIMESTAMP
) {
throw new UnsupportedOperationException(
"Unsupported attribute type for max aggregation: " + attributeType
)
}
new DistributedAggregation[Object](
() => AttributeTypeUtils.minValue(attributeType),
(partial, tuple) => {
val value = tuple.getField[Object](attribute)
val comp = AttributeTypeUtils.compare(value, partial, attributeType)
if (value != null && comp > 0) value else partial
},
(partial1, partial2) =>
if (AttributeTypeUtils.compare(partial1, partial2, attributeType) > 0) partial1
else partial2,
partial => if (partial == AttributeTypeUtils.minValue(attributeType)) null else partial
)
}
private def minAgg(attributeType: AttributeType): DistributedAggregation[Object] =
extremumAgg("min", AttributeTypeUtils.maxValue, _ < 0, attributeType)

private def maxAgg(attributeType: AttributeType): DistributedAggregation[Object] =
extremumAgg("max", AttributeTypeUtils.minValue, _ > 0, attributeType)

private def getNumericalValue(tuple: Tuple): Option[Double] = {
val value: Object = tuple.getField(attribute)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import org.apache.texera.amber.pybuilder.PyStringTypes.EncodableString
* `PythonCodeRawInvalidTextSpec`'s contract that arbitrary `@JsonProperty`
* values must not leak into generated source.
*/
@SerialVersionUID(-3887825915400170939L)
class HuggingFaceInferenceOpDesc extends PythonOperatorDescriptor {

@JsonProperty(value = "hfApiToken", required = true)
Expand Down Expand Up @@ -169,15 +170,10 @@ class HuggingFaceInferenceOpDesc extends PythonOperatorDescriptor {
* keeps `generatePythonCode` total (it never throws on arbitrary input,
* which is required by `PythonCodeRawInvalidTextSpec`).
*/
private val registeredCodegens: Map[String, TaskCodegen] = {
val byTask = scala.collection.mutable.Map.empty[String, TaskCodegen]
byTask += (TextGenCodegen.task -> TextGenCodegen)
ImageTaskCodegen.tasks.foreach(t => byTask += (t -> ImageTaskCodegen))
AudioTaskCodegen.tasks.foreach(t => byTask += (t -> AudioTaskCodegen))
MediaGenCodegen.tasks.foreach(t => byTask += (t -> MediaGenCodegen))
QaRankingCodegen.tasks.foreach(t => byTask += (t -> QaRankingCodegen))
byTask.toMap
}
private val registeredCodegens: Map[String, TaskCodegen] =
Seq(TextGenCodegen, ImageTaskCodegen, AudioTaskCodegen, MediaGenCodegen, QaRankingCodegen)
.flatMap(codegen => codegen.tasks.map(task => task -> codegen))
.toMap

private def codegenForTask(t: String): TaskCodegen =
registeredCodegens.getOrElse(t, TextGenCodegen)
Expand All @@ -200,52 +196,24 @@ class HuggingFaceInferenceOpDesc extends PythonOperatorDescriptor {
override def generatePythonCode(): String = {
val safeTask: EncodableString =
if (task == null || task.trim.isEmpty) "text-generation" else task
val safeModelId: EncodableString =
if (modelId == null) "" else modelId.trim
val safePromptCol: EncodableString =
if (promptColumn == null) "" else promptColumn
val safeResultCol: EncodableString = resolvedResultColumn
val safeSystemPrompt: EncodableString =
if (systemPrompt == null) "" else systemPrompt
val safeToken: EncodableString =
if (hfApiToken == null) "" else hfApiToken

val safeMaxTokens =
math.max(1, math.min(if (maxNewTokens != null) maxNewTokens.intValue else 256, 4096))
val safeTemp =
math.max(0.0, math.min(if (temperature != null) temperature.doubleValue else 0.7, 2.0))

val safeImageInput: EncodableString =
if (imageInput == null) "" else imageInput
val safeInputImageColumn: EncodableString =
if (inputImageColumn == null) "" else inputImageColumn
val safeAudioInput: EncodableString =
if (audioInput == null) "" else audioInput
val safeInputAudioColumn: EncodableString =
if (inputAudioColumn == null) "" else inputAudioColumn
val safeContextColumn: EncodableString =
if (contextColumn == null) "" else contextColumn
val safeCandidateLabels: EncodableString =
if (candidateLabels == null) "" else candidateLabels
val safeSentencesColumn: EncodableString =
if (sentencesColumn == null) "" else sentencesColumn

val ctx = CodegenContext(
hfApiToken = safeToken,
modelId = safeModelId,
promptColumn = safePromptCol,
resultColumn = safeResultCol,
task = safeTask,
systemPrompt = safeSystemPrompt,
safeMaxTokens = safeMaxTokens,
safeTemp = safeTemp,
imageInput = safeImageInput,
inputImageColumn = safeInputImageColumn,
audioInput = safeAudioInput,
inputAudioColumn = safeInputAudioColumn,
contextColumn = safeContextColumn,
candidateLabels = safeCandidateLabels,
sentencesColumn = safeSentencesColumn
modelId = if (modelId == null) "" else modelId.trim,
promptColumn = if (promptColumn == null) "" else promptColumn,
resultColumn = resolvedResultColumn,
systemPrompt = if (systemPrompt == null) "" else systemPrompt,
hfApiToken = if (hfApiToken == null) "" else hfApiToken,
safeMaxTokens =
math.max(1, math.min(if (maxNewTokens != null) maxNewTokens.intValue else 256, 4096)),
safeTemp =
math.max(0.0, math.min(if (temperature != null) temperature.doubleValue else 0.7, 2.0)),
imageInput = if (imageInput == null) "" else imageInput,
inputImageColumn = if (inputImageColumn == null) "" else inputImageColumn,
audioInput = if (audioInput == null) "" else audioInput,
inputAudioColumn = if (inputAudioColumn == null) "" else inputAudioColumn,
contextColumn = if (contextColumn == null) "" else contextColumn,
candidateLabels = if (candidateLabels == null) "" else candidateLabels,
sentencesColumn = if (sentencesColumn == null) "" else sentencesColumn,
task = safeTask
)

HuggingFaceCodegenBase.render(ctx, codegenForTask(safeTask))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,21 @@ object AudioTaskCodegen extends TaskCodegen {
| payload = {"inputs": prompt_value}""".stripMargin

override def parsePython(ctx: CodegenContext): String =
""" if task == "text-to-speech":
s""" if task == "text-to-speech":
| # Every value below comes from the provider, so each is type-checked
| # before use: an unchecked one lands in the result column as a data
| # URL nothing can play (e.g. "data:audio/mpeg;base64,None") or as a
| # non-string cell, instead of falling back to the body.
| if isinstance(body, dict):
| if "output" in body:
| out = body["output"]
| url = out[0] if isinstance(out, list) else out
| if isinstance(url, str) and url.startswith("http"):
| return self._url_to_data_url(url)
|${BinaryResponseFragments.outputUrl}
| if "audio" in body:
| audio = body["audio"]
| if isinstance(audio, dict):
| url = audio.get("url")
| if isinstance(url, str) and url.startswith("http"):
| return self._url_to_data_url(url)
| b64 = audio.get("b64_json")
| if isinstance(b64, str) and b64:
| return f"data:audio/mpeg;base64,{b64}"
|${audioDataUrl("audio")}
| if "data" in body:
| data = body["data"]
| if isinstance(data, list) and data and isinstance(data[0], dict):
| url = data[0].get("url")
| if isinstance(url, str) and url.startswith("http"):
| return self._url_to_data_url(url)
| b64 = data[0].get("b64_json")
| if isinstance(b64, str) and b64:
| return f"data:audio/mpeg;base64,{b64}"
|${audioDataUrl("data[0]")}
| return json.dumps(body)
| elif task == "automatic-speech-recognition":
| if isinstance(body, dict):
Expand All @@ -86,4 +72,12 @@ object AudioTaskCodegen extends TaskCodegen {
| return json.dumps(body)
| elif task == "audio-classification":
| return json.dumps(body)""".stripMargin

private def audioDataUrl(value: String): String =
s""" url = ${value}.get("url")
| if isinstance(url, str) and url.startswith("http"):
| return self._url_to_data_url(url)
| b64 = ${value}.get("b64_json")
| if isinstance(b64, str) and b64:
| return f"data:audio/mpeg;base64,{b64}"""".stripMargin
}
Loading
Loading