Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#1. Importing Lib
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 6 |
+
from sklearn.linear_model import LogisticRegression
|
| 7 |
+
from sklearn.metrics import accuracy_score
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
#2. Data Preprocessing
|
| 11 |
+
df=pd.read_csv("mail_data (1).csv")
|
| 12 |
+
|
| 13 |
+
df.loc[df["Category"]=="spam","Category",]=0
|
| 14 |
+
df.loc[df["Category"]=="ham","Category",]=1
|
| 15 |
+
|
| 16 |
+
# split data into dependent and independednt
|
| 17 |
+
x=df["Message"]
|
| 18 |
+
y=df["Category"]
|
| 19 |
+
|
| 20 |
+
#3. Modeling Part
|
| 21 |
+
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)
|
| 22 |
+
|
| 23 |
+
# Vectorization process for message content
|
| 24 |
+
feature_extraction=TfidfVectorizer(min_df=1,stop_words="english",lowercase=True)
|
| 25 |
+
|
| 26 |
+
x_train_features = feature_extraction.fit_transform(x_train)
|
| 27 |
+
x_test_features = feature_extraction.transform(x_test)
|
| 28 |
+
|
| 29 |
+
y_train = y_train.astype("int")
|
| 30 |
+
y_test = y_test.astype("int")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
model=LogisticRegression()
|
| 35 |
+
model.fit(x_train_features,y_train)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
x_predict=model.predict(x_train_features)
|
| 40 |
+
x_accuracy=accuracy_score(x_predict,y_train)
|
| 41 |
+
|
| 42 |
+
y_predict=model.predict(x_test_features)
|
| 43 |
+
y_accuracy=accuracy_score(y_predict,y_test)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
#4. Gradio Part
|
| 47 |
+
|
| 48 |
+
# Function to predict whether the email is spam or ham
|
| 49 |
+
def classify_email(email_text):
|
| 50 |
+
# Transform the input email text using the same vectorizer used during training
|
| 51 |
+
input_data_features = feature_extraction.transform([email_text])
|
| 52 |
+
|
| 53 |
+
# Predict using the trained model
|
| 54 |
+
prediction = model.predict(input_data_features)
|
| 55 |
+
|
| 56 |
+
# Return the result based on the prediction
|
| 57 |
+
if prediction[0] == 0:
|
| 58 |
+
return "Your email is Spam"
|
| 59 |
+
else:
|
| 60 |
+
return "Your email is Ham"
|
| 61 |
+
|
| 62 |
+
# Create a Gradio interface for user input
|
| 63 |
+
interface = gr.Interface(
|
| 64 |
+
fn=classify_email, # Function to be called when user interacts
|
| 65 |
+
inputs=gr.Textbox(label="Enter your email text here", placeholder="Type your email...", lines=5),
|
| 66 |
+
outputs=gr.Textbox(label="Prediction"),
|
| 67 |
+
live=True # Live prediction update
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# Launch the interface
|
| 71 |
+
interface.launch()
|
| 72 |
+
|