Guangsheng Bao commited on
Commit ·
382f974
1
Parent(s): 8b5ebbb
update classifier
Browse files- app.py +0 -2
- configs/glimpse.json +6 -2
- detector_base.py +15 -3
app.py
CHANGED
|
@@ -57,8 +57,6 @@ def handle_request(detector_name):
|
|
| 57 |
return return_data(0, '', info)
|
| 58 |
except Exception as ex:
|
| 59 |
print(datetime.datetime.now().isoformat(), ex, flush=True)
|
| 60 |
-
import os
|
| 61 |
-
os._exit(1)
|
| 62 |
return return_data(400, 'Bad request', str(ex))
|
| 63 |
|
| 64 |
|
|
|
|
| 57 |
return return_data(0, '', info)
|
| 58 |
except Exception as ex:
|
| 59 |
print(datetime.datetime.now().isoformat(), ex, flush=True)
|
|
|
|
|
|
|
| 60 |
return return_data(400, 'Bad request', str(ex))
|
| 61 |
|
| 62 |
|
configs/glimpse.json
CHANGED
|
@@ -8,7 +8,11 @@
|
|
| 8 |
"prompt": "prompt3",
|
| 9 |
"rank_size": 1000,
|
| 10 |
"top_k": 5,
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
"cache_dir": "../cache"
|
| 14 |
}
|
|
|
|
| 8 |
"prompt": "prompt3",
|
| 9 |
"rank_size": 1000,
|
| 10 |
"top_k": 5,
|
| 11 |
+
"classifier": {
|
| 12 |
+
"mu0": -3.8289,
|
| 13 |
+
"sigma0": 2.0131,
|
| 14 |
+
"mu1": -0.0733,
|
| 15 |
+
"sigma1": 4.0262
|
| 16 |
+
},
|
| 17 |
"cache_dir": "../cache"
|
| 18 |
}
|
detector_base.py
CHANGED
|
@@ -6,9 +6,17 @@ import os
|
|
| 6 |
from utils import load_json
|
| 7 |
from types import SimpleNamespace
|
| 8 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def sigmoid(x):
|
| 11 |
-
return 1 / (1 + np.exp(-x))
|
| 12 |
|
| 13 |
class DetectorBase:
|
| 14 |
def __init__(self, config_name):
|
|
@@ -30,7 +38,11 @@ class DetectorBase:
|
|
| 30 |
|
| 31 |
def compute_prob(self, text):
|
| 32 |
crit, ntoken = self.compute_crit(text)
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
return prob, crit, ntoken
|
| 35 |
|
| 36 |
def __str__(self):
|
|
|
|
| 6 |
from utils import load_json
|
| 7 |
from types import SimpleNamespace
|
| 8 |
import numpy as np
|
| 9 |
+
from scipy.stats import norm
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# Considering balanced classification that p(D0) equals to p(D1), we have
|
| 13 |
+
# p(D1|x) = p(x|D1) / (p(x|D1) + p(x|D0))
|
| 14 |
+
def compute_prob_norm(x, mu0, sigma0, mu1, sigma1):
|
| 15 |
+
pdf_value0 = norm.pdf(x, loc=mu0, scale=sigma0)
|
| 16 |
+
pdf_value1 = norm.pdf(x, loc=mu1, scale=sigma1)
|
| 17 |
+
prob = pdf_value1 / (pdf_value0 + pdf_value1)
|
| 18 |
+
return prob
|
| 19 |
|
|
|
|
|
|
|
| 20 |
|
| 21 |
class DetectorBase:
|
| 22 |
def __init__(self, config_name):
|
|
|
|
| 38 |
|
| 39 |
def compute_prob(self, text):
|
| 40 |
crit, ntoken = self.compute_crit(text)
|
| 41 |
+
mu0 = self.config.classifier['mu0']
|
| 42 |
+
sigma0 = self.config.classifier['sigma0']
|
| 43 |
+
mu1 = self.config.classifier['mu1']
|
| 44 |
+
sigma1 = self.config.classifier['sigma1']
|
| 45 |
+
prob = compute_prob_norm(crit, mu0, sigma0, mu1, sigma1)
|
| 46 |
return prob, crit, ntoken
|
| 47 |
|
| 48 |
def __str__(self):
|