Nikpatil commited on
Commit
5383e20
·
verified ·
1 Parent(s): 39cc5cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -4
app.py CHANGED
@@ -19,8 +19,9 @@ model.eval()
19
 
20
  # Your inference function (adapted from your own)
21
  def inference(text, model, tokenizer, max_length, id2label):
 
22
  inputs = tokenizer(
23
- text,
24
  add_special_tokens=True,
25
  max_length=MAX_LENGTH,
26
  padding='max_length',
@@ -35,15 +36,40 @@ def inference(text, model, tokenizer, max_length, id2label):
35
  probs = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
36
  predicted_class_id = torch.argmax(probs).item()
37
  predicted_class = id2label[predicted_class_id]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- return f"Predicted: {predicted_class}", {id2label[i]: float(probs[i]) for i in range(len(probs))}
40
 
41
  # Gradio interface
42
  demo = gr.Interface(
43
  fn=lambda text: inference(text, model, tokenizer, MAX_LENGTH, id2label),
44
  inputs=gr.Textbox(lines=8, placeholder="Paste your email here..."),
45
- outputs=["text", "label"],
46
- title="Email Classifier (DeBERTa v2)",
47
  description="Classifies emails into Incident, Request, Problem, or Change."
48
  )
49
 
 
19
 
20
  # Your inference function (adapted from your own)
21
  def inference(text, model, tokenizer, max_length, id2label):
22
+ masked_text = mask_pii(text)
23
  inputs = tokenizer(
24
+ masked_text,
25
  add_special_tokens=True,
26
  max_length=MAX_LENGTH,
27
  padding='max_length',
 
36
  probs = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
37
  predicted_class_id = torch.argmax(probs).item()
38
  predicted_class = id2label[predicted_class_id]
39
+ # Step 4: Extract PII fields (back from masked text)
40
+ detected_pii = []
41
+ if "<full_name>" in masked_text:
42
+ detected_pii.append("full_name")
43
+ if "<email>" in masked_text:
44
+ detected_pii.append("email")
45
+ if "<phone_number>" in masked_text:
46
+ detected_pii.append("phone_number")
47
+ if "<dob>" in masked_text:
48
+ detected_pii.append("dob")
49
+ if "<aadhar_num>" in masked_text:
50
+ detected_pii.append("aadhar")
51
+ if "<credit_debit_no>" in masked_text:
52
+ detected_pii.append("credit/debit card")
53
+ if "<cvv_no>" in masked_text:
54
+ detected_pii.append("cvv")
55
+ if "<expiry_no>" in masked_text:
56
+ detected_pii.append("expiry date")
57
+
58
+ return {
59
+ "original_email": text,
60
+ "masked_email": masked_text,
61
+ "predicted_email_class": predicted_class,
62
+ "prediction_confidence": float(probs[predicted_class_id]),
63
+ "detected_pii": detected_pii
64
+ }
65
 
 
66
 
67
  # Gradio interface
68
  demo = gr.Interface(
69
  fn=lambda text: inference(text, model, tokenizer, MAX_LENGTH, id2label),
70
  inputs=gr.Textbox(lines=8, placeholder="Paste your email here..."),
71
+ outputs="json",
72
+ title="Email Classifier",
73
  description="Classifies emails into Incident, Request, Problem, or Change."
74
  )
75