batuergun commited on
Commit
835e258
1 Parent(s): abc2497

Revert "test without exec"

Browse files

This reverts commit 7f467975c1508eb55afa74a02180964fb26ccd63.

Files changed (3) hide show
  1. app.py +4 -25
  2. client_server_interface.py +3 -2
  3. server.py +11 -19
app.py CHANGED
@@ -317,7 +317,8 @@ def decrypt_output(user_id):
317
  user_id (int): The current user's ID.
318
 
319
  Returns:
320
- str: The decrypted output message
 
321
  """
322
  if user_id == "":
323
  raise gr.Error("Please generate the private key first.")
@@ -332,35 +333,13 @@ def decrypt_output(user_id):
332
  with encrypted_output_path.open("rb") as encrypted_output_file:
333
  encrypted_output = encrypted_output_file.read()
334
 
335
- logger.debug(f"Encrypted output size: {len(encrypted_output)} bytes")
336
- logger.debug(f"Encrypted output (first 100 bytes): {encrypted_output[:100].hex()}")
337
-
338
- if not encrypted_output:
339
- raise gr.Error("The encrypted output is empty. Please try running the FHE execution again.")
340
-
341
  # Retrieve the client API
342
  client = get_client(user_id)
343
 
344
  # Deserialize, decrypt and post-process the encrypted output
345
- try:
346
- decrypted_output = client.deserialize_decrypt_post_process(encrypted_output)
347
-
348
- # The decrypted output should be a 1D array with 2 elements
349
- if isinstance(decrypted_output, np.ndarray) and decrypted_output.shape == (2,):
350
- predicted_class = np.argmax(decrypted_output)
351
- confidence = decrypted_output[predicted_class]
352
- result = "Seizure detected" if predicted_class == 1 else "No seizure detected"
353
- return f"{result} (Confidence: {confidence:.2f})"
354
- else:
355
- logger.error(f"Unexpected decrypted output format: {decrypted_output}")
356
- raise ValueError("Unexpected output format from the model")
357
 
358
- except RuntimeError as e:
359
- logger.error(f"Error during deserialization: {str(e)}")
360
- raise gr.Error("Failed to deserialize the encrypted output. The data might be corrupted or in an unexpected format.")
361
- except Exception as e:
362
- logger.error(f"Unexpected error during decryption: {str(e)}")
363
- raise gr.Error(f"An unexpected error occurred during decryption: {str(e)}")
364
 
365
  def resize_img(img, width=256, height=256):
366
  """Resize the image."""
 
317
  user_id (int): The current user's ID.
318
 
319
  Returns:
320
+ bool: The decrypted output (True if seizure detected, False otherwise)
321
+
322
  """
323
  if user_id == "":
324
  raise gr.Error("Please generate the private key first.")
 
333
  with encrypted_output_path.open("rb") as encrypted_output_file:
334
  encrypted_output = encrypted_output_file.read()
335
 
 
 
 
 
 
 
336
  # Retrieve the client API
337
  client = get_client(user_id)
338
 
339
  # Deserialize, decrypt and post-process the encrypted output
340
+ decrypted_output = client.deserialize_decrypt_post_process(encrypted_output)
 
 
 
 
 
 
 
 
 
 
 
341
 
342
+ return "Seizure detected" if decrypted_output else "No seizure detected"
 
 
 
 
 
343
 
344
  def resize_img(img, width=256, height=256):
345
  """Resize the image."""
client_server_interface.py CHANGED
@@ -146,5 +146,6 @@ class FHEClient:
146
  output = self.client.decrypt(encrypted_output)
147
 
148
  # Post-process the output (if needed)
149
- # Assuming the output is already in the correct format (2-element array)
150
- return output
 
 
146
  output = self.client.decrypt(encrypted_output)
147
 
148
  # Post-process the output (if needed)
149
+ seizure_detected = self.seizure_detector.post_processing(output)
150
+
151
+ return seizure_detected
server.py CHANGED
@@ -10,7 +10,6 @@ from fastapi.responses import JSONResponse, Response
10
  from fastapi.middleware.cors import CORSMiddleware
11
 
12
  from concrete.ml.deployment import FHEModelServer
13
- import numpy as np
14
 
15
  import gc
16
 
@@ -101,24 +100,17 @@ def run_fhe(user_id: str = Form()):
101
 
102
  # Run the FHE execution
103
  start = time.time()
104
- # try:
105
- # encrypted_output = FHE_SERVER.run(encrypted_image, evaluation_key)
106
- # except MemoryError:
107
- # logger.error("FHE execution failed due to insufficient memory")
108
- # raise HTTPException(status_code=503, detail="Insufficient memory during FHE execution")
109
- # except Exception as e:
110
- # logger.error(f"FHE execution failed: {str(e)}")
111
- # raise HTTPException(status_code=500, detail="FHE execution failed")
112
- # finally:
113
- # # Force garbage collection after FHE execution
114
- # gc.collect()
115
-
116
- # Placeholder output
117
- # Generate a random 2-element array with values between 0 and 1
118
- placeholder_output = np.random.rand(2)
119
- # Ensure the sum of the two elements is 1 (to mimic softmax output)
120
- placeholder_output = placeholder_output / np.sum(placeholder_output)
121
- encrypted_output = placeholder_output.tobytes()
122
 
123
  fhe_execution_time = round(time.time() - start, 2)
124
 
 
10
  from fastapi.middleware.cors import CORSMiddleware
11
 
12
  from concrete.ml.deployment import FHEModelServer
 
13
 
14
  import gc
15
 
 
100
 
101
  # Run the FHE execution
102
  start = time.time()
103
+ try:
104
+ encrypted_output = FHE_SERVER.run(encrypted_image, evaluation_key)
105
+ except MemoryError:
106
+ logger.error("FHE execution failed due to insufficient memory")
107
+ raise HTTPException(status_code=503, detail="Insufficient memory during FHE execution")
108
+ except Exception as e:
109
+ logger.error(f"FHE execution failed: {str(e)}")
110
+ raise HTTPException(status_code=500, detail="FHE execution failed")
111
+ finally:
112
+ # Force garbage collection after FHE execution
113
+ gc.collect()
 
 
 
 
 
 
 
114
 
115
  fhe_execution_time = round(time.time() - start, 2)
116