This is the source code for my Huggingface Space deployment. You can checkout the deployed application on Huggingface Spaces. The task of Zero Shot Classification is a Natutal Language Processing technique where a model can classify text into categories it has not seen during training. This is acheived by training a model like DeBERTa on a variety of NLP tasks allowing the model to generalize and predict the most relevant class for a given text input based on the context provided by candidate labels.
I have used a DeBERTa model architecture that excels at Zero Shot Tasks very well. I have quantized a finetuned model, sileod/deberta-v3-base-tasksource-nli provided by Damien Sileo to further improve the inference latency. The model has been quantized using Huggingface Optimum for ONNX. You can see the quantize.ipynb for the source code for quantization.
You can access the Gradio App directly on Huggingface Spaces by following this Link. You can embed this application as an iframe using the following HTML code:
<iframe
src="https://arnabdhar-zero-shot-classification-deberta-quantized.hf.space"
frameborder="0"
width="850"
height="450"
></iframe>The Direct URL is: https://arnabdhar-zero-shot-classification-deberta-quantized.hf.space
cURLcommand
curl -X 'POST' \
'https://arnabdhar-zero-shot-classification-deberta-quantized.hf.space/predict' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"text": "I am loving it",
"labels": ["positive", "negative"]
}'- Postman
Base URL: https://arnabdhar-zero-shot-classification-deberta-quantized.hf.space
Method: POST
URL: /predict
Body:
{
"text": "I am loving it",
"labels": ["positive", "negative"]
}- Python
import requests
import json
# Define the URL
url = 'https://arnabdhar-zero-shot-classification-deberta-quantized.hf.space/predict'
# Define the headers
headers = {
'accept': 'application/json',
'Content-Type': 'application/json'
}
# Define the payload
data = {
"text": "I am loving it",
"labels": ["positive", "negative"]
}
# Make the POST request
response = requests.post(url, headers=headers, data=json.dumps(data))
# Print the response
print(response.json())- Javascript
// Define the URL
const url = 'https://arnabdhar-zero-shot-classification-deberta-quantized.hf.space/predict';
// Define the headers
const headers = {
'accept': 'application/json',
'Content-Type': 'application/json'
};
// Define the payload
const data = {
text: 'I am loving it',
labels: ['positive', 'negative']
};
// Make the POST request
fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));To run the application, I highly recommend using Docker but there are also other ways to get started. But first you have to clone the repository onto your local machine.
$ git clone https://github.com/arnabd64/Zero-Shot-Text-Classification.git
$ cd Zero-Shot-Text-Classification- Build the Docker Image:
$ docker build -t zero-shot-text-classification:latest .- Run the following docker command:
$ docker run -itd \
-p 8000:8000 \
-e HF_MODEL=pitangent-ds/deberta-v3-nli-onnx-quantized \
-e PORT=8000 \
-e WORKERS=2 \
zero-shot-text-classification:latestAfter running the docker container wait for a few minutes for the model to download and load into the memory.
$ docker compose up -d --buildAfter running the docker compose wait for a few minutes for the model to download and load into the memory.
Note: Use python 3.11 or later (Python 3.10 should work but I have not yet tested it)
- Create a virtual environment and install dependencies
# create a virtual environment
$ python -m venv .venv
# activate the environment
$ .venv/bin/activate # on linux and macOS
$ .venv/Scripts/Activate.ps1 # on Windows
# install dependencies
$ pip install -e .- Set the following Environment Variables
HF_MODEL=pitangent-ds/deberta-v3-nli-onnx-quantized
PORT=8000
WORKERS=2- Run the application
$ python main.py