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

# Hugging Face Transformers

> Trace Hugging Face Transformers model inference with Netra. Monitor local and hosted model calls, token usage, and latency with auto-instrumentation.

<img src="https://mintcdn.com/netra/IXT7TOAHn4HQhvyF/images/integration-logos/llm-providers/transformers.png?fit=max&auto=format&n=IXT7TOAHn4HQhvyF&q=85&s=63a37d509ca39c5d60fde42676827e70" alt="Hugging Face Transformers" width="480" height="80" data-path="images/integration-logos/llm-providers/transformers.png" />

## Installation

Install both the Netra SDK and Hugging Face Transformers:

<CodeGroup>
  ```bash Python theme={null}
  pip install netra-sdk transformers torch
  ```

  ```bash Typescript theme={null}
  npm install netra-sdk @huggingface/inference
  ```
</CodeGroup>

## Usage

Initialize the Netra SDK with Hugging Face instrumentation enabled. The SDK automatically traces all Transformers operations once initialized.

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra
  from transformers import pipeline
  import os

  # Initialize Netra with Hugging Face instrumentation
  Netra.init(
      app_name="my-ai-app",
      headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
      trace_content=True
  )

  # Use Transformers pipeline - all calls are automatically traced
  generator = pipeline("text-generation", model="gpt2")

  response = generator("What is observability?", max_length=50)
  print(response[0]['generated_text'])
  ```

  ```typescript Typescript theme={null}
  import { Netra } from "netra-sdk";
  import { HfInference } from "@huggingface/inference";

  async function main() {
    // Initialize Netra with Hugging Face instrumentation (must await)
    await Netra.init({
      appName: "my-ai-app",
      headers: `x-api-key=${process.env.NETRA_API_KEY}`,
      traceContent: true
    });

    // Use Hugging Face Inference API - all calls are automatically traced
    const hf = new HfInference(process.env.HF_TOKEN);

    const response = await hf.textGeneration({
      model: "meta-llama/Llama-2-7b-chat-hf",
      inputs: "What is observability?"
    });

    console.log(response.generated_text);
  }

  main();
  ```
</CodeGroup>

### Streaming Responses

The SDK automatically handles streaming responses from Hugging Face models:

<CodeGroup>
  ```python Python theme={null}
  from transformers import TextStreamer

  generator = pipeline(
      "text-generation",
      model="gpt2",
      streamer=TextStreamer(skip_prompt=True)
  )

  # Streaming output is automatically captured
  generator("Tell me a story", max_length=100)
  ```

  ```typescript Typescript theme={null}
  const stream = hf.textGenerationStream({
    model: "meta-llama/Llama-2-7b-chat-hf",
    inputs: "Tell me a story"
  });

  for await (const chunk of stream) {
    process.stdout.write(chunk.token.text);
  }
  ```
</CodeGroup>

### Different Task Types

Various Transformers tasks are automatically instrumented:

<CodeGroup>
  ```python Python theme={null}
  # Text Classification
  classifier = pipeline("sentiment-analysis")
  result = classifier("I love this product!")

  # Summarization
  summarizer = pipeline("summarization")
  summary = summarizer("Long text to summarize...", max_length=50)

  # Question Answering
  qa = pipeline("question-answering")
  answer = qa({
      "question": "What is AI?",
      "context": "Artificial Intelligence is..."
  })
  ```

  ```typescript Typescript theme={null}
  // Text Classification
  const classifier = await hf.textClassification({
    model: "distilbert-base-uncased-finetuned-sst-2-english",
    inputs: "I love this product!"
  });

  // Summarization
  const summary = await hf.summarization({
    model: "facebook/bart-large-cnn",
    inputs: "Long text to summarize..."
  });

  // Question Answering
  const answer = await hf.questionAnswering({
    model: "deepset/roberta-base-squad2",
    inputs: {
      question: "What is AI?",
      context: "Artificial Intelligence is..."
    }
  });
  ```
</CodeGroup>

### Selective Instrumentation

Control which integrations are enabled using the `instruments` or `blockInstruments` configuration:

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra
  from netra.instrumentation.instruments import InstrumentSet

  # Only enable Hugging Face instrumentation
  Netra.init(
      headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
      instruments={InstrumentSet.TRANSFORMERS}
  )

  # Or block specific instrumentations
  Netra.init(
      headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
      block_instruments={InstrumentSet.HTTPX}
  )
  ```

  ```typescript Typescript theme={null}
  import { Netra, NetraInstruments } from "netra-sdk";

  // Only enable Hugging Face instrumentation
  await Netra.init({
    headers: `x-api-key=${process.env.NETRA_API_KEY}`,
    instruments: new Set([NetraInstruments.HUGGINGFACE])
  });

  // Or block specific instrumentations
  await Netra.init({
    headers: `x-api-key=${process.env.NETRA_API_KEY}`,
    blockInstruments: new Set([NetraInstruments.HTTP])
  });
  ```
</CodeGroup>

## Next Steps

* [Quick Start Guide](https://docs.getnetra.ai/quick-start/python) - Complete setup and configuration
* [Auto Instrumentation](https://docs.getnetra.ai/tracing/auto-instrumentation) - Automatic tracing for supported libraries
* [Decorators](https://docs.getnetra.ai/tracing/decorators) - Add custom tracing with `@workflow`, `@agent`, and `@task` decorators
* [Session Tracking](https://docs.getnetra.ai/tracing/session) - Track user sessions and conversations
* [Transformers Documentation](https://huggingface.co/docs/transformers/quicktour) - Official Hugging Face Transformers quick tour
