maheshdev209/fhehp
0
1import os2import shutil3from pathlib import Path4from typing import List, Tuple, Union5 6import numpy7import pandas8 9from concrete.ml.sklearn import XGBClassifier as ConcreteXGBoostClassifier10 11# Max Input to be displayed on the HuggingFace space brower using Gradio12# Too large inputs, slow down the server: https://github.com/gradio-app/gradio/issues/187713INPUT_BROWSER_LIMIT = 40014 15# Store the server's URL16SERVER_URL = "http://localhost:8000/"17 18CURRENT_DIR = Path(__file__).parent19DEPLOYMENT_DIR = CURRENT_DIR / "deployment_files"20KEYS_DIR = DEPLOYMENT_DIR / ".fhe_keys"21CLIENT_DIR = DEPLOYMENT_DIR / "client_dir"22SERVER_DIR = DEPLOYMENT_DIR / "server_dir"23 24ALL_DIRS = [KEYS_DIR, CLIENT_DIR, SERVER_DIR]25 26# Columns that define the target27TARGET_COLUMNS = ["prognosis_encoded", "prognosis"]28 29TRAINING_FILENAME = "./data/Training_preprocessed.csv"30TESTING_FILENAME = "./data/Testing_preprocessed.csv"31 32# pylint: disable=invalid-name33 34 35def pretty_print(36 inputs, case_conversion=str.title, which_replace: str = "_", to_what: str = " ", delimiter=None37):38 """39 Prettify and sort the input as a list of string.40 41 Args:42 inputs (Any): The inputs to be prettified.43 44 Returns:45 List: The prettified and sorted list of inputs.46 47 """48 # Flatten the list if required49 pretty_list = []50 for item in inputs:51 if isinstance(item, list):52 pretty_list.extend(item)53 else:54 pretty_list.append(item)55 56 # Sort57 pretty_list = sorted(list(set(pretty_list)))58 # Replace59 pretty_list = [item.replace(which_replace, to_what) for item in pretty_list]60 pretty_list = [case_conversion(item) for item in pretty_list]61 if delimiter:62 pretty_list = f"{delimiter.join(pretty_list)}."63 64 return pretty_list65 66 67def clean_directory() -> None:68 """69 Clear direcgtories70 """71 print("Cleaning...\n")72 for target_dir in ALL_DIRS:73 if os.path.exists(target_dir) and os.path.isdir(target_dir):74 shutil.rmtree(target_dir)75 target_dir.mkdir(exist_ok=True, parents=True)76 77 78def get_disease_name(encoded_prediction: int, file_name: str = TRAINING_FILENAME) -> str:79 """Return the disease name given its encoded label.80 81 Args:82 encoded_prediction (int): The encoded prediction83 file_name (str): The data file path84 85 Returns:86 str: The according disease name87 """88 df = pandas.read_csv(file_name, usecols=TARGET_COLUMNS).drop_duplicates()89 disease_name, _ = df[df[TARGET_COLUMNS[0]] == encoded_prediction].values.flatten()90 return disease_name91 92 93def load_data() -> Union[Tuple[pandas.DataFrame, numpy.ndarray], List]:94 """95 Return the data96 97 Args:98 None99 100 Return:101 The train, testing set and valid symptoms.102 """103 # Load data104 df_train = pandas.read_csv(TRAINING_FILENAME)105 df_test = pandas.read_csv(TESTING_FILENAME)106 107 # Separate the traget from the training / testing set:108 # TARGET_COLUMNS[0] -> "prognosis_encoded" -> contains the numeric label of the disease109 # TARGET_COLUMNS[1] -> "prognosis" -> contains the name of the disease110 111 y_train = df_train[TARGET_COLUMNS[0]]112 X_train = df_train.drop(columns=TARGET_COLUMNS, axis=1, errors="ignore")113 114 y_test = df_test[TARGET_COLUMNS[0]]115 X_test = df_test.drop(columns=TARGET_COLUMNS, axis=1, errors="ignore")116 117 return (118 (X_train, X_test),119 (y_train, y_test),120 X_train.columns.to_list(),121 df_train[TARGET_COLUMNS[1]].unique().tolist(),122 )123 124 125def load_model(X_train: pandas.DataFrame, y_train: numpy.ndarray):126 """127 Load a pre-trained serialized model128 129 Args:130 X_train (pandas.DataFrame): Training set131 y_train (numpy.ndarray): Targets of the training set132 133 Return:134 The Concrete ML model and its circuit135 """136 # Parameters137 concrete_args = {"max_depth": 1, "n_bits": 3, "n_estimators": 3, "n_jobs": -1}138 classifier = ConcreteXGBoostClassifier(**concrete_args)139 # Train the model140 classifier.fit(X_train, y_train)141 # Compile the model142 circuit = classifier.compile(X_train)143 144 return classifier, circuit145 