schuldt-ogre
commited on
Commit
·
67244f5
1
Parent(s):
bac7294
initial commit for client frontend
Browse files- app.py +292 -143
- requirements.txt +0 -4
app.py
CHANGED
|
@@ -1,24 +1,11 @@
|
|
| 1 |
-
|
| 2 |
from concrete.ml.deployment import FHEModelClient
|
| 3 |
from pathlib import Path
|
| 4 |
import numpy as np
|
| 5 |
import gradio as gr
|
| 6 |
import requests
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
SERVER_URL = "http://127.0.0.1:7860/"
|
| 10 |
-
CURRENT_DIR = Path(__file__).parent
|
| 11 |
-
DEPLOYMENT_DIR = CURRENT_DIR / "deployment_files"
|
| 12 |
-
KEYS_DIR = DEPLOYMENT_DIR / ".fhe_keys"
|
| 13 |
-
CLIENT_DIR = DEPLOYMENT_DIR / "client_dir"
|
| 14 |
-
SERVER_DIR = DEPLOYMENT_DIR / "server_dir"
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
USER_ID = "user_id"
|
| 18 |
-
EXAMPLE_CLINICAL_TRIAL_LINK = "https://www.trials4us.co.uk/ongoing-clinical-trials/recruiting-healthy-adults-c23026?_gl=1*1ysp815*_up*MQ..&gclid=Cj0KCQjwr9m3BhDHARIsANut04bHqi5zE3sjS3f8JK2WRN3YEgY4bTfWbvTdZTxkUTSISxXX5ZWL7qEaAowwEALw_wcB&gbraid=0AAAAAD3Qci2k_3IERmM6U1FGDuYVayZWH"
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
# Define possible categories for fields without predefined categories
|
| 24 |
additional_categories = {
|
|
@@ -37,148 +24,310 @@ additional_categories = {
|
|
| 37 |
"Previous_Trial_Participation": ["Yes", "No"]
|
| 38 |
}
|
| 39 |
|
| 40 |
-
# Define the input components for the form
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
diagnoses_icd10_input = gr.CheckboxGroup(choices=additional_categories["Diagnoses_ICD10"], label="Diagnoses (ICD-10)")
|
| 46 |
medications_input = gr.CheckboxGroup(choices=additional_categories["Medications"], label="Medications")
|
| 47 |
allergies_input = gr.CheckboxGroup(choices=additional_categories["Allergies"], label="Allergies")
|
| 48 |
previous_treatments_input = gr.CheckboxGroup(choices=additional_categories["Previous_Treatments"], label="Previous Treatments")
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
Returns:
|
| 71 |
-
bytes: Encrypted and serialized symptoms.
|
| 72 |
-
"""
|
| 73 |
-
|
| 74 |
-
# Retrieve the client API
|
| 75 |
-
client = FHEModelClient(path_dir=DEPLOYMENT_DIR, key_dir=KEYS_DIR / f"{user_id}")
|
| 76 |
-
client.load()
|
| 77 |
-
|
| 78 |
-
# Ensure the symptoms are properly formatted as an array
|
| 79 |
-
user_symptoms = np.array(user_symptoms).reshape(1, -1)
|
| 80 |
-
|
| 81 |
-
# Encrypt and serialize the symptoms
|
| 82 |
-
encrypted_quantized_user_symptoms = client.quantize_encrypt_serialize(user_symptoms)
|
| 83 |
-
|
| 84 |
-
# Ensure the encryption process returned bytes
|
| 85 |
-
assert isinstance(encrypted_quantized_user_symptoms, bytes)
|
| 86 |
-
|
| 87 |
-
# Save the encrypted data to a file (optional)
|
| 88 |
-
encrypted_input_path = KEYS_DIR / f"{user_id}/encrypted_input"
|
| 89 |
-
with encrypted_input_path.open("wb") as f:
|
| 90 |
-
f.write(encrypted_quantized_user_symptoms)
|
| 91 |
-
|
| 92 |
-
# Return the encrypted data
|
| 93 |
-
return encrypted_quantized_user_symptoms
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
def decrypt_result(encrypted_answer: bytes, user_id: str) -> bool:
|
| 97 |
-
"""
|
| 98 |
-
Decrypt the encrypted result.
|
| 99 |
-
|
| 100 |
-
Args:
|
| 101 |
-
encrypted_answer (bytes): The encrypted result.
|
| 102 |
-
user_id (str): The current user's ID.
|
| 103 |
-
|
| 104 |
-
Returns:
|
| 105 |
-
bool: The decrypted result.
|
| 106 |
-
"""
|
| 107 |
-
|
| 108 |
-
# Retrieve the client API
|
| 109 |
-
client = FHEModelClient(path_dir=DEPLOYMENT_DIR, key_dir=KEYS_DIR / f"{user_id}")
|
| 110 |
-
client.load()
|
| 111 |
-
|
| 112 |
-
# Decrypt the result
|
| 113 |
-
decrypted_result = client.decrypt_deserialize(encrypted_answer)
|
| 114 |
-
|
| 115 |
-
# Return the decrypted result
|
| 116 |
-
return decrypted_result
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
def encode_categorical_data(data):
|
| 121 |
-
categories = ["Gender", "Ethnicity", "Geographic_Location", "Smoking_Status", "Alcohol_Consumption", "Exercise_Habits", "Diet", "Functional_Status", "Previous_Trial_Participation"]
|
| 122 |
encoded_data = []
|
| 123 |
-
for
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
encoded_data.append(sub_cats.index(data[i]) + 1)
|
| 127 |
else:
|
| 128 |
-
encoded_data.append(0)
|
| 129 |
-
|
| 130 |
return encoded_data
|
| 131 |
|
| 132 |
|
| 133 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
response = requests.post(SERVER_URL, data=encrypted_array)
|
| 148 |
-
|
| 149 |
-
# Check if the data was sent successfully
|
| 150 |
-
if response.status_code == 200:
|
| 151 |
-
print("Data sent successfully.")
|
| 152 |
-
else:
|
| 153 |
-
print("Error sending data.")
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
inputs=[
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
],
|
| 178 |
outputs="text",
|
| 179 |
-
title="
|
| 180 |
-
description="Please
|
| 181 |
)
|
| 182 |
|
| 183 |
-
# Launch the
|
| 184 |
-
|
|
|
|
|
|
|
|
|
| 1 |
from concrete.ml.deployment import FHEModelClient
|
| 2 |
from pathlib import Path
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
+
from typing import List
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Define possible categories for fields without predefined categories
|
| 11 |
additional_categories = {
|
|
|
|
| 24 |
"Previous_Trial_Participation": ["Yes", "No"]
|
| 25 |
}
|
| 26 |
|
| 27 |
+
# Define the input components for the researcher form
|
| 28 |
+
min_age_input = gr.Number(label="Minimum Age", value=18)
|
| 29 |
+
max_age_input = gr.Number(label="Maximum Age", value=100)
|
| 30 |
+
gender_input = gr.CheckboxGroup(choices=additional_categories["Gender"], label="Gender")
|
| 31 |
+
ethnicity_input = gr.CheckboxGroup(choices=additional_categories["Ethnicity"], label="Ethnicity")
|
| 32 |
+
geographic_location_input = gr.CheckboxGroup(choices=additional_categories["Geographic_Location"], label="Geographic Location")
|
| 33 |
diagnoses_icd10_input = gr.CheckboxGroup(choices=additional_categories["Diagnoses_ICD10"], label="Diagnoses (ICD-10)")
|
| 34 |
medications_input = gr.CheckboxGroup(choices=additional_categories["Medications"], label="Medications")
|
| 35 |
allergies_input = gr.CheckboxGroup(choices=additional_categories["Allergies"], label="Allergies")
|
| 36 |
previous_treatments_input = gr.CheckboxGroup(choices=additional_categories["Previous_Treatments"], label="Previous Treatments")
|
| 37 |
+
min_blood_glucose_level_input = gr.Number(label="Minimum Blood Glucose Level", value=0)
|
| 38 |
+
max_blood_glucose_level_input = gr.Number(label="Maximum Blood Glucose Level", value=300)
|
| 39 |
+
min_blood_pressure_systolic_input = gr.Number(label="Minimum Blood Pressure (Systolic)", value=80)
|
| 40 |
+
max_blood_pressure_systolic_input = gr.Number(label="Maximum Blood Pressure (Systolic)", value=200)
|
| 41 |
+
min_blood_pressure_diastolic_input = gr.Number(label="Minimum Blood Pressure (Diastolic)", value=40)
|
| 42 |
+
max_blood_pressure_diastolic_input = gr.Number(label="Maximum Blood Pressure (Diastolic)", value=120)
|
| 43 |
+
min_bmi_input = gr.Number(label="Minimum BMI", value=10)
|
| 44 |
+
max_bmi_input = gr.Number(label="Maximum BMI", value=50)
|
| 45 |
+
smoking_status_input = gr.CheckboxGroup(choices=additional_categories["Smoking_Status"], label="Smoking Status")
|
| 46 |
+
alcohol_consumption_input = gr.CheckboxGroup(choices=additional_categories["Alcohol_Consumption"], label="Alcohol Consumption")
|
| 47 |
+
exercise_habits_input = gr.CheckboxGroup(choices=additional_categories["Exercise_Habits"], label="Exercise Habits")
|
| 48 |
+
diet_input = gr.CheckboxGroup(choices=additional_categories["Diet"], label="Diet")
|
| 49 |
+
min_condition_severity_input = gr.Number(label="Minimum Condition Severity", value=1)
|
| 50 |
+
max_condition_severity_input = gr.Number(label="Maximum Condition Severity", value=10)
|
| 51 |
+
functional_status_input = gr.CheckboxGroup(choices=additional_categories["Functional_Status"], label="Functional Status")
|
| 52 |
+
previous_trial_participation_input = gr.CheckboxGroup(choices=additional_categories["Previous_Trial_Participation"], label="Previous Trial Participation")
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def encode_categorical_data(data: List[str], category_name: str) -> List[int]:
|
| 56 |
+
"""Encodes a list of categorical values into their corresponding indices based on additional_categories."""
|
| 57 |
+
sub_cats = additional_categories.get(category_name, [])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
encoded_data = []
|
| 59 |
+
for value in data:
|
| 60 |
+
if value in sub_cats:
|
| 61 |
+
encoded_data.append(sub_cats.index(value) + 1) # Adding 1 to avoid index 0 for valid entries
|
|
|
|
| 62 |
else:
|
| 63 |
+
encoded_data.append(0) # Encode unmatched as 0
|
|
|
|
| 64 |
return encoded_data
|
| 65 |
|
| 66 |
|
| 67 |
+
def process_researcher_data(
|
| 68 |
+
min_age, max_age, gender, ethnicity, geographic_location, diagnoses_icd10, medications, allergies, previous_treatments,
|
| 69 |
+
min_blood_glucose_level, max_blood_glucose_level, min_blood_pressure_systolic, max_blood_pressure_systolic,
|
| 70 |
+
min_blood_pressure_diastolic, max_blood_pressure_diastolic, min_bmi, max_bmi, smoking_status, alcohol_consumption,
|
| 71 |
+
exercise_habits, diet, min_condition_severity, max_condition_severity, functional_status, previous_trial_participation
|
| 72 |
+
):
|
| 73 |
+
# Encode categorical data
|
| 74 |
+
encoded_gender = encode_categorical_data(gender, "Gender")
|
| 75 |
+
encoded_ethnicity = encode_categorical_data(ethnicity, "Ethnicity")
|
| 76 |
+
encoded_geographic_location = encode_categorical_data(geographic_location, "Geographic_Location")
|
| 77 |
+
encoded_diagnoses_icd10 = encode_categorical_data(diagnoses_icd10, "Diagnoses_ICD10")
|
| 78 |
+
encoded_smoking_status = encode_categorical_data(smoking_status, "Smoking_Status")
|
| 79 |
+
encoded_alcohol_consumption = encode_categorical_data(alcohol_consumption, "Alcohol_Consumption")
|
| 80 |
+
encoded_exercise_habits = encode_categorical_data(exercise_habits, "Exercise_Habits")
|
| 81 |
+
encoded_diet = encode_categorical_data(diet, "Diet")
|
| 82 |
+
encoded_functional_status = encode_categorical_data(functional_status, "Functional_Status")
|
| 83 |
+
encoded_previous_trial_participation = encode_categorical_data(previous_trial_participation, "Previous_Trial_Participation")
|
| 84 |
+
|
| 85 |
+
# Create a list of requirements
|
| 86 |
+
requirements = []
|
| 87 |
+
|
| 88 |
+
# Add numerical requirements
|
| 89 |
+
if min_age is not None:
|
| 90 |
+
requirements.append({
|
| 91 |
+
"column_name": "Age",
|
| 92 |
+
"value": int(min_age),
|
| 93 |
+
"comparison_type": "greater_than"
|
| 94 |
+
})
|
| 95 |
+
if max_age is not None:
|
| 96 |
+
requirements.append({
|
| 97 |
+
"column_name": "Age",
|
| 98 |
+
"value": int(max_age),
|
| 99 |
+
"comparison_type": "less_than"
|
| 100 |
+
})
|
| 101 |
+
|
| 102 |
+
if min_blood_glucose_level is not None:
|
| 103 |
+
requirements.append({
|
| 104 |
+
"column_name": "Blood_Glucose_Level",
|
| 105 |
+
"value": int(min_blood_glucose_level),
|
| 106 |
+
"comparison_type": "greater_than"
|
| 107 |
+
})
|
| 108 |
+
if max_blood_glucose_level is not None:
|
| 109 |
+
requirements.append({
|
| 110 |
+
"column_name": "Blood_Glucose_Level",
|
| 111 |
+
"value": int(max_blood_glucose_level),
|
| 112 |
+
"comparison_type": "less_than"
|
| 113 |
+
})
|
| 114 |
+
|
| 115 |
+
if min_blood_pressure_systolic is not None:
|
| 116 |
+
requirements.append({
|
| 117 |
+
"column_name": "Blood_Pressure_Systolic",
|
| 118 |
+
"value": int(min_blood_pressure_systolic),
|
| 119 |
+
"comparison_type": "greater_than"
|
| 120 |
+
})
|
| 121 |
+
if max_blood_pressure_systolic is not None:
|
| 122 |
+
requirements.append({
|
| 123 |
+
"column_name": "Blood_Pressure_Systolic",
|
| 124 |
+
"value": int(max_blood_pressure_systolic),
|
| 125 |
+
"comparison_type": "less_than"
|
| 126 |
+
})
|
| 127 |
+
|
| 128 |
+
if min_blood_pressure_diastolic is not None:
|
| 129 |
+
requirements.append({
|
| 130 |
+
"column_name": "Blood_Pressure_Diastolic",
|
| 131 |
+
"value": int(min_blood_pressure_diastolic),
|
| 132 |
+
"comparison_type": "greater_than"
|
| 133 |
+
})
|
| 134 |
+
if max_blood_pressure_diastolic is not None:
|
| 135 |
+
requirements.append({
|
| 136 |
+
"column_name": "Blood_Pressure_Diastolic",
|
| 137 |
+
"value": int(max_blood_pressure_diastolic),
|
| 138 |
+
"comparison_type": "less_than"
|
| 139 |
+
})
|
| 140 |
|
| 141 |
+
if min_bmi is not None:
|
| 142 |
+
requirements.append({
|
| 143 |
+
"column_name": "BMI",
|
| 144 |
+
"value": float(min_bmi),
|
| 145 |
+
"comparison_type": "greater_than"
|
| 146 |
+
})
|
| 147 |
+
if max_bmi is not None:
|
| 148 |
+
requirements.append({
|
| 149 |
+
"column_name": "BMI",
|
| 150 |
+
"value": float(max_bmi),
|
| 151 |
+
"comparison_type": "less_than"
|
| 152 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
if min_condition_severity is not None:
|
| 155 |
+
requirements.append({
|
| 156 |
+
"column_name": "Condition_Severity",
|
| 157 |
+
"value": int(min_condition_severity),
|
| 158 |
+
"comparison_type": "greater_than"
|
| 159 |
+
})
|
| 160 |
+
if max_condition_severity is not None:
|
| 161 |
+
requirements.append({
|
| 162 |
+
"column_name": "Condition_Severity",
|
| 163 |
+
"value": int(max_condition_severity),
|
| 164 |
+
"comparison_type": "less_than"
|
| 165 |
+
})
|
| 166 |
+
|
| 167 |
+
# Add categorical requirements
|
| 168 |
+
for gender_value in encoded_gender:
|
| 169 |
+
if gender_value > 0:
|
| 170 |
+
requirements.append({
|
| 171 |
+
"column_name": "Gender",
|
| 172 |
+
"value": gender_value,
|
| 173 |
+
"comparison_type": "equal"
|
| 174 |
+
})
|
| 175 |
|
| 176 |
+
for ethnicity_value in encoded_ethnicity:
|
| 177 |
+
if ethnicity_value > 0:
|
| 178 |
+
requirements.append({
|
| 179 |
+
"column_name": "Ethnicity",
|
| 180 |
+
"value": ethnicity_value,
|
| 181 |
+
"comparison_type": "equal"
|
| 182 |
+
})
|
| 183 |
+
|
| 184 |
+
for location_value in encoded_geographic_location:
|
| 185 |
+
if location_value > 0:
|
| 186 |
+
requirements.append({
|
| 187 |
+
"column_name": "Geographic_Location",
|
| 188 |
+
"value": location_value,
|
| 189 |
+
"comparison_type": "equal"
|
| 190 |
+
})
|
| 191 |
+
|
| 192 |
+
for diagnosis_value in encoded_diagnoses_icd10:
|
| 193 |
+
if diagnosis_value > 0:
|
| 194 |
+
requirements.append({
|
| 195 |
+
"column_name": "Diagnoses_ICD10",
|
| 196 |
+
"value": diagnosis_value,
|
| 197 |
+
"comparison_type": "equal"
|
| 198 |
+
})
|
| 199 |
+
|
| 200 |
+
for smoking_status_value in encoded_smoking_status:
|
| 201 |
+
if smoking_status_value > 0:
|
| 202 |
+
requirements.append({
|
| 203 |
+
"column_name": "Smoking_Status",
|
| 204 |
+
"value": smoking_status_value,
|
| 205 |
+
"comparison_type": "equal"
|
| 206 |
+
})
|
| 207 |
+
|
| 208 |
+
for alcohol_value in encoded_alcohol_consumption:
|
| 209 |
+
if alcohol_value > 0:
|
| 210 |
+
requirements.append({
|
| 211 |
+
"column_name": "Alcohol_Consumption",
|
| 212 |
+
"value": alcohol_value,
|
| 213 |
+
"comparison_type": "equal"
|
| 214 |
+
})
|
| 215 |
+
|
| 216 |
+
for exercise_value in encoded_exercise_habits:
|
| 217 |
+
if exercise_value > 0:
|
| 218 |
+
requirements.append({
|
| 219 |
+
"column_name": "Exercise_Habits",
|
| 220 |
+
"value": exercise_value,
|
| 221 |
+
"comparison_type": "equal"
|
| 222 |
+
})
|
| 223 |
+
|
| 224 |
+
for diet_value in encoded_diet:
|
| 225 |
+
if diet_value > 0:
|
| 226 |
+
requirements.append({
|
| 227 |
+
"column_name": "Diet",
|
| 228 |
+
"value": diet_value,
|
| 229 |
+
"comparison_type": "equal"
|
| 230 |
+
})
|
| 231 |
+
|
| 232 |
+
for status in encoded_functional_status:
|
| 233 |
+
if status > 0:
|
| 234 |
+
requirements.append({
|
| 235 |
+
"column_name": "Functional_Status",
|
| 236 |
+
"value": status,
|
| 237 |
+
"comparison_type": "equal"
|
| 238 |
+
})
|
| 239 |
+
|
| 240 |
+
for participation in encoded_previous_trial_participation:
|
| 241 |
+
if participation > 0:
|
| 242 |
+
requirements.append({
|
| 243 |
+
"column_name": "Previous_Trial_Participation",
|
| 244 |
+
"value": participation,
|
| 245 |
+
"comparison_type": "equal"
|
| 246 |
+
})
|
| 247 |
+
|
| 248 |
+
# Encode and add non-categorical fields like medications, allergies, previous treatments
|
| 249 |
+
for medication in medications:
|
| 250 |
+
encoded_medications = encode_categorical_data([medication], "Medications")
|
| 251 |
+
for med_value in encoded_medications:
|
| 252 |
+
if med_value > 0:
|
| 253 |
+
requirements.append({
|
| 254 |
+
"column_name": "Medications",
|
| 255 |
+
"value": med_value,
|
| 256 |
+
"comparison_type": "equal"
|
| 257 |
+
})
|
| 258 |
+
|
| 259 |
+
for allergy in allergies:
|
| 260 |
+
encoded_allergies = encode_categorical_data([allergy], "Allergies")
|
| 261 |
+
for allergy_value in encoded_allergies:
|
| 262 |
+
if allergy_value > 0:
|
| 263 |
+
requirements.append({
|
| 264 |
+
"column_name": "Allergies",
|
| 265 |
+
"value": allergy_value,
|
| 266 |
+
"comparison_type": "equal"
|
| 267 |
+
})
|
| 268 |
+
|
| 269 |
+
for treatment in previous_treatments:
|
| 270 |
+
encoded_treatments = encode_categorical_data([treatment], "Previous_Treatments")
|
| 271 |
+
for treatment_value in encoded_treatments:
|
| 272 |
+
if treatment_value > 0:
|
| 273 |
+
requirements.append({
|
| 274 |
+
"column_name": "Previous_Treatments",
|
| 275 |
+
"value": treatment_value,
|
| 276 |
+
"comparison_type": "equal"
|
| 277 |
+
})
|
| 278 |
+
|
| 279 |
+
# Construct the payload as a regular dictionary
|
| 280 |
+
payload = {
|
| 281 |
+
"model_name": "fhe_model_v1",
|
| 282 |
+
"requirements": requirements
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
# turn the payload into a JSON object
|
| 286 |
+
payload = json.dumps(payload)
|
| 287 |
+
|
| 288 |
+
print("Payload:", payload)
|
| 289 |
+
|
| 290 |
+
# Store the server's URL
|
| 291 |
+
SERVER_URL = "https://ppaihack-match.azurewebsites.net/requirements/create"
|
| 292 |
+
|
| 293 |
+
# Make the request to the server
|
| 294 |
+
try:
|
| 295 |
+
res = requests.post(SERVER_URL, json=payload)
|
| 296 |
+
res.raise_for_status() # Raise an error for bad status codes
|
| 297 |
+
except requests.exceptions.HTTPError as http_err:
|
| 298 |
+
print(f"HTTP error occurred: {http_err}") # For debugging
|
| 299 |
+
return f"HTTP error occurred: {http_err}"
|
| 300 |
+
except Exception as err:
|
| 301 |
+
print(f"Other error occurred: {err}") # For debugging
|
| 302 |
+
return f"Other error occurred: {err}"
|
| 303 |
+
|
| 304 |
+
# Get the response from the server
|
| 305 |
+
try:
|
| 306 |
+
response = res.json()
|
| 307 |
+
print("Server response:", response)
|
| 308 |
+
except ValueError:
|
| 309 |
+
print("Response is not in JSON format.")
|
| 310 |
+
return "Response is not in JSON format."
|
| 311 |
+
|
| 312 |
+
return response.get("message", "No message received from server")
|
| 313 |
+
|
| 314 |
+
|
| 315 |
+
# Create the Gradio interface for researchers
|
| 316 |
+
researcher_demo = gr.Interface(
|
| 317 |
+
fn=process_researcher_data,
|
| 318 |
inputs=[
|
| 319 |
+
min_age_input, max_age_input, gender_input, ethnicity_input, geographic_location_input, diagnoses_icd10_input,
|
| 320 |
+
medications_input, allergies_input, previous_treatments_input, min_blood_glucose_level_input,
|
| 321 |
+
max_blood_glucose_level_input, min_blood_pressure_systolic_input, max_blood_pressure_systolic_input,
|
| 322 |
+
min_blood_pressure_diastolic_input, max_blood_pressure_diastolic_input, min_bmi_input, max_bmi_input,
|
| 323 |
+
smoking_status_input, alcohol_consumption_input, exercise_habits_input, diet_input,
|
| 324 |
+
min_condition_severity_input, max_condition_severity_input, functional_status_input, previous_trial_participation_input
|
| 325 |
],
|
| 326 |
outputs="text",
|
| 327 |
+
title="Clinical Researcher Criteria Form",
|
| 328 |
+
description="Please enter the criteria for the type of patients you are looking for."
|
| 329 |
)
|
| 330 |
|
| 331 |
+
# Launch the researcher interface with a public link
|
| 332 |
+
if __name__ == "__main__":
|
| 333 |
+
researcher_demo.launch(share=True)
|
requirements.txt
CHANGED
|
@@ -11,9 +11,6 @@ certifi==2023.7.22
|
|
| 11 |
charset-normalizer==3.3.2
|
| 12 |
click==8.1.7
|
| 13 |
coloredlogs==15.0.1
|
| 14 |
-
concrete==4.18.2
|
| 15 |
-
concrete-ml
|
| 16 |
-
concrete-python==2.5
|
| 17 |
contourpy==1.3.0
|
| 18 |
cycler==0.12.1
|
| 19 |
dependencies==2.0.1
|
|
@@ -101,7 +98,6 @@ thrift==0.16.0
|
|
| 101 |
tokenizers==0.20.0
|
| 102 |
tomli==2.0.1
|
| 103 |
tomlkit==0.12.0
|
| 104 |
-
torch==1.13.1
|
| 105 |
tqdm==4.66.5
|
| 106 |
transformers==4.45.1
|
| 107 |
typer==0.12.5
|
|
|
|
| 11 |
charset-normalizer==3.3.2
|
| 12 |
click==8.1.7
|
| 13 |
coloredlogs==15.0.1
|
|
|
|
|
|
|
|
|
|
| 14 |
contourpy==1.3.0
|
| 15 |
cycler==0.12.1
|
| 16 |
dependencies==2.0.1
|
|
|
|
| 98 |
tokenizers==0.20.0
|
| 99 |
tomli==2.0.1
|
| 100 |
tomlkit==0.12.0
|
|
|
|
| 101 |
tqdm==4.66.5
|
| 102 |
transformers==4.45.1
|
| 103 |
typer==0.12.5
|