Usage Examples of Hugging Face Models Through Inference API
This document presents various use cases of Hugging Face models through Inference API from MindsDB.
Spam Classifier
Here is an example of a binary classification. The model determines whether a text string is spam or not.
CREATE MODEL mindsdb.spam_classifier
PREDICT PRED
USING
engine = 'hf_inference_api',
task = 'text-classification',
column = 'text_spammy';
Before querying for predictions, we should verify the status of the spam_classifier
model.
DESCRIBE spam_classifier;
On execution, we get:
+---------------+-------+--------+--------+-------+-------------+---------------+------+-----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|NAME |PROJECT|STATUS |ACCURACY|PREDICT|UPDATE_STATUS|MINDSDB_VERSION|ERROR |SELECT_DATA_QUERY|TRAINING_OPTIONS |
+---------------+-------+--------+--------+-------+-------------+---------------+------+-----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|spam_classifier|mindsdb|complete|[NULL] |PRED |up_to_date |22.10.2.1 |[NULL]|[NULL] |{'target': 'PRED', 'using': {'engine': 'huggingface', 'task': 'text-classification', 'model_name': 'mrm8488/bert-tiny-finetuned-sms-spam-detection', 'input_column': 'text_spammy', 'labels': ['ham', 'spam']}}|
+---------------+-------+--------+--------+-------+-------------+---------------+------+-----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Once the status is complete
, we can query for predictions.
SELECT h.*, t.text_spammy AS input_text
FROM example_db.demo_data.hf_test AS t
JOIN mindsdb.spam_classifier AS h;
On execution, we get:
+----+---------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
|PRED|PRED_explain |input_text |
+----+---------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
|spam|{'spam': 0.9051626920700073, 'ham': 0.09483727067708969} |Free entry in 2 a wkly comp to win FA Cup final tkts 21st May 2005. Text FA to 87121 to receive entry question(std txt rate)T&C's apply 08452810075over18's |
|ham |{'ham': 0.9380123615264893, 'spam': 0.061987683176994324}|Nah I don't think he goes to usf, he lives around here though |
|spam|{'spam': 0.9064534902572632, 'ham': 0.09354648739099503} |WINNER!! As a valued network customer you have been selected to receive a £900 prize reward! To claim call 09061701461. Claim code KL341. Valid 12 hours only. |
+----+---------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
Sentiment Classifier
Here is an example of a multi-value classification. The model determines the sentiment of a text string, where possible values are negative
, neutral
, and positive
.
CREATE MODEL mindsdb.sentiment_classifier
PREDICT sentiment
USING
engine = 'hf_inference_api',
task = 'text-classification',
column = 'text_short',
labels = ['negative', 'neutral', 'positive'];
Before querying for predictions, we should verify the status of the sentiment_classifier
model.
DESCRIBE sentiment_classifier;
On execution, we get:
+--------------------+-------+--------+--------+---------+-------------+---------------+------+-----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|NAME |PROJECT|STATUS |ACCURACY|PREDICT |UPDATE_STATUS|MINDSDB_VERSION|ERROR |SELECT_DATA_QUERY|TRAINING_OPTIONS |
+--------------------+-------+--------+--------+---------+-------------+---------------+------+-----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|sentiment_classifier|mindsdb|complete|[NULL] |sentiment|up_to_date |22.10.2.1 |[NULL]|[NULL] |{'target': 'sentiment', 'using': {'engine': 'huggingface', 'task': 'text-classification', 'model_name': 'cardiffnlp/twitter-roberta-base-sentiment', 'input_column': 'text_short', 'labels': ['negative', 'neutral', 'positive']}}|
+--------------------+-------+--------+--------+---------+-------------+---------------+------+-----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Once the status is complete
, we can query for predictions.
SELECT h.*, t.text_short AS input_text
FROM example_db.demo_data.hf_test AS t
JOIN mindsdb.sentiment_classifier AS h;
On execution, we get:
+---------+----------------------------------------------------------------------------------------------------+-------------------+
|sentiment|sentiment_explain |input_text |
+---------+----------------------------------------------------------------------------------------------------+-------------------+
|negative |{'negative': 0.9679920077323914, 'neutral': 0.02736542373895645, 'positive': 0.0046426113694906235} |I hate tacos |
|positive |{'positive': 0.7607280015945435, 'neutral': 0.2332666665315628, 'negative': 0.006005281116813421} |I want to dance |
|positive |{'positive': 0.9835041761398315, 'neutral': 0.014900505542755127, 'negative': 0.0015953202964738011}|Baking is the best |
+---------+----------------------------------------------------------------------------------------------------+-------------------+
Zero-Shot Classifier
Here is an example of a zero-shot classification. The model determines to which of the defined categories a text string belongs.
CREATE MODEL mindsdb.zero_shot_tcd
PREDICT topic
USING
engine = 'hf_inference_api',
task = 'zero-shot-classification',
candidate_labels = ['travel', 'cooking', 'dancing'],
column = 'text_short';
Before querying for predictions, we should verify the status of the zero_shot_tcd
model.
DESCRIBE zero_shot_tcd;
On execution, we get:
+-------------+-------+--------+--------+--------+-------------+---------------+------+-----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|NAME |PROJECT|STATUS |ACCURACY|PREDICT |UPDATE_STATUS|MINDSDB_VERSION|ERROR |SELECT_DATA_QUERY|TRAINING_OPTIONS |
+-------------+-------+--------+--------+--------+-------------+---------------+------+-----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|zero_shot_tcd|mindsdb|complete|[NULL] |topic |up_to_date |22.10.2.1 |[NULL]|[NULL] |{'target': 'topic', 'using': {'engine': 'huggingface', 'task': 'zero-shot-classification', 'model_name': 'facebook/bart-large-mnli', 'input_column': 'text_short', 'candidate_labels': ['travel', 'cooking', 'dancing']}}|
+-------------+-------+--------+--------+--------+-------------+---------------+------+-----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Once the status is complete
, we can query for predictions.
SELECT h.*, t.text_short AS input_text
FROM example_db.demo_data.hf_test AS t
JOIN mindsdb.zero_shot_tcd AS h;
On execution, we get:
+-------+--------------------------------------------------------------------------------------------------+-------------------+
|topic |topic_explain |input_text |
+-------+--------------------------------------------------------------------------------------------------+-------------------+
|cooking|{'cooking': 0.7530364990234375, 'travel': 0.1607145369052887, 'dancing': 0.08624900877475739} |I hate tacos |
|dancing|{'dancing': 0.9746809601783752, 'travel': 0.015539299696683884, 'cooking': 0.009779711253941059} |I want to dance |
|cooking|{'cooking': 0.9936348795890808, 'travel': 0.0034196735359728336, 'dancing': 0.0029454431496560574}|Baking is the best |
+-------+--------------------------------------------------------------------------------------------------+-------------------+
Summarization
Here is an example of input text summarization.
CREATE MODEL mindsdb.summarizer_10_20
PREDICT text_summary
USING
engine = 'hf_inference_api',
task = 'summarization',
column = 'text_long',
min_output_length = 10,
max_output_length = 20;
Before querying for predictions, we should verify the status of the summarizer_10_20
model.
DESCRIBE summarizer_10_20;
On execution, we get:
+----------------+-------+--------+--------+------------+-------------+---------------+------+-----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|NAME |PROJECT|STATUS |ACCURACY|PREDICT |UPDATE_STATUS|MINDSDB_VERSION|ERROR |SELECT_DATA_QUERY|TRAINING_OPTIONS |
+----------------+-------+--------+--------+------------+-------------+---------------+------+-----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|summarizer_10_20|mindsdb|complete|[NULL] |text_summary|up_to_date |22.10.2.1 |[NULL]|[NULL] |{'target': 'text_summary', 'using': {'engine': 'huggingface', 'task': 'summarization', 'model_name': 'sshleifer/distilbart-cnn-12-6', 'input_column': 'text_long', 'min_output_length': 10, 'max_output_length': 20}}|
+----------------+-------+--------+--------+------------+-------------+---------------+------+-----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Once the status is complete
, we can query for predictions.
SELECT h.*, t.text_long AS input_text
FROM example_db.demo_data.hf_test AS t
JOIN mindsdb.summarizer_10_20 AS h;
On execution, we get:
+--------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|text_summary |input_text |
+--------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|A taco is a traditional Mexican food consisting of a small hand-sized corn- or |A taco is a traditional Mexican food consisting of a small hand-sized corn- or wheat-based tortilla topped with a filling. The tortilla is then folded around the filling and eaten by hand. A taco can be made with a variety of fillings, including beef, pork, chicken, seafood, beans, vegetables, and cheese, allowing for great versatility and variety. |
|Dance is a performing art form consisting of sequences of movement, either improvised or purposefully selected|Dance is a performing art form consisting of sequences of movement, either improvised or purposefully selected. This movement has aesthetic and often symbolic value.[nb 1] Dance can be categorized and described by its choreography, by its repertoire of movements, or by its historical period or place of origin. |
|Baking is a method of preparing food that uses dry heat, typically in an oven |Baking is a method of preparing food that uses dry heat, typically in an oven, but can also be done in hot ashes, or on hot stones. The most common baked item is bread but many other types of foods can be baked. Heat is gradually transferred from the surface of cakes, cookies, and pieces of bread to their center. As heat travels through, it transforms batters and doughs into baked goods and more with a firm dry crust and a softer center. Baking can be combined with grilling to produce a hybrid barbecue variant by using both methods simultaneously, or one after the other. Baking is related to barbecuing because the concept of the masonry oven is similar to that of a smoke pit.|
+--------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Fill Mask
Here is an example of a masked language modeling task.
CREATE MODEL mindsdb.fill_mask
PREDICT text_filled
USING
engine = 'hf_inference_api',
task = 'fill-mask',
column = 'text';
Before querying for predictions, we should verify the status of the fill_mask
model.
DESCRIBE fill_mask;
On execution, we get:
+----------------+-------+--------+--------+------------+-------------+---------------+------+-----------------+--------------------------------------------------------------------------------------------------------------------+
|NAME |PROJECT|STATUS |ACCURACY|PREDICT |UPDATE_STATUS|MINDSDB_VERSION|ERROR |SELECT_DATA_QUERY|TRAINING_OPTIONS |
+----------------+-------+--------+--------+------------+-------------+---------------+------+-----------------+--------------------------------------------------------------------------------------------------------------------+
|fill_mask |mindsdb|complete|[NULL] |text_filled |up_to_date |23.3.5.0 |[NULL]|[NULL] |{'target': 'text_filled', 'using': {'task': 'fill-mask', 'model_name': 'bert-base-uncased', 'input_column': 'text'}}|
+----------------+-------+--------+--------+------------+-------------+---------------+------+-----------------+--------------------------------------------------------------------------------------------------------------------+
Once the status is complete
, we can query for predictions.
SELECT h.*, t.text AS input_text
FROM demo.texts AS t
JOIN mindsdb.fill_mask AS h;
On execution, we get:
+-------------------------+---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|text_filled |input_text |text_filled_explain |
+-------------------------+---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|the food was great! |The [MASK] was great! |{'the food was great!': 0.16309359669685364, 'the party was great!': 0.06305009871721268, 'the fun was great!': 0.04633583873510361, 'the show was great!': 0.043319422751665115, 'the music was great!': 0.02990395948290825} |
|the weather is good today|The weather is [MASK] today|{'the weather is good today': 0.22563229501247406, 'the weather is warm today': 0.07954009622335434, 'the weather is fine today': 0.047255873680114746, 'the weather is better today': 0.034303560853004456, 'the weather is mild today': 0.03092862293124199}|
+-------------------------+---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Was this page helpful?