-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathapi.py
More file actions
147 lines (111 loc) · 4.14 KB
/
api.py
File metadata and controls
147 lines (111 loc) · 4.14 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from flask import Flask, request, jsonify, send_file, render_template
import re
from io import BytesIO
# nltk.download('stopwords')
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
import matplotlib.pyplot as plt
import pandas as pd
import pickle
import base64
STOPWORDS = set(stopwords.words("english"))
app = Flask(__name__)
@app.route("/test", methods=["GET"])
def test():
return "Test request received successfully. Service is running."
@app.route("/", methods=["GET", "POST"])
def home():
return render_template("landing.html")
@app.route("/predict", methods=["POST"])
def predict():
# Select the predictor to be loaded from Models folder
predictor = pickle.load(open(r"Models/model_xgb.pkl", "rb"))
scaler = pickle.load(open(r"Models/scaler.pkl", "rb"))
cv = pickle.load(open(r"Models/countVectorizer.pkl", "rb"))
try:
# Check if the request contains a file (for bulk prediction) or text input
if "file" in request.files:
# Bulk prediction from CSV file
file = request.files["file"]
data = pd.read_csv(file)
predictions, graph = bulk_prediction(predictor, scaler, cv, data)
response = send_file(
predictions,
mimetype="text/csv",
as_attachment=True,
download_name="Predictions.csv",
)
response.headers["X-Graph-Exists"] = "true"
response.headers["X-Graph-Data"] = base64.b64encode(
graph.getbuffer()
).decode("ascii")
return response
elif "text" in request.json:
# Single string prediction
text_input = request.json["text"]
predicted_sentiment = single_prediction(predictor, scaler, cv, text_input)
return jsonify({"prediction": predicted_sentiment})
except Exception as e:
return jsonify({"error": str(e)})
def single_prediction(predictor, scaler, cv, text_input):
corpus = []
stemmer = PorterStemmer()
review = re.sub("[^a-zA-Z]", " ", text_input)
review = review.lower().split()
review = [stemmer.stem(word) for word in review if not word in STOPWORDS]
review = " ".join(review)
corpus.append(review)
X_prediction = cv.transform(corpus).toarray()
X_prediction_scl = scaler.transform(X_prediction)
y_predictions = predictor.predict_proba(X_prediction_scl)
y_predictions = y_predictions.argmax(axis=1)[0]
return "Positive" if y_predictions == 1 else "Negative"
def bulk_prediction(predictor, scaler, cv, data):
corpus = []
stemmer = PorterStemmer()
for i in range(0, data.shape[0]):
review = re.sub("[^a-zA-Z]", " ", data.iloc[i]["Sentence"])
review = review.lower().split()
review = [stemmer.stem(word) for word in review if not word in STOPWORDS]
review = " ".join(review)
corpus.append(review)
X_prediction = cv.transform(corpus).toarray()
X_prediction_scl = scaler.transform(X_prediction)
y_predictions = predictor.predict_proba(X_prediction_scl)
y_predictions = y_predictions.argmax(axis=1)
y_predictions = list(map(sentiment_mapping, y_predictions))
data["Predicted sentiment"] = y_predictions
predictions_csv = BytesIO()
data.to_csv(predictions_csv, index=False)
predictions_csv.seek(0)
graph = get_distribution_graph(data)
return predictions_csv, graph
def get_distribution_graph(data):
fig = plt.figure(figsize=(5, 5))
colors = ("green", "red")
wp = {"linewidth": 1, "edgecolor": "black"}
tags = data["Predicted sentiment"].value_counts()
explode = (0.01, 0.01)
tags.plot(
kind="pie",
autopct="%1.1f%%",
shadow=True,
colors=colors,
startangle=90,
wedgeprops=wp,
explode=explode,
title="Sentiment Distribution",
xlabel="",
ylabel="",
)
graph = BytesIO()
plt.savefig(graph, format="png")
plt.close()
return graph
def sentiment_mapping(x):
if x == 1:
return "Positive"
else:
return "Negative"
if __name__ == "__main__":
app.run(port=5000, debug=True)