Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
mariotawfik
commited on
Commit
β’
b59defd
1
Parent(s):
dd436d9
Create app.py
Browse files
app.py
CHANGED
@@ -49,8 +49,6 @@ eval_key = []
|
|
49 |
encodings = []
|
50 |
#User ID
|
51 |
user_id = []
|
52 |
-
#Encoded Vector
|
53 |
-
encoded_vector = []
|
54 |
|
55 |
def clean_tmp_directory():
|
56 |
# Allow 20 user keys to be stored.
|
@@ -94,8 +92,29 @@ def keygen(eval_key):
|
|
94 |
user_id = eval_key[1]
|
95 |
return eval_key[0]
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
-
def encode_quantize(test_file):
|
99 |
ugly = ['Machine', 'SizeOfOptionalHeader', 'Characteristics',
|
100 |
'MajorLinkerVersion', 'MinorLinkerVersion', 'SizeOfCode',
|
101 |
'SizeOfInitializedData', 'SizeOfUninitializedData',
|
@@ -131,7 +150,7 @@ def encode_quantize(test_file):
|
|
131 |
|
132 |
return encodings
|
133 |
|
134 |
-
def encrypt_encoded_quantize():
|
135 |
fhe_api = FHEModelClient(f"fhe_model", f".fhe_keys/{eval_key}")
|
136 |
fhe_api.load()
|
137 |
|
@@ -316,7 +335,7 @@ if __name__ == "__main__":
|
|
316 |
|
317 |
b_extract = gr.Button("π₯ Extract features and save")
|
318 |
|
319 |
-
extracted_vector = gr.
|
320 |
label="Extracted vector:",
|
321 |
max_lines=4,
|
322 |
interactive=False,
|
@@ -363,14 +382,14 @@ if __name__ == "__main__":
|
|
363 |
# Button to extract vector
|
364 |
b_extract.click(
|
365 |
fn=encode_quantize,
|
366 |
-
inputs=[file_input],
|
367 |
outputs=[extracted_vector],
|
368 |
)
|
369 |
|
370 |
# Button to encrypt file
|
371 |
b_encrypt_file.click(
|
372 |
fn=encrypt_encoded_quantize,
|
373 |
-
inputs=[],
|
374 |
outputs=[encrypted_file],
|
375 |
)
|
376 |
|
|
|
49 |
encodings = []
|
50 |
#User ID
|
51 |
user_id = []
|
|
|
|
|
52 |
|
53 |
def clean_tmp_directory():
|
54 |
# Allow 20 user keys to be stored.
|
|
|
92 |
user_id = eval_key[1]
|
93 |
return eval_key[0]
|
94 |
|
95 |
+
def safe_eval(encodings):
|
96 |
+
try:
|
97 |
+
# Debugging: Print the encodings string to understand its format
|
98 |
+
print("Original Encodings String:", encodings)
|
99 |
+
|
100 |
+
# Replace newline characters and multiple spaces
|
101 |
+
sanitized = encodings.replace('\n', ',').replace(' ', '')
|
102 |
+
sanitized = sanitized.replace(',,', ',') # Remove accidental double commas
|
103 |
+
|
104 |
+
# Ensure the string starts and ends properly for evaluation
|
105 |
+
if not sanitized.startswith("["):
|
106 |
+
sanitized = "[" + sanitized
|
107 |
+
if not sanitized.endswith("]"):
|
108 |
+
sanitized = sanitized + "]"
|
109 |
+
|
110 |
+
print("Sanitized Encodings String:", sanitized)
|
111 |
+
|
112 |
+
return eval(sanitized)
|
113 |
+
except SyntaxError as e:
|
114 |
+
raise ValueError(f"Failed to safely parse the encodings string: {e}")
|
115 |
+
|
116 |
|
117 |
+
def encode_quantize(test_file, eval_key, encodings):
|
118 |
ugly = ['Machine', 'SizeOfOptionalHeader', 'Characteristics',
|
119 |
'MajorLinkerVersion', 'MinorLinkerVersion', 'SizeOfCode',
|
120 |
'SizeOfInitializedData', 'SizeOfUninitializedData',
|
|
|
150 |
|
151 |
return encodings
|
152 |
|
153 |
+
def encrypt_encoded_quantize(encodings):
|
154 |
fhe_api = FHEModelClient(f"fhe_model", f".fhe_keys/{eval_key}")
|
155 |
fhe_api.load()
|
156 |
|
|
|
335 |
|
336 |
b_extract = gr.Button("π₯ Extract features and save")
|
337 |
|
338 |
+
extracted_vector = gr.json(
|
339 |
label="Extracted vector:",
|
340 |
max_lines=4,
|
341 |
interactive=False,
|
|
|
382 |
# Button to extract vector
|
383 |
b_extract.click(
|
384 |
fn=encode_quantize,
|
385 |
+
inputs=[file_input, eval_key_input, encodings_input],
|
386 |
outputs=[extracted_vector],
|
387 |
)
|
388 |
|
389 |
# Button to encrypt file
|
390 |
b_encrypt_file.click(
|
391 |
fn=encrypt_encoded_quantize,
|
392 |
+
inputs=[extracted_vector],
|
393 |
outputs=[encrypted_file],
|
394 |
)
|
395 |
|