Create post_endpoint.patch
Browse files- post_endpoint.patch +58 -0
post_endpoint.patch
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
diff --git a/detector/server.py b/detector/server.py
|
2 |
+
index 34a0c85..8c190e4 100644
|
3 |
+
--- a/detector/server.py
|
4 |
+
+++ b/detector/server.py
|
5 |
+
@@ -20,6 +20,22 @@ def log(*args):
|
6 |
+
|
7 |
+
class RequestHandler(SimpleHTTPRequestHandler):
|
8 |
+
|
9 |
+
+ def do_POST(self):
|
10 |
+
+ content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
|
11 |
+
+ post_data = self.rfile.read(content_length) # <--- Gets the data itself
|
12 |
+
+ post_data = json.loads(post_data.decode('utf-8'))
|
13 |
+
+
|
14 |
+
+ self.begin_content('application/json;charset=UTF-8')
|
15 |
+
+
|
16 |
+
+ all_tokens, used_tokens, real, fake = self.infer(post_data['text'])
|
17 |
+
+
|
18 |
+
+ self.wfile.write(json.dumps(dict(
|
19 |
+
+ all_tokens=all_tokens,
|
20 |
+
+ used_tokens=used_tokens,
|
21 |
+
+ real_probability=real,
|
22 |
+
+ fake_probability=fake
|
23 |
+
+ )).encode())
|
24 |
+
+
|
25 |
+
def do_GET(self):
|
26 |
+
query = unquote(urlparse(self.path).query)
|
27 |
+
|
28 |
+
@@ -32,6 +48,16 @@ class RequestHandler(SimpleHTTPRequestHandler):
|
29 |
+
|
30 |
+
self.begin_content('application/json;charset=UTF-8')
|
31 |
+
|
32 |
+
+ all_tokens, used_tokens, real, fake = self.infer(query)
|
33 |
+
+
|
34 |
+
+ self.wfile.write(json.dumps(dict(
|
35 |
+
+ all_tokens=all_tokens,
|
36 |
+
+ used_tokens=used_tokens,
|
37 |
+
+ real_probability=real,
|
38 |
+
+ fake_probability=fake
|
39 |
+
+ )).encode())
|
40 |
+
+
|
41 |
+
+ def infer(query):
|
42 |
+
tokens = tokenizer.encode(query)
|
43 |
+
all_tokens = len(tokens)
|
44 |
+
tokens = tokens[:tokenizer.max_len - 2]
|
45 |
+
@@ -45,12 +71,7 @@ class RequestHandler(SimpleHTTPRequestHandler):
|
46 |
+
|
47 |
+
fake, real = probs.detach().cpu().flatten().numpy().tolist()
|
48 |
+
|
49 |
+
- self.wfile.write(json.dumps(dict(
|
50 |
+
- all_tokens=all_tokens,
|
51 |
+
- used_tokens=used_tokens,
|
52 |
+
- real_probability=real,
|
53 |
+
- fake_probability=fake
|
54 |
+
- )).encode())
|
55 |
+
+ return all_tokens, used_tokens, real, fake
|
56 |
+
|
57 |
+
def begin_content(self, content_type):
|
58 |
+
self.send_response(200)
|