Beispiel #1
0
    def ModelLoad(self):
        """
        模型载入

        Raises:
            Exception: [description]
        """
        if not os.path.exists(self.save_file_path):
            raise Exception("参数 save_file_path 指向的文件路径不存在,请检查.")

        self.model = Model.fromFile(self.save_file_path)
Beispiel #2
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     config = Config().app_config
     if "pmml" in config:
         pmml_config = config["pmml"]
         if "path" in pmml_config:
             pmml_file_path = pmml_config["path"]
             if os.path.exists(pmml_file_path):
                 try:
                     self.model = Model.fromFile(pmml_file_path)
                 except Exception as exp:
                     raise ServerError(
                         f"Config file was loaded however PMML config couldn't be parsed correctly with error: {exp}"
                     )
             else:
                 raise NotFoundError(
                     f"file: {pmml_file_path} could not be loaded, as it doesn't exist"
                 )
Beispiel #3
0
    def is_support(self):
        try:
            from pypmml import Model

            model_content = self.model
            if isinstance(model_content, (bytes, bytearray)):
                model_content = model_content.decode('utf-8')

            if isinstance(model_content, str):
                # Check if a file path
                if os.path.exists(model_content):
                    self.pmml_model = Model.fromFile(model_content)
                else:
                    self.pmml_model = Model.fromString(model_content)
                return True
            else:
                Model.close()
                return False
        except Exception as e:
            return False
Beispiel #4
0
    def run(self):
        exec_env = ExecutionEnvironment.get_execution_environment()
        exec_env.set_parallelism(1)
        t_config = TableConfig()
        t_env = StreamTableEnvironment.create(exec_env, t_config)

        t_env.connect(FileSystem().path('/tmp/input')) \
            .with_format(OldCsv()
                .field('word', DataTypes.STRING())) \
            .with_schema(Schema()
                .field('word', DataTypes.STRING())) \
            .create_temporary_table('mySource')

        t_env.connect(FileSystem().path('/tmp/output')) \
            .with_format(OldCsv()
                         .field_delimiter('\t')
                         .field('word', DataTypes.STRING())
                         .field('count', DataTypes.BIGINT())) \
            .with_schema(Schema()
                         .field('word', DataTypes.STRING())
                         .field('count', DataTypes.BIGINT())) \
            .create_temporary_table('mySink')
        model = Model.fromFile('./../batch_ml/model.pmml')

        t_env.from_path('mySource') \
            .group_by('word') \
            .select('word, count(1)') \
            .insert_into('mySink')

        t_env.execute("tutorial_job")

        self.read_data()
        result = model.predict({
            "Sepal_Length": 5.1,
            "Sepal_Width": 3.5,
            "Petal_Length": 1.4,
            "Petal_Width": 0.2
        })
Beispiel #5
0
# https://pypi.org/project/pypmml/
from pypmml import Model

