Last active
December 26, 2015 15:19
-
-
Save carlotorniai/7171495 to your computer and use it in GitHub Desktop.
Flask script to calssify a chunk of text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| from flask import Flask, request, Response, redirect, url_for | |
| import pickle | |
| from nbpredictor import predict | |
| app = Flask(__name__) | |
| def readpickle(filename): | |
| ''' Reads a pickle file and returns its content''' | |
| infile = open(filename, "rb") | |
| content = pickle.load(infile) | |
| infile.close() | |
| return content | |
| # Load the trained model and the vectorizer | |
| trained_model = readpickle('trained_nb_model.pkl') | |
| print type(trained_model) | |
| vectorizer = readpickle('vectorizer.pkl') | |
| print type(vectorizer) | |
| @app.route("/parsetext", methods=['POST']) | |
| def execute_text(): | |
| text = request.form['text'] | |
| print text | |
| if request.method == 'POST': | |
| if text=='': | |
| results = "You should post some content!" | |
| else: | |
| label = predict(trained_model, vectorizer, text, True) | |
| print label | |
| results = "Your text belongs to the " + label + " section of the NYT" | |
| return Response(results, status=200, mimetype='text/plain') | |
| else: | |
| return "Post a URL form a NYT article!!" | |
| # Order of routes matters | |
| @app.route("/<name>") | |
| def hello(name): | |
| return "Hello " + name + "!\nWelcome to my NYT article section Predictor! )" | |
| @app.route("/") | |
| def index(): | |
| return redirect(url_for('static', filename='index.html')) | |
| if __name__ == "__main__": | |
| app.run(host='0.0.0.0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment