def edit_td_data():
    if session.get('u-type') is None:
        return render_template('error_page.html')
    if request.method =="POST":
        td_id = request.form['td-id']
        td_obj = td.TdDAO()
        p =td_obj.getData(td_id)        
        return render_template("editTrainingDataset.html", p = p)
def edit_fd_cloud_data():
    if session.get('u-type') is None:
        return render_template('error_page.html')
    import boto3
    dynamoDB = boto3.resource('dynamodb')
    dynamoTable = dynamoDB.Table('feedback-form-data')
    fd_date = request.form['fd-date-id']
    response = dynamoTable.get_item(
    Key={ 'date': fd_date})
    item = response['Item']
    fd_obj = fd.FdObj()
    fd_obj.name = item['fd_name']
    fd_obj.image = item['fd_img']
    fd_obj.tid = item['td_id']
    fd_obj.file = item['fd_file']
    fd_obj.desc = item['fd_desc']
    fd_obj.date = fd_date
    fd_obj.result_page = item['result_page']
    fd_obj.fid = fd_date
    req_type = request.form['submit']
    if req_type =="modify":
        td_dao = td.TdDAO()
        training_data_dict = td_dao.fetch_all_td()
        td_obj = td.TdObj()
        td_obj = td_dao.getData(fd_obj.tid)
        return render_template('editFeedbackDataset.html', p = fd_obj, td_id = td_obj.id, dict_training_data = training_data_dict, storage_type= str("cloud"))
    elif req_type =="result-page":
        td_dao = td.TdDAO()
        model_file = td_dao.getFileNamebyID(fd_obj.tid)
        import botocore
        s3 = boto3.resource('s3')
        BUCKET_NAME = 'feedback-data-try'
        try:
            s3.Bucket(BUCKET_NAME).download_file('static/DBAlpha/FeedbackDB/Files/'+str(fd_obj.file), 'static/DBAlpha/FeedbackDB/Files/'+str(fd_obj.file))
        except botocore.exceptions.ClientError as e:
            if e.response['Error']['Code'] == "404":
                print("The object does not exist.")
            else:
                raise

        model_file = re.sub(".json", "", model_file)
        aspect_json, overall_rate, progress_json, suggestions_json = full_analysis(file_name = fd_obj.file, model_file = model_file, no_entities = -1, lang="")
        return render_template("results.html", aspect_json = aspect_json, overall_rate = overall_rate, progress_json = progress_json, suggestions_json = suggestions_json, fd_id = fd_obj.fid)
def generate_pkl():
    if session.get('u-type') is None:
        return render_template('error_page.html')
    import re
    td_file_name = request.form['td_file']
    td_file_name_without_ext = re.sub(".json", "", td_file_name)
    accuracy= make_model(file_name = td_file_name, column_review='reviewText', column_rating='overall', json_balanced = False, have_corpus = False, size = 25000)
    model_name = td_file_name_without_ext + ".h5"
    td_dao = td.TdDAO()
    td_dao.updateModel(td_file_name, model_name, accuracy*100)
    return render_template("confirmTD.html", status = 1)
def edit_fd_data():
    if session.get('u-type') is None:
        return render_template('error_page.html')
    if request.method =="POST":
        fd_id = request.form['fd-id']
        fd_dao = fd.FdDAO()
        fd_obj = fd.FdObj()
        fd_obj = fd_dao.getData(fd_id)
        req_type = request.form['submit']
        if req_type =="modify":
            td_dao = td.TdDAO()
            training_data_dict = td_dao.fetch_all_td()
            td_obj = td.TdObj()
            td_obj = td_dao.getData(fd_obj.tid)
            return render_template("editFeedbackDataset.html", p = fd_obj, td_id = td_obj.id, dict_training_data = training_data_dict, storage_type= str("local"))
        elif req_type =="result-page":
            td_dao = td.TdDAO()
            model_file = td_dao.getFileNamebyID(fd_obj.tid)
            model_file = re.sub(".json", "", model_file)
            aspect_json, overall_rate, progress_json, suggestions_json = full_analysis(file_name = fd_obj.file, model_file = model_file, no_entities = 2, lang="")
            return render_template("results.html", aspect_json = aspect_json, overall_rate = overall_rate, progress_json = progress_json, suggestions_json = suggestions_json, fd_id = fd_id)
def edit_td_data_Confirm():
    if session.get('u-type') is None:
        return render_template('error_page.html')
    if request.method == "POST":
        req_type = request.form['submit']
        td_id = request.form['td-id']
        td_obj = td.TdDAO()        
        if req_type == 'update':
            td_name = request.form['td-name']
            td_desc = request.form['td-desc']
            status = (td_obj.updateName(td_id, td_name)) and (td_obj.updateDesc(td_id, td_desc))
            td_names = td_obj.fetch_all_td()
            return render_template("viewTrainingDatasets.html",td_names = td_names)
        elif req_type == 'remove':
            file_name = request.form['td-file']
            file_name = re.sub(".json ", "", file_name)
            status = td_obj.delete_dataset(td_id, UPLOAD_TRAINING_FILES+str(request.form['td-file']), UPLOAD_TRAINING_IMAGES+str(request.form['td-img']), 
                                           UPLOAD_TRAINING_MODELS+str(file_name)+".h5", UPLOAD_TRAINING_MODELS+"TOKEN_"+str(file_name)+".pkl")
            print("Dataset Deleted Status: ", status)
            td_names = td_obj.fetch_all_td()
            return render_template("viewTrainingDatasets.html",td_names = td_names)
def add_td_confirm():
    if session.get('u-type') is None:
        return render_template('error_page.html')
    if request.method == "POST":
        td_name = request.form['td-name']
        thumb_img  = request.files['thumb-nail']
        td_file = request.files['td-file']
        thumb_img_name = secure_filename(thumb_img.filename)
        td_file_name = secure_filename(td_file.filename)
        desc = request.form['td-desc']
        td_obj = td.TdDAO()        
        from datetime import date,datetime
        today = date.today()    
        text_to_append = str(datetime.now())[:-7].replace(":","").replace(" ","").replace("-","")
        file_name1 = os.path.splitext(thumb_img_name)
        thumb_img_name  ="IMG" + str(text_to_append) +file_name1[1]
        file_name1 = os.path.splitext(td_file_name)
        td_file_name  = "TD"+ str(text_to_append) +file_name1[1]
        thumb_img.save(os.path.join(app.config['UPLOAD_TRAINING_IMAGES'], thumb_img_name))
        td_file.save(os.path.join(app.config['UPLOAD_TRAINING_FILES'], td_file_name))        
        status = td_obj.setData(td_name, thumb_img_name, td_file_name, 0, desc, 'sample.pkl', str(today),97.65)
        json_td_file_name = {"td_file" : td_file_name}
        return render_template("generatePKL.html", status = status, json_td_file_name = json_td_file_name)
def add_fd():
    if session.get('u-type') is None:
        return render_template('error_page.html')
    td_obj = td.TdDAO()
    training_data_dict = td_obj.fetch_all_td()
    return render_template('addFeedbackDatasets.html', dict_training_data = training_data_dict)  
def view_td():
    if session.get('u-type') is None:
        return render_template('error_page.html')
    td_obj = td.TdDAO()
    td_names = td_obj.fetch_all_td()
    return render_template('viewTrainingDatasets.html',td_names = td_names )