-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaiohttp_backend.py
94 lines (80 loc) · 2.76 KB
/
aiohttp_backend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import functools, asyncio
from json import JSONDecodeError
from aiohttp import web
from newspaper import Article
from train import create_classifiers
from pathlib import Path
text_dataset_classifier = create_classifiers()
def get_article(url):
article = Article(url)
article.download()
article.parse()
return [article.title, article.text]
def real_or_fake(url=None, title=None, text=None, classifiers=[0], train_req=None):
if title == None and text == None and url != None:
(title, text) = get_article(url)
elif title == None and text == None and url == None:
raise ValueError("No URL or title or text provided. Can't proceed.")
for item in train_req:
id = item.get("id",-1)
opts = item.get("options", {})
if id in classifiers:
if id == 0:
clfer = (0, opts.get("criterion","entropy"), opts.get("max_depth",5), opts.get("min_samples_leaf",1000), opts.get("min_samples_split",1000) )
elif id == 1:
clfer = (1, opts.get("type","multinomial"), opts.get("alpha",0.5) )
elif id == 2:
clfer = (2, opts.get("k_neighbours",4), opts.get("weight","uniform"), opts.get("power", 2) )
elif id == 3:
clfer = (3, opts.get("param_c", 1), opts.get("kernel","linear"), opts.get("degree",2) )
text_dataset_classifier.train(classifier=clfer)
return_results = {}
results = text_dataset_classifier.predict(title, text, classifiers)
for i in range(len(results)):
return_results[classifiers[i]] = int(results[i].item(0))
for i in [0, 1, 2, 3]:
if return_results.get(i, None) == None:
return_results[i] = -1
return [title, return_results]
routes = web.RouteTableDef()
app = web.Application()
@routes.post("/predict")
async def default_route(request):
try:
data = await request.json()
except JSONDecodeError:
return web.json_response({"message": "Malformed request"}, status=400)
print(data)
request_url = data.get("request_url", "")
classifiers = []
for classifier in data.get("classifiers", []):
if classifier.get("id", -1) != -1 and classifier.get("active", False) == True:
classifiers.append(classifier.get("id", -1))
print(classifiers)
try:
(title, results) = await asyncio.wait_for(asyncio.shield(asyncio.get_running_loop().run_in_executor(
None,
functools.partial(real_or_fake, url=request_url, classifiers=classifiers, train_req=data.get("classifiers", [])),
)),timeout=80)
except asyncio.TimeoutError:
return web.json_response(
{"status": "scheduled"},
status=200,
)
print(request_url, results)
return web.json_response(
{
"status": "processed",
"result": results,
"title": title,
"request_url": request_url,
"request": data.get("classifiers", []),
"stats": [],
},
status=200,
)
@routes.get("/")
async def health(request):
return web.Response(text="OK")
app.add_routes(routes)
web.run_app(app, port=3636)