# pysparky_ai.huggingface

*module*

Spark UDF adapters for preloaded Hugging Face compatible objects.

[View source](https://github.com/PySparky/pysparky-ai/blob/8af5424b9ed84bc25e05b8cb7698367e1cc89478/src/pysparky_ai/huggingface.py#L1-L1)

## pysparky_ai.huggingface.build_text_generation_udf

*function*

```python
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.

[View source](https://github.com/PySparky/pysparky-ai/blob/8af5424b9ed84bc25e05b8cb7698367e1cc89478/src/pysparky_ai/huggingface.py#L26-L73)
