Aspect Term Extraction and Sentiment Classification

Flask Server

[ ]:
!pip install pyabsa
!pip install flask
[1]:
from flask import Flask, request, jsonify
from pyabsa import AspectTermExtraction as ATEPC

app = Flask(__name__)


@app.route("/predict", methods=["POST"])
def predict():
    # Load the model
    triplet_extractor = ATEPC.AspectExtractor("multilingual")

    # Get the text from the request
    data = request.get_json(force=True)
    text = data["text"]

    # Predict
    result = triplet_extractor.predict(text)

    return jsonify(result)


app.run(port=5000, debug=True)
C:\Users\chuan\miniconda3\lib\site-packages\torch\utils\tensorboard\__init__.py:4: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  if not hasattr(tensorboard, "__version__") or LooseVersion(
[2023-03-12 18:53:59] (2.1.6) PyABSA(2.1.6):
[New Feature] Aspect Sentiment Triplet Extraction from v2.1.0 test version (https://github.com/yangheng95/PyABSA/tree/v2/examples-v2/aspect_sentiment_triplet_extration)

If you find any problems, please report them on GitHub. Thanks!
The v2.x versions are not compatible with Google Colab. Please downgrade to 1.16.27.

 * Serving Flask app '__main__'
 * Debug mode: on
C:\Users\chuan\miniconda3\lib\multiprocessing\pool.py:268: ResourceWarning: unclosed running multiprocessing pool <multiprocessing.pool.Pool state=RUN pool_size=1>
  _warn(f"unclosed running multiprocessing pool {self!r}",
ResourceWarning: Enable tracemalloc to get the object allocation traceback
C:\Users\chuan\miniconda3\lib\site-packages\werkzeug\serving.py:718: ResourceWarning: unclosed <socket.socket fd=5952, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0>
  self.socket = socket.fromfd(fd, address_family, socket.SOCK_STREAM)
ResourceWarning: Enable tracemalloc to get the object allocation traceback
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
 * Restarting with stat
An exception has occurred, use %tb to see the full traceback.

SystemExit: 1

C:\Users\chuan\miniconda3\lib\site-packages\IPython\core\interactiveshell.py:3468: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

API Request

[4]:
import requests
import json

url = "http://localhost:5000/predict"
data = {"text": "The food is good, but the service is bad."}
headers = {"Content-type": "application/json", "Accept": "text/plain"}
r = requests.post(url, data=json.dumps(data), headers=headers)
print(r.text)
{
  "Triplets": [
    {
      "Aspect": "food",
      "Opinion": "good,",
      "Polarity": "Positive"
    },
    {
      "Aspect": "food",
      "Opinion": "bad.",
      "Polarity": "Negative"
    },
    {
      "Aspect": "service",
      "Opinion": "good,",
      "Polarity": "Positive"
    },
    {
      "Aspect": "service",
      "Opinion": "bad.",
      "Polarity": "Negative"
    }
  ],
  "True Triplets": [],
  "sentence": "The food is good, but the service is bad.",
  "sentence_id": 0
}