Update app.py
Browse files
app.py
CHANGED
|
@@ -89,13 +89,13 @@ def key_gen_fn() -> Dict:
|
|
| 89 |
|
| 90 |
if not evaluation_key_path.is_file():
|
| 91 |
error_message = (
|
| 92 |
-
f"
|
| 93 |
)
|
| 94 |
print(error_message)
|
| 95 |
return {gen_key_btn: gr.update(value=error_message)}
|
| 96 |
else:
|
| 97 |
print("Keys have been generated ✅")
|
| 98 |
-
return {gen_key_btn: gr.update(value="
|
| 99 |
|
| 100 |
|
| 101 |
def encrypt_doc_fn(doc):
|
|
@@ -103,7 +103,7 @@ def encrypt_doc_fn(doc):
|
|
| 103 |
print(f"\n------------ Step 2.1: Doc encryption: {doc=}")
|
| 104 |
|
| 105 |
if not (KEYS_DIR / f"{USER_ID}/evaluation_key").is_file():
|
| 106 |
-
return {encrypted_doc_box: gr.update(value="Error ❌:
|
| 107 |
|
| 108 |
# Retrieve the client API
|
| 109 |
client = FHEModelClient(path_dir=DEPLOYMENT_DIR, key_dir=KEYS_DIR / f"{USER_ID}")
|
|
@@ -138,14 +138,13 @@ def encrypt_query_fn(query):
|
|
| 138 |
print(f"\n------------ Step 2: Query encryption: {query=}")
|
| 139 |
|
| 140 |
if not (KEYS_DIR / f"{USER_ID}/evaluation_key").is_file():
|
| 141 |
-
return {output_encrypted_box: gr.update(value="Error ❌:
|
| 142 |
|
| 143 |
if is_user_query_valid(query):
|
| 144 |
return {
|
| 145 |
query_box: gr.update(
|
| 146 |
value=(
|
| 147 |
-
"
|
| 148 |
-
"outside the scope of this document. Please refine your query."
|
| 149 |
)
|
| 150 |
)
|
| 151 |
}
|
|
@@ -174,7 +173,7 @@ def encrypt_query_fn(query):
|
|
| 174 |
|
| 175 |
encrypted_tokens.append(encrypted_x)
|
| 176 |
|
| 177 |
-
print("
|
| 178 |
|
| 179 |
assert len({len(token) for token in encrypted_tokens}) == 1
|
| 180 |
|
|
@@ -203,15 +202,15 @@ def send_input_fn(query) -> Dict:
|
|
| 203 |
|
| 204 |
if not evaluation_key_path.is_file():
|
| 205 |
error_message = (
|
| 206 |
-
"
|
| 207 |
-
f"
|
| 208 |
)
|
| 209 |
return {anonymized_query_output: gr.update(value=error_message)}
|
| 210 |
|
| 211 |
if not encrypted_input_path.is_file():
|
| 212 |
error_message = (
|
| 213 |
-
"
|
| 214 |
-
f"
|
| 215 |
)
|
| 216 |
return {anonymized_query_output: gr.update(value=error_message)}
|
| 217 |
|
|
@@ -232,7 +231,7 @@ def send_input_fn(query) -> Dict:
|
|
| 232 |
data=data,
|
| 233 |
files=files,
|
| 234 |
) as resp:
|
| 235 |
-
print("
|
| 236 |
|
| 237 |
|
| 238 |
def run_fhe_in_server_fn() -> Dict:
|
|
@@ -278,7 +277,7 @@ def run_fhe_in_server_fn() -> Dict:
|
|
| 278 |
}
|
| 279 |
else:
|
| 280 |
time.sleep(1)
|
| 281 |
-
print(f"
|
| 282 |
|
| 283 |
|
| 284 |
def get_output_fn() -> Dict:
|
|
@@ -310,7 +309,7 @@ def get_output_fn() -> Dict:
|
|
| 310 |
data=data,
|
| 311 |
) as response:
|
| 312 |
if response.ok:
|
| 313 |
-
print("
|
| 314 |
response_data = response.json()
|
| 315 |
encrypted_output_base64 = response_data["encrypted_output"]
|
| 316 |
length_encrypted_output_base64 = response_data["length"]
|
|
@@ -397,7 +396,7 @@ def decrypt_fn(text) -> Dict:
|
|
| 397 |
else:
|
| 398 |
identified_df = pd.DataFrame(columns=["Identified Words", "Probability"])
|
| 399 |
|
| 400 |
-
print("
|
| 401 |
|
| 402 |
return anonymized_text, identified_df
|
| 403 |
|
|
|
|
| 89 |
|
| 90 |
if not evaluation_key_path.is_file():
|
| 91 |
error_message = (
|
| 92 |
+
f"生成密钥时发生异常 {evaluation_key_path.is_file()=}"
|
| 93 |
)
|
| 94 |
print(error_message)
|
| 95 |
return {gen_key_btn: gr.update(value=error_message)}
|
| 96 |
else:
|
| 97 |
print("Keys have been generated ✅")
|
| 98 |
+
return {gen_key_btn: gr.update(value="密钥生成成功! ✅")}
|
| 99 |
|
| 100 |
|
| 101 |
def encrypt_doc_fn(doc):
|
|
|
|
| 103 |
print(f"\n------------ Step 2.1: Doc encryption: {doc=}")
|
| 104 |
|
| 105 |
if not (KEYS_DIR / f"{USER_ID}/evaluation_key").is_file():
|
| 106 |
+
return {encrypted_doc_box: gr.update(value="Error ❌: 请先生成密钥!", lines=10)}
|
| 107 |
|
| 108 |
# Retrieve the client API
|
| 109 |
client = FHEModelClient(path_dir=DEPLOYMENT_DIR, key_dir=KEYS_DIR / f"{USER_ID}")
|
|
|
|
| 138 |
print(f"\n------------ Step 2: Query encryption: {query=}")
|
| 139 |
|
| 140 |
if not (KEYS_DIR / f"{USER_ID}/evaluation_key").is_file():
|
| 141 |
+
return {output_encrypted_box: gr.update(value="Error ❌: 请先生成密钥!", lines=8)}
|
| 142 |
|
| 143 |
if is_user_query_valid(query):
|
| 144 |
return {
|
| 145 |
query_box: gr.update(
|
| 146 |
value=(
|
| 147 |
+
"不能执行 ❌: 请求超过了长度限制。修改查询后重试。 "
|
|
|
|
| 148 |
)
|
| 149 |
)
|
| 150 |
}
|
|
|
|
| 173 |
|
| 174 |
encrypted_tokens.append(encrypted_x)
|
| 175 |
|
| 176 |
+
print("数据已在客户端加密。 ✅")
|
| 177 |
|
| 178 |
assert len({len(token) for token in encrypted_tokens}) == 1
|
| 179 |
|
|
|
|
| 202 |
|
| 203 |
if not evaluation_key_path.is_file():
|
| 204 |
error_message = (
|
| 205 |
+
"发送数据到服务器时发生异常:"
|
| 206 |
+
f"密钥已经正常生成 - {evaluation_key_path.is_file()=}"
|
| 207 |
)
|
| 208 |
return {anonymized_query_output: gr.update(value=error_message)}
|
| 209 |
|
| 210 |
if not encrypted_input_path.is_file():
|
| 211 |
error_message = (
|
| 212 |
+
"发送数据到服务器时发生异常: 数据没有加密"
|
| 213 |
+
f"在客户端正确 - {encrypted_input_path.is_file()=}"
|
| 214 |
)
|
| 215 |
return {anonymized_query_output: gr.update(value=error_message)}
|
| 216 |
|
|
|
|
| 231 |
data=data,
|
| 232 |
files=files,
|
| 233 |
) as resp:
|
| 234 |
+
print("数据发送到服务器。 ✅" if resp.ok else "发送到服务器时出现错误。 ❌ ")
|
| 235 |
|
| 236 |
|
| 237 |
def run_fhe_in_server_fn() -> Dict:
|
|
|
|
| 277 |
}
|
| 278 |
else:
|
| 279 |
time.sleep(1)
|
| 280 |
+
print(f"匿名化查询在以每句柄{response.json():.2f} 秒的速率执行。")
|
| 281 |
|
| 282 |
|
| 283 |
def get_output_fn() -> Dict:
|
|
|
|
| 309 |
data=data,
|
| 310 |
) as response:
|
| 311 |
if response.ok:
|
| 312 |
+
print("数据从远程服务器接收到。 ✅")
|
| 313 |
response_data = response.json()
|
| 314 |
encrypted_output_base64 = response_data["encrypted_output"]
|
| 315 |
length_encrypted_output_base64 = response_data["length"]
|
|
|
|
| 396 |
else:
|
| 397 |
identified_df = pd.DataFrame(columns=["Identified Words", "Probability"])
|
| 398 |
|
| 399 |
+
print("在客户端完成了解密。 ✅")
|
| 400 |
|
| 401 |
return anonymized_text, identified_df
|
| 402 |
|