Spaces:
Running
Running
Create two_sample_tester.py
Browse files- two_sample_tester.py +47 -0
two_sample_tester.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from roberta_model_loader import roberta_model
|
3 |
+
from feature_ref_loader import feature_two_sample_tester_ref
|
4 |
+
from meta_train import net
|
5 |
+
from regression_model_loader import regression_model
|
6 |
+
from MMD import MMD_batch2
|
7 |
+
from utils import DEVICE, FeatureExtractor
|
8 |
+
|
9 |
+
|
10 |
+
class TwoSampleTester:
|
11 |
+
def __init__(self, net=net, model=roberta_model):
|
12 |
+
print("TwoSample Tester init")
|
13 |
+
self.net = net
|
14 |
+
self.model = model
|
15 |
+
self.feature_extractor = FeatureExtractor(model, net)
|
16 |
+
|
17 |
+
def test(self, input_text):
|
18 |
+
print("TwoSample Tester test")
|
19 |
+
# Get the feature for input text
|
20 |
+
feature_for_input_text = self.feature_extractor.process(input_text)
|
21 |
+
# print(
|
22 |
+
# "DEBUG: feature_for_input_text:",
|
23 |
+
# feature_for_input_text.shape,
|
24 |
+
# feature_two_sample_tester_ref.shape,
|
25 |
+
# )
|
26 |
+
# Calculate MMD
|
27 |
+
mmd_feature_for_input_text = MMD_batch2(
|
28 |
+
torch.cat([feature_two_sample_tester_ref, feature_for_input_text], dim=0),
|
29 |
+
feature_two_sample_tester_ref.shape[0],
|
30 |
+
0,
|
31 |
+
self.net.sigma,
|
32 |
+
self.net.sigma0_u,
|
33 |
+
self.net.ep,
|
34 |
+
).to("cpu")
|
35 |
+
# Use the regression model to get the 2-sample test result
|
36 |
+
y_pred_loaded = regression_model.model.predict(
|
37 |
+
mmd_feature_for_input_text.detach().numpy().reshape(-1, 1)
|
38 |
+
)
|
39 |
+
|
40 |
+
prediction = int(y_pred_loaded[0])
|
41 |
+
if prediction == 0:
|
42 |
+
return "Human"
|
43 |
+
elif prediction == 1:
|
44 |
+
return "AI"
|
45 |
+
|
46 |
+
|
47 |
+
two_sample_tester = TwoSampleTester()
|