# The model is from http://dmg.org/pmml/pmml_examples/KNIME_PMML_4.1_Examples/single_iris_dectree.xml
model = Model.fromFile('pmmlFiles\single_iris_dectree.xml')
result = model.predict({
    "Sepal_Length": 5.1,
    "Sepal_Width": 3.5,
    "Petal_Length": 1.4,
    "Petal_Width": 0.2
})
print("r1", result)
result = model.predict({
    'sepal_length': 5.1,
    'sepal_width': 3.5,
    'petal_length': 1.4,
    'petal_width': 0.2
})
{
    'probability_Iris-setosa': 1.0,
    'probability_Iris-versicolor': 0.0,
    'probability': 1.0,
    'predicted_class': 'Iris-setosa',
    'probability_Iris-virginica': 0.0,
    'node_id': '1'
}
print("r2", result)
Beispiel #6
0
    def test_from_file(self):
        # The model is from here: http://dmg.org/pmml/pmml_examples/KNIME_PMML_4.1_Examples/single_iris_dectree.xml
        model = Model.fromFile('./resources/models/single_iris_dectree.xml')
        self.assertEqual(model.version, '4.1')

        app = model.header.application
        self.assertEqual(app.name, 'KNIME')
        self.assertEqual(app.version, '2.8.0')

        self.assertEqual(model.modelElement, 'TreeModel')
        self.assertEqual(model.functionName, 'classification')
        self.assertEqual(model.modelName, 'DecisionTree')
        self.assertEqual(model.algorithmName, None)

        self.assertEqual(
            model.inputNames,
            ['sepal_length', 'sepal_width', 'petal_length', 'petal_width'])
        inputs = model.inputFields
        self.assertEqual(len(inputs), 4)
        self.assertEqual(inputs[0].name, 'sepal_length')
        self.assertEqual(inputs[0].dataType, 'double')
        self.assertEqual(inputs[0].opType, 'continuous')

        self.assertEqual(model.targetNames, ['class'])
        targets = model.targetFields
        self.assertEqual(len(targets), 1)
        self.assertEqual(targets[0].name, 'class')
        self.assertEqual(targets[0].dataType, 'string')
        self.assertEqual(targets[0].opType, 'nominal')
        self.assertEqual(model.classes,
                         ['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'])

        self.assertEqual(model.outputNames, [
            'predicted_class', 'probability', 'probability_Iris-setosa',
            'probability_Iris-versicolor', 'probability_Iris-virginica',
            'node_id'
        ])
        outputs = model.outputFields
        self.assertEqual(outputs[0].feature, 'predictedValue')
        self.assertEqual(outputs[1].feature, 'probability')
        self.assertEqual(outputs[1].value, None)
        self.assertEqual(outputs[2].feature, 'probability')
        self.assertEqual(outputs[2].value, 'Iris-setosa')
        self.assertEqual(outputs[3].feature, 'probability')
        self.assertEqual(outputs[3].value, 'Iris-versicolor')
        self.assertEqual(outputs[4].feature, 'probability')
        self.assertEqual(outputs[4].value, 'Iris-virginica')
        self.assertEqual(outputs[5].feature, 'entityId')

        # Data in dict
        result = model.predict({
            'sepal_length': 5.1,
            'sepal_width': 3.5,
            'petal_length': 1.4,
            'petal_width': 0.2
        })
        self.assertEqual(result['predicted_class'], 'Iris-setosa')
        self.assertEqual(result['probability'], 1.0)
        self.assertEqual(result['node_id'], '1')

        result = model.predict({
            'sepal_length': 7,
            'sepal_width': 3.2,
            'petal_length': 4.7,
            'petal_width': 1.4
        })
        self.assertEqual(result['predicted_class'], 'Iris-versicolor')
        self.assertEqual(result['probability'], 0.9074074074074074)
        self.assertEqual(result['node_id'], '3')

        # Data in json
        result = model.predict(
            '[{"sepal_length": 5.1, "sepal_width": 3.5, "petal_length": 1.4, "petal_width": 0.2}]'
        )
        self.assertEqual(
            result,
            '[{"node_id":"1","probability_Iris-setosa":1.0,"predicted_class":"Iris-setosa","probability_Iris-virginica":0.0,"probability_Iris-versicolor":0.0,"probability":1.0}]'
        )

        result = model.predict(
            '{"columns": ["sepal_length", "sepal_width", "petal_length", "petal_width"], "data": [[7, 3.2, 4.7, 1.4]]}'
        )
        self.assertEqual(
            result,
            '{"columns":["predicted_class","probability","probability_Iris-setosa","probability_Iris-versicolor","probability_Iris-virginica","node_id"],"data":[["Iris-versicolor",0.9074074074074074,0.0,0.9074074074074074,0.09259259259259259,"3"]]}'
        )

        # Data in list
        result = model.predict([5.1, 3.5, 1.4, 0.2])
        self.assertEqual(result[0], 'Iris-setosa')
        self.assertEqual(result[1], 1.0)
        self.assertEqual(result[2], 1.0)
        self.assertEqual(result[3], 0.0)
        self.assertEqual(result[4], 0.0)
        self.assertEqual(result[5], '1')

        # Data in list of list
        result = model.predict([[5.1, 3.5, 1.4, 0.2], [7, 3.2, 4.7, 1.4]])
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0][0], 'Iris-setosa')
        self.assertEqual(result[0][1], 1.0)
        self.assertEqual(result[0][2], 1.0)
        self.assertEqual(result[0][3], 0.0)
        self.assertEqual(result[0][4], 0.0)
        self.assertEqual(result[0][5], '1')
        self.assertEqual(result[1][0], 'Iris-versicolor')
        self.assertEqual(result[1][1], 0.9074074074074074)
        self.assertEqual(result[1][2], 0.0)
        self.assertEqual(result[1][3], 0.9074074074074074)
        self.assertEqual(result[1][4], 0.09259259259259259)
        self.assertEqual(result[1][5], '3')

        # Data in numpy

        # Shutdown the gateway
        Model.close()
Beispiel #7
0
                                                    random_state=seed)

pipeline = Pipeline([('scaling', StandardScaler()),
                     ('xgb', XGBClassifier(n_estimators=5, seed=seed))])

pipeline.fit(X_train, y_train)
y_pred = pipeline.predict(X_test)
y_pred_proba = pipeline.predict_proba(X_test)

import pickle
import numpy as np

d = pickle.dumps(pipeline)
saved_pipeline = pickle.loads(d)
y_pred_saved = saved_pipeline.predict(X_test)
y_pred_proba_saved = saved_pipeline.predict_proba(X_test)

assert np.array_equal(y_pred, y_pred_saved), "Not equal after saved"
assert np.array_equal(y_pred_proba_saved,
                      y_pred_proba), "Not equal after saved"

from nyoka import xgboost_to_pmml
xgboost_to_pmml(saved_pipeline, features, target, "xgb-iris.pmml")

from pypmml import Model
model = Model.fromFile("xgb-iris.pmml")
y_pred_pmml = model.predict(X_test)

assert np.array_equal(
    y_pred, y_pred_pmml["predicted_Species"]), "Not equal after saved"
