HuggingFace Transformer Pipeline - Vision: How to Use, Deploy and Serve
Tags: huggingface, machinelearning, deeplearning, tutorial
Published: 2022-04-17 03:04:05


In this tutorial, we will explore how to use Hugging Face pipeline, and how to deploy it with [Pinferencia](https://github.com/underneathall/pinferencia) as REST API.

> Never heard of Pinferencia? It's not late. Check it out at [GitHub](https://github.com/underneathall/pinferencia)

---

Download the model and predict
The model will be automatically downloaded.

```python
from transformers import pipeline
vision_classifier = pipeline(task="image-classification")

vision_classifier(
    images="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"
)
```

![hfimg](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg)

Result:

```python
[{'label': 'lynx, catamount', 'score': 0.4403027892112732},
 {'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor',
  'score': 0.03433405980467796},
 {'label': 'snow leopard, ounce, Panthera uncia',
  'score': 0.032148055732250214},
 {'label': 'Egyptian cat', 'score': 0.02353910356760025},
 {'label': 'tiger cat', 'score': 0.023034192621707916}]
```
Let's try another image, and let's try predict two image in one batch:

```python
image = "https://cdn.pixabay.com/photo/2018/08/12/16/59/parrot-3601194_1280.jpg"
vision_classifier(
    images=[image, image]
)
```
![parrot](https://cdn.pixabay.com/photo/2018/08/12/16/59/parrot-3601194_1280.jpg)

Result:

```python
[[{'score': 0.9489120244979858, 'label': 'macaw'},
  {'score': 0.014800671488046646, 'label': 'broom'},
  {'score': 0.009150494821369648, 'label': 'swab, swob, mop'},
  {'score': 0.0018255198374390602, 'label': "plunger, plumber's helper"},
  {'score': 0.0017631321679800749,
   'label': 'African grey, African gray, Psittacus erithacus'}],
 [{'score': 0.9489120244979858, 'label': 'macaw'},
  {'score': 0.014800671488046646, 'label': 'broom'},
  {'score': 0.009150494821369648, 'label': 'swab, swob, mop'},
  {'score': 0.0018255198374390602, 'label': "plunger, plumber's helper"},
  {'score': 0.0017631321679800749,
   'label': 'African grey, African gray, Psittacus erithacus'}]]
```

Amazingly easy! Now let's try:

## Deploy the model

Without deployment, how could a machine learning tutorial be complete?

First, let's install [Pinferencia](https://github.com/underneathall/pinferencia).

```bash
pip install "pinferencia[uvicorn]"
```

If you haven't heard of Pinferencia go to its [github](https://github.com/underneathall/pinferencia) page or its [homepage](https://pinferencia.underneathall.app/) to check it out, it's an amazing library help you deploy your model with ease.

Now let's create an app.py file with the codes:

```python
from transformers import pipeline
from pinferencia import Server
vision_classifier = pipeline(task="image-classification")

def predict(data):
    return vision_classifier(images=data)

service = Server()
service.register(
    model_name="vision",
    model=predict,
)
```
Easy, right?

## Predict

Curl

```bash
curl --location --request POST 'http://127.0.0.1:8000/v1/models/vision/predict' \
--header 'Content-Type: application/json' \
--data-raw '{
    "data": "https://cdn.pixabay.com/photo/2018/08/12/16/59/parrot-3601194_1280.jpg"
}'
```
or use python requests

```python
import requests

response = requests.post(
    url="http://localhost:8000/v1/models/vision/predict",
    json={
        "data": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"  # noqa
    },
)
print("Prediction:", response.json()["data"])
```
Result:
`python test.py`

```
Prediction: [
    {'score': 0.433499813079834, 'label': 'lynx, catamount'},
    {'score': 0.03479616343975067, 'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'},
    {'score': 0.032401904463768005, 'label': 'snow leopard, ounce, Panthera uncia'},
    {'score': 0.023944756016135216, 'label': 'Egyptian cat'},
    {'score': 0.022889181971549988, 'label': 'tiger cat'}
]
```

Even cooler, go to http://127.0.0.1:8000, and you will have a interactive ui.

You can send predict requests just there!

Go [Pinferencia](https://github.com/underneathall/pinferencia)!


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8nn2q5eanufls2ha4yh.jpg)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/63bbymvijacfv8yb84bs.jpg)

If you like Pinferencia, don't forget to go to https://github.com/underneathall/pinferencia and give it a star.