@app.route('/predict', methods=['POST', 'GET'])
def predict():
    if (request.method == 'POST'):
        email_raw = request.files['email_raw']
        if (email_raw.filename != ''):
            temp_name = next(tempfile._get_candidate_names())
            with open(f'./app/data/uploads/{temp_name}.eml', 'wb') as f:
                f.write(email_raw.read())
            (spam, prediction) = predict_email.predict_emails([f'./app/data/uploads/{temp_name}.eml'])
            if spam:
                page = 'spam.html'
                score = int(round((prediction[0][1] * 100)))
            else:
                page = 'ham.html'
                score = int(round((prediction[0][0] * 100)))
            r = make_response(render_template(page, prediction=score))
            r.headers.add('Access-Control-Allow-Origin', '*')
            r.headers.add('Access-Control-Expose-Headers', 'Content-Disposition')
            return r
        else:
            return render_template('home.html')
    else:
        return render_template('home.html')
@app.route('/predict', methods=['POST', 'GET'])
def predict():
    if (request.method == 'POST'):
        email_raw = request.files['email_raw']
        if (email_raw.filename != ):
            temp_name = next(tempfile._get_candidate_names())
            with open(f'./app/data/uploads/{temp_name}.eml', 'wb') as f:
                f.write(email_raw.read())
            (spam, prediction) = predict_email.predict_emails([f'./app/data/uploads/{temp_name}.eml'])
            if spam:
                page = 'spam.html'
                score = int(round((prediction[0][1] * 100)))
            else:
                page = 'ham.html'
                score = int(round((prediction[0][0] * 100)))
            r = make_response(render_template(page, prediction=score))
            r.headers.add('Access-Control-Allow-Origin', '*')
            r.headers.add('Access-Control-Expose-Headers', 'Content-Disposition')
            return r
        else:
            return render_template('home.html')
    else:
        return render_template('home.html')