File size: 1,488 Bytes
24e6e4b
 
 
 
 
 
 
 
 
 
 
 
aea0af1
24e6e4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#Develop an API server on python using Fast API for the model created in the previous step.

from string import punctuation 
from nltk.tokenize import word_tokenize
import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from os.path import dirname, join, realpath
import joblib
import uvicorn
from fastapi import FastAPI
import requests as r

#from pyramid_swagger import add_swagger_view






app = FastAPI(
    title="Sentiment Analysis API",
    description="A simple API that use NLP model to predict the sentiment of the airline reviews",
    version="0.1",
)

# Load the model
model = joblib.load('sentiment_classifier.pkl')
vectorizer = joblib.load('vectorizer.pkl')

class Inference:
    def __init__(self, model, vectorizer):
        self.model = model
        self.vectorizer = vectorizer

    def get_sentiment(self, review):
        new_review = [review]
        new_review = self.vectorizer.transform(new_review)
        pred = self.model.predict(new_review)
        if pred == 1:
            return 'Positive'
        else:
            return 'Negative'

inference = Inference(model, vectorizer)

@app.get("/")
def home():
    return {"message": "Welcome to Sentiment Analysis API"}

@app.get("/predict-review/{review}")
def predict_sentiment(review: str):
    return {"sentiment": inference.get_sentiment(review)}

#app.include_router(swagger_ui_bundle, tags=["Swagger UI"])
#app.include_router(swagger_ui_expose, tags=["Swagger UI"])