Spaces:
Build error
Build error
Download app.py from PryvX/Encrypted-Search: direct link, hf CLI and curl.
- Browser
- Download file 2.64 kB
-
https://huggingface.co/spaces/PryvX/Encrypted-Search/resolve/main/app.py
- Command line
-
hf download hf://spaces/PryvX/Encrypted-Search/app.py
-
curl -L -o app.py https://huggingface.co/spaces/PryvX/Encrypted-Search/resolve/main/app.py
2.64 kB
| import gradio as gr | |
| import pandas as pd | |
| from pryvx import OPRF, PHE | |
| hash_key = OPRF.get_key() | |
| phe1 = PHE() | |
| public_key, private_key = phe1.keygen() | |
| def display_csv_a(): | |
| # ADD YOUR DATASET HERE | |
| global dataset_0 | |
| dataset_0 = pd.DataFrame({ | |
| 'location': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Miami'] | |
| }) | |
| return dataset_0.head().to_markdown() | |
| def process_query(query): | |
| query = query.lower() | |
| dataset_0[dataset_0.columns[0]] = dataset_0[dataset_0.columns[0]].str.lower() | |
| encrypted_db = [] | |
| for x in dataset_0[dataset_0.columns[0]].to_list(): | |
| encrypted_db.append(phe1.encrypt(public_key, int(dataset_0.index[dataset_0[dataset_0.columns[0]] == x][0]))) | |
| dataset_1 = pd.DataFrame({ | |
| dataset_0.columns[0]: encrypted_db | |
| }) | |
| index_x = dataset_0.index[dataset_0[dataset_0.columns[0]] == query] | |
| if len(index_x): | |
| enc_query = phe1.encrypt(public_key, int(index_x[0])) | |
| results = [] | |
| for enc_value in encrypted_db: | |
| results.append(phe1.homomorphic_sub(enc_value, enc_query, public_key)) | |
| decrypted_results = [] | |
| for x in results: | |
| decrypted_results.append(phe1.decrypt(public_key, private_key, x) == 0) | |
| if sum(decrypted_results): | |
| return enc_query, dataset_1.head().to_markdown(), "True" | |
| else: | |
| return enc_query, dataset_1.head().to_markdown(), "False" | |
| else: | |
| return "None", dataset_1.head().to_markdown(), "False" | |
| with gr.Blocks() as app: | |
| gr.Markdown("# Encrypted Search") | |
| 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.") | |
| with gr.Row(): | |
| # Party A Section | |
| with gr.Column(): | |
| gr.Markdown("### Private Database on Server") | |
| output_a = gr.Markdown() | |
| input_query = gr.Textbox(label="Enter item to be searched") | |
| search_button_a = gr.Button("Encrypted Search") | |
| output_result = gr.Textbox(label="Item Found") | |
| encrypted_query = gr.Textbox(label="Encrypted Input") | |
| encrypted_db = gr.Markdown(label="Encrypted Database") | |
| app.load(display_csv_a, inputs=None, outputs=output_a) | |
| search_button_a.click( | |
| process_query, | |
| inputs=[input_query], | |
| outputs=[encrypted_query, encrypted_db, output_result] | |
| ) | |
| app.launch() |