lieudat commited on
Commit
ac646bf
·
verified ·
1 Parent(s): 9d1c980

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -8
app.py CHANGED
@@ -26,15 +26,30 @@ def mask_phone(phone):
26
  s = str(phone).split('.')[0].strip().zfill(10)
27
  return s[:3] + '*****' + s[-2:]
28
 
29
- def hash_email(email):
30
- return hashlib.sha256(str(email).strip().lower().encode()).hexdigest()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
 
33
  # ==========================================
34
  # PHẦN 2: LUỒNG ETL CHÍNH (PIPELINE)
35
  # ==========================================
36
 
37
- def process_data(file_obj, opt_name, opt_cccd, opt_phone, opt_email):
38
  if file_obj is None:
39
  return None, None, None
40
 
@@ -56,7 +71,13 @@ def process_data(file_obj, opt_name, opt_cccd, opt_phone, opt_email):
56
  df_secured['So_Dien_Thoai'] = df_secured['So_Dien_Thoai'].apply(mask_phone)
57
 
58
  if opt_email and 'Email' in df_secured.columns:
59
- df_secured['Email'] = df_secured['Email'].apply(hash_email)
 
 
 
 
 
 
60
 
61
  df_secured_preview = df_secured.head(5).copy()
62
 
@@ -77,8 +98,8 @@ def process_data(file_obj, opt_name, opt_cccd, opt_phone, opt_email):
77
  # PHẦN 3: XÂY DỰNG GIAO DIỆN WEB VỚI GRADIO
78
  # ==========================================
79
 
80
- # Sử dụng Blocks để chia giao diện thành các cột/hàng chuyên nghiệp
81
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
82
  gr.Markdown("# Hệ Thống Ẩn Danh Hóa Dữ Liệu (PII Masking Tool)")
83
  gr.Markdown("Công cụ tự động làm sạch và che giấu thông tin định danh cá nhân trước khi trích xuất dữ liệu.")
84
 
@@ -91,7 +112,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
91
  chk_name = gr.Checkbox(label="Che khuất Họ Tên (Partial Masking)", value=True)
92
  chk_cccd = gr.Checkbox(label="Che khuất số CCCD (Giữ 3 số đầu/cuối)", value=True)
93
  chk_phone = gr.Checkbox(label="Che khuất Số Điện Thoại (Che 5 số giữa)", value=True)
94
- chk_email = gr.Checkbox(label="Băm dữ liệu Email (Thuật toán SHA-256)", value=True)
 
 
95
 
96
  btn_run = gr.Button("THỰC THI PIPELINE", variant="primary")
97
 
@@ -106,7 +129,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
106
  # Gắn sự kiện: Khi bấm nút "Thực thi", chạy hàm process_data
107
  btn_run.click(
108
  fn=process_data,
109
- inputs=[input_file, chk_name, chk_cccd, chk_phone, chk_email],
110
  outputs=[df_before, df_after, output_file]
111
  )
112
 
 
26
  s = str(phone).split('.')[0].strip().zfill(10)
27
  return s[:3] + '*****' + s[-2:]
28
 
29
+ def mask_email(email):
30
+ email_str = str(email).strip()
31
+ if '@' not in email_str:
32
+ return email_str
33
+
34
+ name_part, domain_part = email_str.split('@', 1)
35
+ # Giữ 2 ký tự đầu, nếu tên email quá ngắn (1-2 ký tự) thì giữ 1 ký tự
36
+ if len(name_part) <= 2:
37
+ masked_name = name_part[0] + '*' * (len(name_part)-1) if len(name_part) > 0 else ""
38
+ else:
39
+ masked_name = name_part[:2] + '*' * (len(name_part) - 2)
40
+
41
+ return f"{masked_name}@{domain_part}"
42
+
43
+ def hash_sha256(data):
44
+ # Dùng chung thuật toán SHA-256 cho Thẻ Tín Dụng và Mật Khẩu
45
+ return hashlib.sha256(str(data).strip().encode()).hexdigest()
46
 
47
 
48
  # ==========================================
49
  # PHẦN 2: LUỒNG ETL CHÍNH (PIPELINE)
50
  # ==========================================
51
 
52
+ def process_data(file_obj, opt_name, opt_cccd, opt_phone, opt_email, opt_credit, opt_password):
53
  if file_obj is None:
54
  return None, None, None
55
 
 
71
  df_secured['So_Dien_Thoai'] = df_secured['So_Dien_Thoai'].apply(mask_phone)
72
 
73
  if opt_email and 'Email' in df_secured.columns:
74
+ df_secured['Email'] = df_secured['Email'].apply(mask_email)
75
+
76
+ if opt_credit and 'So_The_Tin_Dung' in df_secured.columns:
77
+ df_secured['So_The_Tin_Dung'] = df_secured['So_The_Tin_Dung'].apply(hash_sha256)
78
+
79
+ if opt_password and 'Mat_Khau' in df_secured.columns:
80
+ df_secured['Mat_Khau'] = df_secured['Mat_Khau'].apply(hash_sha256)
81
 
82
  df_secured_preview = df_secured.head(5).copy()
83
 
 
98
  # PHẦN 3: XÂY DỰNG GIAO DIỆN WEB VỚI GRADIO
99
  # ==========================================
100
 
101
+ # Xóa theme để dùng theme mặc định (tránh lỗi version như lần trước)
102
+ with gr.Blocks() as demo:
103
  gr.Markdown("# Hệ Thống Ẩn Danh Hóa Dữ Liệu (PII Masking Tool)")
104
  gr.Markdown("Công cụ tự động làm sạch và che giấu thông tin định danh cá nhân trước khi trích xuất dữ liệu.")
105
 
 
112
  chk_name = gr.Checkbox(label="Che khuất Họ Tên (Partial Masking)", value=True)
113
  chk_cccd = gr.Checkbox(label="Che khuất số CCCD (Giữ 3 số đầu/cuối)", value=True)
114
  chk_phone = gr.Checkbox(label="Che khuất Số Điện Thoại (Che 5 số giữa)", value=True)
115
+ chk_email = gr.Checkbox(label="Che khuất Email (Giữ 2 ký tự đầu và tên miền)", value=True)
116
+ chk_credit = gr.Checkbox(label="Băm Số Thẻ Tín Dụng (Thuật toán SHA-256)", value=True)
117
+ chk_password = gr.Checkbox(label="Băm Mật Khẩu (Thuật toán SHA-256)", value=True)
118
 
119
  btn_run = gr.Button("THỰC THI PIPELINE", variant="primary")
120
 
 
129
  # Gắn sự kiện: Khi bấm nút "Thực thi", chạy hàm process_data
130
  btn_run.click(
131
  fn=process_data,
132
+ inputs=[input_file, chk_name, chk_cccd, chk_phone, chk_email, chk_credit, chk_password],
133
  outputs=[df_before, df_after, output_file]
134
  )
135