Thomas Chardonnens
commited on
Commit
·
e62353e
1
Parent(s):
4c7366a
log fastapi errors
Browse files
server.py
CHANGED
|
@@ -2,119 +2,113 @@
|
|
| 2 |
|
| 3 |
import time
|
| 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 |
|
| 11 |
# Load the server object for seizure detection
|
| 12 |
FHE_SERVER = FHEServer(model_path=SEIZURE_DETECTION_MODEL_PATH)
|
| 13 |
|
| 14 |
def get_server_file_path(name, user_id):
|
| 15 |
-
"""Get the correct temporary file path for the server.
|
| 16 |
-
|
| 17 |
-
Args:
|
| 18 |
-
name (str): The desired file name.
|
| 19 |
-
user_id (int): The current user's ID.
|
| 20 |
-
|
| 21 |
-
Returns:
|
| 22 |
-
pathlib.Path: The file path.
|
| 23 |
-
"""
|
| 24 |
return SERVER_TMP_PATH / f"{name}_seizure_detection_{user_id}"
|
| 25 |
|
| 26 |
-
|
| 27 |
# Initialize an instance of FastAPI
|
| 28 |
app = FastAPI()
|
| 29 |
|
| 30 |
-
# Define the default route
|
| 31 |
@app.get("/")
|
| 32 |
def root():
|
| 33 |
return {"message": "Welcome to Your Seizure Detection FHE Server!"}
|
| 34 |
|
| 35 |
-
|
| 36 |
@app.post("/send_input")
|
| 37 |
-
def send_input(
|
| 38 |
user_id: str = Form(),
|
| 39 |
files: List[UploadFile] = File(),
|
| 40 |
):
|
| 41 |
"""Send the inputs to the server."""
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 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."""
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
) as evaluation_key_file:
|
| 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 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
-
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
task = asyncio.create_task(run_fhe_task())
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
if task
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
@app.post("/get_output")
|
| 109 |
-
def get_output(
|
| 110 |
user_id: str = Form(),
|
| 111 |
):
|
| 112 |
"""Retrieve the encrypted output."""
|
| 113 |
-
|
| 114 |
-
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
encrypted_output = encrypted_output_file.read()
|
| 119 |
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import time
|
| 4 |
from typing import List
|
| 5 |
+
from fastapi import FastAPI, File, Form, UploadFile, HTTPException
|
| 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 |
+
import logging
|
| 11 |
+
|
| 12 |
+
# Set up logging
|
| 13 |
+
logging.basicConfig(level=logging.INFO)
|
| 14 |
+
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
# Load the server object for seizure detection
|
| 17 |
FHE_SERVER = FHEServer(model_path=SEIZURE_DETECTION_MODEL_PATH)
|
| 18 |
|
| 19 |
def get_server_file_path(name, user_id):
|
| 20 |
+
"""Get the correct temporary file path for the server."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
return SERVER_TMP_PATH / f"{name}_seizure_detection_{user_id}"
|
| 22 |
|
|
|
|
| 23 |
# Initialize an instance of FastAPI
|
| 24 |
app = FastAPI()
|
| 25 |
|
|
|
|
| 26 |
@app.get("/")
|
| 27 |
def root():
|
| 28 |
return {"message": "Welcome to Your Seizure Detection FHE Server!"}
|
| 29 |
|
|
|
|
| 30 |
@app.post("/send_input")
|
| 31 |
+
async def send_input(
|
| 32 |
user_id: str = Form(),
|
| 33 |
files: List[UploadFile] = File(),
|
| 34 |
):
|
| 35 |
"""Send the inputs to the server."""
|
| 36 |
+
try:
|
| 37 |
+
encrypted_image_path = get_server_file_path("encrypted_image", user_id)
|
| 38 |
+
evaluation_key_path = get_server_file_path("evaluation_key", user_id)
|
| 39 |
+
|
| 40 |
+
async with encrypted_image_path.open("wb") as encrypted_image, evaluation_key_path.open("wb") as evaluation_key:
|
| 41 |
+
encrypted_image.write(await files[0].read())
|
| 42 |
+
evaluation_key.write(await files[1].read())
|
| 43 |
+
|
| 44 |
+
return JSONResponse(content={"message": "Input received successfully"}, status_code=200)
|
| 45 |
+
except Exception as e:
|
| 46 |
+
logger.error(f"Error in send_input: {str(e)}")
|
| 47 |
+
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
| 48 |
|
| 49 |
@app.post("/run_fhe")
|
| 50 |
async def run_fhe(
|
| 51 |
user_id: str = Form(),
|
| 52 |
):
|
| 53 |
"""Execute seizure detection on the encrypted input image using FHE."""
|
| 54 |
+
try:
|
| 55 |
+
encrypted_image_path = get_server_file_path("encrypted_image", user_id)
|
| 56 |
+
evaluation_key_path = get_server_file_path("evaluation_key", user_id)
|
| 57 |
+
|
| 58 |
+
async with encrypted_image_path.open("rb") as encrypted_image_file, evaluation_key_path.open("rb") as evaluation_key_file:
|
| 59 |
+
encrypted_image = await encrypted_image_file.read()
|
| 60 |
+
evaluation_key = await evaluation_key_file.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
async def run_fhe_task():
|
| 63 |
+
start = time.time()
|
| 64 |
+
encrypted_output = FHE_SERVER.run(encrypted_image, evaluation_key)
|
| 65 |
+
fhe_execution_time = round(time.time() - start, 2)
|
| 66 |
|
| 67 |
+
encrypted_output_path = get_server_file_path("encrypted_output", user_id)
|
| 68 |
+
async with encrypted_output_path.open("wb") as encrypted_output_file:
|
| 69 |
+
await encrypted_output_file.write(encrypted_output)
|
| 70 |
|
| 71 |
+
return fhe_execution_time
|
|
|
|
| 72 |
|
| 73 |
+
task = asyncio.create_task(run_fhe_task())
|
| 74 |
+
return JSONResponse(content={"message": "FHE execution started", "task_id": str(id(task))})
|
| 75 |
+
except Exception as e:
|
| 76 |
+
logger.error(f"Error in run_fhe: {str(e)}")
|
| 77 |
+
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
| 78 |
|
| 79 |
@app.get("/fhe_status/{task_id}")
|
| 80 |
async def fhe_status(task_id: str):
|
| 81 |
+
"""Check the status of an FHE execution task."""
|
| 82 |
+
try:
|
| 83 |
+
for task in asyncio.all_tasks():
|
| 84 |
+
if str(id(task)) == task_id:
|
| 85 |
+
if task.done():
|
| 86 |
+
try:
|
| 87 |
+
execution_time = await task
|
| 88 |
+
return JSONResponse(content={"status": "completed", "execution_time": execution_time})
|
| 89 |
+
except Exception as e:
|
| 90 |
+
logger.error(f"Error in task execution: {str(e)}")
|
| 91 |
+
return JSONResponse(content={"status": "error", "message": str(e)})
|
| 92 |
+
else:
|
| 93 |
+
return JSONResponse(content={"status": "running"})
|
| 94 |
+
|
| 95 |
+
return JSONResponse(content={"status": "not_found"})
|
| 96 |
+
except Exception as e:
|
| 97 |
+
logger.error(f"Error in fhe_status: {str(e)}")
|
| 98 |
+
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
| 99 |
|
| 100 |
@app.post("/get_output")
|
| 101 |
+
async def get_output(
|
| 102 |
user_id: str = Form(),
|
| 103 |
):
|
| 104 |
"""Retrieve the encrypted output."""
|
| 105 |
+
try:
|
| 106 |
+
encrypted_output_path = get_server_file_path("encrypted_output", user_id)
|
| 107 |
|
| 108 |
+
async with encrypted_output_path.open("rb") as encrypted_output_file:
|
| 109 |
+
encrypted_output = await encrypted_output_file.read()
|
|
|
|
| 110 |
|
| 111 |
+
return Response(content=encrypted_output, media_type="application/octet-stream")
|
| 112 |
+
except Exception as e:
|
| 113 |
+
logger.error(f"Error in get_output: {str(e)}")
|
| 114 |
+
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|