mariotawfik commited on
Commit
31692b0
Β·
verified Β·
1 Parent(s): 099486b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -19
app.py CHANGED
@@ -1,8 +1,4 @@
1
  import gradio as gr
2
- import time
3
- import os
4
- from pathlib import Path
5
-
6
  import subprocess
7
  from concrete.ml.deployment import FHEModelClient
8
  from requests import head
@@ -12,13 +8,13 @@ from pathlib import Path
12
  import requests
13
  import json
14
  import base64
15
- import subprocess
16
  import shutil
17
  import time
18
  import pandas as pd
19
  import pickle
20
  import numpy as np
21
  import pdb
 
22
  # This repository's directory
23
  REPO_DIR = Path(__file__).parent
24
  subprocess.Popen(["uvicorn", "server:app"], cwd=REPO_DIR)
@@ -72,7 +68,7 @@ def clean_tmp_directory():
72
  file.unlink()
73
 
74
 
75
- def keygen(eval_key):
76
  # Clean tmp directory if needed
77
  clean_tmp_directory()
78
 
@@ -89,8 +85,8 @@ def keygen(eval_key):
89
 
90
  numpy.save(f"tmp/tmp_evaluation_key_{user_id}.npy", evaluation_key)
91
 
92
- eval_key = [list(evaluation_key)[:ENCRYPTED_DATA_BROWSER_LIMIT], user_id]
93
- return eval_key[0]
94
 
95
  def encode_quantize(test_file, eval_key, encodings):
96
  ugly = ['Machine', 'SizeOfOptionalHeader', 'Characteristics',
@@ -128,8 +124,7 @@ def encode_quantize(test_file, eval_key, encodings):
128
 
129
  return encodings
130
 
131
- def encrypt_encoded_quantize(encodings, eval_key):
132
- global user_id
133
  fhe_api = FHEModelClient(f"fhe_model", f".fhe_keys/{user_id}")
134
  fhe_api.load()
135
 
@@ -147,8 +142,7 @@ def encrypt_encoded_quantize(encodings, eval_key):
147
  encrypted_quantized_encoding_shorten_hex = "".join(f"{i:02x}" for i in encrypted_quantized_encoding_shorten)
148
  return (encodings,quantized_encodings,encrypted_quantized_encoding_shorten_hex)
149
 
150
- def run_fhe():
151
- global user_id
152
  encoded_data_path = Path(f"tmp/tmp_encrypted_quantized_encoding_{user_id}.npy")
153
  encrypted_quantized_encoding = numpy.load(encoded_data_path)
154
 
@@ -180,8 +174,7 @@ def run_fhe():
180
  return encrypted_prediction_shorten_hex
181
 
182
 
183
- def decrypt_prediction():
184
- global user_id
185
  encoded_data_path = Path(f"tmp/tmp_encrypted_prediction_{user_id}.npy")
186
 
187
  # Read encrypted_prediction from the file
@@ -351,11 +344,11 @@ if __name__ == "__main__":
351
  "The encrypted scan result is sent back to the client, who can finally decrypt it with their private key. Only the client is aware of the original file content and the scan result."
352
  )
353
  b_decrypt_result = gr.Button("πŸ” Decrypt scan result")
 
354
  scan_result = gr.Textbox(label="Scan Result:")
355
-
356
  eval_key_input = gr.Textbox(value=eval_key, visible=False)
357
  # Button for key generation
358
- b_gen_key.click(fn=keygen, inputs=[eval_key_input], outputs=[evaluation_key])
359
 
360
  encodings_input = gr.Textbox(value=encodings, visible=False)
361
  # Button to extract vector
@@ -368,15 +361,15 @@ if __name__ == "__main__":
368
  # Button to encrypt file
369
  b_encrypt_file.click(
370
  fn=encrypt_encoded_quantize,
371
- inputs=[extracted_vector, eval_key_input],
372
  outputs=[encrypted_file],
373
  )
374
 
375
  # Button to run FHE-based malware scan
376
- b_run_fhe_scan.click(fn=run_fhe, inputs=[], outputs=[encrypted_scan_result])
377
 
