-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
58 lines (36 loc) · 1.47 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Flask App
# Import dependencies
from flask import Flask, render_template, jsonify, redirect, url_for, request
from flask_sqlalchemy import SQLAlchemy
import os
import pickle
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.svm import LinearSVC
# import and load saved transformer and model
vector_path = "main_cnt_vec.pkl"
trans_path = "main_text3_tfidf.pkl"
model_path = "linearSVC.pkl"
with open(vector_path, "rb") as f:
text_vector = pickle.load(f)
with open(trans_path, "rb") as f:
text_transformer = pickle.load(f)
with open(model_path, "rb") as f:
lsvc_model = pickle.load(f)
app = Flask(__name__)
# create route that renders index.html template with prediction app
@app.route("/", methods=['GET', 'POST'])
def index():
"""Returns the homepage with Prediction App"""
if request.method == 'GET':
return render_template('index.html')
if request.method == 'POST':
message = request.form['text']
data_vector = text_vector.transform([message])
data_transform = text_transformer.transform(data_vector)
prediction = lsvc_model.predict(data_transform)
# output_prediction = lsvc_model.predict(data_transform)
# return render_template('result.html', output_prediction = prediction)
return render_template('index.html', output_prediction = prediction)
if __name__ == "__main__":
app.run(debug=True)