Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions django/dkg/cancer/consumers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.http import HttpResponse
from channels.handler import AsgiHandler


def ws_message(message):
# ASGI WebSocket packet-received and send-packet message types
# both have a "text" key for their textual data.
message.reply_channel.send({
"text": message.content['text'],
})


def ws_connect(message):
message.reply_channel.send({
"text": 'connected',
})


def ws_disconnect(message):
print message.content
15 changes: 13 additions & 2 deletions django/dkg/cancer/json_api/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,24 @@ def train(request):
persistence.save_batch(request_body['articles'])
articles_with_keywords_and_probas = persistence.load_data()

# retrain model and save to disk
cancer.model_api.model.train_model(
import concurrent.futures
executor = concurrent.futures.ThreadPoolExecutor(max_workers=4)
# retrain model and save to disc
training_future = executor.submit(
cancer.model_api.model.train_model,
articles_with_keywords_and_probas,
django.conf.settings.MODEL_PATH,
django.conf.settings.LABEL_CODES_PATH
)

def meh(future):
print future.result()
return 1

training_future.add_done_callback(meh)

# TODO: send notification (ws) to client, that model training is done

return HttpResponse(status=200)


Expand Down
8 changes: 8 additions & 0 deletions django/dkg/cancer/routing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from channels.routing import route
from cancer.consumers import ws_message, ws_connect, ws_disconnect

channel_routing = [
route("websocket.receive", ws_message),
route("websocket.connect", ws_connect),
route("websocket.disconnect", ws_disconnect),
]
13 changes: 13 additions & 0 deletions django/dkg/cancer/templates/cancer/bs.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>

<script>
// Note that the path doesn't matter for routing; any WebSocket
// connection gets bumped over to WebSocket consumers
socket = new WebSocket("ws://" + window.location.host + "/events/");
socket.onmessage = function(e) {
alert(e.data);
}
//socket.onopen = function() {
// socket.send("hello world");
//}
// Call onopen directly if socket is already open
//if (socket.readyState == WebSocket.OPEN) socket.onopen();
</script>
<script>
var labeller_name = 'dkg-test';

Expand Down
12 changes: 11 additions & 1 deletion django/dkg/dkg/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'cancer'
'cancer',
'channels'
]

MIDDLEWARE = [
Expand Down Expand Up @@ -137,3 +138,12 @@

# label pruning normalized value counts threshold
LABEL_PRUNING_VALUE_COUNTS_THRESHOLD = 0.66


# django channels config
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgiref.inmemory.ChannelLayer",
"ROUTING": "cancer.routing.channel_routing",
},
}
3 changes: 2 additions & 1 deletion django/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Django==1.11
Django==1.11.4
numpy==1.12.1
pandas==0.19.2
scikit-learn==0.18.1
scipy==0.19.0
channels==1.1.6