Beispiel #8
0
def init():
    global model_tag
    global pmmlFields
    # 下载模型
    download_model.download_model(download_model_zip_path, unzip_path)
    try:
        #如果模型路径下存在pmml文件,那么直接加载pmml模型
        #pmml文件压缩包的结构是model/xxx.pmml文件
        #因为pmml文件结构的特殊性,所以解压函数要修改代码
        model_path_childs = os.listdir(local_model_path)
        logging.info(f'模型文件夹下的文件有:{model_path_childs}')
        for child in model_path_childs:
            if child.endswith(".pmml"):
                full_path = os.path.join(local_model_path, child)
                break
                #或者是保存在model/model/part-00000中的pmml模型
            elif child == "model":
                for file in os.listdir(os.path.join(local_model_path,"model")):
                    if file.startswith("part"):
                        full_path = local_model_path + "/model/" + file
                        break

        logging.info(f'获取到的模型路径是:{full_path}')
        print("模型大小是:",os.path.getsize(full_path))
        global pmmlModel
        pmmlModel = loadPmml.fromFile(full_path)
        pmmlFields = parse_xml(full_path)
        logging.info(f'成功加载pmml模型')
        model_tag = 1
    except:
        logging.info("从pmml模型的加载处理中跳出")
        # 获取模型路径
        get_model_path(local_model_path)
        # 加载模型
        try:
            global model
            logging.info("尝试加载PipelineModel")
            model = PipelineModel.load(local_model_path)#加载模型
            model_tag = 2
        except:
            try:
            # H2O模型必须走这里
                from pysparkling.ml import H2OMOJOSettings, H2OMOJOModel
                logging.info("从加载PipelineModel的try中跳出")
                print("在except的try中尝试加载H2OMOJOModel")
                settings = H2OMOJOSettings(withDetailedPredictionCol=True)
                model = H2OMOJOModel.createFromMojo(local_model_path + '/mojo_model', settings)
                model_tag = 3
            except:
                global pipeline_model
                print("从加载H2OMOJOModel的try中跳出")
                print("尝试加载XGBModel")
                # model = XGBoostClassificationModel.load(local_model_path)
                model = load_xgb_model(local_model_path,m_type='XGBoostClassificationModel')
                if not model:
                    logging.error('XGBoostClassificationModel没有加载成功')
                pipeline_model = load_xgb_model(local_model_path, "PipelineModel")
                if not pipeline_model:
                    logging.error('XGB需要的pipelinemodel没有加载成功')
                    logging.error(pipeline_model)

                model_tag = 4
        global final_transform_json_path
        final_transform_json_path = get_jsonfile_fullname()

        # 读取json,model_json: 模型中存储的json
        with open(final_transform_json_path, encoding='utf-8') as f:
            global model_json
            model_json = json.load(f)
