Aspect setiment triplet extraction
Flask Server
[ ]:
!pip install pyabsa
!pip install flask
[3]:
from flask import Flask, request, jsonify
from pyabsa import AspectSentimentTripletExtraction as ASTE
app = Flask(__name__)
@app.route("/predict", methods=["POST"])
def predict():
# Load the model
triplet_extractor = ASTE.AspectSentimentTripletExtractor("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=6000, debug=True)
* Serving Flask app '__main__'
* Debug mode: on
C:\Users\chuan\miniconda3\lib\site-packages\werkzeug\serving.py:718: ResourceWarning: unclosed <socket.socket fd=1632, 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
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
}