def create_api_model(): # try: if request.headers["CONTENT_TYPE"] == "application/json": try: data_request = request.json modelId = data_request["modelId"] if modelId == None: return "[error] modelId not found check (keys) modelId and values " else: data_test_json = data_request["data"] data_test_dataFrame = pd.DataFrame(data_test_json) r = API.get_model("Model", modelId) modelUrl = r["modelFile"]["url"] Nu_SVC_classifier = joblib.load(urlopen(modelUrl)) KQ = np.array(Nu_SVC_classifier.predict(data_test_dataFrame)) data_transform = { "0": "Thiếu cân (Insufficient weight)", "1": "Bình thường (Normal weight)", "2": "Thừa cân loại 1 (Overweight level 1)", "3": "Thừa cân loại 2 (Overweight level 2)", "4": "Béo phì loại 1 (Obesity type I)", "5": "git Béo phì loại 2 (Obesity type II)", "6": "Béo phì loại 3 (Obesity type III)", } dataReturn = { "result": [], } for rs in KQ: dataReturn["result"].append(data_transform[str(rs)]) return dataReturn except: print("[error] check key (inputColumns) and value") return ( "[error] check key (inputColumns) and value (check type inputColumns)" ) pass else: modelId = request.form.get("modelId") if modelId == None: return "[error] modelId not found check (keys) modelId and values " else: file_test = request.files.getlist("data")[0] file_name = secure_filename(file_test.filename) if file_name == "": return "[error] Can't find data, check keys 'data' and values" else: try: filename_random = str(uuid.uuid4())[:8] + "_" + file_name if os.path.exists(app.config["DATA_API_FOLDER"]): file_path_test = os.path.join( app.config["DATA_API_FOLDER"], filename_random) else: os.makedirs(app.config["DATA_API_FOLDER"]) file_path_test = os.path.join( app.config["DATA_API_FOLDER"], filename_random) file_test.save(file_path_test) df_test, columns, n, m = DATA.read("csv", file_path_test, ",") except Exception as e: print(e) return "[error] can't save data, request fail" pass try: col_feature_test_string = request.form.getlist( "inputColumns")[0] col_feature_test_list = ast.literal_eval( col_feature_test_string) col_feature_test_array = np.array(col_feature_test_list) r = API.get_model("Model", modelId) modelUrl = r["modelFile"]["url"] Nu_SVC_classifier = joblib.load(urlopen(modelUrl)) except: print("[error] request fail") notification = ( "[error] request fail check key 'modelId', model " + str(modelId) + " not found") return notification pass try: data_test = df_test.iloc[:, col_feature_test_array] KQ = np.array(Nu_SVC_classifier.predict(data_test)) data_transform = { "0": "Thiếu cân (Insufficient weight)", "1": "Bình thường (Normal weight)", "2": "Thừa cân loại 1 (Overweight level 1)", "3": "Thừa cân loại 2 (Overweight level 2)", "4": "Béo phì loại 1 (Obesity type I)", "5": "Béo phì loại 2 (Obesity type II)", "6": "Béo phì loại 3 (Obesity type III)", } dataReturn = { "result": [], } for rs in KQ: dataReturn["result"].append(data_transform[str(rs)]) os.remove(file_path_test) return dataReturn except IndexError: print("[error] check key (inputColumns) and value") return "[error] check key (inputColumns) and value check (number inputColumns)" pass except ValueError: print("[error] check key (inputColumns) and value") return "[error] check key (inputColumns) and value (check type inputColumns)" pass
def upload_file_url(): try: # Get file url = request.args.get("urlData") data_name = request.args.get("dataName") separator = request.args.get("separator") if data_name == "": data_name = "dataset_not_name" user_id = request.args.get("userId") except AttributeError: print("[error] can't find file_upload (upfile function app.py)") pass try: # Create random id random_id = str(uuid.uuid4())[:8] # Random file_name filename_upload_random = str(random_id) + "_" + "upload.csv" # get file_path if os.path.exists(app.config["UPLOAD_FOLDER"]): file_path_upload = os.path.join(app.config["UPLOAD_FOLDER"], filename_upload_random) else: os.makedirs(app.config["UPLOAD_FOLDER"]) file_path_upload = os.path.join(app.config["UPLOAD_FOLDER"], filename_upload_random) # save file # Save file locally a = urlretrieve(url, file_path_upload) # Read file into a DataFrame and print its head except UnboundLocalError: print( "[error] local variable 'filename' referenced before assignment (upfile function app.py)" ) pass except ValueError: data_return_err = {"error": "unknown url"} return data_return_err try: data, col, n, m = DATA.read("csv", file_path_upload, separator) file_name_csv = data_name + ".csv" file_path_save_csv = os.path.join(app.config["UPLOAD_FOLDER"], file_name_csv) export_csv = data.to_csv(file_path_save_csv, index=None, header=True) data_str = DATA.convert_str(file_path_save_csv) data_str = str(data_str) data_post = { "jsonData": data_str, "dataName": data_name, "userUpload": { "__type": "Pointer", "className": "_User", "objectId": user_id, }, "delimiter": separator, "uploadFrom": "url", } class_name = "Data" data = API.post(class_name, data_post) print(data) return data except UnboundLocalError: print("[error] ") return "fail, can't upload dataset"