File size: 2,077 Bytes
92385b3
 
 
ab3c5c9
 
92385b3
ab3c5c9
92385b3
 
 
 
ab3c5c9
92385b3
 
 
ab3c5c9
 
92385b3
 
ab3c5c9
 
92385b3
 
 
 
 
ab3c5c9
 
92385b3
 
ab3c5c9
92385b3
 
 
 
 
ab3c5c9
92385b3
ab3c5c9
92385b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c6ed00
92385b3
 
 
 
 
 
 
ab3c5c9
92385b3
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
76
77
78
79
80
81
from fastapi import FastAPI, HTTPException, Body
from pydantic import BaseModel
import uvicorn
from models.pii_masker import PIIMasker
from models.classifier import EmailClassifier
from utils.utils import preprocess_email

# Initialize FastAPI app
app = FastAPI(title="Email Classification API",
              description="API for classifying support emails and masking PII",
              version="1.0.0")

# Initialize PII masker and classifier
pii_masker = PIIMasker()
classifier = EmailClassifier(model_path="models/email_classifier.joblib")


class EmailRequest(BaseModel):
    email_body: str


class EmailResponse(BaseModel):
    input_email_body: str
    list_of_masked_entities: list
    masked_email: str
    category_of_the_email: str


@app.post("/classify-email", response_model=EmailResponse)
async def classify_email(request: EmailRequest = Body(...)):
    """
    Classify an email and mask PII information.

    Args:
        request: Email request object containing the email body

    Returns:
        dict: Response with masked email and classification
    """
    try:
        email_body = request.email_body

        # Mask PII
        masked_email, entities = pii_masker.mask_pii(email_body)

        # Preprocess for classification
        processed_email = preprocess_email(masked_email)

        # Classify email
        category = classifier.classify(processed_email)

        # Prepare response
        response = {
            "input_email_body": email_body,
            "list_of_masked_entities": entities,
            "masked_email": masked_email,
            "category_of_the_email": category
        }

        return response

    except Exception as e:
        error_msg = f"Error processing request: {str(e)}"
        raise HTTPException(status_code=500, detail=error_msg)


@app.get("/health")
async def health_check():
    """
    Health check endpoint.

    Returns:
        dict: Status message
    """
    return {"status": "healthy"}


# For local development
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)