PryvX commited on
Commit
91b6074
·
verified ·
1 Parent(s): eb6c036

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from pryvx import OPRF, PHE
4
+
5
+ hash_key = OPRF.get_key()
6
+
7
+ phe1 = PHE()
8
+ public_key, private_key = phe1.keygen()
9
+
10
+
11
+ def display_csv_a():
12
+ # ADD YOUR DATASET HERE
13
+ global dataset_0
14
+ dataset_0 = pd.DataFrame({
15
+ 'location': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Miami']
16
+ })
17
+ return dataset_0.head().to_markdown()
18
+
19
+ def process_query(query):
20
+
21
+ query = query.lower()
22
+ dataset_0[dataset_0.columns[0]] = dataset_0[dataset_0.columns[0]].str.lower()
23
+
24
+ encrypted_db = []
25
+ for x in dataset_0[dataset_0.columns[0]].to_list():
26
+ encrypted_db.append(phe1.encrypt(public_key, int(dataset_0.index[dataset_0[dataset_0.columns[0]] == x][0])))
27
+
28
+ dataset_1 = pd.DataFrame({
29
+ dataset_0.columns[0]: encrypted_db
30
+ })
31
+
32
+ index_x = dataset_0.index[dataset_0[dataset_0.columns[0]] == query]
33
+
34
+ if len(index_x):
35
+
36
+ enc_query = phe1.encrypt(public_key, int(index_x[0]))
37
+
38
+ results = []
39
+ for enc_value in encrypted_db:
40
+ results.append(phe1.homomorphic_sub(enc_value, enc_query, public_key))
41
+
42
+ decrypted_results = []
43
+ for x in results:
44
+ decrypted_results.append(phe1.decrypt(public_key, private_key, x) == 0)
45
+
46
+ if sum(decrypted_results):
47
+ return enc_query, dataset_1.head().to_markdown(), "True"
48
+ else:
49
+ return enc_query, dataset_1.head().to_markdown(), "False"
50
+ else:
51
+ return "None", dataset_1.head().to_markdown(), "False"
52
+
53
+ with gr.Blocks() as app:
54
+ gr.Markdown("# Encrypted Search")
55
+ gr.Markdown("### This is demo on Private information retrieval (PIR) which is a cryptographic protocol that allows a user to retrieve a specific item from a database server without revealing which item was requested. The main goal of PIR is to protect the privacy of the user's query.")
56
+
57
+ with gr.Row():
58
+ # Party A Section
59
+ with gr.Column():
60
+ gr.Markdown("### Private Database on Server")
61
+ output_a = gr.Markdown()
62
+
63
+ input_query = gr.Textbox(label="Enter item to be searched")
64
+ search_button_a = gr.Button("Encrypted Search")
65
+
66
+ output_result = gr.Textbox(label="Item Found")
67
+ encrypted_query = gr.Textbox(label="Encrypted Input")
68
+ encrypted_db = gr.Markdown(label="Encrypted Database")
69
+
70
+ app.load(display_csv_a, inputs=None, outputs=output_a)
71
+
72
+ search_button_a.click(
73
+ process_query,
74
+ inputs=[input_query],
75
+ outputs=[encrypted_query, encrypted_db, output_result]
76
+ )
77
+
78
+ app.launch()