80cols commited on
Commit
700bd71
·
verified ·
1 Parent(s): ad51e63

Delete server.py

Browse files
Files changed (1) hide show
  1. server.py +0 -105
server.py DELETED
@@ -1,105 +0,0 @@
1
- """Server that will listen for GET and POST requests from the client."""
2
-
3
- import base64
4
- import time
5
- from typing import List
6
-
7
- import numpy
8
- from fastapi import FastAPI, File, Form, UploadFile
9
- from fastapi.responses import JSONResponse
10
- from utils_demo import *
11
- from utils_demo import SERVER_DIR
12
-
13
- from concrete.ml.deployment import FHEModelServer
14
-
15
- # Load the FHE server
16
- FHE_SERVER = FHEModelServer(DEPLOYMENT_DIR)
17
-
18
- # Initialize an instance of FastAPI
19
- app = FastAPI()
20
-
21
- # Define the default route
22
- @app.get("/")
23
- def root():
24
- """
25
- Root endpoint of the health prediction API.
26
- Returns:
27
- dict: The welcome message.
28
- """
29
- return {"message": "Welcome to your encrypted anonymization use-case with FHE!"}
30
-
31
-
32
- @app.post("/send_input")
33
- def send_input(
34
- user_id: str = Form(),
35
- files: List[UploadFile] = File(),
36
- ):
37
- """Send the inputs to the server."""
38
-
39
- # Save the files using the above paths
40
- write_bytes(SERVER_DIR / f"{user_id}_valuation_key", files[0].file.read())
41
- write_bytes(SERVER_DIR / f"{user_id}_encrypted_input", files[1].file.read())
42
- write_bytes(SERVER_DIR / f"{user_id}_encrypted_len_input", files[2].file.read())
43
-
44
-
45
- @app.post("/run_fhe")
46
- def run_fhe(
47
- user_id: str = Form(),
48
- ):
49
- """Inference in FHE."""
50
-
51
- evaluation_key_path = SERVER_DIR / f"{user_id}_valuation_key"
52
- encrypted_input_path = SERVER_DIR / f"{user_id}_encrypted_input"
53
- encrypted_input_len_path = SERVER_DIR / f"{user_id}_encrypted_len_input"
54
-
55
- # Read the files (Evaluation key + Encrypted symptoms) using the above paths
56
- with encrypted_input_path.open("rb") as encrypted_output_file, evaluation_key_path.open(
57
- "rb"
58
- ) as evaluation_key_file, encrypted_input_len_path.open("rb") as lenght:
59
- evaluation_key = evaluation_key_file.read()
60
- encrypted_tokens = encrypted_output_file.read()
61
- length = int.from_bytes(lenght.read(), "big")
62
-
63
- timing, encrypted_output = [], []
64
- for i in range(0, len(encrypted_tokens), length):
65
- enc_x = encrypted_tokens[i : i + length]
66
- start_time = time.time()
67
- enc_y = FHE_SERVER.run(enc_x, evaluation_key)
68
- timing.append(round(time.time() - start_time, 2))
69
- encrypted_output.append(enc_y)
70
-
71
- # Write the files
72
- write_bytes(SERVER_DIR / f"{user_id}_encrypted_output", b"".join(encrypted_output))
73
- write_bytes(
74
- SERVER_DIR / f"{user_id}_encrypted_output_len", len(encrypted_output[0]).to_bytes(10, "big")
75
- )
76
-
77
- return JSONResponse(content=numpy.mean(timing))
78
-
79
-
80
- @app.post("/get_output")
81
- def get_output(user_id: str = Form()):
82
- """Retrieve the encrypted output from the server."""
83
-
84
- # Path where the encrypted output is saved
85
- encrypted_output_path = SERVER_DIR / f"{user_id}_encrypted_output"
86
- encrypted_output_len_path = SERVER_DIR / f"{user_id}_encrypted_output_len"
87
-
88
- # Read the file using the above path
89
- with encrypted_output_path.open("rb") as f:
90
- encrypted_output = f.read()
91
-
92
- # Read the file using the above path
93
- with encrypted_output_len_path.open("rb") as f:
94
- length = f.read()
95
-
96
- time.sleep(1)
97
-
98
- # Encode the binary data to a format suitable for JSON serialization
99
- content = {
100
- "encrypted_output": base64.b64encode(encrypted_output).decode("utf-8"),
101
- "length": base64.b64encode(length).decode("utf-8"),
102
- }
103
-
104
- # Send the encrypted output
105
- return JSONResponse(content)