Created
August 9, 2013 14:34
-
-
Save theonewolf/6194075 to your computer and use it in GitHub Desktop.
Flask app for CMU SAMS Project 2 in Cyber Security
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
| #!/usr/bin/env python | |
| import json | |
| import subprocess | |
| import tempfile | |
| from flask import Flask, url_for, redirect, render_template, request | |
| from os import fdopen,remove | |
| app = Flask(__name__) | |
| BCLASSES = [ 'Artemis-Fowl', | |
| 'Bible', | |
| 'Fairy-Tales', | |
| 'Peter-Pan', | |
| 'Pride-and-Prejudice', | |
| 'Redwall', | |
| 'Vampire-Diaries' | |
| ] | |
| MCLASSES = [ 'Avengers', | |
| 'Batman-Begins-Dark-Knight', | |
| 'Lion-King', | |
| 'Pitch-Perfect', | |
| 'Star-Wars', | |
| 'Sweeney-Todd', | |
| 'Titanic', | |
| 'Toy-Story' | |
| ] | |
| TOKENIZE_CMD = '''/home/ubuntu/spamfiction/nbayes/nbayes_tokenizer.py %s''' | |
| B_CMD = '''/home/ubuntu/spamfiction/nbayes/nbayes_decider.py /home/ubuntu/results-books/Artemis-Fowl_counts.json /home/ubuntu/results-books/Bible_counts.json /home/ubuntu/results-books/Fairy-Tales_counts.json /home/ubuntu/results-books/Peter-Pan_counts.json /home/ubuntu/results-books/Pride-and-Prejudice_counts.json /home/ubuntu/results-books/Redwall_counts.json /home/ubuntu/results-books/Vampire-Diaries_counts.json %s''' | |
| M_CMD = '''/home/ubuntu/spamfiction/nbayes/nbayes_decider.py /home/ubuntu/results-movies/Avengers_counts.json /home/ubuntu/results-movies/Batman-Begins-Dark-Knight_counts.json /home/ubuntu/results-movies/Lion-King_counts.json /home/ubuntu/results-movies/Pitch-Perfect_counts.json /home/ubuntu/results-movies/Star-Wars_counts.json /home/ubuntu/results-movies/Sweeney-Todd_counts.json /home/ubuntu/results-movies/Titanic_counts.json /home/ubuntu/results-movies/Toy-Story_counts.json %s''' | |
| @app.route('/') | |
| def index(): | |
| return render_template('index.html') | |
| @app.route('/query', methods = ['POST']) | |
| def query(): | |
| answer = None | |
| CMD = None | |
| CLASSES = None | |
| if ('box' not in request.form) or \ | |
| ('type' not in request.form): | |
| answer = "Try again" | |
| else: | |
| if request.form['type'] == 'movie': | |
| CMD = M_CMD | |
| CLASSES = MCLASSES | |
| else: | |
| CMD = B_CMD | |
| CLASSES = BCLASSES | |
| # get raw data | |
| f, name = tempfile.mkstemp(prefix='submissions') | |
| f = fdopen(f,'w') | |
| f.write(request.form['box'].encode('utf-8','ignore')) | |
| f.close() | |
| # tokenize raw data | |
| f = open(name, 'r+') | |
| f.write(subprocess.check_output((TOKENIZE_CMD % name).split())) | |
| f.close() | |
| # classify | |
| json_out=json.loads(subprocess.check_output((CMD % name).split())) | |
| answer = CLASSES[int(json_out['chosen_class'])] | |
| remove(name) | |
| return redirect(url_for('answer', theanswer=answer)) | |
| @app.route('/answer/<theanswer>') | |
| def answer(theanswer=None): | |
| return render_template('answer.html', answer=theanswer) | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment