-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
53 lines (41 loc) · 1.32 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
# importing libraries
from flask import Flask, request, render_template
import sklearn
import pickle
import pandas as pd
import re
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
app = Flask(__name__)
@app.route('/')
def home():
return render_template("home.html")
@app.route("/predict", methods = ["POST"])
def predict():
# loading the dataset
data = pd.read_csv("data/small_vocab_en-lg.csv")
y = data["Language"]
# label encoding
y = le.fit_transform(y)
#loading the model and cv
model = pickle.load(open("model.pkl", "rb"))
cv = pickle.load(open("transform.pkl", "rb"))
if request.method == "POST":
# taking the input
text = request.form["text"]
# preprocessing the text
text = re.sub(r'[!@#$(),\n"%^*?\:;~`0-9]', '', text)
text = re.sub(r'[[]]', '', text)
text = text.lower()
dat = [text]
# creating the vector
vect = cv.transform(dat).toarray()
# prediction
my_pred = model.predict(vect)
my_pred = le.inverse_transform(my_pred)
return my_pred[0]
if __name__ =="__main__":
app.run(debug=True)