from __future__ import division
from flask import Flask, render_template, request, url_for,send_from_directory,session,redirect,make_response,jsonify

import os

import binascii


from mlModule.FastPredict import FastPredict

from mlModule.Predict23LSTM import Predictor23LSTM

from SimilarUIRoutes import similarUIRoutes

from SUTutorialRoutes import sUTutorialRoutes

# Setting all folders
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(APP_ROOT, 'templates/uploads')
STYLESHEETS_FOLDER = os.path.join(APP_ROOT, 'templates','stylesheets')
IMAGES_FOLDER = os.path.join(APP_ROOT, 'templates','images')
FONTS_FOLDER = os.path.join(APP_ROOT, 'templates','fonts')
FONTS_AWESOME_CSS_FOLDER = os.path.join(APP_ROOT, 'templates','font-awesome','css')
FONTS_AWESOME_FONTS_FOLDER = os.path.join(APP_ROOT, 'templates','font-awesome','fonts')
SCRIPTS_FOLDER = os.path.join(APP_ROOT, 'templates','javascripts')
STORAGE_FOLDER = os.path.join(APP_ROOT, 'PrimaryScreenShots','StoreElement')
UI_IMAGES_FOLDER= os.path.join(APP_ROOT, 'PrimaryScreenShots','PrimaryFocus')
TURK_UI_IMAGES_FOLDER= os.path.join(APP_ROOT, 'PrimaryScreenShots','SecondBatch')
FULL_UI_IMAGES_FOLDER= os.path.join(APP_ROOT, 'PrimaryScreenShots','FullUI')

output_directory = os.path.join(APP_ROOT,'My23Records5')
export_dir = os.path.join(APP_ROOT,'My23Records5','tb')



ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg','PNG','JPG','JPEG'])
TemplateFolder = os.path.join(APP_ROOT, 'templates')
ProjectGenerateFolder = os.path.join(APP_ROOT, 'templates', 'sketchupload')




# Loading Model for faster prediction

PREDICTOR = Predictor23LSTM(export_dir,output_directory)
FASTPREDICT = FastPredict(PREDICTOR.classifier,PREDICTOR.example_input_fn)


app = Flask(__name__)
app.secret_key = "pix2appSMDrawKey"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

# Creating Blue Print for Tutorial and Similar Retrieval
app.register_blueprint(similarUIRoutes)
app.register_blueprint(sUTutorialRoutes)

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS




def generateToken(tokenSize):
    byteToken = os.urandom(tokenSize)
    hexToken = binascii.hexlify(byteToken)
    return hexToken.decode("utf-8")


# Set Canvas Size for Retrieval
@app.route('/SetCanvasSize/', methods=['GET','POST'])
def SetCanvasSize(): 
    
    session['canvas_width'] =  request.form['canvas_width']    
    session['canvas_height'] =  request.form['canvas_height']    
    response = jsonify(success ="ok")
    return response


def rectObjtoJson(rectObj):
    dictObj = {'x':str(rectObj.x), 'y':str(rectObj.y),'width':str(rectObj.width),'height':str(rectObj.height),'iconID':str(rectObj.iconID),'elementId':str(rectObj.elementId)}
    return dictObj


# When user select a different icon from radio prediction change in accordingly
@app.route('/ClassSelect/', methods=['GET', 'POST'])
def ClassSelect():
    if request.method == 'POST':
        session['CurrentClassLabel'] = str(request.form['selectClassLabel'])
    return "Success"

@app.route('/stylesheets/<filename>')
def send_css(filename):
    return send_from_directory(STYLESHEETS_FOLDER, filename)

@app.route('/images/<filename>')
def send_img(filename):
    return send_from_directory(IMAGES_FOLDER, filename)

@app.route('/javascripts/<filename>')
def send_jss(filename):
    return send_from_directory(SCRIPTS_FOLDER, filename)

@app.route('/doodle/images/<filename>')
def send_fullUI_img(filename):
    return send_from_directory(IMAGES_FOLDER, filename)
if __name__ == "__main__":
    
    app.run(threaded=True)

from __future__ import division
from flask import Flask, render_template, request, url_for,send_from_directory,session,redirect,make_response,jsonify

