menimeni123 commited on
Commit
b36a521
·
1 Parent(s): 1ad94a4
Files changed (1) hide show
  1. handler.py → app.py +36 -30
handler.py → app.py RENAMED
@@ -1,49 +1,55 @@
1
- import os
2
  import torch
3
- from transformers import BertTokenizer
4
- from joblib import load
 
5
  import torch.nn.functional as F
6
 
7
- # Load tokenizer (ensure it's the same one used in training)
8
- tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
9
-
10
- # Load the model (ensure the same model that was trained)
11
- current_dir = os.path.dirname(os.path.abspath(__file__))
12
- model_path = os.path.join(current_dir, "model.joblib")
13
-
14
- print(f"Loading model from: {model_path}")
15
- model = load(model_path)
16
 
17
- # Set the model to evaluation mode
 
 
18
  model.eval()
19
 
20
- # Define class names (same as used during training)
21
- class_names = ["JAILBREAK", "INJECTION", "PHISHING", "SAFE"]
 
22
 
23
- # Inference function that will be called by Hugging Face Inference API
24
  def classify_text(text):
25
  encoding = tokenizer(str(text), truncation=True, padding=True, max_length=128, return_tensors='pt')
26
- input_ids = encoding['input_ids']
27
- attention_mask = encoding['attention_mask']
28
-
29
- # Move tensors to device if needed
30
- if torch.cuda.is_available():
31
- input_ids = input_ids.cuda()
32
- attention_mask = attention_mask.cuda()
33
- model.cuda()
34
 
35
  with torch.no_grad():
36
  outputs = model(input_ids, attention_mask=attention_mask)
37
  logits = outputs.logits
38
  probabilities = F.softmax(logits, dim=-1)
39
  confidence, predicted_class = torch.max(probabilities, dim=-1)
40
-
 
41
  predicted_label = class_names[predicted_class.item()]
42
  confidence_score = confidence.item()
43
 
44
- return {"label": predicted_label, "confidence": confidence_score}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- # The inference API calls this method with a JSON request.
47
- def inference(inputs):
48
- text = inputs["inputs"]
49
- return classify_text(text)
 
 
1
  import torch
2
+ import joblib
3
+ from flask import Flask, request, jsonify
4
+ from transformers import BertTokenizer, BertForSequenceClassification
5
  import torch.nn.functional as F
6
 
7
+ # Initialize Flask application
8
+ app = Flask(__name__)
 
 
 
 
 
 
 
9
 
10
+ # Load model and tokenizer
11
+ model = joblib.load('model.joblib')
12
+ tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
13
  model.eval()
14
 
15
+ # Set device to CUDA if available
16
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17
+ model.to(device)
18
 
19
+ # Inference function
20
  def classify_text(text):
21
  encoding = tokenizer(str(text), truncation=True, padding=True, max_length=128, return_tensors='pt')
22
+ input_ids = encoding['input_ids'].to(device)
23
+ attention_mask = encoding['attention_mask'].to(device)
 
 
 
 
 
 
24
 
25
  with torch.no_grad():
26
  outputs = model(input_ids, attention_mask=attention_mask)
27
  logits = outputs.logits
28
  probabilities = F.softmax(logits, dim=-1)
29
  confidence, predicted_class = torch.max(probabilities, dim=-1)
30
+
31
+ class_names = ["JAILBREAK", "INJECTION", "PHISHING", "SAFE"]
32
  predicted_label = class_names[predicted_class.item()]
33
  confidence_score = confidence.item()
34
 
35
+ return predicted_label, confidence_score
36
+
37
+ # Define the inference route
38
+ @app.route('/inference', methods=['POST'])
39
+ def inference():
40
+ data = request.json
41
+ if 'text' not in data:
42
+ return jsonify({"error": "No text provided"}), 400
43
+
44
+ text = data['text']
45
+ label, confidence = classify_text(text)
46
+
47
+ return jsonify({
48
+ 'text': text,
49
+ 'classification': label,
50
+ 'confidence': confidence
51
+ })
52
 
53
+ # Start the Flask server
54
+ if __name__ == '__main__':
55
+ app.run(host='0.0.0.0', port=8080)