Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 30,908 Bytes
3d845fb f5aa6c7 9b80a97 13fb76e 7681953 13fb76e 7681953 f5aa6c7 3d845fb 49a1dd4 58df7f1 be82820 58df7f1 7681953 58df7f1 f5aa6c7 13fb76e 9b80a97 4fdac74 9b80a97 4fdac74 9b80a97 4fdac74 9b80a97 4fdac74 9b80a97 7681953 13fb76e 9b80a97 4fdac74 9b80a97 4fdac74 58df7f1 13fb76e 9b80a97 13fb76e 9b80a97 3d845fb 9b80a97 58df7f1 13fb76e 7681953 13fb76e dd11b8e 7681953 dd11b8e 58df7f1 7681953 bae49a6 7681953 bae49a6 58df7f1 13fb76e 58df7f1 13fb76e 58df7f1 3d845fb 58df7f1 3d845fb 4fdac74 58df7f1 3d845fb 7681953 3d845fb 58df7f1 3d845fb 58df7f1 3d845fb 58df7f1 3d845fb 58df7f1 3d845fb 58df7f1 f5aa6c7 3d845fb e3e1dc8 3d845fb 58df7f1 7681953 3d845fb 9b80a97 3d845fb 4fdac74 58df7f1 3d845fb 58df7f1 7681953 3d845fb 58df7f1 3d845fb 58df7f1 3d845fb f5aa6c7 4fdac74 3d845fb 58df7f1 bae49a6 7681953 3d845fb 9b80a97 58df7f1 f5aa6c7 4fdac74 f5aa6c7 4fdac74 f5aa6c7 58df7f1 f5aa6c7 7681953 f5aa6c7 3d845fb 58df7f1 4fdac74 3d845fb f5aa6c7 58df7f1 3d845fb 7681953 3d845fb f5aa6c7 58df7f1 f5aa6c7 58df7f1 f5aa6c7 7681953 58df7f1 f5aa6c7 3d845fb f5aa6c7 4fdac74 f5aa6c7 3d845fb f5aa6c7 3d845fb 9b80a97 f5aa6c7 58df7f1 4fdac74 3d845fb 9b80a97 4fdac74 3d845fb 58df7f1 4fdac74 58df7f1 7681953 58df7f1 4fdac74 7681953 4fdac74 7681953 58df7f1 7681953 58df7f1 7681953 58df7f1 9b80a97 4fdac74 9b80a97 4fdac74 58df7f1 7681953 58df7f1 9b80a97 58df7f1 be82820 58df7f1 bae49a6 9b80a97 4fdac74 7681953 e3e1dc8 9b80a97 4fdac74 58df7f1 7681953 58df7f1 7681953 58df7f1 4fdac74 58df7f1 7681953 bae49a6 7681953 bae49a6 7681953 bae49a6 7681953 dd11b8e 58df7f1 7681953 58df7f1 3d845fb 49a1dd4 4fdac74 3d845fb f5aa6c7 3d845fb 13fb76e bae49a6 7681953 58df7f1 7681953 58df7f1 13fb76e 58df7f1 7681953 58df7f1 7681953 dd11b8e 7681953 dd11b8e 58df7f1 d6fc88d 13fb76e 9b80a97 13fb76e e3e1dc8 58df7f1 13fb76e 7681953 e0195cb bae49a6 13fb76e 58df7f1 c1c1978 58df7f1 13fb76e 7681953 bae49a6 13fb76e 58df7f1 49a1dd4 9b80a97 58df7f1 49a1dd4 58df7f1 7681953 58df7f1 4fdac74 58df7f1 7681953 bae49a6 7681953 13fb76e bae49a6 13fb76e bae49a6 e3e1dc8 dd11b8e 7681953 dd11b8e 7681953 58df7f1 7681953 58df7f1 7681953 58df7f1 7681953 bae49a6 dd11b8e 58df7f1 bae49a6 58df7f1 bae49a6 58df7f1 bae49a6 58df7f1 7681953 bae49a6 7681953 58df7f1 13fb76e 58df7f1 7681953 58df7f1 13fb76e 7681953 13fb76e 58df7f1 7681953 58df7f1 3d845fb 58df7f1 dd11b8e 58df7f1 7681953 dd11b8e 7681953 dd11b8e 7681953 13fb76e dd11b8e 7681953 13fb76e 7681953 58df7f1 7681953 13fb76e 58df7f1 dd11b8e 13fb76e 7681953 dd11b8e 7681953 dd11b8e 7681953 dd11b8e 7681953 13fb76e 7681953 f5aa6c7 58df7f1 7681953 58df7f1 13fb76e 58df7f1 13fb76e 7681953 13fb76e 7681953 13fb76e 58df7f1 bae49a6 58df7f1 3d845fb 7681953 bae49a6 7681953 bae49a6 7681953 dd11b8e 3d845fb 49a1dd4 3d845fb bd4e798 bae49a6 9b80a97 58df7f1 7681953 58df7f1 3d845fb 13fb76e 58df7f1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 |
import subprocess
import time
from typing import Dict, List, Tuple
import gradio as gr # pylint: disable=import-error
import numpy as np
import pandas as pd
import requests
from symptoms_categories import SYMPTOMS_LIST
from utils import (
CLIENT_DIR,
CURRENT_DIR,
DEPLOYMENT_DIR,
INPUT_BROWSER_LIMIT,
KEYS_DIR,
SERVER_URL,
TARGET_COLUMNS,
TRAINING_FILENAME,
clean_directory,
get_disease_name,
load_data,
pretty_print,
)
from concrete.ml.deployment import FHEModelClient
subprocess.Popen(["uvicorn", "server:app"], cwd=CURRENT_DIR)
time.sleep(3)
# pylint: disable=c-extension-no-member,invalid-name
def is_none(obj) -> bool:
"""
Check if the object is None.
Args:
obj (any): The input to be checked.
Returns:
bool: True if the object is None or empty, False otherwise.
"""
return obj is None or (obj is not None and len(obj) < 1)
def display_default_symptoms_fn(default_disease: str) -> Dict:
"""
Displays the symptoms of a given existing disease.
Args:
default_disease (str): Disease
Returns:
Dict: The according symptoms
"""
df = pd.read_csv(TRAINING_FILENAME)
df_filtred = df[df[TARGET_COLUMNS[1]] == default_disease]
return {
default_symptoms: gr.update(
visible=True,
value=pretty_print(
df_filtred.columns[df_filtred.eq(1).any()].to_list(), delimiter=", "
),
)
}
def get_user_symptoms_from_checkboxgroup(checkbox_symptoms: List) -> np.array:
"""
Convert the user symptoms into a binary vector representation.
Args:
checkbox_symptoms (List): A list of user symptoms.
Returns:
np.array: A binary vector representing the user's symptoms.
Raises:
KeyError: If a provided symptom is not recognized as a valid symptom.
"""
symptoms_vector = {key: 0 for key in valid_symptoms}
for pretty_symptom in checkbox_symptoms:
original_symptom = "_".join((pretty_symptom.lower().split(" ")))
if original_symptom not in symptoms_vector.keys():
raise KeyError(
f"The symptom '{original_symptom}' you provided is not recognized as a valid "
f"symptom.\nHere is the list of valid symptoms: {symptoms_vector}"
)
symptoms_vector[original_symptom] = 1
user_symptoms_vect = np.fromiter(symptoms_vector.values(), dtype=float)[np.newaxis, :]
assert all(value == 0 or value == 1 for value in user_symptoms_vect.flatten())
return user_symptoms_vect
def get_features_fn(*checked_symptoms: Tuple[str]) -> Dict:
"""
Get vector features based on the selected symptoms.
Args:
checked_symptoms (Tuple[str]): User symptoms
Returns:
Dict: The encoded user vector symptoms.
"""
if not any(lst for lst in checked_symptoms if lst):
return {
error_box1: gr.update(visible=True, value="β οΈ Please provide your chief complaints."),
}
if len(pretty_print(checked_symptoms)) < 5:
print("Provide at least 5 symptoms.")
return {
error_box1: gr.update(visible=True, value="β οΈ Provide at least 5 symptoms"),
user_vect_box1: None,
}
return {
error_box1: gr.update(visible=False),
user_vect_box1: gr.update(
visible=False,
value=get_user_symptoms_from_checkboxgroup(pretty_print(checked_symptoms)),
),
submit_button: gr.update(value="Data Submitted β
"),
}
def key_gen_fn(user_symptoms: List[str]) -> Dict:
"""
Generate keys for a given user.
Args:
user_symptoms (List[str]): The vector symptoms provided by the user.
Returns:
dict: A dictionary containing the generated keys and related information.
"""
clean_directory()
if is_none(user_symptoms):
print("Error: Please submit your symptoms or select a default disease.")
return {
error_box2: gr.update(visible=True, value="β οΈ Please submit your symptoms first."),
}
# Generate a random user ID
user_id = np.random.randint(0, 2**32)
print(f"Your user ID is: {user_id}....")
client = FHEModelClient(path_dir=DEPLOYMENT_DIR, key_dir=KEYS_DIR / f"{user_id}")
client.load()
# Creates the private and evaluation keys on the client side
client.generate_private_and_evaluation_keys()
# Get the serialized evaluation keys
serialized_evaluation_keys = client.get_serialized_evaluation_keys()
assert isinstance(serialized_evaluation_keys, bytes)
# Save the evaluation key
evaluation_key_path = KEYS_DIR / f"{user_id}/evaluation_key"
with evaluation_key_path.open("wb") as f:
f.write(serialized_evaluation_keys)
serialized_evaluation_keys_shorten_hex = serialized_evaluation_keys.hex()[:INPUT_BROWSER_LIMIT]
return {
error_box2: gr.update(visible=False),
key_box: gr.update(visible=False, value=serialized_evaluation_keys_shorten_hex),
user_id_box: gr.update(visible=True, value=user_id),
key_len_box: gr.update(
visible=False, value=f"{len(serialized_evaluation_keys) / (10**6):.2f} MB"
),
}
def encrypt_fn(user_symptoms: np.ndarray, user_id: str) -> None:
"""
Encrypt the user symptoms vector in the `Client Side`.
Args:
user_symptoms (List[str]): The vector symptoms provided by the user
user_id (user): The current user's ID
"""
if is_none(user_id) or is_none(user_symptoms):
print("Error in encryption step: Provide your symptoms and generate the evaluation keys.")
return {
error_box3: gr.update(
visible=True,
value="β οΈ Please ensure that your symptoms have been submitted and "
"that you have generated the evaluation key.",
)
}
# Retrieve the client API
client = FHEModelClient(path_dir=DEPLOYMENT_DIR, key_dir=KEYS_DIR / f"{user_id}")
client.load()
user_symptoms = np.fromstring(user_symptoms[2:-2], dtype=int, sep=".").reshape(1, -1)
quant_user_symptoms = client.model.quantize_input(user_symptoms)
encrypted_quantized_user_symptoms = client.quantize_encrypt_serialize(user_symptoms)
assert isinstance(encrypted_quantized_user_symptoms, bytes)
encrypted_input_path = KEYS_DIR / f"{user_id}/encrypted_input"
with encrypted_input_path.open("wb") as f:
f.write(encrypted_quantized_user_symptoms)
encrypted_quantized_user_symptoms_shorten_hex = encrypted_quantized_user_symptoms.hex()[
:INPUT_BROWSER_LIMIT
]
return {
error_box3: gr.update(visible=False),
user_vect_box2: gr.update(visible=True, value=user_symptoms),
enc_vect_box: gr.update(visible=True, value=encrypted_quantized_user_symptoms_shorten_hex),
}
def send_input_fn(user_id: str, user_symptoms: np.ndarray) -> Dict:
"""Send the encrypted data and the evaluation key to the server.
Args:
user_id (str): The current user's ID
user_symptoms (np.ndarray): The user symptoms
"""
if is_none(user_id) or is_none(user_symptoms):
return {
error_box4: gr.update(
visible=True,
value="β οΈ Please check your connectivity \n"
"β οΈ Ensure that the symptoms have been submitted and the evaluation "
"key has been generated before sending the data to the server.",
)
}
evaluation_key_path = KEYS_DIR / f"{user_id}/evaluation_key"
encrypted_input_path = KEYS_DIR / f"{user_id}/encrypted_input"
if not evaluation_key_path.is_file():
print(
"Error Encountered While Sending Data to the Server: "
f"The key has been generated correctly - {evaluation_key_path.is_file()=}"
)
return {
error_box4: gr.update(visible=True, value="β οΈ Please generate the private key first.")
}
if not encrypted_input_path.is_file():
print(
"Error Encountered While Sending Data to the Server: The data has not been encrypted "
f"correctly on the client side - {encrypted_input_path.is_file()=}"
)
return {
error_box4: gr.update(
visible=True,
value="β οΈ Please encrypt the data with the private key first.",
),
}
# Define the data and files to post
data = {
"user_id": user_id,
"input": user_symptoms,
}
files = [
("files", open(encrypted_input_path, "rb")),
("files", open(evaluation_key_path, "rb")),
]
# Send the encrypted input and evaluation key to the server
url = SERVER_URL + "send_input"
with requests.post(
url=url,
data=data,
files=files,
) as response:
print(f"Sending Data: {response.ok=}")
return {
error_box4: gr.update(visible=False),
srv_resp_send_data_box: "Data sent",
}
def run_fhe_fn(user_id: str) -> Dict:
"""Send the encrypted input and the evaluation key to the server.
Args:
user_id (int): The current user's ID.
"""
if is_none(user_id):
return {
error_box5: gr.update(
visible=True,
value="β οΈ Please check your connectivity \n"
"β οΈ Ensure that the symptoms have been submitted, the evaluation "
"key has been generated and the server received the data "
"before processing the data.",
),
fhe_execution_time_box: None,
}
data = {
"user_id": user_id,
}
url = SERVER_URL + "run_fhe"
with requests.post(
url=url,
data=data,
) as response:
if not response.ok:
return {
error_box5: gr.update(
visible=True,
value=(
"β οΈ An error occurred on the Server Side. "
"Please check connectivity and data transmission."
),
),
fhe_execution_time_box: gr.update(visible=False),
}
else:
time.sleep(1)
print(f"response.ok: {response.ok}, {response.json()} - Computed")
return {
error_box5: gr.update(visible=False),
fhe_execution_time_box: gr.update(visible=True, value=f"{response.json():.2f} seconds"),
}
def get_output_fn(user_id: str, user_symptoms: np.ndarray) -> Dict:
"""Retreive the encrypted data from the server.
Args:
user_id (str): The current user's ID
user_symptoms (np.ndarray): The user symptoms
"""
if is_none(user_id) or is_none(user_symptoms):
return {
error_box6: gr.update(
visible=True,
value="β οΈ Please check your connectivity \n"
"β οΈ Ensure that the server has successfully processed and transmitted the data to the client.",
)
}
data = {
"user_id": user_id,
}
# Retrieve the encrypted output
url = SERVER_URL + "get_output"
with requests.post(
url=url,
data=data,
) as response:
if response.ok:
print(f"Receive Data: {response.ok=}")
encrypted_output = response.content
# Save the encrypted output to bytes in a file as it is too large to pass through
# regular Gradio buttons (see https://github.com/gradio-app/gradio/issues/1877)
encrypted_output_path = CLIENT_DIR / f"{user_id}_encrypted_output"
with encrypted_output_path.open("wb") as f:
f.write(encrypted_output)
return {error_box6: gr.update(visible=False), srv_resp_retrieve_data_box: "Data received"}
def decrypt_fn(
user_id: str, user_symptoms: np.ndarray, *checked_symptoms, threshold: int = 0.5
) -> Dict:
"""Dencrypt the data on the `Client Side`.
Args:
user_id (str): The current user's ID
user_symptoms (np.ndarray): The user symptoms
threshold (float): Probability confidence threshold
Returns:
Decrypted output
"""
if is_none(user_id) or is_none(user_symptoms):
return {
error_box7: gr.update(
visible=True,
value="β οΈ Please check your connectivity \n"
"β οΈ Ensure that the client has successfully received the data from the server.",
)
}
# Get the encrypted output path
encrypted_output_path = CLIENT_DIR / f"{user_id}_encrypted_output"
if not encrypted_output_path.is_file():
print("Error in decryption step: Please run the FHE execution, first.")
return {
error_box7: gr.update(
visible=True,
value="β οΈ Please ensure that: \n"
"- the connectivity \n"
"- the symptoms have been submitted \n"
"- the evaluation key has been generated \n"
"- the server processed the encrypted data \n"
"- the Client received the data from the Server before decrypting the prediction",
),
decrypt_target_box: None,
}
# Load the encrypted output as bytes
with encrypted_output_path.open("rb") as f:
encrypted_output = f.read()
# Retrieve the client API
client = FHEModelClient(path_dir=DEPLOYMENT_DIR, key_dir=KEYS_DIR / f"{user_id}")
client.load()
# Deserialize, decrypt and post-process the encrypted output
output = client.deserialize_decrypt_dequantize(encrypted_output)
top3_diseases = np.argsort(output.flatten())[-3:][::-1]
top3_proba = output[0][top3_diseases]
if (
(top3_proba[0] < threshold)
or (np.sum(top3_proba) < threshold)
or (abs(top3_proba[0] - top3_proba[1]) < threshold)
):
out = "β οΈ The prediction appears uncertain; including more symptoms may improve the results.\n\n"
else:
out = ""
out = (
f"{out}"
f"Given the symptoms you provided: {pretty_print(checked_symptoms, case_conversion=str.capitalize, delimiter=', ')}\n\n"
"Here are the top3 predictions:\n\n"
f"1. Β« {get_disease_name(top3_diseases[0])} Β» with a probability of {top3_proba[0]:.2%}\n"
f"2. Β« {get_disease_name(top3_diseases[1])} Β» with a probability of {top3_proba[1]:.2%}\n"
f"3. Β« {get_disease_name(top3_diseases[2])} Β» with a probability of {top3_proba[2]:.2%}\n"
)
return {
error_box7: gr.update(visible=False),
decrypt_target_box: out,
}
def reset_fn():
"""Reset the space and clear all the box outputs."""
clean_directory()
return {
user_vect_box2: None,
submit_button: gr.update(value="Confirm Symptoms"),
user_id_box: gr.update(visible=False, value=None, interactive=False),
user_vect_box1: None,
recap_symptoms_box: gr.update(visible=False, value=None),
default_symptoms: gr.update(visible=True, value=None),
disease_box: gr.update(visible=True, value=None),
quant_vect_box: gr.update(visible=False, value=None, interactive=False),
enc_vect_box: gr.update(visible=True, value=None, interactive=False),
key_box: gr.update(visible=True, value=None, interactive=False),
key_len_box: gr.update(visible=False, value=None, interactive=False),
fhe_execution_time_box: gr.update(visible=True, value=None, interactive=False),
decrypt_target_box: None,
error_box7: gr.update(visible=False),
error_box1: gr.update(visible=False),
error_box2: gr.update(visible=False),
error_box3: gr.update(visible=False),
error_box4: gr.update(visible=False),
error_box5: gr.update(visible=False),
error_box6: gr.update(visible=False),
srv_resp_send_data_box: None,
srv_resp_retrieve_data_box: None,
**{box: None for box in check_boxes},
}
CSS = """
#them {color: grey}
#them {font-size: 24px}
#them {font-weight: bold}
.gradio-container {background-color: white}
.gradio-button {color: red; font-size: 20px;}
/* .feedback {font-size: 3px !important} */
#svelte-s1r2yt {color: grey}
#svelte-s1r2yt {font-size: 25px}
#svelte-s1r2yt {font-weight: bold}
/* #them {text-align: center} */
"""
if __name__ == "__main__":
print("Starting demo ...")
clean_directory()
(X_train, X_test), (y_train, y_test), valid_symptoms, diseases = load_data()
with gr.Blocks(css="them") as demo:
# Link + images
gr.Markdown(
"""
<p align="center">
<img width=200 src="https://user-images.githubusercontent.com/5758427/197816413-d9cddad3-ba38-4793-847d-120975e1da11.png">
</p>
<h2 align="center">Health Prediction On Encrypted Data Using Fully Homomorphic Encryption.</h2>
<p align="center">
<a href="https://github.com/zama-ai/concrete-ml"> <img style="vertical-align: middle; display:inline-block; margin-right: 3px;" width=15 src="https://user-images.githubusercontent.com/5758427/197972109-faaaff3e-10e2-4ab6-80f5-7531f7cfb08f.png">Concrete-ML</a>
β
<a href="https://docs.zama.ai/concrete-ml"> <img style="vertical-align: middle; display:inline-block; margin-right: 3px;" width=15 src="https://user-images.githubusercontent.com/5758427/197976802-fddd34c5-f59a-48d0-9bff-7ad1b00cb1fb.png">Documentation</a>
β
<a href="https://zama.ai/community"> <img style="vertical-align: middle; display:inline-block; margin-right: 3px;" width=15 src="https://user-images.githubusercontent.com/5758427/197977153-8c9c01a7-451a-4993-8e10-5a6ed5343d02.png">Community</a>
β
<a href="https://twitter.com/zama_fhe"> <img style="vertical-align: middle; display:inline-block; margin-right: 3px;" width=15 src="https://user-images.githubusercontent.com/5758427/197975044-bab9d199-e120-433b-b3be-abd73b211a54.png">@zama_fhe</a>
</p>
<p align="center">
<img width="90%" height="30%" src="https://raw.githubusercontent.com/kcelia/Img/main/hf_space_3_health_prediction_cover_image.png">
</p>
"""
)
gr.Markdown("# Introduction")
gr.Markdown(
"Welcome to our Healthcare Prediction space using Fully Homomorphic Encryption (FHE) with Concrete ML library."
)
gr.Markdown(
"Through this user-friendly and secure client-server interface you can confidently submit your chief complaints, which you have locally "
"encrypted on your end and transmitted to an untrusted server for processing.\n\n"
"Thanks to the FHE scheme, the remote server is able to perform computations without ever decrypting the data and return result the encrypted to the client for local decryption. \n\n\n"
"FHE ensures end-to-end data encryption and guarantees patient privacy."
)
gr.Markdown(
"Disclaimer: We are not medical professionals. For accurate diagnosis and treatment, "
"please consult a qualified healthcare provider."
)
with gr.Tabs(eelem_id="them") as tabs:
with gr.TabItem("1. Chief Complaints", id=0):
gr.Markdown("<span style='color:grey'>Client Side</span>")
gr.Markdown(
"## Provide at least 5 chief complaints by filling in the boxes below. "
)
# Box symptoms
check_boxes = []
for i, category in enumerate(SYMPTOMS_LIST):
with gr.Accordion(
pretty_print(category.keys()),
open=False,
elem_classes="feedback",
) as accordion:
check_box = gr.CheckboxGroup(
pretty_print(category.values()),
show_label=False,
)
check_boxes.append(check_box)
error_box1 = gr.Textbox(label="Error β", visible=False)
# <!> This part has been paused due to UI issues.
# Default disease, picked from the dataframe
gr.Markdown(
"## You can choose an **existing disease** and explore its associated symptoms."
)
with gr.Row():
with gr.Column(scale=2):
disease_box = gr.Dropdown(sorted(diseases), label="Diseases π")
with gr.Column(scale=5):
default_symptoms = gr.Textbox(
label="Related Symptoms:", visible=True, interactive=False
)
disease_box.change(
fn=display_default_symptoms_fn, inputs=[disease_box], outputs=[default_symptoms]
)
gr.Markdown(
"#### Submit your chief complaints by clicking on **Confirm Symptoms π** then go to the **Next Step π**"
)
user_vect_box1 = gr.Textbox(
visible=False,
)
with gr.Row():
with gr.Column():
# Submit botton
submit_button = gr.Button("Confirm Symptoms π")
with gr.Column():
next_tab = gr.Button("Next Step π")
next_tab.click(lambda _: gr.Tabs.update(selected=1), None, tabs)
# Clear botton
clear_button = gr.Button("Reset Space π")
with gr.TabItem("2. Data Encryption", id=1):
gr.Markdown("<span style='color:grey'>Client Side</span>")
gr.Markdown("## Key Generation")
gr.Markdown(
"In FHE schemes, a secret (enc/dec)ryption keys are generated for encrypting and decrypting data owned by the client. \n\n"
"Additionally, a public evaluation key is generated, enabling external entities to perform homomorphic operations on encrypted data, without the need to decrypt them. \n\n"
"The evaluation key will be transmitted to the server for further processing."
)
gen_key_btn = gr.Button("Generate the evaluation key π")
error_box2 = gr.Textbox(label="Error β", visible=False)
user_id_box = gr.Textbox(label="User ID:", interactive=False, visible=True)
key_len_box = gr.Textbox(
label="Evaluation Key Size:", interactive=False, visible=False
)
key_box = gr.Textbox(
label="Evaluation key (truncated):",
max_lines=3,
interactive=False,
visible=False,
)
gr.Markdown("## Encrypt the data")
encrypt_btn = gr.Button("Encrypt the data using the π private secret key π")
error_box3 = gr.Textbox(label="Error β", visible=False)
quant_vect_box = gr.Textbox(
label="Quantized Vector:",
interactive=False,
visible=False,
)
with gr.Row():
with gr.Column():
user_vect_box2 = gr.Textbox(
label="User Symptoms Vector:", interactive=False, max_lines=10
)
with gr.Column():
enc_vect_box = gr.Textbox(
label="Encrypted Vector:",
max_lines=10,
interactive=False,
)
encrypt_btn.click(
encrypt_fn,
inputs=[user_vect_box1, user_id_box],
outputs=[
user_vect_box2,
enc_vect_box,
error_box3,
],
)
gr.Markdown(
"## Send the encrypted data to the "
"<span style='color:grey'>Server Side</span>"
)
error_box4 = gr.Textbox(label="Error β", visible=False)
with gr.Row().style(equal_height=False):
with gr.Column(scale=4):
send_input_btn = gr.Button("Send the encrypted data π")
with gr.Column(scale=1):
srv_resp_send_data_box = gr.Checkbox(
label="Data Sent", show_label=False, interactive=False
)
send_input_btn.click(
send_input_fn,
inputs=[user_id_box, user_vect_box1],
outputs=[error_box4, srv_resp_send_data_box],
)
gr.Markdown("\n\n")
with gr.Row().style(equal_height=True):
with gr.Column(scale=1):
prev_tab = gr.Button("π Previous Step")
prev_tab.click(lambda _: gr.Tabs.update(selected=0), None, tabs)
with gr.Column(scale=1):
next_tab = gr.Button("Next Step π")
next_tab.click(lambda _: gr.Tabs.update(selected=2), None, tabs)
with gr.TabItem("3. FHE execution", id=2):
gr.Markdown("<span style='color:grey'>Server Side</span>")
gr.Markdown("## Run the FHE evaluation")
gr.Markdown(
"Once the server receives the encrypted data, it can process and compute the output without ever decrypting the data just as it would on clear data.\n\n"
"This server employs a logistic regression model that has been trained on this [data-set](https://github.com/anujdutt9/Disease-Prediction-from-Symptoms/tree/master/dataset)."
)
run_fhe_btn = gr.Button("Run the FHE evaluation π")
error_box5 = gr.Textbox(label="Error β", visible=False)
fhe_execution_time_box = gr.Textbox(
label="Total FHE Execution Time:", interactive=False, visible=True
)
run_fhe_btn.click(
run_fhe_fn,
inputs=[user_id_box],
outputs=[fhe_execution_time_box, error_box5],
)
gr.Markdown("\n\n")
with gr.Row().style(equal_height=True):
with gr.Column(scale=1):
prev_tab = gr.Button("π Previous Step")
prev_tab.click(lambda _: gr.Tabs.update(selected=1), None, tabs)
with gr.Column(scale=1):
next_tab = gr.Button("Next Step π ")
next_tab.click(lambda _: gr.Tabs.update(selected=3), None, tabs)
with gr.TabItem("4. Data Decryption", id=3):
gr.Markdown("<span style='color:grey'>Client Side</span>")
gr.Markdown("## Get the data from the <span style='color:grey'>Server Side</span>")
error_box6 = gr.Textbox(label="Error β", visible=False)
with gr.Row().style(equal_height=True):
with gr.Column(scale=4):
get_output_btn = gr.Button("Get data π")
with gr.Column(scale=1):
srv_resp_retrieve_data_box = gr.Checkbox(
label="Data Received", show_label=False, interactive=False
)
get_output_btn.click(
get_output_fn,
inputs=[user_id_box, user_vect_box1],
outputs=[srv_resp_retrieve_data_box, error_box6],
)
gr.Markdown("## Decrypt the output")
recap_symptoms_box = gr.Textbox(
label="Summary of chief complaints:", visible=False, max_lines=3
)
decrypt_target_btn = gr.Button(
"Decrypt the output with the π private secret decryption key π"
)
error_box7 = gr.Textbox(label="Error β", visible=False)
decrypt_target_box = gr.Textbox(label="Decrypted Output:", interactive=False)
decrypt_target_btn.click(
decrypt_fn,
inputs=[user_id_box, user_vect_box1, *check_boxes],
outputs=[decrypt_target_box, error_box7],
)
with gr.Row().style(equal_height=True):
with gr.Column(scale=1):
prev_tab = gr.Button("π Previous Step")
prev_tab.click(lambda _: gr.Tabs.update(selected=2), None, tabs)
with gr.Column(scale=1):
next_tab = gr.Button("π π Go back to start")
next_tab.click(lambda _: gr.Tabs.update(selected=0), None, tabs)
gen_key_btn.click(
key_gen_fn,
inputs=user_vect_box1,
outputs=[
key_box,
user_id_box,
key_len_box,
error_box2,
],
)
submit_button.click(
fn=get_features_fn,
inputs=[*check_boxes],
outputs=[user_vect_box1, error_box1, submit_button],
)
clear_button.click(
reset_fn,
outputs=[
user_vect_box2,
user_vect_box1,
submit_button,
# disease_box,
error_box1,
error_box2,
error_box3,
error_box4,
error_box5,
error_box6,
error_box7,
disease_box,
default_symptoms,
recap_symptoms_box,
user_id_box,
key_len_box,
key_box,
quant_vect_box,
enc_vect_box,
srv_resp_send_data_box,
srv_resp_retrieve_data_box,
fhe_execution_time_box,
decrypt_target_box,
*check_boxes,
],
)
demo.launch()
|