Beispiel #9
0
@author: pedro
"""

import pandas as pd
#from rest_framework import status
#from rest_framework.response import Response
from Propythia_ML_PMML import MachineLearning_PMML
#from sklearn2pmml import sklearn2pmml
from pypmml import Model

seq = "STGQKSIVAYTMSLGAENSIAYANNSIAIPTNFSISVTTEVMPVSMAKTAVDCTMYICGDSLECSNLLLQYGSFCTQLNRALTGIAIEQDKNTQEVFAQVKQMYKTPAIKDFGGFNFSQILPDPSKPTKRSFIEDLLFNKVTLADAGFMKQYGDCLGDVSARDLICAQKFNGLTVLPPLLTDEMVAAYTAALVSGTATAGWTFGAGAALQIPFAMQMAYRFNGIGVTQNVLYENQKLIANQFNSAIGKIQESLSSTASALGKLQDVVNQNAQALNTLVKQLSSNFGAISSVLNDILSRLDKVEAEVQIDRLITGRLQSLQTYVTQQLIRAAEIRASANLAATKMSECVLGQSKRVDFCGKGYHLMSFPQSAPHGVVFLHVTYVPSQEKNFTTAPAICHEGKAYFPREGVFVSNGTSWFITQRNFYSPQLITTDNTFVSGNCDVVIGIINNTVYDPLQPELDSFKEELDKYFKNHTSPDVDLGDISGINASVVNIQKEIDRLNEVAKNLNESLIDLQELGKYEQYIKWPWYVWLGFIAGLIAIVMVTILLCCMTSCCSCLKGACSCGSCCKFDEDDSEPVLKGVKLHYT"
gap = 1
window_size = 15

model = Model.fromFile('Model_pmml_svm_28052020.pmml')

print(model)

dataset = pd.read_csv(r'dataset3_all_svc.csv', delimiter=',')

x_original = dataset.loc[:, dataset.columns != 'labels']
labels = dataset['labels']

# create Machine learning object
# ml = MachineLearning(x_original, labels, classes=['non_vfp', 'vfp'])
ml = MachineLearning_PMML(x_original, labels, classes=['non_vfp', 'vfp'])

result = ml.predict_window(model,
                           seq=seq,
                           x=None,
Beispiel #10
0
def get_app():

    BackEndApp = Flask(__name__)
    cors = CORS(BackEndApp, resources={r"/*": {"origins": "*"}})

    mydb = mysql.connector.connect(host="127.0.0.1",
                                   user="******",
                                   passwd="",
                                   database="cactus",
                                   raise_on_warnings=True)

    mycursor = mydb.cursor(dictionary=True)
    mycursor1 = mydb.cursor()

    mycursor.execute("SELECT * FROM Model_Data WHERE Status = '1'")
    Model2 = mycursor.fetchone()

    model = Model.fromFile("ModelGBT/" + Model2['Name'])
    Accuracy = float(Model2['Accuracy'])
    print("LoadModel...({0})".format(Model2['Name']))

    with open('dataFT.json', encoding="utf8") as json_file:
        jsonFTData = json.load(json_file)

    print("====Server Start====")

    @BackEndApp.route('/')
    def hello():
        return '{"result" : "Hello","No" : 1}'

    @BackEndApp.route('/Model/Accuracy/')
    def ModelAccuracy():
        data = {"Accuracy": Accuracy}
        return json.dumps(data)

    @BackEndApp.route('/countCactusName/')
    def countCactusName():
        sql = "SELECT Result, COUNT(Result) as count_result  FROM Predict_Data GROUP BY Result ORDER BY COUNT(Result) DESC"
        mycursor.execute(sql)
        data = mycursor.fetchall()
        result = {"countCactusName": data}
        return json.dumps(result)

    @BackEndApp.route('/countDataStatus/')
    def countDataStatus():
        sql = "SELECT DataStatus, COUNT(DataStatus) as count_datastatus FROM Predict_Data GROUP BY DataStatus "
        mycursor.execute(sql)
        data = mycursor.fetchall()
        result = {"countDataStatus": data}
        return json.dumps(result)

    @BackEndApp.route('/countUserData/')
    def countUserData():
        sql = "SELECT EmV, COUNT(EmV) as count_countUserData FROM User_data GROUP BY EmV "
        mycursor.execute(sql)
        data = mycursor.fetchall()
        result = {"countUserData": data}
        return json.dumps(result)

    @BackEndApp.route('/checkServer/')
    def checkStatus():
        sql = "SELECT * FROM Sys_Status"
        mycursor.execute(sql)
        data = mycursor.fetchall()
        status = {"status_server": data}

        return json.dumps(status)

    @BackEndApp.route("/userAll/", methods=['GET'])
    def userAll():
        sql = "SELECT * FROM User_Data WHERE role = 'M'"
        mycursor.execute(sql)
        data = mycursor.fetchall()
        user = {"user": data}
        return json.dumps(user)

    @BackEndApp.route("/userAll/search/Email", methods=['GET'])
    def userAll_Email():
        sql = "SELECT * FROM User_Data WHERE role = 'M'AND Email LIKE '%{0}%'".format(
            request.args.get("email"))
        mycursor.execute(sql)
        data = mycursor.fetchall()
        user = {"user": data}
        return json.dumps(user)

    @BackEndApp.route("/userAll/search/Name", methods=['GET'])
    def userAll_Name():
        sql = "SELECT * FROM User_Data WHERE role = 'M'AND Name LIKE '%{0}%'".format(
            request.args.get("name"))
        mycursor.execute(sql)
        data = mycursor.fetchall()
        user = {"user": data}
        return json.dumps(user)

    @BackEndApp.route("/userAll/search/Surname", methods=['GET'])
    def userAll_Surname():
        sql = "SELECT * FROM User_Data WHERE role = 'M'AND Surname LIKE '%{0}%'".format(
            request.args.get("surname"))
        mycursor.execute(sql)
        data = mycursor.fetchall()
        user = {"user": data}
        return json.dumps(user)

    @BackEndApp.route("/history/all/", methods=['POST'])
    def historyAll():
        resp = request.get_json()
        sort = resp['sort']
        sortBy = resp['sortBy']
        memberId = int(resp['id'])
        cactusName = str(resp['CactusName'])
        if memberId > 0:
            if cactusName == 'null':
                if sortBy == 'id':
                    if sort == 'asc':
                        sql = "SELECT *  FROM Predict_Data WHERE Member_code = {0} ORDER BY Transaction_id".format(
                            memberId)
                    elif sort == 'desc':
                        sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} ORDER BY Transaction_id DESC".format(
                            memberId)
                elif sortBy == 'result':
                    if sort == 'asc':
                        sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} ORDER BY Result".format(
                            memberId)
                    elif sort == 'desc':
                        sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} ORDER BY Result DESC".format(
                            memberId)
                elif sortBy == 'time':
                    if sort == 'asc':
                        sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} ORDER BY Timestamp".format(
                            memberId)
                    elif sort == 'desc':
                        sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} ORDER BY Timestamp DESC".format(
                            memberId)
            else:
                if sortBy == 'id':
                    if sort == 'asc':
                        sql = "SELECT *  FROM Predict_Data WHERE Member_code = {0} AND Result LIKE '%{1}%' ORDER BY Transaction_id".format(
                            memberId, cactusName)
                    elif sort == 'desc':
                        sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} AND Result LIKE '%{1}%'  ORDER BY Transaction_id DESC".format(
                            memberId, cactusName)
                elif sortBy == 'result':
                    if sort == 'asc':
                        sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} AND Result LIKE '%{1}%'  ORDER BY Result".format(
                            memberId, cactusName)
                elif sort == 'desc':
                    sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} AND Result LIKE '%{1}%' ORDER BY Result DESC".format(
                        memberId, cactusName)
                elif sortBy == 'time':
                    if sort == 'asc':
                        sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} AND Result LIKE '%{1}%'  ORDER BY Timestamp".format(
                            memberId, cactusName)
                    elif sort == 'desc':
                        sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} AND Result LIKE '%{1}%' ORDER BY Timestamp DESC".format(
                            memberId, cactusName)
        else:
            if cactusName == 'null':
                if sortBy == 'id':
                    if sort == 'asc':
                        sql = "SELECT * FROM Predict_Data ORDER BY Transaction_id"
                    elif sort == 'desc':
                        sql = "SELECT * FROM Predict_Data ORDER BY Transaction_id DESC"
                elif sortBy == 'result':
                    if sort == 'asc':
                        sql = "SELECT * FROM Predict_Data ORDER BY Result"
                    elif sort == 'desc':
                        sql = "SELECT * FROM Predict_Data ORDER BY Result DESC"
                elif sortBy == 'time':
                    if sort == 'asc':
                        sql = "SELECT * FROM Predict_Data ORDER BY Timestamp"
                    elif sort == 'desc':
                        sql = "SELECT * FROM Predict_Data ORDER BY Timestamp DESC"
            else:
                if sortBy == 'id':
                    if sort == 'asc':
                        sql = "SELECT *  FROM Predict_Data WHERE Result LIKE '%{0}%' ORDER BY Transaction_id".format(
                            cactusName)
                    elif sort == 'desc':
                        sql = "SELECT * FROM Predict_Data WHERE Result LIKE '%{0}%'  ORDER BY Transaction_id DESC".format(
                            cactusName)
                elif sortBy == 'result':
                    if sort == 'asc':
                        sql = "SELECT * FROM Predict_Data WHERE Result LIKE '%{0}%'  ORDER BY Result".format(
                            cactusName)
                elif sort == 'desc':
                    sql = "SELECT * FROM Predict_Data WHERE Result LIKE '%{0}%' ORDER BY Result DESC".format(
                        cactusName)
                elif sortBy == 'time':
                    if sort == 'asc':
                        sql = "SELECT * FROM Predict_Data WHERE Result LIKE '%{0}%'  ORDER BY Timestamp".format(
                            cactusName)
                    elif sort == 'desc':
                        sql = "SELECT * FROM Predict_Data WHERE Result LIKE '%{0}%' ORDER BY Timestamp DESC".format(
                            cactusName)
        mycursor.execute(sql)

        data = mycursor.fetchall()
        for i in range(len(data)):
            FT = []
            for j in range(13):
                FT.append(jsonFTData['DataFT'][j]['FtList'][
                    int(data[i]['Feature'][j]) - 1]['Name'])
            data[i]['Feature'] = FT

        predict = {"predict": data}

        return json.dumps(predict)

    @BackEndApp.route("/history/", methods=['POST'])
    def history():
        resp = request.get_json()
        sort = resp['sort']
        sortBy = resp['sortBy']
        memberId = int(resp['id'])
        cactusName = str(resp['CactusName'])
        if cactusName == 'null':
            if sortBy == 'id':
                if sort == 'asc':
                    sql = "SELECT *  FROM Predict_Data WHERE Member_code = {0} ORDER BY Transaction_id".format(
                        memberId)
                elif sort == 'desc':
                    sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} ORDER BY Transaction_id DESC".format(
                        memberId)
            elif sortBy == 'result':
                if sort == 'asc':
                    sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} ORDER BY Result".format(
                        memberId)
                elif sort == 'desc':
                    sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} ORDER BY Result DESC".format(
                        memberId)
            elif sortBy == 'time':
                if sort == 'asc':
                    sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} ORDER BY Timestamp".format(
                        memberId)
                elif sort == 'desc':
                    sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} ORDER BY Timestamp DESC".format(
                        memberId)
        else:
            if sortBy == 'id':
                if sort == 'asc':
                    sql = "SELECT *  FROM Predict_Data WHERE Member_code = {0} AND Result LIKE '%{1}%' ORDER BY Transaction_id".format(
                        memberId, cactusName)
                elif sort == 'desc':
                    sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} AND Result LIKE '%{1}%'  ORDER BY Transaction_id DESC".format(
                        memberId, cactusName)
            elif sortBy == 'result':
                if sort == 'asc':
                    sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} AND Result LIKE '%{1}%'  ORDER BY Result".format(
                        memberId, cactusName)
                elif sort == 'desc':
                    sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} AND Result LIKE '%{1}%' ORDER BY Result DESC".format(
                        memberId, cactusName)
            elif sortBy == 'time':
                if sort == 'asc':
                    sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} AND Result LIKE '%{1}%'  ORDER BY Timestamp".format(
                        memberId, cactusName)
                elif sort == 'desc':
                    sql = "SELECT * FROM Predict_Data WHERE Member_code = {0} AND Result LIKE '%{1}%' ORDER BY Timestamp DESC".format(
                        memberId, cactusName)
        mycursor.execute(sql)

        data = mycursor.fetchall()
        for i in range(len(data)):
            FT = []
            for j in range(13):
                FT.append(jsonFTData['DataFT'][j]['FtList'][
                    int(data[i]['Feature'][j]) - 1]['Name'])
            data[i]['Feature'] = FT

        predict = {"predict": data}
        return json.dumps(predict)

    @BackEndApp.route('/predict-save/note/', methods=['POST'])
    def predictSavewithNote():
        resp = request.get_json()
        code = resp['memberCode']
        ft = resp['ft']
        result = resp['result']
        note = resp['note']
        timestamp = str(int(time.time()))
        mycursor1.execute(
            "INSERT INTO Predict_Data (Member_code,Feature,Result,Timestamp,DataStatus,note) VALUES (%s,%s,%s,%s,%s,%s)",
            (code, ft, result, timestamp, '1', note))
        mydb.commit()
        return ('{"Status" : 1}')

    @BackEndApp.route('/predict-save/', methods=['POST'])
    def predictSave():
        resp = request.get_json()
        code = resp['memberCode']
        ft = resp['ft']
        result = resp['result']
        timestamp = str(int(time.time()))
        mycursor1.execute(
            "INSERT INTO Predict_Data (Member_code,Feature,Result,Timestamp,DataStatus) VALUES (%s,%s,%s,%s,%s)",
            (code, ft, result, timestamp, '1'))

        mydb.commit()

        minPreProcess = 500
        minTraining = 500

        mycursor.execute(
            "SELECT COUNT(*) FROM Predict_Data WHERE DataStatus = '1' ")
        dataStatus1 = mycursor.fetchone()

        mycursor.execute(
            "SELECT COUNT(*) FROM Predict_Data WHERE DataStatus = '2' ")
        dataStatus2 = mycursor.fetchone()

        mycursor.execute('SELECT * FROM Sys_Status WHERE Name = "preData" ')
        preDataResult = mycursor.fetchone()

        mycursor.execute('SELECT * FROM Sys_Status WHERE Name = "Training" ')
        trainingResult = mycursor.fetchone()

        predataStatus = int(preDataResult['Status'])
        trainingStatus = int(trainingResult['Status'])

        countData1 = int(dataStatus1['COUNT(*)'])
        countData2 = int(dataStatus2['COUNT(*)'])
        #if predataStatus == 0:
        #    if countData1 >= minPreProcess:
        #       mycursor1.execute(
        #            "UPDATE Sys_Status SET Status = %s WHERE Name = %s", ('1', "preData"))
        #        mydb.commit()
        #        appscript.app('Terminal').do_script(
        #            "python /Users/admin/Documents/Project/preData.py /Users/admin/Documents/Project/")

        mycursor.execute('SELECT * FROM Sys_Status WHERE Name = "preData" ')
        preDataResult1 = mycursor.fetchone()
        predataStatus1 = int(preDataResult1['Status'])

        #if trainingStatus == 0 and predataStatus1 == 0:
        #    if countData2 >= minTraining:
        #        mycursor1.execute(
        #            "UPDATE Sys_Status SET Status = %s WHERE Name = %s", ('1', "Training"))
        #       mydb.commit()
        #        appscript.app('Terminal').do_script(
        #            "python /Users/admin/Documents/Project/Training.py /Users/admin/Documents/Project/")

        return ('{"Status" : 1}')

    @BackEndApp.route('xxxxxxx', methods=['GET'])
    def predictV2():
        def partition(start, end):
            pivot = y['data'][0][start]
            low = start + 1
            high = end

            while True:
                while low <= high and y['data'][0][high] >= pivot:
                    high = high - 1

                while low <= high and y['data'][0][low] <= pivot:
                    low = low + 1

                if low <= high:
                    y['data'][0][low], y['data'][0][high] = y['data'][0][
                        high], y['data'][0][low]
                    y['columns'][low], y['columns'][high] = y['columns'][
                        high], y['columns'][low]

                else:
                    break

            y['data'][0][start], y['data'][0][high] = y['data'][0][high], y[
                'data'][0][start]
            y['columns'][start], y['columns'][high] = y['columns'][high], y[
                'columns'][start]

            return (high)

        def quick_sort(start, end):
            if start >= end:
                return

            p = partition(start, end)
            quick_sort(start, p - 1)
            quick_sort(p + 1, end)

        resp = str(request.args.get("ft"))
        ft = '[['
        for i in range(13):
            if i < 12:
                ft += str(float(resp[i])) + ','
            if i == 12:
                ft += str(float(resp[i])) + ']]}'

        result = model.predict(
            '{"columns": ["f1", "f2", "f3", "f4","f5","f6","f7", "f8", "f9", "f10","f11","f12","f13"], "data": '
            + ft)
        JsonData = str(result).replace('probability(', '')
        JsonData = JsonData.replace(')', '')
        y = json.loads(JsonData)
        for i in range(26):
            y['data'][0][i] = round(float(y['data'][0][i]) * 100, 2)

        quick_sort(0, 25)
        return json.dumps(y)

    @BackEndApp.route("/get-image/", methods=['GET'])
    def Get_Image():
        try:
            image_name = str(request.args.get("name"))
            return send_from_directory('IMG/',
                                       filename=image_name,
                                       as_attachment=True,
                                       mimetype='image/jpeg',
                                       attachment_filename="nom_image")
        except FileNotFoundError:
            pass

    @BackEndApp.route("/get-dataset/", methods=['GET'])
    def Get_Dataset():
        filename = "train_new_dataset_cactus.xlsx"
        try:
            return send_from_directory('dataset/',
                                       filename=filename,
                                       as_attachment=True)
        except FileNotFoundError:
            print("File not found")

    @BackEndApp.route('/register', methods=['POST'])
    def register():
        resp = request.get_json()
        email = str(resp["email"])
        password = str(resp["password"])
        name = str(resp["name"])
        surname = str(resp["surname"])
        uid = str(uuid.uuid4())
        role = 'M'
        sql = "INSERT INTO User_Data (Email,Password,Name,Surname,Role,Uid,isLogin,EmV) VALUES (%s, %s, %s, %s, %s,%s,%s,%s)"
        val = (email, password, name, surname, role, uid, '0', '0')
        mycursor1.execute(sql, val)
        mydb.commit()
        sendEmail_EmV(email, name, surname, uid)
        return ("ok")

    @BackEndApp.route('/edit/name', methods=['POST'])
    def editName():
        resp = request.get_json()
        nameOld = resp['nameOld']
        nameNew = resp['nameNew']
        userId = resp['userId']
        mycursor1.execute("UPDATE User_Data SET Name = %s WHERE Code = %s",
                          (nameNew, userId))
        mydb.commit()
        Time = int(time.time())
        Desciption = "Old Name is " + nameOld + ",New Name is " + nameNew
        mycursor1.execute(
            "INSERT INTO Log (JobName,Description,Time,User_id,IP) VALUES (%s,%s,%s,%s,%s)",
            ("UPDATE SET Name", Desciption, Time, userId, request.remote_addr))
        mydb.commit()
        return ('{"status" : 1}')

    @BackEndApp.route('/edit/surname', methods=['POST'])
    def editSurnName():
        resp = request.get_json()
        surnameOld = resp['surnameOld']
        surnameNew = resp['surnameNew']
        userId = resp['userId']
        mycursor1.execute("UPDATE User_Data SET Surname = %s WHERE Code = %s",
                          (surnameNew, userId))
        mydb.commit()
        Time = int(time.time())
        Desciption = "Old Surname is " + surnameOld + ",New Surname is " + surnameNew
        mycursor1.execute(
            "INSERT INTO Log (JobName,Description,Time,User_id,IP) VALUES (%s, %s, %s, %s,%s)",
            ("UPDATE Surname", Desciption, Time, userId, request.remote_addr))
        mydb.commit()
        return ('{"status" : 1}')

    gmail_user = '******'
    gmail_password = '******'

    def sendEmail_EmV(email, Name, Surname, Uid):
        try:
            server = smtplib.SMTP('smtp.gmail.com', 587)
            server.starttls()
            server.login(gmail_user, gmail_password)
            send_from = gmail_user
            to = str(email)
            subject = 'ระบบยืนยันอีเมล์จากเว็ปไซต์การสืบค้นอนุกรมวิธานแคคตัส'
            url = "http://*****:*****@BackEndApp.route('/forgot/', methods=['GET'])
    def forgot():
        email = str(request.args.get("email"))
        mycursor.execute("SELECT * FROM User_Data WHERE Email = '" + email +
                         "'")
        check = mycursor.fetchone()
        if check:
            statusSend = sendEmail_CPass(email, check['Name'],
                                         check['Surname'], check['Uid'])
            if statusSend == 1:
                status = {"status": 1}
            elif statusSend == 0:
                status = {"status": 2}
        else:
            status = {"status": 0}
        jsonData = json.dumps(status)
        return (jsonData)

    def sendEmail_CPass(email, Name, Surname, Uid):
        try:
            server = smtplib.SMTP('smtp.gmail.com', 587)
            server.starttls()
            server.login(gmail_user, gmail_password)
            send_from = gmail_user
            to = str(email)
            subject = 'ระบบยืนยันอีเมล์จากเว็ปไซต์การสืบค้นอนุกรมวิธานแคคตัส'
            url = "http://*****:*****@BackEndApp.route('/forgot/password/', methods=['GET'])
    def forgotPassword():
        email = str(request.args.get("email"))
        uid = str(request.args.get("uid"))
        mycursor.execute(
            "SELECT * FROM User_Data WHERE Email = %s AND Uid = %s",
            (email, uid))
        check = mycursor.fetchone()
        if check:
            return render_template('CPass.html', text=email)
        else:
            return render_template('notFound.html')

    @BackEndApp.route('/changePasswordE', methods=['POST'])
    def changePasswordE():
        resp = request.get_json()
        email = resp['email']
        password = resp['password']
        mycursor1.execute(
            "UPDATE User_Data SET Password = %s WHERE Email = %s",
            (password, email))
        mydb.commit()
        uid = str(uuid.uuid4())
        mycursor1.execute("UPDATE User_Data SET Uid = %s WHERE Email = %s",
                          (uid, email))
        mydb.commit()
        return ('{"status" : 1}')

    @BackEndApp.route('/changePassword', methods=['POST'])
    def changePassword():
        resp = request.get_json()
        oldPassword = resp['oldPassword']
        newPassword = resp['newPassword']
        userID = int(resp['userId'])
        mycursor.execute("SELECT * FROM User_Data WHERE Code = " + str(userID))
        check = mycursor.fetchall()
        if check:
            if check[0]['Password'] == oldPassword:
                mycursor1.execute(
                    "UPDATE User_Data SET Password = %s WHERE Code = %s",
                    (newPassword, userID))
                mydb.commit()
                status = {"status": 1}
            else:
                status = {"status": 0}
        else:
            status = {"status": 0}
        jsonData = json.dumps(status)

        return (jsonData)

    @BackEndApp.route('/email-verifier/', methods=['GET'])
    def email_verifier():
        email = str(request.args.get("email"))
        uid = str(request.args.get("uid"))
        mycursor.execute(
            'SELECT * FROM User_Data WHERE Email = %s AND Uid = %s',
            (email, uid))
        check = mycursor.fetchone()

        if check:
            text = "ยืนยันอีเมล์สำเร็จ ยินดีต้อนรับคุณ " + \
                check["Name"]+" เข้าเป็นสมาชิก"
            mycursor1.execute("UPDATE User_Data SET Emv = %s WHERE Code = %s",
                              ('1', check["Code"]))
            mydb.commit()
            uid = str(uuid.uuid4())
            mycursor1.execute("UPDATE User_Data SET Uid = %s WHERE Code = %s",
                              (uid, check["Code"]))
            mydb.commit()
        else:
            text = "ยืนยันอีเมลล์ไม่สำเร็จกรุณาสมัครสมาชิกหรือตรวจสอบอีเมลล์อีกครั้ง"
        return render_template('Emv.html', text=text)

    @BackEndApp.route('/checkEmail/', methods=['GET'])
    def checkEmail():
        email = str(request.args.get("email"))
        mycursor.execute("SELECT * FROM User_Data WHERE Email = '" + email +
                         "'")
        check = mycursor.fetchall()
        if check:
            status = {"status": 1}
        else:
            status = {"status": 0}
        jsonData = json.dumps(status)
        return (jsonData)

    @BackEndApp.route('/login', methods=['POST'])
    def login():
        resp = request.get_json()
        email = str(resp["email"])
        password = str(resp["password"])
        mycursor.execute(
            'SELECT * FROM User_Data WHERE Email = %s AND Password = %s',
            (email, password))
        account = mycursor.fetchone()

        if account:
            code = str(account["Code"])
            if int(account["EmV"]) == 0:
                status = 1
            elif int(account["EmV"]) == 1:
                status = 2
            elif int(account["EmV"]) == 2:
                status = 3

            mycursor1.execute(
                "UPDATE User_Data SET isLogin = %s WHERE Code = %s",
                (str(int(time.time())), str(code)))
            mydb.commit()

            user = {
                "Code": code,
                "Email": account["Email"],
                "Name": account["Name"],
                "Surname": account["Surname"],
                "Role": account["Role"],
                "Uid": account["Uid"],
                "Status": status
            }
            Data = json.dumps(user)

        else:
            Data = '{"Code" : -1,"Email" : "null","Name" : "null","Surname" : "null","Role" : "null","Uid": "null","Status" : 0}'

        return (Data)

    @BackEndApp.route('/ban/account/', methods=['GET'])
    def banAccount():
        mycursor1.execute("UPDATE User_Data SET EmV = %s WHERE Code = %s",
                          ('2', str(request.args.get("id"))))
        mydb.commit()
        return ('{"status" : 1}')

    @BackEndApp.route('/unban/account/', methods=['GET'])
    def UnbanAccount():
        mycursor1.execute("UPDATE User_Data SET EmV = %s WHERE Code = %s",
                          ('1', str(request.args.get("id"))))
        mydb.commit()
        return ('{"status" : 1}')

    return BackEndApp
Beispiel #11
0
from pypmml import Model

_model = Model.fromFile('./resources/model.pmml')


def predict(X: object):
    return _model.predict(X)
cors = CORS(BackEndApp, resources={r"/*": {"origins": "*"}})

mydb = mysql.connector.connect(
        host="127.0.0.1",
        user="******",
        passwd="",
        database="cactus",
        raise_on_warnings=True)

mycursor = mydb.cursor(dictionary=True)
mycursor1 = mydb.cursor()
    
mycursor.execute("SELECT * FROM Model_Data WHERE Status = '1'")
Model2 = mycursor.fetchone()

model = Model.fromFile("ModelGBT/" + Model2['Name'])
Accuracy = float(Model2['Accuracy'])
print("LoadModel...({0})".format(Model2['Name']))
    
with open('dataFT.json', encoding="utf8") as json_file:
    jsonFTData = json.load(json_file)

print("====Server Start====")
        

@BackEndApp.route('/')
def hello():
    return '{"result" : "Hello","No" : 1}'
    
@BackEndApp.route('/Model/Accuracy/')
def ModelAccuracy():