Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,27 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
from pydantic import BaseModel
|
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
|
11 |
-
docs_url="/
|
12 |
-
redoc_url="/redoc"
|
13 |
)
|
14 |
|
15 |
-
|
16 |
-
# Load the model once when the app starts
|
17 |
model = joblib.load("model.joblib")
|
18 |
|
19 |
-
#
|
20 |
-
@app.get("/")
|
21 |
-
def root():
|
22 |
-
return {"message": "Email Classification API is running."}
|
23 |
-
|
24 |
-
# Define input data schema
|
25 |
class EmailInput(BaseModel):
|
26 |
subject: str = ""
|
27 |
email: str
|
28 |
|
29 |
-
#
|
30 |
def mask_and_store_all_pii(text):
|
31 |
-
text = str(text)
|
32 |
pii_map = {}
|
33 |
-
|
34 |
patterns = {
|
35 |
"email": r"\b[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+\b",
|
36 |
"phone_number": r"\b\d{10}\b",
|
@@ -51,7 +42,7 @@ def mask_and_store_all_pii(text):
|
|
51 |
|
52 |
return text, pii_map
|
53 |
|
54 |
-
#
|
55 |
@app.post("/classify")
|
56 |
def classify_email(data: EmailInput):
|
57 |
raw_text = f"{data.subject} {data.email}"
|
@@ -59,7 +50,11 @@ def classify_email(data: EmailInput):
|
|
59 |
prediction = model.predict([masked_text])[0]
|
60 |
|
61 |
return {
|
62 |
-
"predicted_category": prediction,
|
63 |
"masked_text": masked_text,
|
|
|
64 |
"pii_map": pii_map
|
65 |
}
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
from pydantic import BaseModel
|
3 |
import joblib
|
4 |
import re
|
5 |
|
|
|
6 |
app = FastAPI(
|
7 |
title="Email Classification API",
|
8 |
version="1.0.0",
|
9 |
+
description="Classifies support emails into categories and masks personal information.",
|
10 |
+
docs_url="/docs", # Swagger UI enabled here
|
11 |
+
redoc_url="/redoc" # Optional ReDoc UI
|
12 |
)
|
13 |
|
14 |
+
# Load model
|
|
|
15 |
model = joblib.load("model.joblib")
|
16 |
|
17 |
+
# Email input structure
|
|
|
|
|
|
|
|
|
|
|
18 |
class EmailInput(BaseModel):
|
19 |
subject: str = ""
|
20 |
email: str
|
21 |
|
22 |
+
# PII masker
|
23 |
def mask_and_store_all_pii(text):
|
|
|
24 |
pii_map = {}
|
|
|
25 |
patterns = {
|
26 |
"email": r"\b[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+\b",
|
27 |
"phone_number": r"\b\d{10}\b",
|
|
|
42 |
|
43 |
return text, pii_map
|
44 |
|
45 |
+
# Main endpoint
|
46 |
@app.post("/classify")
|
47 |
def classify_email(data: EmailInput):
|
48 |
raw_text = f"{data.subject} {data.email}"
|
|
|
50 |
prediction = model.predict([masked_text])[0]
|
51 |
|
52 |
return {
|
|
|
53 |
"masked_text": masked_text,
|
54 |
+
"predicted_category": prediction,
|
55 |
"pii_map": pii_map
|
56 |
}
|
57 |
+
|
58 |
+
@app.get("/")
|
59 |
+
def root():
|
60 |
+
return {"message": "Email Classification API is running."}
|