Update api.py
Browse files
api.py
CHANGED
|
@@ -1,108 +1,80 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
|
|
|
| 3 |
from models.pii_masker import PIIMasker
|
| 4 |
from models.classifier import EmailClassifier
|
| 5 |
-
from utils.utils import
|
| 6 |
-
parse_emails_dataset)
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
data_path = "data/emails.csv"
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 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 |
-
|
| 28 |
-
|
| 29 |
-
classifier.save_model(model_path)
|
| 30 |
-
print("Model trained and saved successfully!")
|
| 31 |
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
-
|
|
|
|
| 39 |
"""
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
Returns:
|
| 44 |
-
|
| 45 |
"""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 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 |
-
#
|
| 108 |
-
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Body
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import uvicorn
|
| 4 |
from models.pii_masker import PIIMasker
|
| 5 |
from models.classifier import EmailClassifier
|
| 6 |
+
from utils.utils import preprocess_email
|
|
|
|
| 7 |
|
| 8 |
+
# Initialize FastAPI app
|
| 9 |
+
app = FastAPI(title="Email Classification API",
|
| 10 |
+
description="API for classifying support emails and masking PII",
|
| 11 |
+
version="1.0.0")
|
|
|
|
| 12 |
|
| 13 |
+
# Initialize PII masker and classifier
|
| 14 |
+
pii_masker = PIIMasker()
|
| 15 |
+
classifier = EmailClassifier(model_path="models/email_classifier.joblib")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
class EmailRequest(BaseModel):
|
| 19 |
+
email_body: str
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
+
class EmailResponse(BaseModel):
|
| 23 |
+
input_email_body: str
|
| 24 |
+
list_of_masked_entities: list
|
| 25 |
+
masked_email: str
|
| 26 |
+
category_of_the_email: str
|
| 27 |
|
| 28 |
|
| 29 |
+
@app.post("/classify-email", response_model=EmailResponse)
|
| 30 |
+
async def classify_email(request: EmailRequest = Body(...)):
|
| 31 |
"""
|
| 32 |
+
Classify an email and mask PII information.
|
| 33 |
+
|
| 34 |
+
Args:
|
| 35 |
+
request: Email request object containing the email body
|
| 36 |
+
|
| 37 |
Returns:
|
| 38 |
+
dict: Response with masked email and classification
|
| 39 |
"""
|
| 40 |
+
try:
|
| 41 |
+
email_body = request.email_body
|
| 42 |
+
|
| 43 |
+
# Mask PII
|
| 44 |
+
masked_email, entities = pii_masker.mask_pii(email_body)
|
| 45 |
+
|
| 46 |
+
# Preprocess for classification
|
| 47 |
+
processed_email = preprocess_email(masked_email)
|
| 48 |
+
|
| 49 |
+
# Classify email
|
| 50 |
+
category = classifier.classify(processed_email)
|
| 51 |
+
|
| 52 |
+
# Prepare response
|
| 53 |
+
response = {
|
| 54 |
+
"input_email_body": email_body,
|
| 55 |
+
"list_of_masked_entities": entities,
|
| 56 |
+
"masked_email": masked_email,
|
| 57 |
+
"category_of_the_email": category
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
return response
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
error_msg = f"Error processing request: {str(e)}"
|
| 64 |
+
raise HTTPException(status_code=500, detail=error_msg)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
@app.get("/health")
|
| 68 |
+
async def health_check():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
"""
|
| 70 |
+
Health check endpoint.
|
| 71 |
+
|
| 72 |
+
Returns:
|
| 73 |
+
dict: Status message
|
| 74 |
+
"""
|
| 75 |
+
return {"status": "healthy"}
|
| 76 |
+
|
| 77 |
|
| 78 |
+
# For local development
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|