Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,13 +6,12 @@ import os
|
|
| 6 |
|
| 7 |
# 1. โหลดโมเดล NER (เหมือนเดิม)
|
| 8 |
print("กำลังโหลดโมเดล...")
|
| 9 |
-
# ตรวจสอบว่ามี HF_TOKEN ใน Secrets หรือไม่
|
| 10 |
hf_token = os.getenv("HF_TOKEN")
|
| 11 |
ner_pipeline = pipeline(
|
| 12 |
"token-classification",
|
| 13 |
model="loolootech/no-name-ner-th",
|
| 14 |
device=-1,
|
| 15 |
-
token=hf_token
|
| 16 |
)
|
| 17 |
print("โมเดลพร้อมใช้งานแล้ว")
|
| 18 |
|
|
@@ -39,10 +38,10 @@ def merge_entities(ner_results):
|
|
| 39 |
return merged_entities
|
| 40 |
|
| 41 |
|
| 42 |
-
# 3.
|
| 43 |
def deidentify_single_text(text):
|
| 44 |
if pd.isna(text) or not isinstance(text, str) or not text.strip():
|
| 45 |
-
return ""
|
| 46 |
|
| 47 |
ner_results = ner_pipeline(text)
|
| 48 |
merged = merge_entities(ner_results)
|
|
@@ -55,12 +54,10 @@ def deidentify_single_text(text):
|
|
| 55 |
return redacted_text
|
| 56 |
|
| 57 |
|
| 58 |
-
# 4. [
|
| 59 |
-
def
|
| 60 |
if uploaded_file is None:
|
| 61 |
raise gr.Error("กรุณาอัปโหลดไฟล์ก่อน")
|
| 62 |
-
if not column_name:
|
| 63 |
-
raise gr.Error("กรุณาระบุ 'ชื่อคอลัมน์' ที่ต้องการตรวจสอบ")
|
| 64 |
|
| 65 |
file_path = uploaded_file.name
|
| 66 |
|
|
@@ -75,38 +72,39 @@ def process_file(uploaded_file, column_name, progress=gr.Progress(track_tqdm=Tru
|
|
| 75 |
except Exception as e:
|
| 76 |
raise gr.Error(f"ไม่สามารถอ่านไฟล์ได้: {e}")
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
|
| 80 |
-
raise gr.Error(f"ไม่พบคอลัมน์ '{column_name}' ในไฟล์ของคุณ คอลัมน์ที่มีคือ: {list(df.columns)}")
|
| 81 |
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
# สร้างไฟล์ผลลัพธ์เพื่อให้ผู้ใช้ดาวน์โหลด
|
| 90 |
-
output_filepath = "
|
| 91 |
-
|
| 92 |
-
df.to_csv(output_filepath, index=False, encoding='utf-8-sig')
|
| 93 |
|
| 94 |
-
return
|
| 95 |
|
| 96 |
|
| 97 |
-
# 5. [
|
| 98 |
iface = gr.Interface(
|
| 99 |
-
fn=
|
| 100 |
inputs=[
|
| 101 |
-
gr.File(label="อัปโหลดไฟล์ CSV หรือ Excel", file_types=[".csv", ".xlsx", ".xls"])
|
| 102 |
-
gr.Textbox(label="ชื่อคอลัมน์ที่ต้องการตรวจสอบ (Column Name)", placeholder="เช่น: note, detail, description")
|
| 103 |
],
|
| 104 |
outputs=[
|
| 105 |
-
gr.DataFrame(label="ตารางผลลัพธ์ (Output Table Preview)", wrap=True),
|
| 106 |
gr.File(label="ดาวน์โหลดผลลัพธ์ (Download Result as CSV)")
|
| 107 |
],
|
| 108 |
-
title="📁
|
| 109 |
-
description="อัปโหลดไฟล์ตาราง (CSV, Excel)
|
| 110 |
allow_flagging="never"
|
| 111 |
)
|
| 112 |
|
|
|
|
| 6 |
|
| 7 |
# 1. โหลดโมเดล NER (เหมือนเดิม)
|
| 8 |
print("กำลังโหลดโมเดล...")
|
|
|
|
| 9 |
hf_token = os.getenv("HF_TOKEN")
|
| 10 |
ner_pipeline = pipeline(
|
| 11 |
"token-classification",
|
| 12 |
model="loolootech/no-name-ner-th",
|
| 13 |
device=-1,
|
| 14 |
+
token=hf_token
|
| 15 |
)
|
| 16 |
print("โมเดลพร้อมใช้งานแล้ว")
|
| 17 |
|
|
|
|
| 38 |
return merged_entities
|
| 39 |
|
| 40 |
|
| 41 |
+
# 3. ฟังก์ชันสำหรับ De-identification ของข้อความ 1 บรรทัด (เหมือนเดิม)
|
| 42 |
def deidentify_single_text(text):
|
| 43 |
if pd.isna(text) or not isinstance(text, str) or not text.strip():
|
| 44 |
+
return ""
|
| 45 |
|
| 46 |
ner_results = ner_pipeline(text)
|
| 47 |
merged = merge_entities(ner_results)
|
|
|
|
| 54 |
return redacted_text
|
| 55 |
|
| 56 |
|
| 57 |
+
# 4. [อัปเดต] ฟังก์ชันสำหรับประมวลผลไฟล์ (ไม่ต้องรับชื่อคอลัมน์แล้ว)
|
| 58 |
+
def process_entire_file(uploaded_file, progress=gr.Progress(track_tqdm=True)):
|
| 59 |
if uploaded_file is None:
|
| 60 |
raise gr.Error("กรุณาอัปโหลดไฟล์ก่อน")
|
|
|
|
|
|
|
| 61 |
|
| 62 |
file_path = uploaded_file.name
|
| 63 |
|
|
|
|
| 72 |
except Exception as e:
|
| 73 |
raise gr.Error(f"ไม่สามารถอ่านไฟล์ได้: {e}")
|
| 74 |
|
| 75 |
+
# สร้าง DataFrame ใหม่สำหรับเก็บผลลัพธ์
|
| 76 |
+
df_redacted = df.copy()
|
|
|
|
| 77 |
|
| 78 |
+
# [Key Change] ค้นหาคอลัมน์ทั้งหมดที่มีข้อมูลเป็นประเภทข้อความ (object)
|
| 79 |
+
text_columns = df.select_dtypes(include=['object']).columns
|
| 80 |
+
|
| 81 |
+
if len(text_columns) == 0:
|
| 82 |
+
raise gr.Error("ไม่พบคอลัมน์ที่เป็นข้อมูลประเภทข้อความ (text) ในไฟล์นี้เลย")
|
| 83 |
+
|
| 84 |
+
# วนลูปและประมวลผลทุกคอลัมน์ที่หาเจอ
|
| 85 |
+
print(f"กำลังประมวลผลคอลัมน์: {list(text_columns)}")
|
| 86 |
+
for col_name in progress.tqdm(text_columns, desc="Processing text columns"):
|
| 87 |
+
df_redacted[col_name] = df[col_name].astype(str).apply(deidentify_single_text)
|
| 88 |
|
| 89 |
# สร้างไฟล์ผลลัพธ์เพื่อให้ผู้ใช้ดาวน์โหลด
|
| 90 |
+
output_filepath = "processed_output_full.csv"
|
| 91 |
+
df_redacted.to_csv(output_filepath, index=False, encoding='utf-8-sig')
|
|
|
|
| 92 |
|
| 93 |
+
return df_redacted, output_filepath
|
| 94 |
|
| 95 |
|
| 96 |
+
# 5. [อัปเดต] สร้างหน้าเว็บ Gradio (ตัดช่องใส่ชื่อคอลัมน์ออก)
|
| 97 |
iface = gr.Interface(
|
| 98 |
+
fn=process_entire_file,
|
| 99 |
inputs=[
|
| 100 |
+
gr.File(label="อัปโหลดไฟล์ CSV หรือ Excel ที่ต้องการตรวจสอบทั้งตาราง", file_types=[".csv", ".xlsx", ".xls"])
|
|
|
|
| 101 |
],
|
| 102 |
outputs=[
|
| 103 |
+
gr.DataFrame(label="ตารางผลลัพธ์ (Output Table Preview)", wrap=True, max_rows=10),
|
| 104 |
gr.File(label="ดาวน์โหลดผลลัพธ์ (Download Result as CSV)")
|
| 105 |
],
|
| 106 |
+
title="📁 Automatic Table De-identification",
|
| 107 |
+
description="อัปโหลดไฟล์ตาราง (CSV, Excel) แล้วระบบจะค้นหาคอลัมน์ที่เป็น 'ข้อความ' ทั้งหมดโดยอัตโนมัติ และทำการปกปิดข้อมูลส่วนบุคคลให้ทันที",
|
| 108 |
allow_flagging="never"
|
| 109 |
)
|
| 110 |
|