378
  # Button to decrypt the scan result
379
- b_decrypt_result.click(fn=decrypt_prediction, inputs=[], outputs=[scan_result])
380
 
381
  gr.Markdown(
382
  "ClairVault is built using advanced Fully Homomorphic Encryption techniques to ensure your data remains private and secure throughout the entire malware scanning process."
 
1
  import gradio as gr
 
 
 
 
2
  import subprocess
3
  from concrete.ml.deployment import FHEModelClient
4
  from requests import head
 
8
  import requests
9
  import json
10
  import base64
 
11
  import shutil
12
  import time
13
  import pandas as pd
14
  import pickle
15
  import numpy as np
16
  import pdb
17
+
18
  # This repository's directory
19
  REPO_DIR = Path(__file__).parent
20
  subprocess.Popen(["uvicorn", "server:app"], cwd=REPO_DIR)
 
68
  file.unlink()
69
 
70
 
71
+ def keygen(eval_key, user_id):
72
  # Clean tmp directory if needed
73
  clean_tmp_directory()
74
 
 
85
 
86
  numpy.save(f"tmp/tmp_evaluation_key_{user_id}.npy", evaluation_key)
87
 
88
+ eval_key = list(evaluation_key)[:ENCRYPTED_DATA_BROWSER_LIMIT]
89
+ return eval_key, user_id
90
 
91
  def encode_quantize(test_file, eval_key, encodings):
92
  ugly = ['Machine', 'SizeOfOptionalHeader', 'Characteristics',
 
124
 
125
  return encodings
126
 
127
+ def encrypt_encoded_quantize(encodings, user_id, eval_key):
 
128
  fhe_api = FHEModelClient(f"fhe_model", f".fhe_keys/{user_id}")
129
  fhe_api.load()
130
 
 
142
  encrypted_quantized_encoding_shorten_hex = "".join(f"{i:02x}" for i in encrypted_quantized_encoding_shorten)
143
  return (encodings,quantized_encodings,encrypted_quantized_encoding_shorten_hex)
144
 
145
+ def run_fhe(user_id):
 
146
  encoded_data_path = Path(f"tmp/tmp_encrypted_quantized_encoding_{user_id}.npy")
147
  encrypted_quantized_encoding = numpy.load(encoded_data_path)
148
 
 
174
  return encrypted_prediction_shorten_hex
175
 
176
 
177
+ def decrypt_prediction(user_id):
 
178
  encoded_data_path = Path(f"tmp/tmp_encrypted_prediction_{user_id}.npy")
179
 
180
  # Read encrypted_prediction from the file
 
344
  "The encrypted scan result is sent back to the client, who can finally decrypt it with their private key. Only the client is aware of the original file content and the scan result."
345
  )
346
  b_decrypt_result = gr.Button("πŸ” Decrypt scan result")
347
+ user_id_input = gr.Number(visible=False)
348
  scan_result = gr.Textbox(label="Scan Result:")
 
349
  eval_key_input = gr.Textbox(value=eval_key, visible=False)
350
  # Button for key generation
351
+ b_gen_key.click(fn=keygen, inputs=[eval_key_input, user_id_input], outputs=[evaluation_key, user_id_input])
352
 
353
  encodings_input = gr.Textbox(value=encodings, visible=False)
354
  # Button to extract vector
 
361
  # Button to encrypt file
362
  b_encrypt_file.click(
363
  fn=encrypt_encoded_quantize,
364
+ inputs=[extracted_vector, user_id_input, eval_key_input],
365
  outputs=[encrypted_file],
366
  )
367
 
368
  # Button to run FHE-based malware scan
369
+ b_run_fhe_scan.click(fn=run_fhe, inputs=[user_id_input], outputs=[encrypted_scan_result])
370
 
371
  # Button to decrypt the scan result
372
+ b_decrypt_result.click(fn=decrypt_prediction, inputs=[user_id_input], outputs=[scan_result])
373
 
374
  gr.Markdown(
375
  "ClairVault is built using advanced Fully Homomorphic Encryption techniques to ensure your data remains private and secure throughout the entire malware scanning process."