Sentiment and text classification API.
จำแนกประเภทและหาความรู้สึกของข้อความ
sentiment_cli.py is a small program with command line interface to test how actual classification logic and model is working, without the need to start the entire API server.
Start a prompt waiting for a text to be classified (hit Ctrl-C to quit):
python3 sentiment_cli.py
Classify a text "This is a test":
python3 sentiment_cli.py "This is a test"
Test with predefined sample texts:
python3 sentiment_cli.py -sample
Benchmark the max speed (without network overhead) with 200 messages:
python3 sentiment_cli.py -benchmark 200
Note: Classification models (files with .ftz extensions) should be already in models/ directory. If it's not, run sh build.sh test train first.
Make POST request to these endpoints:
/hello - test if serivce is alive/get-sentiment - sentiment analysis (negative, neutral, positive)/is-question - if text is a question (true, false)/is-rejected - if text should be excluded from an analysis (true, false)/is-salespam -if text is a sale spam (true, false)header must include this:"Authorization": "JWT SOMETOKENS0M3T0K3N"
/get-sentiment
Input:
{
"text" : "มนุษย์ทั้งหลายเกิดมาอิสระเสรีและเท่าเทียมกันทั้งศักดิ์ศรีและสิทธิ
ทุกคนได้รับการประสิทธิประสาทเหตุผลและมโนธรรม
และควรปฏิบัติต่อกันอย่างฉันพี่น้อง"
}
Output:
{
"confidence": 0.6536582112312317,
"has_royal_word": false,
"neg": false,
"neg_confidence": 0.5727977752685547,
"pos": false,
"pos_confidence": 0.7345186471939087,
"value": "neutral"
}
value is a string with three possible values:
/is-question
Input:
{ "text" : "ราคาเท่าไหร่" }
Output:
{
"confidence": 0.9991546869277954,
"value": true
}
/is-rejected
If a text should be rejected as irrelevant and get excluded from the analysis. -- Used internally by Analytic team.
Input:
{ "text" : "ราคาเท่าไหร่" }
Output:
{
"confidence": 0.9223958253860474,
"value": false
}
/is-salespam
Note that the definition of "sale spam" is vary.
Input:
{ "text" : "ราคาเท่าไหร่" }
Output:
{
"confidence": 0.9899575114250183,
"value": false
}
Once the server is started, you can try access it from interactive Python shell or Jupyter Notebook. Notebooks are inside notebooks/ directory.
This notebook shows how to process texts in Excel file and get sentiment of each text: https://github.com/ThothMedia/sentiment-api/blob/master/notebooks/sentiment_excel.ipynb
models/ directorymodel_data/
model_data/MODEL_VERSIONS.txt is used to keep track (manually) of model versions.
model_data/MODEL_VERSIONS.txt will also be used by CircleCI to determine if it can use previously trained (and cached) models or it has to train models again.sh build.sh test train
train.sh
The app will be run in a Docker container.
To understand the build steps, consult these files:
Build scripts:
build.shtest_init_env.sh <-- use this to build all the necessary packages required for the server to runtrain.shDocker configs:
docker/*/Dockerfiledocker-compose.*.ymlCircleCI config:
.circleci/config.ymlSentiment API is running in Docker container. To start the service run
sh build.sh production
Sentiment API running docker container in network_mode: "host" so the port of the machine will be reserved.
Sentiment API in production should be use with Nginx with proper configuration.
Sentiment API will run as a server.
You can make a request using command-line tools like curl and wget. For convenience, we recommend Postman (GUI), as you can save different requests and run them as a collection.
Sentiment API can be run from a Docker container. To start the service, run:
sh build.sh <test|staging>
To bring down the running docker, run:
docker-compose --file docker-compose.<test|staging>.yml down
If you haven't train the classification model yet, or want to retrain it, run:
sh build.sh <test|staging> train
To start the service in no daemon mode, run:
sh build.sh <test|staging> nodaemon
virtualenv -p python3 env
. env/bin/activate
pip install -r requirements.txt
You may also need to install fastText Python binding and datrie manually.
Run this command to start uWSGI
uwsgi sentiment-api.uwsgi.ini
Use vegeta for load testing.
brew update && brew install vegeta
To run the test, go inside tests/ directory and run:
vegeta attack -duration=10s -rate=500 -targets targets.txt | tee results.bin | vegeta report
tests/targets.txt will describe the requests, while the actual input will be in tests/data/ directory.
See the result:
vegeta report results.bin
Plot graph and save to an HTML file:
vegeta plot results.bin > graph.html
Monitor uWSGI stats:
pip install uwsgitop
uwsgitop :3032
train.sh and other scripts will use fasttext executable as accessible from PATH e.g. /usr/local/bin, /usr/binmodels/ directory.ftz (a quantized model)Get access token by following these steps.
POST request to this URL: {hostname}:3031/authcontent-type as application/json and contains this data:{
"username": "tz",
"password": "=*394A6$Q+N)665%"
}
{
"access_token": "SOMETOKENS0M3T0K3N"
}
Content type
Image
Digest
Size
47 MB
Last updated
over 7 years ago
docker pull wisesight/fasttext