Loading server.py +10 −7 Original line number Diff line number Diff line Loading @@ -25,7 +25,7 @@ def get_script(): doctype = 'text/js' return send_from_directory('web/js', 'main.js', mimetype=doctype) # Return the prediction label to the frontend # Endpoint to return the prediction label to the frontend @app.route('/predict', methods=["POST"]) def predict(): json = request.get_json() Loading @@ -34,17 +34,19 @@ def predict(): message = json['message'] #response = json.dumps({'prediction': 'spam'}) response = {'prediction': ml.makePrediction(message)} return response return response # just for debugging, for now... TODO integrate the code with ml.py to get a response # TODO hopefully it's obvious that these functions need to be implemented # TODO implement the partial fitting portion of the code here # Endpoint to allow user to correct the prediction and partial fit with each algorithm @app.route('/correctPrediction', methods=["POST"]) def correctPrediction(): json = request.get_json() # message = json["message"] # label = json["label"] print(json) return json # just for debugging TODO integrate the code with ml.py to get a response # TODO make this get the actual ranking of algorithms @app.route('/getAlgorithms', methods=["GET"]) def getTopFiveAlgorithms(): name = ["This (in use)", "Comes", "From", "The", "Server"] Loading @@ -62,6 +64,7 @@ def getTopFiveAlgorithms(): } return val # TODO make this get the actual top feature words @app.route('/getWords', methods=["GET"]) def getTopFiveWords(): word = ["Larry", "Schultheis", "Larry", "Schultheis", "Larry"] Loading web/js/main.js +25 −23 Original line number Diff line number Diff line Loading @@ -40,36 +40,38 @@ function getPrediction() { // todo fix this to actually send the message data function correctPrediction(correctLabel) { if (document.getElementById("message").value != '') { var data = JSON.stringify({ "message": document.getElementById('message').value, label: correctLabel }); console.log(data) // debug var message = document.getElementById('message').value; console.log(message + " is " + correctLabel); // starter code var url = "http://localhost:8080" var endpoint = "/correctPrediction" var replyObj var http = new XMLHttpRequest(); http.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); http.open("GET", url + endpoint, true); http.open("POST", url + endpoint, true); http.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); http.onreadystatechange = function () { var DONE = 4; // 4 means that the request is done var OK = 200; // 200 means a successful return if (http.readyState == DONE && http.status == OK && http.responseText) { // JSON string replyString = http.responseText; // JSON -> JS obj // JSON -> JS object replyObj = JSON.parse(replyString); document.getElementById("prediction").innerText = replyObj.prediction; // signifies that we need a string called prediction from the backend console.log(replyString) } else { console.log("Error correcting prediction from the backend server. (Is it running?)"); } http.send(data); } } Loading Loading
server.py +10 −7 Original line number Diff line number Diff line Loading @@ -25,7 +25,7 @@ def get_script(): doctype = 'text/js' return send_from_directory('web/js', 'main.js', mimetype=doctype) # Return the prediction label to the frontend # Endpoint to return the prediction label to the frontend @app.route('/predict', methods=["POST"]) def predict(): json = request.get_json() Loading @@ -34,17 +34,19 @@ def predict(): message = json['message'] #response = json.dumps({'prediction': 'spam'}) response = {'prediction': ml.makePrediction(message)} return response return response # just for debugging, for now... TODO integrate the code with ml.py to get a response # TODO hopefully it's obvious that these functions need to be implemented # TODO implement the partial fitting portion of the code here # Endpoint to allow user to correct the prediction and partial fit with each algorithm @app.route('/correctPrediction', methods=["POST"]) def correctPrediction(): json = request.get_json() # message = json["message"] # label = json["label"] print(json) return json # just for debugging TODO integrate the code with ml.py to get a response # TODO make this get the actual ranking of algorithms @app.route('/getAlgorithms', methods=["GET"]) def getTopFiveAlgorithms(): name = ["This (in use)", "Comes", "From", "The", "Server"] Loading @@ -62,6 +64,7 @@ def getTopFiveAlgorithms(): } return val # TODO make this get the actual top feature words @app.route('/getWords', methods=["GET"]) def getTopFiveWords(): word = ["Larry", "Schultheis", "Larry", "Schultheis", "Larry"] Loading
web/js/main.js +25 −23 Original line number Diff line number Diff line Loading @@ -40,36 +40,38 @@ function getPrediction() { // todo fix this to actually send the message data function correctPrediction(correctLabel) { if (document.getElementById("message").value != '') { var data = JSON.stringify({ "message": document.getElementById('message').value, label: correctLabel }); console.log(data) // debug var message = document.getElementById('message').value; console.log(message + " is " + correctLabel); // starter code var url = "http://localhost:8080" var endpoint = "/correctPrediction" var replyObj var http = new XMLHttpRequest(); http.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); http.open("GET", url + endpoint, true); http.open("POST", url + endpoint, true); http.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); http.onreadystatechange = function () { var DONE = 4; // 4 means that the request is done var OK = 200; // 200 means a successful return if (http.readyState == DONE && http.status == OK && http.responseText) { // JSON string replyString = http.responseText; // JSON -> JS obj // JSON -> JS object replyObj = JSON.parse(replyString); document.getElementById("prediction").innerText = replyObj.prediction; // signifies that we need a string called prediction from the backend console.log(replyString) } else { console.log("Error correcting prediction from the backend server. (Is it running?)"); } http.send(data); } } Loading