Nikpatil commited on
Commit
920cd26
·
verified ·
1 Parent(s): 128f619

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +57 -54
api.py CHANGED
@@ -1,54 +1,57 @@
1
- from flask import Blueprint, request, jsonify
2
- from transformers import DebertaV2Tokenizer, DebertaV2ForSequenceClassification
3
- import torch
4
- from utils import mask_pii
5
-
6
- api_bp = Blueprint("api", __name__)
7
-
8
- # Repo of Hugging Face Model Hub where Model is Pushed
9
- REPO_ID = "Nikpatil/Email_classifier"
10
- MAX_LENGTH = 256
11
-
12
- tokenizer = DebertaV2Tokenizer.from_pretrained(REPO_ID)
13
- model = DebertaV2ForSequenceClassification.from_pretrained(REPO_ID)
14
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
- model.to(device)
16
- model.eval()
17
-
18
- id2label = {0: "Incident", 1: "Request", 2: "Problem", 3: "Change"}
19
-
20
- @api_bp.route("/classify", methods=["POST"])
21
- def classify_email():
22
- data = request.get_json()
23
- email_body = data.get("email_body", "")
24
-
25
- if not email_body:
26
- return jsonify({"Error": "Email body field is required"}), 400
27
-
28
- masked_email, entities = mask_pii(email_body)
29
-
30
- inputs = tokenizer(
31
- masked_email,
32
- add_special_tokens=True,
33
- max_length=MAX_LENGTH,
34
- padding='max_length',
35
- truncation=True,
36
- return_tensors='pt'
37
- )
38
- inputs = {k: v.to(device) for k, v in inputs.items()}
39
-
40
- with torch.no_grad():
41
- outputs = model(**inputs)
42
-
43
- probs = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
44
- predicted_class_id = torch.argmax(probs).item()
45
- predicted_class = id2label[predicted_class_id]
46
-
47
- return jsonify({
48
- "input_email_body": email_body,
49
- "list_of_masked_entities": entities,
50
- "masked_email": masked_email,
51
- "category_of_the_email": predicted_class
52
- }), 200
53
-
54
-
 
 
 
 
1
+ import os
2
+ os.environ["HF_HOME"] = "/tmp/huggingface"
3
+
4
+ from flask import Blueprint, request, jsonify
5
+ from transformers import DebertaV2Tokenizer, DebertaV2ForSequenceClassification
6
+ import torch
7
+ from utils import mask_pii
8
+
9
+ api_bp = Blueprint("api", __name__)
10
+
11
+ # Repo of Hugging Face Model Hub where Model is Pushed
12
+ REPO_ID = "Nikpatil/Email_classifier"
13
+ MAX_LENGTH = 256
14
+
15
+ tokenizer = DebertaV2Tokenizer.from_pretrained(REPO_ID)
16
+ model = DebertaV2ForSequenceClassification.from_pretrained(REPO_ID)
17
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
18
+ model.to(device)
19
+ model.eval()
20
+
21
+ id2label = {0: "Incident", 1: "Request", 2: "Problem", 3: "Change"}
22
+
23
+ @api_bp.route("/classify", methods=["POST"])
24
+ def classify_email():
25
+ data = request.get_json()
26
+ email_body = data.get("email_body", "")
27
+
28
+ if not email_body:
29
+ return jsonify({"Error": "Email body field is required"}), 400
30
+
31
+ masked_email, entities = mask_pii(email_body)
32
+
33
+ inputs = tokenizer(
34
+ masked_email,
35
+ add_special_tokens=True,
36
+ max_length=MAX_LENGTH,
37
+ padding='max_length',
38
+ truncation=True,
39
+ return_tensors='pt'
40
+ )
41
+ inputs = {k: v.to(device) for k, v in inputs.items()}
42
+
43
+ with torch.no_grad():
44
+ outputs = model(**inputs)
45
+
46
+ probs = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
47
+ predicted_class_id = torch.argmax(probs).item()
48
+ predicted_class = id2label[predicted_class_id]
49
+
50
+ return jsonify({
51
+ "input_email_body": email_body,
52
+ "list_of_masked_entities": entities,
53
+ "masked_email": masked_email,
54
+ "category_of_the_email": predicted_class
55
+ }), 200
56
+
57
+