Spaces:
Sleeping
Sleeping
Rename gradioapp.py to app.py
Browse files- gradioapp.py → app.py +60 -66
gradioapp.py → app.py
RENAMED
|
@@ -1,66 +1,60 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import numpy as np
|
| 3 |
-
import re
|
| 4 |
-
import nltk
|
| 5 |
-
from nltk.tokenize import word_tokenize
|
| 6 |
-
from nltk.corpus import stopwords
|
| 7 |
-
from nltk.stem import WordNetLemmatizer
|
| 8 |
-
from sklearn.feature_extraction.text import CountVectorizer
|
| 9 |
-
import pickle
|
| 10 |
-
import gradio as gr
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
nltk.download("
|
| 14 |
-
nltk.download("
|
| 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 |
-
description="Enter an email message to detect if it's Spam or Non-Spam.",
|
| 62 |
-
allow_flagging=False
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
# Launch the app
|
| 66 |
-
iface.launch()
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import re
|
| 4 |
+
import nltk
|
| 5 |
+
from nltk.tokenize import word_tokenize
|
| 6 |
+
from nltk.corpus import stopwords
|
| 7 |
+
from nltk.stem import WordNetLemmatizer
|
| 8 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
| 9 |
+
import pickle
|
| 10 |
+
import gradio as gr
|
| 11 |
+
|
| 12 |
+
nltk.download("punkt")
|
| 13 |
+
nltk.download("stopwords")
|
| 14 |
+
nltk.download("wordnet")
|
| 15 |
+
|
| 16 |
+
with open("count_vectorizer_spam.pkl", "rb") as f:
|
| 17 |
+
vectorizer = pickle.load(f)
|
| 18 |
+
|
| 19 |
+
with open("nb_model_spam.pkl", "rb") as f:
|
| 20 |
+
nb_model = pickle.load(f)
|
| 21 |
+
|
| 22 |
+
def preprocess_text(text):
|
| 23 |
+
words = word_tokenize(text)
|
| 24 |
+
words_without_punct = [word for word in words if word.isalnum()]
|
| 25 |
+
clean_text = ' '.join(words_without_punct)
|
| 26 |
+
clean_text = clean_text.lower()
|
| 27 |
+
|
| 28 |
+
stop_words = set(stopwords.words('english'))
|
| 29 |
+
words = word_tokenize(clean_text)
|
| 30 |
+
filtered_words = [word for word in words if word.lower() not in stop_words]
|
| 31 |
+
clean_text_without_stopwords = ' '.join(filtered_words)
|
| 32 |
+
|
| 33 |
+
lemmatizer = WordNetLemmatizer()
|
| 34 |
+
words = word_tokenize(clean_text_without_stopwords)
|
| 35 |
+
lemmatized_words = [lemmatizer.lemmatize(word) for word in words]
|
| 36 |
+
lemmatized_text = ' '.join(lemmatized_words)
|
| 37 |
+
|
| 38 |
+
text = re.sub(r'[^a-z\s]', '', lemmatized_text)
|
| 39 |
+
return text
|
| 40 |
+
|
| 41 |
+
def predict_spam(text):
|
| 42 |
+
if text.strip() == "":
|
| 43 |
+
return "Please enter an email!"
|
| 44 |
+
|
| 45 |
+
cleaned_text = preprocess_text(text)
|
| 46 |
+
X_input = vectorizer.transform([cleaned_text])
|
| 47 |
+
prediction = nb_model.predict(X_input)[0]
|
| 48 |
+
|
| 49 |
+
return "Spam" if prediction == 1 else "Non-Spam"
|
| 50 |
+
|
| 51 |
+
iface = gr.Interface(
|
| 52 |
+
fn=predict_spam,
|
| 53 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter email here..."),
|
| 54 |
+
outputs="text",
|
| 55 |
+
title="Spam Detection",
|
| 56 |
+
description="Enter an email message to detect if it's Spam or Non-Spam.",
|
| 57 |
+
allow_flagging=False
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|