Update app.py
Browse files
app.py
CHANGED
|
@@ -1,94 +1,108 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import argparse
|
| 3 |
from models.pii_masker import PIIMasker
|
| 4 |
from models.classifier import EmailClassifier
|
| 5 |
-
from utils.utils import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
)
|
| 12 |
-
""
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
model_output_path: Path to save the trained model
|
| 18 |
-
"""
|
| 19 |
-
# Create sample dataset if data_path is not provided
|
| 20 |
-
if not data_path or not os.path.exists(data_path):
|
| 21 |
-
print("No dataset provided or file not found.")
|
| 22 |
-
data_path = "data/emails.csv"
|
| 23 |
create_sample_dataset(data_path)
|
| 24 |
|
| 25 |
-
#
|
| 26 |
df = parse_emails_dataset(data_path)
|
| 27 |
X = df['email'].tolist()
|
| 28 |
y = df['type'].tolist()
|
| 29 |
|
| 30 |
-
# Initialize and train classifier
|
| 31 |
classifier = EmailClassifier()
|
| 32 |
-
|
| 33 |
|
| 34 |
# Save trained model
|
| 35 |
-
os.makedirs(os.path.dirname(
|
| 36 |
-
classifier.save_model(
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
-
def
|
| 42 |
"""
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
"""
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
|
|
|
| 48 |
test_email = (
|
| 49 |
"Hello, my name is John Doe, and my email is johndoe@example.com.\n"
|
| 50 |
"My phone number is 555-123-4567 and I was born on 15/04/1985.\n"
|
| 51 |
"My Aadhar number is 1234 5678 9012 and my credit card number is "
|
| 52 |
"4111 1111 1111 1111 with CVV 123 expiring on 12/25."
|
| 53 |
)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
"""
|
| 70 |
-
|
| 71 |
-
parser.add_argument("--train", action="store_true",
|
| 72 |
-
help="Train the classification model")
|
| 73 |
-
parser.add_argument("--test-masking", action="store_true",
|
| 74 |
-
help="Test PII masking functionality")
|
| 75 |
-
parser.add_argument("--data-path", type=str,
|
| 76 |
-
help="Path to email dataset for training")
|
| 77 |
-
|
| 78 |
-
args = parser.parse_args()
|
| 79 |
-
|
| 80 |
-
if args.train:
|
| 81 |
-
print("Training classification model...")
|
| 82 |
-
train_model(args.data_path)
|
| 83 |
-
|
| 84 |
-
if args.test_masking:
|
| 85 |
-
print("Testing PII masking functionality...")
|
| 86 |
-
test_pii_masking()
|
| 87 |
-
|
| 88 |
-
if not args.train and not args.test_masking:
|
| 89 |
-
print("No action specified. Use --train to train the model or "
|
| 90 |
-
"--test-masking to test PII masking.")
|
| 91 |
-
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import os
|
|
|
|
| 3 |
from models.pii_masker import PIIMasker
|
| 4 |
from models.classifier import EmailClassifier
|
| 5 |
+
from utils.utils import (preprocess_email, create_sample_dataset,
|
| 6 |
+
parse_emails_dataset)
|
| 7 |
+
|
| 8 |
+
# Check if model exists, if not train it
|
| 9 |
+
model_path = "models/email_classifier.joblib"
|
| 10 |
+
if not os.path.exists(model_path):
|
| 11 |
+
print("Training model as it doesn't exist yet...")
|
| 12 |
+
data_path = "data/emails.csv"
|
| 13 |
+
|
| 14 |
+
# Create sample dataset if needed
|
| 15 |
+
if not os.path.exists(data_path):
|
| 16 |
+
print("Creating sample dataset...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
create_sample_dataset(data_path)
|
| 18 |
|
| 19 |
+
# Train model
|
| 20 |
df = parse_emails_dataset(data_path)
|
| 21 |
X = df['email'].tolist()
|
| 22 |
y = df['type'].tolist()
|
| 23 |
|
|
|
|
| 24 |
classifier = EmailClassifier()
|
| 25 |
+
classifier.train(X, y)
|
| 26 |
|
| 27 |
# Save trained model
|
| 28 |
+
os.makedirs(os.path.dirname(model_path), exist_ok=True)
|
| 29 |
+
classifier.save_model(model_path)
|
| 30 |
+
print("Model trained and saved successfully!")
|
| 31 |
+
|
| 32 |
|
| 33 |
+
# Initialize components
|
| 34 |
+
pii_masker = PIIMasker()
|
| 35 |
+
classifier = EmailClassifier(model_path=model_path)
|
| 36 |
|
| 37 |
|
| 38 |
+
def process_email(email_body):
|
| 39 |
"""
|
| 40 |
+
Process email by masking PII and classifying it.
|
| 41 |
+
Args:
|
| 42 |
+
email_body: Raw email text
|
| 43 |
+
Returns:
|
| 44 |
+
tuple: (masked_email, entities_text, category)
|
| 45 |
"""
|
| 46 |
+
# Mask PII
|
| 47 |
+
masked_email, entities = pii_masker.mask_pii(email_body)
|
| 48 |
+
|
| 49 |
+
# Preprocess for classification
|
| 50 |
+
processed_email = preprocess_email(masked_email)
|
| 51 |
+
|
| 52 |
+
# Classify email
|
| 53 |
+
category = classifier.classify(processed_email)
|
| 54 |
+
|
| 55 |
+
# Format entities for display
|
| 56 |
+
entities_text = "\n".join([
|
| 57 |
+
f"- {entity['classification']}: {entity['entity']}"
|
| 58 |
+
for entity in entities
|
| 59 |
+
])
|
| 60 |
+
|
| 61 |
+
return masked_email, entities_text, category
|
| 62 |
+
|
| 63 |
|
| 64 |
+
def test_masking():
|
| 65 |
+
"""Example function to demonstrate PII masking"""
|
| 66 |
test_email = (
|
| 67 |
"Hello, my name is John Doe, and my email is johndoe@example.com.\n"
|
| 68 |
"My phone number is 555-123-4567 and I was born on 15/04/1985.\n"
|
| 69 |
"My Aadhar number is 1234 5678 9012 and my credit card number is "
|
| 70 |
"4111 1111 1111 1111 with CVV 123 expiring on 12/25."
|
| 71 |
)
|
| 72 |
+
return test_email
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# Create Gradio interface
|
| 76 |
+
demo = gr.Interface(
|
| 77 |
+
fn=process_email,
|
| 78 |
+
inputs=gr.Textbox(
|
| 79 |
+
lines=10,
|
| 80 |
+
label="Email Content",
|
| 81 |
+
placeholder="Enter email text to classify and mask PII..."
|
| 82 |
+
),
|
| 83 |
+
outputs=[
|
| 84 |
+
gr.Textbox(label="Masked Email"),
|
| 85 |
+
gr.Textbox(label="Detected PII Entities"),
|
| 86 |
+
gr.Textbox(label="Email Category")
|
| 87 |
+
],
|
| 88 |
+
title="Email Classification System",
|
| 89 |
+
description=(
|
| 90 |
+
"This application classifies support emails and masks personally "
|
| 91 |
+
"identifiable information (PII)."
|
| 92 |
+
),
|
| 93 |
+
examples=[
|
| 94 |
+
["Hello, my name is John Doe, and my email is johndoe@example.com. "
|
| 95 |
+
"I need help with my account."],
|
| 96 |
+
["I'm having trouble logging in to my account. My username is user"
|
| 97 |
+
"123."]
|
| 98 |
+
],
|
| 99 |
+
article="""
|
| 100 |
+
## How It Works
|
| 101 |
+
1. **PII Masking**: The system identifies and masks personal information
|
| 102 |
+
2. **Email Classification**: The masked email is classified into categories
|
| 103 |
+
3. **Results**: View the masked version, detected PII, and email category
|
| 104 |
"""
|
| 105 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
# Launch the app
|
| 108 |
+
demo.launch()
|