Spaces:
Runtime error
Runtime error
redo code
Browse files
app.py
CHANGED
|
@@ -1,42 +1,56 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
from phe import paillier
|
| 4 |
|
| 5 |
# Generate Paillier keypair
|
| 6 |
public_key, private_key = paillier.generate_paillier_keypair(n_length=1024)
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#data
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
print("Secure Encoding -- data owner")
|
| 13 |
-
print("===============================")
|
| 14 |
-
|
| 15 |
-
# Input data as a text box
|
| 16 |
-
data = gr.Textbox.input(label="Enter patient smoking status (smoker/non-smoker):", type="string")
|
| 17 |
-
|
| 18 |
-
# capture the user input
|
| 19 |
-
user_input = data.value
|
| 20 |
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
|
| 27 |
-
print("
|
| 28 |
-
print("===================================")
|
|
|
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
print(
|
|
|
|
| 36 |
|
|
|
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
iface = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
# Launch the
|
| 42 |
-
iface.launch(
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from phe import paillier
|
| 3 |
|
| 4 |
# Generate Paillier keypair
|
| 5 |
public_key, private_key = paillier.generate_paillier_keypair(n_length=1024)
|
| 6 |
|
| 7 |
+
def secure_encoding(input_data):
|
| 8 |
+
print("Secure Encoding -- data owner")
|
| 9 |
+
print("===============================")
|
| 10 |
|
| 11 |
+
# Convert input data to a list
|
| 12 |
+
data = input_data.split(', ')
|
| 13 |
+
print("Input Data:", data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Encode data
|
| 16 |
+
encoded_data = [1 if val.split(':')[1] == "1" else 0 for val in data]
|
| 17 |
+
print("Encoded Data:", encoded_data)
|
| 18 |
|
| 19 |
+
# Encrypt and send to cloud
|
| 20 |
+
print("Encrypting data and send to cloud...")
|
| 21 |
+
print("===================================")
|
| 22 |
|
| 23 |
+
encrypted_data = [public_key.encrypt(x) for x in encoded_data]
|
| 24 |
|
| 25 |
+
print("Third party only sees encrypted data and cannot figure out the nature of the data or encoding")
|
| 26 |
+
print("=========================================")
|
| 27 |
+
print("Encrypted Ciphertext of the First Element:", encrypted_data[0].ciphertext())
|
| 28 |
|
| 29 |
+
# Perform homomorphic addition to count smokers
|
| 30 |
+
encrypted_count = encrypted_data[0]
|
| 31 |
+
for i in range(1, len(encrypted_data)):
|
| 32 |
+
encrypted_count += encrypted_data[i]
|
| 33 |
|
| 34 |
+
# Output encrypted count
|
| 35 |
+
print("Result is also encrypted")
|
| 36 |
+
print("========================")
|
| 37 |
+
print("Encrypted Count:", encrypted_count)
|
| 38 |
|
| 39 |
+
# Send result to the data owner
|
| 40 |
+
decrypted_count = private_key.decrypt(encrypted_count)
|
| 41 |
+
print("Send result to the data owner")
|
| 42 |
+
print("Decrypted Count:", decrypted_count)
|
| 43 |
|
| 44 |
+
return f"Encrypted Ciphertext: {encrypted_data[0].ciphertext()}", f"Encrypted Count: {encrypted_count}", f"Decrypted Count: {decrypted_count}"
|
| 45 |
|
| 46 |
+
# Gradio Interface
|
| 47 |
+
iface = gr.Interface(
|
| 48 |
+
fn=secure_encoding,
|
| 49 |
+
inputs="text",
|
| 50 |
+
outputs=["text", "text", "text"],
|
| 51 |
+
live=True,
|
| 52 |
+
capture_session=True
|
| 53 |
+
)
|
| 54 |
|
| 55 |
+
# Launch the Gradio app
|
| 56 |
+
iface.launch()
|