joaocansi commited on
Commit
357cf66
·
1 Parent(s): b9cf3e2

feat: add bert and isoforest

Browse files
Files changed (1) hide show
  1. app.py +25 -3
app.py CHANGED
@@ -1,7 +1,29 @@
 
 
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
  demo.launch()
 
1
+ from transformers import BertTokenizer, BertModel, AutoTokenizer, AutoModel
2
+ from sklearn.ensemble import IsolationForest
3
+ from tqdm import tqdm
4
+
5
+ import torch
6
  import gradio as gr
7
+ import numpy as np
8
+
9
+ tokenizer = AutoTokenizer.from_pretrained("neuralmind/bert-base-portuguese-cased")
10
+ model = AutoModel.from_pretrained("neuralmind/bert-base-portuguese-cased")
11
+ model.eval()
12
+
13
+ data = np.load("X_test.npy")
14
+ iso_forest = IsolationForest(contamination=0.1, random_state=42)
15
+ iso_forest.fit(data)
16
 
17
+ def test_email(text):
18
+ with torch.no_grad():
19
+ inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True, max_length=256)
20
+ outputs = model(**inputs)
21
+ cls_embedding = outputs.last_hidden_state[:, 0, :].cpu().numpy()
22
+ pred = iso_forest.predict(cls_embedding)[0]
23
+ if pred == -1:
24
+ return "Anomaly detected"
25
+ else:
26
+ return "Normal"
27
 
28
+ demo = gr.Interface(fn=test_email, inputs="text", outputs="text")
29
  demo.launch()