-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
57 lines (45 loc) · 1.55 KB
/
app.py
File metadata and controls
57 lines (45 loc) · 1.55 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
import pandas as pd
from flask import Flask, request, jsonify, render_template
from joblib import load
from nltk.corpus import stopwords
import nltk
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')
nltk.download('omw-1.4')
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
import module
# Create flask app
app = Flask(__name__)
vector = load("vectors.joblib")
model = load("model.joblib")
stop_words = stopwords.words('english')
wordnet_lem = WordNetLemmatizer()
@app.route("/")
def Home():
return render_template("index.html")
@app.route("/predict", methods = ["POST"])
def predict():
text=[request.form.get("text")]
df = pd.DataFrame(data=text, columns=['text'])
df['text'] = df['text'].apply(module.cleaning)
# remove stop word:
df['text'] = df['text'].apply(lambda x: ' '.join([word for word in x.split() if word not in (stop_words)]))
df['text'] = df['text'].apply(wordnet_lem.lemmatize)
#prediction
vec = vector.transform(df['text'])
prediction = model.predict(vec)
print(prediction)
prediction = int(prediction)
proba = model.predict_proba(vec)
print("proportion",proba[0][0])
ra=round(proba[0][1],2)
ng=round(proba[0][0],2)
if prediction >0:
prediction="positive"
else:
prediction = "negative"
return render_template("index.html", datas=["sentiment: {} ".format(prediction),"Positive Rating: {} ".format(ra),"Negative Rating: {} ".format(ng)])
if __name__ == "__main__":
app.run(debug=True)