philip11 commited on
Commit
92385b3
·
verified ·
1 Parent(s): 5c6ed00

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +65 -93
api.py CHANGED
@@ -1,108 +1,80 @@
1
- import gradio as gr
2
- import os
 
3
  from models.pii_masker import PIIMasker
4
  from models.classifier import EmailClassifier
5
- from utils.utils import (preprocess_email, create_sample_dataset,
6
- parse_emails_dataset)
7
 
8
- # Check if model exists, if not train it
9
- model_path = "models/email_classifier.joblib"
10
- if not os.path.exists(model_path):
11
- print("Training model as it doesn't exist yet...")
12
- data_path = "data/emails.csv"
13
 
14
- # Create sample dataset if needed
15
- if not os.path.exists(data_path):
16
- print("Creating sample dataset...")
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
- # Save trained model
28
- os.makedirs(os.path.dirname(model_path), exist_ok=True)
29
- classifier.save_model(model_path)
30
- print("Model trained and saved successfully!")
31
 
32
 
33
- # Initialize components
34
- pii_masker = PIIMasker()
35
- classifier = EmailClassifier(model_path=model_path)
 
 
36
 
37
 
38
- def process_email(email_body):
 
39
  """
40
- Process email by masking PII and classifying it.
41
- Args:
42
- email_body: Raw email text
 
 
43
  Returns:
44
- tuple: (masked_email, entities_text, category)
45
  """
46
- # Mask PII
47
- masked_email, entities = pii_masker.mask_pii(email_body)
48
-
49
- # Preprocess for classification
50
- processed_email = preprocess_email(masked_email)
51
-
52
- # Classify email
53
- category = classifier.classify(processed_email)
54
-
55
- # Format entities for display
56
- entities_text = "\n".join([
57
- f"- {entity['classification']}: {entity['entity']}"
58
- for entity in entities
59
- ])
60
-
61
- return masked_email, entities_text, category
62
-
63
-
64
- def test_masking():
65
- """Example function to demonstrate PII masking"""
66
- test_email = (
67
- "Hello, my name is John Doe, and my email is johndoe@example.com.\n"
68
- "My phone number is 555-123-4567 and I was born on 15/04/1985.\n"
69
- "My Aadhar number is 1234 5678 9012 and my credit card number is "
70
- "4111 1111 1111 1111 with CVV 123 expiring on 12/25."
71
- )
72
- return test_email
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
- # Launch the app
108
- demo.launch()
 
 
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)