Skip to main content

Get your prediction in 3 steps

This guide walks you through setting up the microservice and making your first emotion prediction request.
1

Install dependencies

Create a virtual environment and install the required packages:
python3 -m venv text_prediction
source text_prediction/bin/activate
pip3 install -r requirements.txt
The installation includes PyTorch, Flask, NLTK, scikit-learn, and other ML libraries. This may take a few minutes.
2

Start the microservice

Run the Flask server to start accepting prediction requests:
python3 microservice.py
You should see output confirming the server is running:
Microserver running in port 3200
 * Serving Flask app 'microservice'
 * Running on http://127.0.0.1:3200
The service loads three pre-trained models on startup:
  • Word embedding dictionary (models/word_dict.json)
  • PyTorch neural network (models/model_26_87.12.pth)
  • Emotion classifier (models/emotion_classifier.model)
  • TF-IDF vectorizer (models/vectorizer2.pickle)
3

Make your first prediction

Send a GET request to the /textbased_emotion endpoint with your text:
curl "http://127.0.0.1:3200/textbased_emotion?text=This%20is%20a%20wonderful%20day"
The endpoint returns an HTML page with prediction results showing:
  • Whether the text contains inappropriate language
  • Emotion classification (Positive/Negative)
  • Scores for all six toxicity categories (toxic, severe_toxic, obscene, threat, insult, identity_hate)
  • Additional extracted entities (countries, people names, dates)

Understanding the response

The microservice processes your text through multiple stages:
  1. Text preprocessing (microservice.py:94-120)
    • Converts to lowercase
    • Expands contractions (“can’t” → “cannot”)
    • Removes hyperlinks and special characters
    • Applies lemmatization
  2. Neural network inference (microservice.py:203-204)
    • Converts cleaned text to word embeddings
    • Passes through the 4-layer neural network
    • Generates six probability scores
  3. Toxicity classification (microservice.py:206-219)
    • Applies 0.29 threshold to each score
    • Returns “Yes”/“No” for each category
    • Flags as inappropriate if all six categories are positive
  4. Sentiment analysis (microservice.py:221-229)
    • Vectorizes text using TF-IDF
    • Classifies overall emotion as Positive or Negative
    • Adjusts based on inappropriate content detection
The threshold value of 0.29 (microservice.py:206) balances precision and recall based on validation set performance.

Try different examples

Test the model with various text inputs:
# Positive sentiment
curl "http://127.0.0.1:3200/textbased_emotion?text=I%20love%20this%20amazing%20product"

# Negative sentiment
curl "http://127.0.0.1:3200/textbased_emotion?text=This%20is%20terrible%20and%20disappointing"

# Text with entities
curl "http://127.0.0.1:3200/textbased_emotion?text=Meeting%20in%20Paris%20on%20March%2020,%202024"

Next steps

Installation guide

Learn about detailed setup options and model file requirements

Model architecture

Dive deeper into the neural network design and training

Build docs developers (and LLMs) love