Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,22 +3,27 @@ from pydantic import BaseModel
|
|
| 3 |
import joblib
|
| 4 |
import re
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# Load the model once when the app starts
|
| 7 |
model = joblib.load("model.joblib")
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
@app.get("/")
|
| 12 |
def root():
|
| 13 |
return {"message": "Email Classification API is running."}
|
| 14 |
|
| 15 |
-
|
| 16 |
-
# Input data schema
|
| 17 |
class EmailInput(BaseModel):
|
| 18 |
subject: str = ""
|
| 19 |
email: str
|
| 20 |
|
| 21 |
-
#
|
| 22 |
def mask_and_store_all_pii(text):
|
| 23 |
text = str(text)
|
| 24 |
pii_map = {}
|
|
@@ -43,24 +48,13 @@ def mask_and_store_all_pii(text):
|
|
| 43 |
|
| 44 |
return text, pii_map
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
def restore_pii(masked_text, pii_map):
|
| 48 |
-
for placeholder, original in pii_map.items():
|
| 49 |
-
masked_text = masked_text.replace(placeholder, original)
|
| 50 |
-
return masked_text
|
| 51 |
-
|
| 52 |
@app.post("/classify")
|
| 53 |
def classify_email(data: EmailInput):
|
| 54 |
-
# Combine subject + email text
|
| 55 |
raw_text = f"{data.subject} {data.email}"
|
| 56 |
-
|
| 57 |
-
# Mask PII
|
| 58 |
masked_text, pii_map = mask_and_store_all_pii(raw_text)
|
| 59 |
-
|
| 60 |
-
# Predict class
|
| 61 |
prediction = model.predict([masked_text])[0]
|
| 62 |
|
| 63 |
-
# Return prediction and masked email
|
| 64 |
return {
|
| 65 |
"predicted_category": prediction,
|
| 66 |
"masked_text": masked_text,
|
|
|
|
| 3 |
import joblib
|
| 4 |
import re
|
| 5 |
|
| 6 |
+
# Initialize the FastAPI app with default docs enabled
|
| 7 |
+
app = FastAPI(
|
| 8 |
+
title="Email Classification API",
|
| 9 |
+
version="1.0.0",
|
| 10 |
+
description="Classifies emails and masks PII/PCI information."
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
# Load the model once when the app starts
|
| 14 |
model = joblib.load("model.joblib")
|
| 15 |
|
| 16 |
+
# Define the root endpoint
|
|
|
|
| 17 |
@app.get("/")
|
| 18 |
def root():
|
| 19 |
return {"message": "Email Classification API is running."}
|
| 20 |
|
| 21 |
+
# Define input data schema
|
|
|
|
| 22 |
class EmailInput(BaseModel):
|
| 23 |
subject: str = ""
|
| 24 |
email: str
|
| 25 |
|
| 26 |
+
# Function to mask and store PII
|
| 27 |
def mask_and_store_all_pii(text):
|
| 28 |
text = str(text)
|
| 29 |
pii_map = {}
|
|
|
|
| 48 |
|
| 49 |
return text, pii_map
|
| 50 |
|
| 51 |
+
# Endpoint to classify email
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
@app.post("/classify")
|
| 53 |
def classify_email(data: EmailInput):
|
|
|
|
| 54 |
raw_text = f"{data.subject} {data.email}"
|
|
|
|
|
|
|
| 55 |
masked_text, pii_map = mask_and_store_all_pii(raw_text)
|
|
|
|
|
|
|
| 56 |
prediction = model.predict([masked_text])[0]
|
| 57 |
|
|
|
|
| 58 |
return {
|
| 59 |
"predicted_category": prediction,
|
| 60 |
"masked_text": masked_text,
|