Deploying a Sentiment Analysis Text Classifier With FastAPI

FastAPI provides an easy way to create APIs. By combining this with Cohere's LLMs, we can build custom API endpoints to access state-of-the-art NLP

Deploying a Sentiment Analysis Text Classifier With FastAPI

FastAPI has recently been making waves as an easy-to-use Python framework for creating APIs. If you're developing apps with FastAPI, you can add language processing capabilities to it by integrating Cohere's Large Language Models.

In this article, you will learn how to create and finetune a Cohere sentiment analysis classification model, and generate predictions by making API calls to it using FastAPI.

Prerequisites

To follow this tutorial, you will need a Cohere account to generate an API key, create a finetuned model, and generate API calls. You also need a Python coding environment, such as VS Code.

Cohere project

Cohere offers three categories of NLP models — Generate, Embed, and Classify — which allow you to obtain AI-generated text, create graphical representations of words and sentences, and segment pieces of text into defined buckets or classes. You can access all these NLP applications through a well-documented, intuitive user interface. It even offers a “Playground” feature, with examples of each use case.

First, create a Cohere account and generate an API key. Cohere offers a $75 credit upon signup for use on API calls to baseline and finetuned models.

API keys are found on the Cohere dashboard.

With Cohere, you get an API with /generate, /embed, /classify and /tokenize endpoints, as well as Python, Node, and Go client SDKs.

Creating a Sentiment Analysis Classifier

To use the baseline Cohere models and create a sentiment analysis classifier, you only have to provide a list of text to classify (up to 32 input texts), an array of labeled examples of your text, and the expected classifications or sentiments. You can provide up to 50 samples for baseline models.

Additionally, you can declare optional variables to choose the model’s size and add model descriptors and prompts that help model prediction. Check the Cohere documentation for more information.

Open a terminal in your Python coding environment and input the following command to install the Cohere package locally.

pip install cohere

You can now call our API in your Python code. Here's an example of what that call would look like. We'll use this code later on.

import cohere
from cohere.classify import Example
co = cohere.Client('{apiKey}')
response = co.classify(
model='xlarge',
taskDescription='Classify Product reviews into positive, negative, and neutral',
outputIndicator='Classify this Product Review',
inputs=["Delivery was late", "This item came damaged"],
examples=[Example("The order came 5 days early", "positive"), Example("The item exceeded my expectations", "positive"), Example("I ordered more for my friends", "positive"), Example("I would buy this again", "positive"), Example("I would recommend this to others", "positive"), Example("The package was damaged", "negative"), Example("The order is 5 days late", "negative"), Example("The order was incorrect", "negative"), Example("I want to return my item", "negative"), Example("The item\'s material feels low quality", "negative"), Example("The item was nothing special", "neutral"), Example("I would not buy this again but it wasn\'t a waste of money", "neutral"), Example("The item was neither amazing or terrible", "neutral"), Example("The item was okay", "neutral"), Example("I have no emotions towards this item", "neutral")])
print('The confidence levels of the labels are: {}'.format(
response.classifications))

This creates our largest Classification model instance that predicts the sentiments of two text inputs based on 15 examples and the Cohere baseline classification model. When run, it outputs the input texts, the predicted sentiments, and the confidence levels of the predictions.

Building a FastAPI Endpoint for Your Cohere Model

Now let's create a FastAPI wrapper around that code to extend the model as an API endpoint.

First, install FastAPI with the following line.

pip install fastapi[all]

This command installs the FastAPI package — Uvicorn — a compatible server interface for asynchronous frameworks and other dependencies.

Next, create a Python file named sentiment_classifier.py and input the following code with your API key.

from fastapi import FastAPI
from pydantic import BaseModel, conlist
import cohere
from cohere.classify import Example

co = cohere.Client('{apiKey}')
app = FastAPI()

class ProductReviews(BaseModel):
reviews: conlist(str, min_items=1)
@app.post("/baselineprediction")

def predict_sentiment(product_reviews: ProductReviews):
response = co.classify(
model='xlarge',
taskDescription='Classify Product reviews into positive, negative, and neutral',
outputIndicator='Classify this Product Review',
inputs=product_reviews.reviews,
examples=[Example("The order came 5 days early", "positive"), Example("The item exceeded my expectations", "positive"), Example("I ordered more for my friends", "positive"), Example("I would buy this again", "positive"), Example("I would recommend this to others", "positive"), Example("The package was damaged", "negative"), Example("The order is 5 days late", "negative"), Example("The order was incorrect", "negative"), Example("I want to return my item", "negative"), Example("The item\'s material feels low quality", "negative"), Example("The item was nothing special", "neutral"), Example("I would not buy this again but it wasn\'t a waste of money", "neutral"), Example("The item was neither amazing or terrible", "neutral"), Example("The item was okay", "neutral"), Example("I have no emotions towards this item", "neutral")])

