Vipul-Chauhan commited on
Commit
fc91528
Β·
1 Parent(s): 839726e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle as pkl
2
+
3
+ import re
4
+ import numpy as np
5
+
6
+ from nltk.stem import PorterStemmer
7
+ from textblob import Word
8
+ from nltk.corpus import stopwords
9
+
10
+ def process_row(row):
11
+ from string import punctuation
12
+ row = re.sub('(\S+@\S+)(com|\s+com)', ' ', row)
13
+ row = re.sub('(\S+@\S+)', ' ', row)
14
+ punctuation = punctuation + '\n' + 'β€”β€œ,β€β€˜-’' + '0123456789'
15
+ row = ''.join(word for word in row if word not in punctuation)
16
+ row = row.lower()
17
+ stop = stopwords.words('english')
18
+ row = ' '.join(word for word in row.split() if word not in stop )
19
+ row = " ".join([Word(word).lemmatize() for word in row.split()])
20
+ ps = PorterStemmer()
21
+ row = " ".join([ps.stem(word) for word in row.split()])
22
+ row = re.sub('\s{1,}', ' ', row)
23
+ row = " ".join([word for word in row.split() if len(word) > 2])
24
+ return row
25
+
26
+ def predict_class(doc):
27
+ model = pkl.load(open("logistic_model.pk","rb"))
28
+ vectorizer = pkl.load(open("tfidf_vectorizer.pk","rb"))
29
+ clean_doc=process_row(doc)
30
+ vector =np.array(vectorizer.transform([clean_doc]).todense())
31
+ class_pred = model.predict(vector)
32
+ # print(class_pred[0])
33
+ return class_pred[0]
34
+
35
+ # predict_class(" Barack Obama is seeking for a conference to be conducted in USA")
36
+
37
+ import gradio as gr
38
+ iface = gr.Interface(fn = predict_class,
39
+ inputs = gr.Textbox(type="text", label="Enter Your Document"),
40
+ # outputs = ["text", "text"],
41
+ outputs = [
42
+ gr.Textbox(type="text", value=". . . ", label="Predicted Class")
43
+ ],
44
+ title = "News Class Predictor",
45
+ description ="Predicts whether the News belongs to 'Politics'class or 'Sports' class")
46
+ iface.launch()