Spaces:
Sleeping
Sleeping
vishal2023
commited on
Commit
•
ac030ca
1
Parent(s):
5f4fc7d
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import joblib
|
2 |
+
import string
|
3 |
+
import re
|
4 |
+
import nltk
|
5 |
+
nltk.download('stopwords')
|
6 |
+
from nltk.corpus import stopwords
|
7 |
+
from nltk.stem.porter import PorterStemmer
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
model = joblib.load('/content/gdrive/MyDrive/Colab Notebooks/Email Spam Classifier/naive_bayes_model.sav')
|
11 |
+
tfidf = joblib.load('/content/gdrive/MyDrive/Colab Notebooks/Email Spam Classifier/TfIdfVectorizer.sav')
|
12 |
+
pipe = joblib.load('/content/gdrive/MyDrive/Colab Notebooks/Email Spam Classifier/pipeline.sav')
|
13 |
+
|
14 |
+
def predict_NB(text):
|
15 |
+
text = text.lower()
|
16 |
+
text = re.sub('[^A-Za-z]',' ',text)
|
17 |
+
text = text.translate(str.maketrans('','',string.punctuation))
|
18 |
+
text = ' '.join(word for word in text.split() if word not in stopwords.words('english'))
|
19 |
+
ps = PorterStemmer()
|
20 |
+
text = ' '.join([ps.stem(word) for word in text.split()])
|
21 |
+
X = tfidf.transform([text]).toarray()
|
22 |
+
return 'spam' if model.predict(X)[0] == 1 else 0
|
23 |
+
|
24 |
+
def predict_PIPE(text):
|
25 |
+
result = pipe(text)[0]
|
26 |
+
return f'''{'spam' if result['label']=='LABEL_1' else 0}
|
27 |
+
confidence : {result['score']}'''
|
28 |
+
|
29 |
+
def fn(model_choice, input):
|
30 |
+
if model_choice=="naive-bayes":
|
31 |
+
return predict_NB(input)
|
32 |
+
elif model_choice=="tiny-bert":
|
33 |
+
return predict_PIPE(input)
|
34 |
+
|
35 |
+
gr.Interface(fn, inputs = [gr.inputs.Dropdown(["naive-bayes", "tiny-bert"],default = 'naive-bayes'),'text'],
|
36 |
+
outputs = "text",
|
37 |
+
title = 'Spam Classifier').launch()
|