return response.classifications

This imports FastAPI and Cohere as well as Pydantic for structuring inputs to the API. A FastAPI path or endpoint, /baselineprediction, is created for a function that receives the user’s inputted list of strings and runs it through the Cohere baseline model for predicted classifications. The second half of the above code is the same code we used earlier, wrapped inside FastAPI.

To test this out locally, use the Uvicorn package to spin up a server for the FastAPI extension.

Switch your terminal working directory to the location of your saved Python file, then input this command.

uvicorn sentiment_classifier:app

This opens up a server on your localhost.


Go to http://127.0.0.1:8000/docs in a browser — FastAPI offers easy documentation based on OpenAPI standards showcasing API endpoints, sample values, responses, and their types.


This also allows you to test your API endpoints with curl commands. Click the Try it out button and add comma-separated text to the Request body section.


Click Execute. A sample curl command of your request is shown, along with the API call results.

Finetuning Your Cohere Model

Cohere enables you to improve your baseline model, optimizing the predictions to fit your use case. While you can use baseline models with up to 50 examples, it’s recommended to create a finetuned model with at least 250 use case examples (though we advise 500).

For our sentiment analysis use case, lets use the Google GoEmotions dataset, which offers 28 label categories of text and their emotional context.

There are some general guidelines to follow when finetuning. First, all labels should have at least five examples to be included in the final model. This also ensures balanced label examples to avoid overfitting. Second, the dataset should be in a CSV format with only two columns — one for examples and one for labels. Finally, it is recommended to have at least 250 examples in total to create your fine-tuned model.

To conform to these guidelines, use a formatted version of the GoEmotions dataset.

Now, go to the Cohere dashboard to create your fine-tuned model.


Click Create finetune. Select Representation (Embed, Classify) under model type and xlarge under model size. Then, upload your downloaded GoEmotions dataset.

Click Preview data. This offers a visual sample of your dataset, allowing you to catch any mistakes.


Click the Remove column headings checkbox and then Review data. Cohere will validate the data, ensuring it passes expected checks. For example, duplicated rows will be trimmed.

Click Continue. Name your finetuned model and start the finetuning process.


Depending on the size of your dataset and the backlog at Cohere, the finetuning process may take some time. An email will be sent to your account when the model is ready.

Once that’s done, copy the link to the model from your Cohere dashboard. This serves as an identifier for your fine-tuned model.

Calling Your Finetuned Model Via FastAPI

Using your previously named sentiment_classifier.py, tweak the predict_sentiment function to make two API calls generating two response sets, one from your baseline and one from the fine-tuned model.

from fastapi import FastAPI
from pydantic import BaseModel, conlist
import cohere
from cohere.classify import Example

co = cohere.Client('{apiKey}')
app = FastAPI()

class ProductReviews(BaseModel):
reviews: conlist(str, min_items=1)
@app.post("/baselineprediction")

def predict_sentiment(product_reviews: ProductReviews):
response = co.classify(
model='xlarge',
taskDescription='Classify Product reviews into positive, negative, and neutral',
outputIndicator='Classify this Product Review',
inputs=product_reviews.reviews,
examples=[Example("The order came 5 days early", "positive"), Example("The item exceeded my expectations", "positive"), Example("I ordered more for my friends", "positive"), Example("I would buy this again", "positive"), Example("I would recommend this to others", "positive"), Example("The package was damaged", "negative"), Example("The order is 5 days late", "negative"), Example("The order was incorrect", "negative"), Example("I want to return my item", "negative"), Example("The item\'s material feels low quality", "negative"), Example("The item was nothing special", "neutral"), Example("I would not buy this again but it wasn\'t a waste of money", "neutral"), Example("The item was neither amazing or terrible", "neutral"), Example("The item was okay", "neutral"), Example("I have no emotions towards this item", "neutral")])

finetuned_response = co.classify(
model="{model_id}",
taskDescription='Classify Product reviews according to GoEmotions Dataset',
outputIndicator='Classify this Product Review',
inputs=product_reviews.reviews
)

return {"baseline" : response.classifications, "finetune" : finetuned_response.classifications}

Spin up a Uvicorn server again, go to http://127.0.0.1:8000/docs and try out the model endpoint.

This endpoint produces results from both models, classifying your inputs as positive, negative or neutral, as well as the 28 GoEmotions labels that include anger, surprise, admiration, and so on.

Conclusion

In this tutorial, you used the Cohere baseline classification model, created your own fine-tuned model, and generated a FastAPI endpoint that queries both models for sentiment predictions.

You can now use Cohere and FastAPI to create API endpoints for users or other parts of your application, streamlining your development process.
Cohere offers an extensive NLP toolkit with various real-world applications, which you can extend with FastAPI. Check out Cohere today to get started.