forked from Niketkumardheeryan/ML-CaPsule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
42 lines (31 loc) · 1.68 KB
/
app.py
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
# Importing essential libraries
from flask import Flask, render_template, request, redirect, url_for
import pickle
import numpy as np
import nltk
# load pickle files
filename = 'nlp.pkl'
classifier = pickle.load(open(filename, 'rb')) ## classfier model
cv = pickle.load(open('transform.pkl', 'rb')) ## this is used for transforming the data using CV class
app = Flask(__name__) ## Flask constructor takes the name of current module (__name__) as argument
@app.route('/')
def home():
return render_template('home.html') ## inital home page
@app.route('/predict', methods=['POST'])
def predict():
errors=[] ## for finding out errors
if request.method == 'POST':
try: ## try block starts here
message = request.form['text'] ## get the data from home.html
data = [message]
vect = cv.transform(data).toarray() ## transform them to array for prediction
prediction = classifier.predict(vect) ## predict over the model
string = " " ## an empty string , will be used for getting the emotion
emotion = string.join(prediction) ## join to return the answer
except:
errors.append(
"Unable to get URL. Please make sure it's valid and try again." ## If any errors would , then this block would execute
)
return render_template('home.html', prediction=emotion, errors=errors) ## render the page for the user
if __name__ == '__main__': ## Execution starts here
app.run(debug=True)