Spaces:
Runtime error
Runtime error
ErtugrulDemir
commited on
Commit
·
12f5886
1
Parent(s):
b62cfa6
push root
Browse filesPushing 'Sms Spam Classification' portfolio project from local (cloud) to remote repository
- .gitattributes +1 -0
- app.py +80 -0
- rf_model.sav +3 -0
- tfidf_transformer +0 -0
- vectorizer +0 -0
.gitattributes
CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
rf_model.sav filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import re
|
3 |
+
import string
|
4 |
+
import numpy as np
|
5 |
+
import nltk
|
6 |
+
import pandas as pd
|
7 |
+
import sklearn
|
8 |
+
import gradio as gr
|
9 |
+
from nltk.stem.porter import PorterStemmer
|
10 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
11 |
+
from nltk.corpus import stopwords
|
12 |
+
|
13 |
+
|
14 |
+
# File Paths
|
15 |
+
model_path = 'rf_model.sav'
|
16 |
+
bow_vectorizer_path = "vectorizer"
|
17 |
+
tfidf_path = "tfidf_transformer"
|
18 |
+
|
19 |
+
# Loading the files
|
20 |
+
model = pickle.load(open(model_path, 'rb'))
|
21 |
+
bow_vectorizer = pickle.load(open(bow_vectorizer_path, 'rb'))
|
22 |
+
tfidf_transformer = pickle.load(open(tfidf_path, 'rb'))
|
23 |
+
nltk.download("stopwords")
|
24 |
+
|
25 |
+
labels = ["not spam", "spam"]
|
26 |
+
|
27 |
+
# declerating the example case
|
28 |
+
Examples = [
|
29 |
+
"Go until jurong point, crazy.. Available only in bugis n great world la e buffet... Cine there got amore wat...",
|
30 |
+
"Dear John, thank you for submitting your application for the open position. We appreciate your interest and will be in touch soon regarding next steps",
|
31 |
+
"Hi Sarah, just wanted to follow up on our meeting last week and see if you had any further questions or concerns. Let me know if there's anything else I can help you with.",
|
32 |
+
"Free entry in 2 a wkly comp to win FA Cup final tkts 21st May 2005. Text FA to 87121 to receive entry question(std txt rate)T&C's apply 08452810075over18's",
|
33 |
+
"Congratulations! You have been selected to receive a free trip to the Bahamas. To claim your prize, simply click on the link below and fill out the registration form.",
|
34 |
+
"URGENT: Your account has been compromised. Please click on the link below to reset your password and secure your account.",
|
35 |
+
]
|
36 |
+
|
37 |
+
# Util functions
|
38 |
+
def text_preprocessing(text):
|
39 |
+
|
40 |
+
# getting the stopwords
|
41 |
+
STOPWORDS = stopwords.words('english') + ['u', 'ü', 'ur', '4', '2', 'im', 'dont', 'doin', 'ure']
|
42 |
+
|
43 |
+
# selecting non puction words
|
44 |
+
nopunc = [char for char in text if char not in string.punctuation]
|
45 |
+
nopunc = ''.join(nopunc)
|
46 |
+
|
47 |
+
# clearning and joining the words to create text
|
48 |
+
clean_text = ' '.join([word for word in nopunc.split() if word.lower() not in STOPWORDS])
|
49 |
+
|
50 |
+
return clean_text
|
51 |
+
|
52 |
+
def vector_transformation(sentence):
|
53 |
+
return bow_vectorizer.transform(sentence)
|
54 |
+
def tfidf_transformation(bow):
|
55 |
+
return tfidf_transformer.transform(bow)
|
56 |
+
|
57 |
+
def predict(text):
|
58 |
+
|
59 |
+
# preparing the input into convenient form
|
60 |
+
sentence = text_preprocessing(text)
|
61 |
+
|
62 |
+
# converting the text into numerical representation
|
63 |
+
bow = bow_vectorizer.transform([sentence])
|
64 |
+
features = tfidf_transformation(bow)
|
65 |
+
|
66 |
+
# prediction
|
67 |
+
probabilities = model.predict_proba(features) #.predict(features)
|
68 |
+
probs = probabilities.flatten()
|
69 |
+
|
70 |
+
# output form
|
71 |
+
results = {l : np.round(p, 3) for l, p in zip(labels, probs)}
|
72 |
+
|
73 |
+
return results
|
74 |
+
|
75 |
+
# GUI Component
|
76 |
+
demo = gr.Interface(predict, "text", "label", examples = Examples)
|
77 |
+
|
78 |
+
# Launching the demo
|
79 |
+
if __name__ == "__main__":
|
80 |
+
demo.launch()
|
rf_model.sav
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3539929b7acf3379a5876dbefd64b462d82b1e1abd6c19e0546fc73edbf606b2
|
3 |
+
size 4778296
|
tfidf_transformer
ADDED
Binary file (150 kB). View file
|
|
vectorizer
ADDED
Binary file (118 kB). View file
|
|