TomSmail commited on
Commit
0a96a85
·
1 Parent(s): bac7294

feat: add jsonification

Browse files
Files changed (1) hide show
  1. app.py +61 -14
app.py CHANGED
@@ -4,9 +4,10 @@ from pathlib import Path
4
  import numpy as np
5
  import gradio as gr
6
  import requests
 
7
 
8
  # Store the server's URL
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"
@@ -118,7 +119,7 @@ def decrypt_result(encrypted_answer: bytes, user_id: str) -> bool:
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 i in range(len(categories)):
124
  sub_cats = additional_categories[categories[i]]
@@ -129,22 +130,69 @@ def encode_categorical_data(data):
129
 
130
  return encoded_data
131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
  def process_patient_data(age, gender, ethnicity, geographic_location, diagnoses_icd10, medications, allergies, previous_treatments, blood_glucose_level, blood_pressure_systolic, blood_pressure_diastolic, bmi, smoking_status, alcohol_consumption, exercise_habits, diet, condition_severity, functional_status, previous_trial_participation):
134
 
135
  # Encode the data
136
- categorical_data = [gender, ethnicity, geographic_location, smoking_status, alcohol_consumption, exercise_habits, diet, functional_status, previous_trial_participation]
137
  print(f"Categorical data: {categorical_data}")
138
  encoded_categorical_data = encode_categorical_data(categorical_data)
139
  numerical_data = np.array([age, blood_glucose_level, blood_pressure_systolic, blood_pressure_diastolic, bmi, condition_severity])
140
  print(f"Numerical data: {numerical_data}")
141
  print(f"One-hot encoded data: {encoded_categorical_data}")
142
  combined_data = np.hstack((numerical_data, encoded_categorical_data))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  print(f"Combined data: {combined_data}")
144
- encrypted_array = encrypt_array(combined_data, "user_id")
145
 
146
- # Send the encrypted data to the server
147
- response = requests.post(SERVER_URL, data=encrypted_array)
 
148
 
149
  # Check if the data was sent successfully
150
  if response.status_code == 200:
@@ -153,20 +201,19 @@ def process_patient_data(age, gender, ethnicity, geographic_location, diagnoses_
153
  print("Error sending data.")
154
 
155
  # Decrypt the result
156
- decrypted_result = decrypt_result(response.content, USER_ID)
157
-
 
158
  # If the answer is True, return the link
159
  if decrypted_result:
160
  return (
161
- f"Encrypted data: {encrypted_array}",
162
- f"Decrypted result: {decrypted_result}",
163
- f"You may now access the link to the [clinical trial]({EXAMPLE_CLINICAL_TRIAL_LINK})"
164
  )
165
  else:
166
  return (
167
- f"Encrypted data: {encrypted_array}",
168
- f"Decrypted result: {decrypted_result}",
169
- f"Unfortunately, there are no clinical trials available for the provided criteria."
170
  )
171
 
172
  # Create the Gradio interface
 
4
  import numpy as np
5
  import gradio as gr
6
  import requests
7
+ import json
8
 
9
  # Store the server's URL
10
+ SERVER_URL = "https://ppaihack-match.azurewebsites.net/"
11
  CURRENT_DIR = Path(__file__).parent
12
  DEPLOYMENT_DIR = CURRENT_DIR / "deployment_files"
13
  KEYS_DIR = DEPLOYMENT_DIR / ".fhe_keys"
 
119
 
120
 
121
  def encode_categorical_data(data):
122
+ categories = ["Gender", "Ethnicity", "Geographic_Location", "Diagnoses_ICD10", "Medications", "Allergies", "Previous_Treatments", "Smoking_Status", "Alcohol_Consumption", "Exercise_Habits", "Diet", "Functional_Status", "Previous_Trial_Participation"]
123
  encoded_data = []
124
  for i in range(len(categories)):
125
  sub_cats = additional_categories[categories[i]]
 
130
 
131
  return encoded_data
132
 
133
+ def clear_data_to_json(data):
134
+ print(data)
135
+ patient_data = {
136
+ "model_names": ["my_model"],
137
+ "patient": {
138
+ "Age": data.get("age", 30),
139
+ "Blood_Glucose_Level": data.get("blood_glucose_level", 0),
140
+ "Blood_Pressure_Systolic": data.get("blood_pressure_systolic", 0),
141
+ "Blood_Pressure_Diastolic": data.get("blood_pressure_diastolic", 0),
142
+ "BMI": data.get("bmi", 0),
143
+ "Condition_Severity": data.get("condition_severity", 0),
144
+ "Gender": data.get("Gender", 0),
145
+ "Ethnicity": data.get("Ethnicity", 0),
146
+ "Geographic_Location": data.get("Geographic_Location", 0),
147
+ "Smoking_Status": data.get("Smoking_Status", 0),
148
+ "Diagnoses_ICD10": data.get("Diagnoses_ICD10", 0),
149
+ "Medications": data.get("Medications", 0),
150
+ "Allergies": data.get("Allergies", 0),
151
+ "Previous_Treatments": data.get("Previous_Treatments", 0),
152
+ "Alcohol_Consumption": data.get("Alcohol_Consumption", 0),
153
+ "Exercise_Habits": data.get("Exercise_Habits", 0),
154
+ "Diet": data.get("Diet", 0),
155
+ "Functional_Status": data.get("Functional_Status", 0),
156
+ "Previous_Trial_Participation": data.get("Previous_Trial_Participation", 0)
157
+ }
158
+ }
159
+ return json.dumps(patient_data, indent=4)
160
+
161
 
162
  def process_patient_data(age, gender, ethnicity, geographic_location, diagnoses_icd10, medications, allergies, previous_treatments, blood_glucose_level, blood_pressure_systolic, blood_pressure_diastolic, bmi, smoking_status, alcohol_consumption, exercise_habits, diet, condition_severity, functional_status, previous_trial_participation):
163
 
164
  # Encode the data
165
+ categorical_data = [gender, ethnicity, geographic_location, diagnoses_icd10, medications, allergies, previous_treatments, smoking_status, alcohol_consumption, exercise_habits, diet, functional_status, previous_trial_participation]
166
  print(f"Categorical data: {categorical_data}")
167
  encoded_categorical_data = encode_categorical_data(categorical_data)
168
  numerical_data = np.array([age, blood_glucose_level, blood_pressure_systolic, blood_pressure_diastolic, bmi, condition_severity])
169
  print(f"Numerical data: {numerical_data}")
170
  print(f"One-hot encoded data: {encoded_categorical_data}")
171
  combined_data = np.hstack((numerical_data, encoded_categorical_data))
172
+
173
+ ordered_categories = ["Gender", "Ethnicity", "Geographic_Location", "Diagnoses_ICD10", "Medications", "Allergies", "Previous_Treatments", "Smoking_Status", "Alcohol_Consumption", "Exercise_Habits", "Diet", "Functional_Status", "Previous_Trial_Participation"]
174
+ zipped_data = zip(ordered_categories, encoded_categorical_data)
175
+ # Convert the zipped data to a dictionary
176
+ encoded_categorical_dict = {category: value for category, value in zipped_data}
177
+
178
+ # Convert the data to JSON
179
+ json_data = clear_data_to_json({
180
+ "age": age,
181
+ "blood_glucose_level": blood_glucose_level,
182
+ "blood_pressure_systolic": blood_pressure_systolic,
183
+ "blood_pressure_diastolic": blood_pressure_diastolic,
184
+ "bmi": bmi,
185
+ "condition_severity": condition_severity,
186
+ **encoded_categorical_dict
187
+ })
188
+
189
+ print(f"JSON data: {json_data}")
190
  print(f"Combined data: {combined_data}")
191
+ # encrypted_array = encrypt_array(combined_data, "user_id")
192
 
193
+ # Send the data to the server
194
+ url = SERVER_URL + "clear-match"
195
+ response = requests.post(url, data=json_data)
196
 
197
  # Check if the data was sent successfully
198
  if response.status_code == 200:
 
201
  print("Error sending data.")
202
 
203
  # Decrypt the result
204
+ # decrypted_result = decrypt_result(response.content, USER_ID)
205
+ print()
206
+ decrypted_result = response.json()
207
  # If the answer is True, return the link
208
  if decrypted_result:
209
  return (
210
+ # f"Encrypted data: {encrypted_array}",
211
+ f"Decrypted result: {response.json()}"
 
212
  )
213
  else:
214
  return (
215
+ # f"Encrypted data: {encrypted_array}",
216
+ f"Decrypted result: {response.json()}"
 
217
  )
218
 
219
  # Create the Gradio interface