import os

import binascii


from mlModule.FastPredict import FastPredict

from mlModule.Predict23LSTM import Predictor23LSTM

from SimilarUIRoutes import similarUIRoutes

from SUTutorialRoutes import sUTutorialRoutes

# Setting all folders
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(APP_ROOT, 'templates/uploads')
STYLESHEETS_FOLDER = os.path.join(APP_ROOT, 'templates','stylesheets')
IMAGES_FOLDER = os.path.join(APP_ROOT, 'templates','images')
FONTS_FOLDER = os.path.join(APP_ROOT, 'templates','fonts')
FONTS_AWESOME_CSS_FOLDER = os.path.join(APP_ROOT, 'templates','font-awesome','css')
FONTS_AWESOME_FONTS_FOLDER = os.path.join(APP_ROOT, 'templates','font-awesome','fonts')
SCRIPTS_FOLDER = os.path.join(APP_ROOT, 'templates','javascripts')
STORAGE_FOLDER = os.path.join(APP_ROOT, 'PrimaryScreenShots','StoreElement')
UI_IMAGES_FOLDER= os.path.join(APP_ROOT, 'PrimaryScreenShots','PrimaryFocus')
TURK_UI_IMAGES_FOLDER= os.path.join(APP_ROOT, 'PrimaryScreenShots','SecondBatch')
FULL_UI_IMAGES_FOLDER= os.path.join(APP_ROOT, 'PrimaryScreenShots','FullUI')

output_directory = os.path.join(APP_ROOT,'My23Records5')
export_dir = os.path.join(APP_ROOT,'My23Records5','tb')



ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg','PNG','JPG','JPEG'])
TemplateFolder = os.path.join(APP_ROOT, 'templates')
ProjectGenerateFolder = os.path.join(APP_ROOT, 'templates', 'sketchupload')




# Loading Model for faster prediction

PREDICTOR = Predictor23LSTM(export_dir,output_directory)
FASTPREDICT = FastPredict(PREDICTOR.classifier,PREDICTOR.example_input_fn)


app = Flask(__name__)
app.secret_key = "pix2appSMDrawKey"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

# Creating Blue Print for Tutorial and Similar Retrieval
app.register_blueprint(similarUIRoutes)
app.register_blueprint(sUTutorialRoutes)

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS




def generateToken(tokenSize):
    byteToken = os.urandom(tokenSize)
    hexToken = binascii.hexlify(byteToken)
    return hexToken.decode("utf-8")


# Set Canvas Size for Retrieval
@app.route('/SetCanvasSize/', methods=['GET','POST'])
def SetCanvasSize(): 
    
    session['canvas_width'] =  request.form['canvas_width']    
    session['canvas_height'] =  request.form['canvas_height']    
    response = jsonify(success ="ok")
    return response


def rectObjtoJson(rectObj):
    dictObj = {'x':str(rectObj.x), 'y':str(rectObj.y),'width':str(rectObj.width),'height':str(rectObj.height),'iconID':str(rectObj.iconID),'elementId':str(rectObj.elementId)}
    return dictObj


# When user select a different icon from radio prediction change in accordingly
@app.route('/ClassSelect/', methods=['GET', 'POST'])
def ClassSelect():
    if request.method == 'POST':
        session['CurrentClassLabel'] = str(request.form['selectClassLabel'])
    return "Success"

@app.route('/stylesheets/<filename>')
def send_css(filename):
    return send_from_directory(STYLESHEETS_FOLDER, filename)

@app.route('/images/<filename>')
def send_img(filename):
    return send_from_directory(IMAGES_FOLDER, filename)

@app.route('/javascripts/<filename>')
def send_jss(filename):
    return send_from_directory(SCRIPTS_FOLDER, filename)

@app.route('/doodle/images/<filename>')
def send_fullUI_img(filename):
    return send_from_directory(IMAGES_FOLDER, filename)
if __name__ == "__main__":
    
    app.run(threaded=True)

# Setting all folders # Loading Model for faster prediction # Creating Blue Print for Tutorial and Similar Retrieval # Set Canvas Size for Retrieval # When user select a different icon from radio prediction change in accordingly