Thomas Chardonnens commited on
Commit
3ab9bf6
1 Parent(s): d313727

add background job

Browse files
Files changed (3) hide show
  1. app.py +5 -3
  2. requirements.txt +1 -0
  3. server.py +38 -15
app.py CHANGED
@@ -365,19 +365,21 @@ with demo:
365
  "detection directly over encrypted values. Once the computation is finished, the server returns "
366
  "the encrypted results to the client."
367
  )
368
-
369
  gr.Markdown("### Step 4: Send the encrypted image to the server.")
370
  send_input_button = gr.Button("Send the encrypted image to the server.")
371
  send_input_checkbox = gr.Checkbox(label="Encrypted image sent.", interactive=False)
372
 
373
  gr.Markdown("### Step 5: Run FHE execution.")
374
  execute_fhe_button = gr.Button("Run FHE execution.")
 
375
  fhe_execution_time = gr.Textbox(
376
  label="Total FHE execution time (in seconds):", max_lines=1, interactive=False
377
  )
 
378
 
379
- gr.Markdown("### Step 6: Receive the encrypted output from the server.")
380
- get_output_button = gr.Button("Receive the encrypted output from the server.")
 
381
 
382
  with gr.Row():
383
  encrypted_output = gr.Textbox(
 
365
  "detection directly over encrypted values. Once the computation is finished, the server returns "
366
  "the encrypted results to the client."
367
  )
 
368
  gr.Markdown("### Step 4: Send the encrypted image to the server.")
369
  send_input_button = gr.Button("Send the encrypted image to the server.")
370
  send_input_checkbox = gr.Checkbox(label="Encrypted image sent.", interactive=False)
371
 
372
  gr.Markdown("### Step 5: Run FHE execution.")
373
  execute_fhe_button = gr.Button("Run FHE execution.")
374
+ fhe_status = gr.Textbox(label="FHE execution status:", max_lines=1, interactive=False)
375
  fhe_execution_time = gr.Textbox(
376
  label="Total FHE execution time (in seconds):", max_lines=1, interactive=False
377
  )
378
+ task_id = gr.Textbox(label="Task ID:", visible=False)
379
 
380
+ gr.Markdown("### Step 6: Check FHE execution status and receive the encrypted output from the server.")
381
+ check_status_button = gr.Button("Check FHE execution status")
382
+ get_output_button = gr.Button("Receive the encrypted output from the server.", interactive=False)
383
 
384
  with gr.Row():
385
  encrypted_output = gr.Textbox(
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  concrete-ml
2
  gradio
3
  fastapi
 
 
1
  concrete-ml
2
  gradio
3
  fastapi
4
+ asyncio
server.py CHANGED
@@ -4,7 +4,7 @@ import time
4
  from typing import List
5
  from fastapi import FastAPI, File, Form, UploadFile
6
  from fastapi.responses import JSONResponse, Response
7
-
8
  from common import SERVER_TMP_PATH, SEIZURE_DETECTION_MODEL_PATH
9
  from client_server_interface import FHEServer
10
 
@@ -52,7 +52,7 @@ def send_input(
52
 
53
 
54
  @app.post("/run_fhe")
55
- def run_fhe(
56
  user_id: str = Form(),
57
  ):
58
  """Execute seizure detection on the encrypted input image using FHE."""
@@ -67,19 +67,42 @@ def run_fhe(
67
  encrypted_image = encrypted_image_file.read()
68
  evaluation_key = evaluation_key_file.read()
69
 
70
- # Run the FHE execution
71
- start = time.time()
72
- encrypted_output = FHE_SERVER.run(encrypted_image, evaluation_key)
73
- fhe_execution_time = round(time.time() - start, 2)
74
-
75
- # Retrieve the encrypted output path
76
- encrypted_output_path = get_server_file_path("encrypted_output", user_id)
77
-
78
- # Write the file using the above path
79
- with encrypted_output_path.open("wb") as encrypted_output_file:
80
- encrypted_output_file.write(encrypted_output)
81
-
82
- return JSONResponse(content=fhe_execution_time)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
 
85
  @app.post("/get_output")
 
4
  from typing import List
5
  from fastapi import FastAPI, File, Form, UploadFile
6
  from fastapi.responses import JSONResponse, Response
7
+ import asyncio
8
  from common import SERVER_TMP_PATH, SEIZURE_DETECTION_MODEL_PATH
9
  from client_server_interface import FHEServer
10
 
 
52
 
53
 
54
  @app.post("/run_fhe")
55
+ async def run_fhe(
56
  user_id: str = Form(),
57
  ):
58
  """Execute seizure detection on the encrypted input image using FHE."""
 
67
  encrypted_image = encrypted_image_file.read()
68
  evaluation_key = evaluation_key_file.read()
69
 
70
+ # Run the FHE execution in a background task
71
+ async def run_fhe_task():
72
+ start = time.time()
73
+ encrypted_output = FHE_SERVER.run(encrypted_image, evaluation_key)
74
+ fhe_execution_time = round(time.time() - start, 2)
75
+
76
+ # Retrieve the encrypted output path
77
+ encrypted_output_path = get_server_file_path("encrypted_output", user_id)
78
+
79
+ # Write the file using the above path
80
+ with encrypted_output_path.open("wb") as encrypted_output_file:
81
+ encrypted_output_file.write(encrypted_output)
82
+
83
+ return fhe_execution_time
84
+
85
+ # Start the background task
86
+ task = asyncio.create_task(run_fhe_task())
87
+
88
+ # Return a response immediately
89
+ return JSONResponse(content={"message": "FHE execution started", "task_id": str(id(task))})
90
+
91
+ @app.get("/fhe_status/{task_id}")
92
+ async def fhe_status(task_id: str):
93
+ """Check the status of an FHE execution task and return the execution time if completed."""
94
+ for task in asyncio.all_tasks():
95
+ if str(id(task)) == task_id:
96
+ if task.done():
97
+ try:
98
+ execution_time = task.result()
99
+ return JSONResponse(content={"status": "completed", "execution_time": execution_time})
100
+ except Exception as e:
101
+ return JSONResponse(content={"status": "error", "message": str(e)})
102
+ else:
103
+ return JSONResponse(content={"status": "running"})
104
+
105
+ return JSONResponse(content={"status": "not_found"})
106
 
107
 
108
  @app.post("/get_output")