Developing endpoint API for NLP Model

Mulianaraul
2 min readMar 7, 2021

Let’s Code!

In this case, i tried to developing an endpoint of an API for NLP Model. The goals of this article is implemented an endpoint called “feedback”. How the “/feedack” endpoint should work?

“feedback” endpoint solution

An “/feedback” endpoint should receives an JSON input something like this:

{
"text": "im glad ur doing well",
"sentiment": "positive"
}

So, what task should be done from “/feedback” endpoint?

  • It checks if the text exists in either positive.txt or negative.txt (just do simple string matching). If it exists, we ignore the request, i.e. do nothing and return something like:
{
"data": {
"text": "im glad ur doing well",
"sentiment": "positive",
"msg": "We have it already!"
}
}

If it doesn’t exist, then add it to the suitable file (either positive.txt or negative.txt) and return something like:

{
"data": {
"text": "im glad ur doing well",
"sentiment": "positive",
"msg": "Your feedback is well received!"
}
}
  • Keep track of how many new data are added. Hint: Don’t store it as a variable, but in an output file (Why?). For example, you can create a file called count_new_data_added.txt and update it every time you receive a feedback and add new data.
  • Retrain your model (feature extractor and classifier) whenever 10 new data is added! Hint: Call train.main() function to retrain.

Here is the code solution snapshot!

app.py file for an API

Note: In practice, 10 data is insignificant. We usually train if the data size grows 5-10% larger from the previous version. In our case, that means 5-10k data. We don't want to retrain too often as it waste resources. Also, it's common in industry to retrain periodically as well (every 2 weeks for example).

--

--