Hugging Face Inference API
This documentation describes the integration of MindsDB with Hugging Face Inference API. The integration allows for the deployment of Hugging Face models through Inference API within MindsDB, providing the models with access to data from various data sources.
Prerequisites
Before proceeding, ensure the following prerequisites are met:
- Install MindsDB locally via Docker or Docker Desktop.
- To use Hugging Face Inference API within MindsDB, install the required dependencies following this instruction.
- Obtain the API key for Hugging Face Inference API required to deploy and use Hugging Face models through Inference API within MindsDB. Generate tokens in the
Settings -> Access Tokens
tab of the Hugging Face account.
Setup
Create an AI engine from the Hugging Face Inference API handler.
CREATE ML_ENGINE huggingface_api_engine
FROM huggingface_api
USING
huggingface_api_api_key = 'api-key-value';
Create a model using huggingface_api_engine
as an engine.
CREATE MODEL huggingface_api_model
PREDICT target_column
USING
engine = 'huggingface_api_engine', -- engine name as created via CREATE ML_ENGINE
task = 'task_name', -- choose one of 'text-classification', 'text-generation', 'question-answering', 'sentence-similarity', 'zero-shot-classification', 'summarization', 'fill-mask', 'image-classification', 'object-detection', 'automatic-speech-recognition', 'audio-classification'
input_column = 'column_name', -- column that stores input/question to the model
labels = ['label 1', 'label 2']; -- labels used to classify data (used for classification tasks)
The following parameters are supported in the USING
clause of the CREATE MODEL
statement:
Parameter | Required | Description |
---|---|---|
engine | Yes | It is the name of the ML engine created with the CREATE ML_ENGINE statement. |
task | Only if model_name is not provided | It describes a task to be performed. |
model_name | Only if task is not provided | It specifies a model to be used. |
input_column | Yes | It is the name of the column that stores input to the model. |
endpoint | No | It defines the endpoint to use for API calls. If not specified, the hosted Inference API from Hugging Face will be used. |
options | No | It is a JSON object containing additional options to pass to the API call. More information about the available options for each task can be found here. |
parameters | No | It is a JSON object containing additional parameters to pass to the API call. More information about the available parameters for each task can be found here. |
context_column | Only if task is question-answering | It is used for the question-answering task to provide context to the question. |
input_column2 | Only if task is sentence-similarity | It is used for the sentence-similarity task to provide the second input sentence for comparison. |
candidate_labels | Only if task is zero-shot-classification | It is used for the zero-shot-classification task to classify input data according to provided labels. |
Usage
The following usage examples utilize huggingface_api_engine
to create a model with the CREATE MODEL
statement.
Create a model to classify input text as spam or ham.
CREATE MODEL spam_classifier
PREDICT is_spam
USING
engine = 'huggingface_api_engine',
task = 'text-classification',
column = 'text';
Query the model to get predictions.
SELECT text, is_spam
FROM spam_classifier
WHERE text = 'Subscribe to this channel asap';
Here is the output:
+--------------------------------+---------+
| text | is_spam |
+--------------------------------+---------+
| Subscribe to this channel asap | spam |
+--------------------------------+---------+
Find more quick examples below:
Next Steps
Follow this link to see more use case examples.
Was this page helpful?