sanabanu31 commited on
Commit
6b21b32
·
verified ·
1 Parent(s): 1b6f003

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -18
app.py CHANGED
@@ -1,36 +1,27 @@
1
- from fastapi import FastAPI, HTTPException
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 PII/PCI information.",
11
- docs_url="/swagger", # Try custom path
12
- redoc_url="/redoc" # Optional: enable ReDoc
13
  )
14
 
15
-
16
- # Load the model once when the app starts
17
  model = joblib.load("model.joblib")
18
 
19
- # Define the root endpoint
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
- # Function to mask and store PII
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
- # Endpoint to classify email
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."}