Spaces:
Sleeping
Sleeping
import gradio as gr | |
import joblib | |
import xgboost as xgb | |
import numpy as np | |
def classify_email(email_text): | |
tfidf = joblib.load('tfidf_vectorizer.joblib') | |
model = joblib.load('spam_model.joblib') | |
email_tfidf = tfidf.transform([email_text]) | |
email_dmatrix = xgb.DMatrix(email_tfidf) | |
prediction = model.predict(email_dmatrix)[0] | |
confidence = max(prediction, 1 - prediction) | |
label = "Spam" if prediction > 0.5 else "Not Spam" | |
return {label: float(confidence)} | |
def analyze_email(email_text): | |
tfidf = joblib.load('tfidf_vectorizer.joblib') | |
model = joblib.load('spam_model.joblib') | |
email_tfidf = tfidf.transform([email_text]) | |
email_dmatrix = xgb.DMatrix(email_tfidf) | |
prediction = model.predict(email_dmatrix)[0] | |
confidence = max(prediction, 1 - prediction) | |
label = "Spam" if prediction > 0.5 else "Not Spam" | |
# Create Gradio interface | |
with gr.Blocks(css="footer {visibility: hidden}") as iface: | |
gr.Markdown( | |
""" | |
# π Spam Email Classifier | |
Using Machine Learning to detect spam emails with high accuracy! | |
""" | |
) | |
with gr.Row(): | |
with gr.Column(scale=2): | |
email_input = gr.Textbox(lines=5, label="Enter email text") | |
with gr.Row(): | |
classify_btn = gr.Button("Classify") | |
with gr.Column(scale=1): | |
label_output = gr.Label(label="Classification") | |
examples = [ | |
["Get fat quick! Buy our cheese burger now!"], | |
["Hi Ajibola, let's go out on a date tonight"], | |
["Congratulations! You've won a free iPhone. Click here to claim."], | |
["Please find attached the report for Q2 sales figures."] | |
] | |
gr.Examples(examples, inputs=email_input) | |
classify_btn.click(classify_email, inputs=email_input, outputs=label_output) | |
gr.Markdown( | |
""" | |
### How it works | |
This classifier uses an XGBoost model trained on a large dataset of over 190,000 emails. | |
The model achieved a 98% accuracy on the training data and 94% accuracy on the test data. | |
It analyzes the content and structure of the email to determine if it's spam or not. | |
### Tip for use | |
- Enter the full text of the email for best results | |
""" | |
) | |
# Launch the interface | |
iface.launch() | |