forked from Aayush452-cell/New-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
68 lines (53 loc) · 1.99 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
59
60
61
62
63
64
65
66
67
68
from flask import Flask, render_template, request
import jsonify
import requests
import pickle
from sklearn.feature_extraction.text import CountVectorizer
import tensorflow
from nltk.stem.porter import PorterStemmer
from nltk.corpus import stopwords
import nltk
nltk.download(['punkt', 'wordnet'])
import re
import numpy as np
import pandas as pd
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
app = Flask(__name__)
model = pickle.load(open('cyber_bullying_prediction_model.pkl', 'rb'))
vectorizer = pickle.load(open('vectorizer.pkl', 'rb'))
tfidrizer = pickle.load(open('tfidrizer.pkl', 'rb'))
@app.route('/',methods=['GET'])
def Home():
return render_template('index.html')
'''def tokenize(text):
tokens = word_tokenize(text)
lemmatizer = WordNetLemmatizer()
clean_tokens = []
for tok in tokens:
clean_tok = lemmatizer.lemmatize(tok).lower().strip()
clean_tokens.append(clean_tok)
return clean_tokens'''
@app.route("/predict", methods=['POST'])
def predict():
if request.method == 'POST':
msg = request.form['msg']
tfidf = TfidfTransformer()
X_test_counts = vectorizer.transform([msg])
X_test_tfidf = tfidrizer.transform(X_test_counts)
prediction = model.predict(X_test_tfidf)
output=prediction[0]
print(output)
if output == '1':
return render_template('index.html',prediction_text="It contains wrong words")
else:
return render_template('index.html',prediction_text="It is all right")
else:
return render_template('index.html')
if __name__=="__main__":
vectorizer = pickle.load(open('vectorizer.pkl', 'rb'))
app.run(debug=True)