Skip to content

pysparky_ai.huggingface

Spark UDF adapters for preloaded Hugging Face compatible objects.

Functions

NameDescription
build_text_generation_udfCreate a Spark UDF that generates text with broadcast model objects.

build_text_generation_udffunction#

def build_text_generation_udf(model_bc: Any, tokenizer_bc: Any, system_prompt: str) -> Any

Create a Spark UDF that generates text with broadcast model objects.

model_bc and tokenizer_bc must be Spark broadcasts containing already-loaded model and tokenizer objects. Broadcast values are accessed inside the worker callable, not resolved while this factory runs on the driver. Models are neither loaded nor downloaded by this package.

Each non-null input is passed to the tokenizer as f"{system_prompt}: {user_input}" with return_tensors="pt". The first generated sequence is decoded with special tokens skipped. A null input returns null without accessing either broadcast; an empty string is sent to the model with the prompt separator.

The caller is responsible for loading a CPU-ready model and tokenizer and broadcasting them. This adapter does not select a device, manage worker lifecycle, or catch model errors.

Raises

ImportError
If PySpark is not installed.
TypeError
If either broadcast argument is not a Spark Broadcast or system_prompt is not a string.