Spaces:
Sleeping
Sleeping
| #1. Importing Lib | |
| import numpy as np | |
| import pandas as pd | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.feature_extraction.text import TfidfVectorizer | |
| from sklearn.linear_model import LogisticRegression | |
| from sklearn.metrics import accuracy_score | |
| import gradio as gr | |
| #2. Data Preprocessing | |
| df=pd.read_csv("mail_data.csv") | |
| df.loc[df["Category"]=="spam","Category",]=0 | |
| df.loc[df["Category"]=="ham","Category",]=1 | |
| # Spliting Data into xand y | |
| x=df["Message"] | |
| y=df["Category"] | |
| #3. Modeling Part | |
| x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0) | |
| # Features extractions using TfidfVectorizer | |
| feature_extraction=TfidfVectorizer(min_df=1,stop_words="english",lowercase=True) | |
| x_train_features = feature_extraction.fit_transform(x_train) | |
| x_test_features = feature_extraction.transform(x_test) | |
| y_train = y_train.astype("int") | |
| y_test = y_test.astype("int") | |
| model=LogisticRegression() | |
| # Trains the model only at Train data features | |
| model.fit(x_train_features,y_train) | |
| x_predict=model.predict(x_train_features) | |
| x_accuracy=accuracy_score(x_predict,y_train) | |
| y_predict=model.predict(x_test_features) | |
| y_accuracy=accuracy_score(y_predict,y_test) | |
| #4. UI For Model | |
| # Function to predict whether the email is spam or ham | |
| def classify_email(email_text): | |
| # Transform the input email text using the same vectorizer used during training | |
| input_data_features = feature_extraction.transform([email_text]) | |
| # Predict using the trained model | |
| prediction = model.predict(input_data_features) | |
| # Return the result based on the prediction | |
| if prediction[0] == 0: | |
| return "Your email is Spam" | |
| else: | |
| return "Your email is Ham" | |
| # Create a Gradio interface for user input | |
| interface = gr.Interface( | |
| fn=classify_email, # Function to be called when user interacts | |
| inputs=gr.Textbox(label="Enter your email text here", placeholder="Type your email...", lines=5), | |
| outputs=gr.Textbox(label="Prediction"), | |
| live=True # Live prediction update | |
| ) | |
| # Launch the interface | |
| interface.launch() | |