Created
October 23, 2021 04:10
-
-
Save rtkilian/0576ca4b938c20f7d727e89a1dbf0291 to your computer and use it in GitHub Desktop.
A Flask API to classify the sentiment of a review using spaCyTextBlob
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request | |
from flask_restful import Resource, Api, reqparse | |
import spacy | |
from spacytextblob.spacytextblob import SpacyTextBlob | |
nlp = spacy.load('en_core_web_sm') | |
nlp.add_pipe('spacytextblob') | |
app = Flask(__name__) | |
api = Api(app) | |
parser = reqparse.RequestParser() # used to parse incoming requests | |
parser.add_argument('review', required=True, | |
help='Review cannot be blank!') | |
class PredictSentiment(Resource): | |
def post(self): | |
args = parser.parse_args() | |
review = args['review'] | |
doc = nlp(review) | |
score = doc._.polarity | |
return {'score': score} | |
api.add_resource(PredictSentiment, '/predict') | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment