> ## Documentation Index
> Fetch the complete documentation index at: https://docs.raxe.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Hugging Face Integration

> Protect Hugging Face pipelines with RAXE

> [!NOTE]
>
> New to RAXE? Start with the [Quickstart](https://docs.raxe.ai/quickstart.md) and learn [how detection works](https://docs.raxe.ai/concepts/detection-engine.md).

## Overview

RAXE integrates with Hugging Face Transformers to provide automatic security scanning for local model pipelines.

## Installation

```bash theme={null}
pip install raxe transformers torch
```

## RaxePipeline Wrapper

Use the RAXE pipeline wrapper for automatic scanning:

```python theme={null}
from raxe import RaxePipeline

# Wrap any Hugging Face pipeline
pipe = RaxePipeline(
    task="text-generation",
    model="gpt2"
)

# All inputs and outputs are automatically scanned
result = pipe("Once upon a time")
```

## Supported Pipelines

| Task | Example Model | Scanning |
| - | - | - |
| `text-generation` | gpt2, llama-2, mistral | Input + Output |
| `text2text-generation` | t5-base, flan-t5 | Input + Output |
| `conversational` | DialoGPT | Messages |
| `question-answering` | distilbert-squad | Question + Context |
| `summarization` | bart-large-cnn | Input + Summary |
| `translation` | opus-mt-en-de | Input + Translation |

## Configuration

```python theme={null}
from raxe import Raxe
from raxe import RaxePipeline

pipe = RaxePipeline(
    task="text-generation",
    model="gpt2",

    # RAXE options
    raxe=Raxe(telemetry=False),           # Custom client
    raxe_block_on_input_threats=False,    # Log-only (default)
    raxe_block_on_output_threats=False,   # Log-only (default)

    # Pipeline options
    device="cuda",                         # GPU acceleration
    max_length=100,                        # Generation params
)
```

## Blocking Mode

Enable blocking to prevent malicious inputs:

```python theme={null}
from raxe import RaxePipeline
from raxe import RaxeBlockedError

pipe = RaxePipeline(
    task="text-generation",
    model="gpt2",
    raxe_block_on_input_threats=True,
    raxe_block_on_output_threats=True,
)

try:
    result = pipe(user_input)
except RaxeBlockedError as e:
    print(f"Blocked: {e.message}")
```

## Pipeline Examples

### Text Generation

```python theme={null}
pipe = RaxePipeline(task="text-generation", model="gpt2")

result = pipe(
    "Once upon a time",
    max_length=50,
    num_return_sequences=3,
)
```

### Question Answering

```python theme={null}
pipe = RaxePipeline(
    task="question-answering",
    model="distilbert-base-cased-distilled-squad"
)

result = pipe(
    question="What is the capital of France?",
    context="France is a country in Europe. Its capital is Paris."
)

print(result["answer"])  # "Paris"
```

### Summarization

```python theme={null}
pipe = RaxePipeline(
    task="summarization",
    model="facebook/bart-large-cnn"
)

result = pipe(long_article, max_length=100, min_length=30)
print(result[0]["summary_text"])
```

## Factory Function

```python theme={null}
from raxe import create_huggingface_pipeline

# Quick setup with blocking
pipe = create_huggingface_pipeline(
    task="text-generation",
    model="gpt2",
    block_on_threats=True,
)
```

## Performance Tips

### GPU Acceleration

```python theme={null}
pipe = RaxePipeline(
    task="text-generation",
    model="gpt2",
    device="cuda:0",  # Use first GPU
)
```

### Large Models

```python theme={null}
pipe = RaxePipeline(
    task="text-generation",
    model="meta-llama/Llama-2-7b-hf",
    pipeline_kwargs={
        "torch_dtype": "float16",
        "device_map": "auto",
    },
)
```

## Related

> [card] [LiteLLM](https://docs.raxe.ai/integrations/litellm.md)
>
> 200+ cloud providers through LiteLLM

> [card] [OpenAI](https://docs.raxe.ai/sdk/openai-wrapper.md)
>
> Drop-in OpenAI wrapper

## What's Next

> [card] [OpenAI Wrapper](https://docs.raxe.ai/sdk/openai-wrapper.md)
>
> Use RAXE with the OpenAI-compatible API

> [card] [Production Checklist](https://docs.raxe.ai/guides/production-checklist.md)
>
> Deploy RAXE safely to production


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.