Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
|
@@ -27,7 +30,56 @@ LABEL_MAP = {
|
|
| 27 |
"LAN": "Bahasa",
|
| 28 |
}
|
| 29 |
|
| 30 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
EXAMPLES = [
|
| 32 |
(
|
| 33 |
"Contoh 1 β RUPS & Dana Cadangan",
|
|
@@ -70,44 +122,71 @@ EXAMPLES = [
|
|
| 70 |
),
|
| 71 |
]
|
| 72 |
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
def run_ner(text: str):
|
| 75 |
if not text or not text.strip():
|
| 76 |
-
return "<p style='color:
|
| 77 |
|
| 78 |
results = ner(text.strip())
|
| 79 |
if not results:
|
| 80 |
-
return "<p style='color:
|
| 81 |
|
| 82 |
rows_html = ""
|
| 83 |
row_num = 1
|
| 84 |
for ent in results:
|
| 85 |
raw_label = ent["entity_group"]
|
| 86 |
-
label_id =
|
| 87 |
-
label_id = label_id.split("-")[-1].upper().strip()
|
| 88 |
label_idn = LABEL_MAP.get(label_id, raw_label)
|
| 89 |
-
word = ent["word"]
|
| 90 |
-
|
| 91 |
if not word:
|
| 92 |
continue
|
| 93 |
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
rows_html += f"""
|
| 96 |
-
<tr style="background:{
|
| 97 |
-
<td style="padding:
|
| 98 |
-
|
| 99 |
-
<td style="padding:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
</tr>"""
|
| 101 |
row_num += 1
|
| 102 |
|
| 103 |
table_html = f"""
|
| 104 |
-
<div style="overflow-x:auto; margin-top:
|
| 105 |
-
|
|
|
|
|
|
|
| 106 |
<thead>
|
| 107 |
-
<tr style="background:#
|
| 108 |
-
<th style="padding:
|
| 109 |
-
|
| 110 |
-
<th style="padding:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
</tr>
|
| 112 |
</thead>
|
| 113 |
<tbody>{rows_html}</tbody>
|
|
@@ -116,86 +195,445 @@ def run_ner(text: str):
|
|
| 116 |
return table_html
|
| 117 |
|
| 118 |
|
| 119 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
CUSTOM_CSS = """
|
| 121 |
-
|
| 122 |
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
.example-btn {
|
| 126 |
display: block !important;
|
| 127 |
width: 100% !important;
|
| 128 |
text-align: left !important;
|
| 129 |
padding: 12px 16px !important;
|
| 130 |
margin-bottom: 8px !important;
|
| 131 |
-
background:
|
| 132 |
-
border: 1px solid
|
| 133 |
-
border-radius:
|
| 134 |
cursor: pointer !important;
|
| 135 |
font-size: 13px !important;
|
| 136 |
-
line-height: 1.
|
| 137 |
-
color: #
|
| 138 |
white-space: normal !important;
|
| 139 |
height: auto !important;
|
|
|
|
|
|
|
| 140 |
}
|
| 141 |
.example-btn:hover {
|
| 142 |
-
background:
|
| 143 |
-
border-color: #
|
|
|
|
|
|
|
| 144 |
}
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
|
|
|
| 148 |
color: white !important;
|
| 149 |
-
font-weight:
|
| 150 |
font-size: 15px !important;
|
| 151 |
-
border-radius:
|
| 152 |
-
padding:
|
| 153 |
-
margin-top:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
}
|
| 155 |
-
#analyze-btn:hover { background: #2563eb !important; }
|
| 156 |
"""
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
btn = gr.Button(f"{title}\n\n{body}", elem_classes="example-btn")
|
| 174 |
-
example_btns.append((btn, body))
|
| 175 |
|
| 176 |
-
gr.
|
| 177 |
-
text_input = gr.Textbox(
|
| 178 |
-
lines=8,
|
| 179 |
-
placeholder="Ketik atau tempel teks di siniβ¦",
|
| 180 |
-
label="",
|
| 181 |
-
show_label=False,
|
| 182 |
-
)
|
| 183 |
-
analyze_btn = gr.Button("Lakukan Analisis", elem_id="analyze-btn")
|
| 184 |
|
| 185 |
-
gr.
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
for btn, body in example_btns:
|
| 192 |
btn.click(fn=lambda b=body: b, inputs=[], outputs=text_input)
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
if __name__ == "__main__":
|
| 201 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import fitz
|
| 3 |
+
import tempfile
|
| 4 |
import gradio as gr
|
| 5 |
from transformers import pipeline
|
| 6 |
|
|
|
|
| 30 |
"LAN": "Bahasa",
|
| 31 |
}
|
| 32 |
|
| 33 |
+
# Color palette per label
|
| 34 |
+
# Each label gets a distinct, vivid but elegant highlight color
|
| 35 |
+
LABEL_COLORS = {
|
| 36 |
+
"PER": (1.0, 0.75, 0.75), # rose
|
| 37 |
+
"ORG": (0.68, 0.85, 1.0), # sky blue
|
| 38 |
+
"NOR": (0.55, 0.78, 1.0), # cornflower
|
| 39 |
+
"LOC": (0.72, 1.0, 0.72), # mint green
|
| 40 |
+
"GPE": (0.60, 0.95, 0.80), # seafoam
|
| 41 |
+
"FAC": (0.85, 1.0, 0.65), # lime
|
| 42 |
+
"DAT": (1.0, 0.90, 0.55), # amber
|
| 43 |
+
"TIM": (1.0, 0.80, 0.40), # orange-gold
|
| 44 |
+
"MON": (0.80, 1.0, 0.85), # pale green
|
| 45 |
+
"CRD": (0.95, 0.80, 1.0), # lavender
|
| 46 |
+
"ORD": (0.88, 0.75, 1.0), # violet
|
| 47 |
+
"PRC": (1.0, 0.95, 0.70), # pale yellow
|
| 48 |
+
"QTY": (0.78, 0.95, 0.95), # cyan-light
|
| 49 |
+
"LAW": (1.0, 0.72, 0.72), # salmon
|
| 50 |
+
"EVT": (1.0, 0.85, 0.65), # peach
|
| 51 |
+
"PRD": (0.75, 0.90, 1.0), # baby blue
|
| 52 |
+
"REG": (0.90, 0.85, 1.0), # periwinkle
|
| 53 |
+
"WOA": (1.0, 0.90, 0.85), # blush
|
| 54 |
+
"LAN": (0.80, 1.0, 0.95), # aqua
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
# CSS hex equivalents for the legend table
|
| 58 |
+
LABEL_HEX = {
|
| 59 |
+
"PER": "#FFBFBF",
|
| 60 |
+
"ORG": "#AEDDFF",
|
| 61 |
+
"NOR": "#8DC7FF",
|
| 62 |
+
"LOC": "#B8FFB8",
|
| 63 |
+
"GPE": "#99F2CC",
|
| 64 |
+
"FAC": "#D9FF A5",
|
| 65 |
+
"DAT": "#FFE58C",
|
| 66 |
+
"TIM": "#FFCC66",
|
| 67 |
+
"MON": "#CCFFDA",
|
| 68 |
+
"CRD": "#F2CCFF",
|
| 69 |
+
"ORD": "#E0BFFF",
|
| 70 |
+
"PRC": "#FFF2B2",
|
| 71 |
+
"QTY": "#C7F2F2",
|
| 72 |
+
"LAW": "#FFBABA",
|
| 73 |
+
"EVT": "#FFD9A5",
|
| 74 |
+
"PRD": "#BFDFFF",
|
| 75 |
+
"REG": "#E6DAFF",
|
| 76 |
+
"WOA": "#FFE6DA",
|
| 77 |
+
"LAN": "#CCFFF2",
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
MAX_PDF_PAGES = 5
|
| 81 |
+
|
| 82 |
+
# Example texts
|
| 83 |
EXAMPLES = [
|
| 84 |
(
|
| 85 |
"Contoh 1 β RUPS & Dana Cadangan",
|
|
|
|
| 122 |
),
|
| 123 |
]
|
| 124 |
|
| 125 |
+
|
| 126 |
+
# Helper clean entity word
|
| 127 |
+
def clean_word(word: str) -> str:
|
| 128 |
+
return word.replace("β", " ").replace("##", "").strip()
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def get_label_id(raw_label: str) -> str:
|
| 132 |
+
label_id = raw_label.replace("B-", "").replace("I-", "").replace("B_", "").replace("I_", "")
|
| 133 |
+
return label_id.split("-")[-1].upper().strip()
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
# Text NER
|
| 137 |
def run_ner(text: str):
|
| 138 |
if not text or not text.strip():
|
| 139 |
+
return "<p style='color:#94a3b8; font-style:italic; padding:16px;'>Masukkan teks terlebih dahulu.</p>"
|
| 140 |
|
| 141 |
results = ner(text.strip())
|
| 142 |
if not results:
|
| 143 |
+
return "<p style='color:#94a3b8; font-style:italic; padding:16px;'>Tidak ada entitas yang ditemukan.</p>"
|
| 144 |
|
| 145 |
rows_html = ""
|
| 146 |
row_num = 1
|
| 147 |
for ent in results:
|
| 148 |
raw_label = ent["entity_group"]
|
| 149 |
+
label_id = get_label_id(raw_label)
|
|
|
|
| 150 |
label_idn = LABEL_MAP.get(label_id, raw_label)
|
| 151 |
+
word = clean_word(ent["word"])
|
|
|
|
| 152 |
if not word:
|
| 153 |
continue
|
| 154 |
|
| 155 |
+
hex_color = LABEL_HEX.get(label_id, "#e2e8f0")
|
| 156 |
+
score = f"{ent['score']:.2%}"
|
| 157 |
+
even = row_num % 2 == 0
|
| 158 |
+
row_bg = "#f8faff" if even else "#ffffff"
|
| 159 |
+
|
| 160 |
rows_html += f"""
|
| 161 |
+
<tr style="background:{row_bg}; transition:background 0.15s;">
|
| 162 |
+
<td style="padding:9px 14px; border-bottom:1px solid #e8edf5; text-align:center;
|
| 163 |
+
color:#64748b; font-size:12px;">{row_num}</td>
|
| 164 |
+
<td style="padding:9px 14px; border-bottom:1px solid #e8edf5; font-weight:600;
|
| 165 |
+
color:#1e293b;">{word}</td>
|
| 166 |
+
<td style="padding:9px 14px; border-bottom:1px solid #e8edf5;">
|
| 167 |
+
<span style="background:{hex_color}; padding:3px 10px; border-radius:20px;
|
| 168 |
+
font-size:12px; font-weight:600; color:#1e293b;">{label_idn}</span>
|
| 169 |
+
</td>
|
| 170 |
+
<td style="padding:9px 14px; border-bottom:1px solid #e8edf5; font-size:12px;
|
| 171 |
+
color:#64748b; text-align:center;">{score}</td>
|
| 172 |
</tr>"""
|
| 173 |
row_num += 1
|
| 174 |
|
| 175 |
table_html = f"""
|
| 176 |
+
<div style="overflow-x:auto; margin-top:4px; border-radius:12px;
|
| 177 |
+
border:1px solid #e2e8f0; box-shadow:0 2px 12px rgba(0,0,0,0.06);">
|
| 178 |
+
<table style="width:100%; border-collapse:collapse; font-size:14px;
|
| 179 |
+
font-family:'Segoe UI', sans-serif;">
|
| 180 |
<thead>
|
| 181 |
+
<tr style="background:linear-gradient(135deg, #1e40af, #6d28d9);">
|
| 182 |
+
<th style="padding:12px 14px; color:#fff; width:55px; font-weight:600;
|
| 183 |
+
font-size:12px; letter-spacing:0.05em;">NO</th>
|
| 184 |
+
<th style="padding:12px 14px; color:#fff; text-align:left; font-weight:600;
|
| 185 |
+
font-size:12px; letter-spacing:0.05em;">KATA / FRASA</th>
|
| 186 |
+
<th style="padding:12px 14px; color:#fff; text-align:left; font-weight:600;
|
| 187 |
+
font-size:12px; letter-spacing:0.05em;">ENTITAS</th>
|
| 188 |
+
<th style="padding:12px 14px; color:#fff; width:90px; font-weight:600;
|
| 189 |
+
font-size:12px; letter-spacing:0.05em;">SKOR</th>
|
| 190 |
</tr>
|
| 191 |
</thead>
|
| 192 |
<tbody>{rows_html}</tbody>
|
|
|
|
| 195 |
return table_html
|
| 196 |
|
| 197 |
|
| 198 |
+
# PDF NER
|
| 199 |
+
def run_ner_pdf(pdf_file):
|
| 200 |
+
"""Highlight NER entities in PDF and return (output_path, legend_html, warning_html)."""
|
| 201 |
+
|
| 202 |
+
# No file uploaded
|
| 203 |
+
if pdf_file is None:
|
| 204 |
+
return (
|
| 205 |
+
None,
|
| 206 |
+
"",
|
| 207 |
+
"<p style='color:#94a3b8; font-style:italic; padding:16px;'>Unggah file PDF terlebih dahulu.</p>",
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
pdf_path = pdf_file if isinstance(pdf_file, str) else pdf_file.name
|
| 211 |
+
|
| 212 |
+
doc = fitz.open(pdf_path)
|
| 213 |
+
page_count = len(doc)
|
| 214 |
+
|
| 215 |
+
# Page limit check
|
| 216 |
+
if page_count > MAX_PDF_PAGES:
|
| 217 |
+
doc.close()
|
| 218 |
+
warn_html = f"""
|
| 219 |
+
<div style="background:#fff1f2; border:1.5px solid #fda4af; border-radius:12px;
|
| 220 |
+
padding:16px 20px; display:flex; align-items:center; gap:12px;">
|
| 221 |
+
<span style="font-size:24px;">β οΈ</span>
|
| 222 |
+
<div>
|
| 223 |
+
<p style="margin:0; font-weight:700; color:#be123c; font-size:15px;">
|
| 224 |
+
PDF Terlalu Banyak Halaman</p>
|
| 225 |
+
<p style="margin:4px 0 0; color:#9f1239; font-size:13px;">
|
| 226 |
+
File Anda memiliki <strong>{page_count} halaman</strong>.
|
| 227 |
+
Maksimal yang diizinkan adalah <strong>{MAX_PDF_PAGES} halaman</strong>.
|
| 228 |
+
Silakan unggah PDF yang lebih pendek atau potong halamannya terlebih dahulu.
|
| 229 |
+
</p>
|
| 230 |
+
</div>
|
| 231 |
+
</div>"""
|
| 232 |
+
return None, "", warn_html
|
| 233 |
+
|
| 234 |
+
# ββ Collect all text from pages, run NER ββ
|
| 235 |
+
full_text = ""
|
| 236 |
+
for page in doc:
|
| 237 |
+
full_text += page.get_text() + " "
|
| 238 |
+
|
| 239 |
+
ner_results = ner(full_text.strip())
|
| 240 |
+
|
| 241 |
+
# Build word β label_id mapping (longest match wins when overlapping)
|
| 242 |
+
entity_map: dict[str, str] = {}
|
| 243 |
+
for ent in ner_results:
|
| 244 |
+
word = clean_word(ent["word"])
|
| 245 |
+
if len(word) < 2:
|
| 246 |
+
continue
|
| 247 |
+
label_id = get_label_id(ent["entity_group"])
|
| 248 |
+
# prefer longer entity spans
|
| 249 |
+
if word not in entity_map or len(word) > len(entity_map.get(word, "")):
|
| 250 |
+
entity_map[word] = label_id
|
| 251 |
+
|
| 252 |
+
if not entity_map:
|
| 253 |
+
doc.close()
|
| 254 |
+
return None, "", "<p style='color:#94a3b8; padding:16px;'>Tidak ada entitas yang ditemukan dalam PDF.</p>"
|
| 255 |
+
|
| 256 |
+
# Sort by length descending so longer phrases get highlighted first
|
| 257 |
+
sorted_entities = sorted(entity_map.items(), key=lambda x: len(x[0]), reverse=True)
|
| 258 |
+
|
| 259 |
+
# Track which labels actually appeared
|
| 260 |
+
found_labels: set[str] = set()
|
| 261 |
+
|
| 262 |
+
# Highlight each page
|
| 263 |
+
for page in doc:
|
| 264 |
+
for word, label_id in sorted_entities:
|
| 265 |
+
color = LABEL_COLORS.get(label_id, (0.9, 0.9, 0.9))
|
| 266 |
+
instances = page.search_for(word, quads=False)
|
| 267 |
+
for rect in instances:
|
| 268 |
+
annot = page.add_highlight_annot(rect)
|
| 269 |
+
annot.set_colors(stroke=list(color))
|
| 270 |
+
annot.update()
|
| 271 |
+
found_labels.add(label_id)
|
| 272 |
+
|
| 273 |
+
# Save to temp file
|
| 274 |
+
out_fd, out_path = tempfile.mkstemp(suffix=".pdf")
|
| 275 |
+
os.close(out_fd)
|
| 276 |
+
doc.save(out_path)
|
| 277 |
+
doc.close()
|
| 278 |
+
|
| 279 |
+
# Build color legend table
|
| 280 |
+
legend_rows = ""
|
| 281 |
+
for label_id in sorted(found_labels):
|
| 282 |
+
label_idn = LABEL_MAP.get(label_id, label_id)
|
| 283 |
+
hex_color = LABEL_HEX.get(label_id, "#e2e8f0")
|
| 284 |
+
legend_rows += f"""
|
| 285 |
+
<tr>
|
| 286 |
+
<td style="padding:8px 14px; border-bottom:1px solid #e8edf5;">
|
| 287 |
+
<span style="display:inline-block; width:24px; height:24px; border-radius:6px;
|
| 288 |
+
background:{hex_color}; vertical-align:middle;
|
| 289 |
+
border:1px solid rgba(0,0,0,0.1);"></span>
|
| 290 |
+
</td>
|
| 291 |
+
<td style="padding:8px 14px; border-bottom:1px solid #e8edf5; font-weight:600;
|
| 292 |
+
color:#1e293b; font-size:13px;">{label_id}</td>
|
| 293 |
+
<td style="padding:8px 14px; border-bottom:1px solid #e8edf5; color:#475569;
|
| 294 |
+
font-size:13px;">{label_idn}</td>
|
| 295 |
+
</tr>"""
|
| 296 |
+
|
| 297 |
+
legend_html = f"""
|
| 298 |
+
<div style="margin-top:4px;">
|
| 299 |
+
<p style="font-size:13px; color:#64748b; margin-bottom:10px;">
|
| 300 |
+
Ditemukan <strong style='color:#1e40af;'>{len(found_labels)}</strong> jenis entitas
|
| 301 |
+
di <strong style='color:#1e40af;'>{page_count}</strong> halaman PDF.
|
| 302 |
+
</p>
|
| 303 |
+
<div style="overflow-x:auto; border-radius:12px; border:1px solid #e2e8f0;
|
| 304 |
+
box-shadow:0 2px 12px rgba(0,0,0,0.06);">
|
| 305 |
+
<table style="width:100%; border-collapse:collapse; font-family:'Segoe UI',sans-serif;">
|
| 306 |
+
<thead>
|
| 307 |
+
<tr style="background:linear-gradient(135deg, #1e40af, #6d28d9);">
|
| 308 |
+
<th style="padding:11px 14px; color:#fff; font-size:12px;
|
| 309 |
+
letter-spacing:0.05em; width:50px;">WARNA</th>
|
| 310 |
+
<th style="padding:11px 14px; color:#fff; font-size:12px;
|
| 311 |
+
letter-spacing:0.05em; text-align:left;">KODE</th>
|
| 312 |
+
<th style="padding:11px 14px; color:#fff; font-size:12px;
|
| 313 |
+
letter-spacing:0.05em; text-align:left;">NAMA ENTITAS</th>
|
| 314 |
+
</tr>
|
| 315 |
+
</thead>
|
| 316 |
+
<tbody>{legend_rows}</tbody>
|
| 317 |
+
</table>
|
| 318 |
+
</div>
|
| 319 |
+
</div>"""
|
| 320 |
+
|
| 321 |
+
return out_path, legend_html, ""
|
| 322 |
+
|
| 323 |
+
|
| 324 |
+
# βββ Custom CSS βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 325 |
CUSTOM_CSS = """
|
| 326 |
+
@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700;900&family=DM+Sans:wght@400;500;600&display=swap');
|
| 327 |
|
| 328 |
+
/* ββ Root & Background ββ */
|
| 329 |
+
.gradio-container {
|
| 330 |
+
max-width: 100% !important;
|
| 331 |
+
padding: 0 !important;
|
| 332 |
+
background: linear-gradient(160deg, #0f172a 0%, #1e1b4b 40%, #0f172a 100%) !important;
|
| 333 |
+
min-height: 100vh;
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
+
/* ββ Hero header ββ */
|
| 337 |
+
#hero-header {
|
| 338 |
+
background: linear-gradient(135deg, #1e40af 0%, #6d28d9 50%, #be185d 100%);
|
| 339 |
+
padding: 40px 48px 36px;
|
| 340 |
+
text-align: center;
|
| 341 |
+
position: relative;
|
| 342 |
+
overflow: hidden;
|
| 343 |
+
}
|
| 344 |
+
#hero-header::before {
|
| 345 |
+
content: '';
|
| 346 |
+
position: absolute; inset: 0;
|
| 347 |
+
background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.04'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
/* ββ Section cards ββ */
|
| 351 |
+
.section-card {
|
| 352 |
+
background: rgba(255,255,255,0.04) !important;
|
| 353 |
+
border: 1px solid rgba(255,255,255,0.10) !important;
|
| 354 |
+
border-radius: 16px !important;
|
| 355 |
+
padding: 24px !important;
|
| 356 |
+
margin: 0 32px 24px !important;
|
| 357 |
+
backdrop-filter: blur(10px);
|
| 358 |
+
}
|
| 359 |
|
| 360 |
+
/* ββ Tabs ββ */
|
| 361 |
+
.tab-nav {
|
| 362 |
+
background: rgba(255,255,255,0.05) !important;
|
| 363 |
+
border-radius: 12px !important;
|
| 364 |
+
padding: 4px !important;
|
| 365 |
+
border: 1px solid rgba(255,255,255,0.1) !important;
|
| 366 |
+
margin: 0 32px 0 !important;
|
| 367 |
+
}
|
| 368 |
+
.tab-nav button {
|
| 369 |
+
background: transparent !important;
|
| 370 |
+
color: #94a3b8 !important;
|
| 371 |
+
border-radius: 8px !important;
|
| 372 |
+
padding: 10px 24px !important;
|
| 373 |
+
font-family: 'DM Sans', sans-serif !important;
|
| 374 |
+
font-weight: 600 !important;
|
| 375 |
+
font-size: 14px !important;
|
| 376 |
+
transition: all 0.2s !important;
|
| 377 |
+
}
|
| 378 |
+
.tab-nav button.selected {
|
| 379 |
+
background: linear-gradient(135deg, #1e40af, #6d28d9) !important;
|
| 380 |
+
color: #ffffff !important;
|
| 381 |
+
box-shadow: 0 4px 12px rgba(109,40,217,0.4) !important;
|
| 382 |
+
}
|
| 383 |
+
|
| 384 |
+
/* ββ Labels ββ */
|
| 385 |
+
label span {
|
| 386 |
+
color: #cbd5e1 !important;
|
| 387 |
+
font-family: 'DM Sans', sans-serif !important;
|
| 388 |
+
font-weight: 600 !important;
|
| 389 |
+
font-size: 12px !important;
|
| 390 |
+
letter-spacing: 0.07em !important;
|
| 391 |
+
text-transform: uppercase !important;
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
/* ββ Textbox ββ */
|
| 395 |
+
textarea, .gr-textbox textarea {
|
| 396 |
+
background: rgba(15,23,42,0.7) !important;
|
| 397 |
+
border: 1.5px solid rgba(255,255,255,0.12) !important;
|
| 398 |
+
border-radius: 10px !important;
|
| 399 |
+
color: #e2e8f0 !important;
|
| 400 |
+
font-family: 'DM Sans', sans-serif !important;
|
| 401 |
+
font-size: 14px !important;
|
| 402 |
+
padding: 14px !important;
|
| 403 |
+
transition: border 0.2s !important;
|
| 404 |
+
}
|
| 405 |
+
textarea:focus {
|
| 406 |
+
border-color: #6d28d9 !important;
|
| 407 |
+
outline: none !important;
|
| 408 |
+
}
|
| 409 |
+
|
| 410 |
+
/* ββ Example buttons ββ */
|
| 411 |
.example-btn {
|
| 412 |
display: block !important;
|
| 413 |
width: 100% !important;
|
| 414 |
text-align: left !important;
|
| 415 |
padding: 12px 16px !important;
|
| 416 |
margin-bottom: 8px !important;
|
| 417 |
+
background: rgba(30,64,175,0.15) !important;
|
| 418 |
+
border: 1px solid rgba(99,102,241,0.30) !important;
|
| 419 |
+
border-radius: 10px !important;
|
| 420 |
cursor: pointer !important;
|
| 421 |
font-size: 13px !important;
|
| 422 |
+
line-height: 1.6 !important;
|
| 423 |
+
color: #cbd5e1 !important;
|
| 424 |
white-space: normal !important;
|
| 425 |
height: auto !important;
|
| 426 |
+
font-family: 'DM Sans', sans-serif !important;
|
| 427 |
+
transition: all 0.2s !important;
|
| 428 |
}
|
| 429 |
.example-btn:hover {
|
| 430 |
+
background: rgba(109,40,217,0.25) !important;
|
| 431 |
+
border-color: #6d28d9 !important;
|
| 432 |
+
color: #e2e8f0 !important;
|
| 433 |
+
transform: translateX(3px);
|
| 434 |
}
|
| 435 |
|
| 436 |
+
/* ββ Analyze button ββ */
|
| 437 |
+
#analyze-btn, #analyze-pdf-btn {
|
| 438 |
+
background: linear-gradient(135deg, #1e40af, #6d28d9) !important;
|
| 439 |
color: white !important;
|
| 440 |
+
font-weight: 700 !important;
|
| 441 |
font-size: 15px !important;
|
| 442 |
+
border-radius: 10px !important;
|
| 443 |
+
padding: 12px 0 !important;
|
| 444 |
+
margin-top: 8px !important;
|
| 445 |
+
font-family: 'DM Sans', sans-serif !important;
|
| 446 |
+
letter-spacing: 0.03em !important;
|
| 447 |
+
box-shadow: 0 4px 16px rgba(109,40,217,0.35) !important;
|
| 448 |
+
transition: all 0.2s !important;
|
| 449 |
+
border: none !important;
|
| 450 |
+
}
|
| 451 |
+
#analyze-btn:hover, #analyze-pdf-btn:hover {
|
| 452 |
+
transform: translateY(-2px) !important;
|
| 453 |
+
box-shadow: 0 6px 24px rgba(109,40,217,0.50) !important;
|
| 454 |
+
}
|
| 455 |
+
|
| 456 |
+
/* ββ Section headings ββ */
|
| 457 |
+
.section-heading {
|
| 458 |
+
font-family: 'DM Sans', sans-serif;
|
| 459 |
+
font-weight: 700;
|
| 460 |
+
font-size: 11px;
|
| 461 |
+
letter-spacing: 0.12em;
|
| 462 |
+
text-transform: uppercase;
|
| 463 |
+
color: #818cf8;
|
| 464 |
+
margin-bottom: 12px;
|
| 465 |
+
display: flex;
|
| 466 |
+
align-items: center;
|
| 467 |
+
gap: 8px;
|
| 468 |
+
}
|
| 469 |
+
.section-heading::before {
|
| 470 |
+
content: '';
|
| 471 |
+
display: inline-block;
|
| 472 |
+
width: 18px;
|
| 473 |
+
height: 2px;
|
| 474 |
+
background: linear-gradient(90deg, #6d28d9, #be185d);
|
| 475 |
+
border-radius: 2px;
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
/* ββ Output HTML container ββ */
|
| 479 |
+
.output-box {
|
| 480 |
+
background: rgba(15,23,42,0.6) !important;
|
| 481 |
+
border: 1px solid rgba(255,255,255,0.10) !important;
|
| 482 |
+
border-radius: 12px !important;
|
| 483 |
+
padding: 16px !important;
|
| 484 |
+
min-height: 80px;
|
| 485 |
+
}
|
| 486 |
+
|
| 487 |
+
/* ββ File upload ββ */
|
| 488 |
+
.gr-file {
|
| 489 |
+
background: rgba(15,23,42,0.7) !important;
|
| 490 |
+
border: 1.5px dashed rgba(99,102,241,0.40) !important;
|
| 491 |
+
border-radius: 10px !important;
|
| 492 |
+
color: #94a3b8 !important;
|
| 493 |
+
}
|
| 494 |
+
|
| 495 |
+
/* ββ Download button ββ */
|
| 496 |
+
.gr-file-download {
|
| 497 |
+
background: rgba(30,64,175,0.2) !important;
|
| 498 |
+
border: 1px solid rgba(99,102,241,0.4) !important;
|
| 499 |
+
border-radius: 10px !important;
|
| 500 |
+
color: #a5b4fc !important;
|
| 501 |
+
font-family: 'DM Sans', sans-serif !important;
|
| 502 |
+
}
|
| 503 |
+
|
| 504 |
+
/* ββ Footer ββ */
|
| 505 |
+
#footer {
|
| 506 |
+
text-align: center;
|
| 507 |
+
padding: 20px;
|
| 508 |
+
color: rgba(148,163,184,0.5);
|
| 509 |
+
font-family: 'DM Sans', sans-serif;
|
| 510 |
+
font-size: 12px;
|
| 511 |
+
letter-spacing: 0.04em;
|
| 512 |
+
}
|
| 513 |
+
|
| 514 |
+
/* ββ Markdown headings in dark ββ */
|
| 515 |
+
.gradio-container h3 {
|
| 516 |
+
color: #e2e8f0 !important;
|
| 517 |
}
|
|
|
|
| 518 |
"""
|
| 519 |
|
| 520 |
+
HERO_HTML = """
|
| 521 |
+
<div id="hero-header">
|
| 522 |
+
<p style="font-family:'DM Sans',sans-serif; font-size:12px; font-weight:700;
|
| 523 |
+
letter-spacing:0.20em; text-transform:uppercase; color:rgba(165,180,252,0.8);
|
| 524 |
+
margin:0 0 10px;">Tugas Kelompok Β· NLP & Text Mining</p>
|
| 525 |
|
| 526 |
+
<h1 style="font-family:'Playfair Display',serif; font-size:clamp(32px,5vw,56px);
|
| 527 |
+
font-weight:900; color:#ffffff; margin:0 0 8px; line-height:1.1;
|
| 528 |
+
text-shadow: 0 2px 20px rgba(109,40,217,0.5);">
|
| 529 |
+
NER <span style="color:#a78bfa;">for</span> Financial Statements
|
| 530 |
+
</h1>
|
| 531 |
|
| 532 |
+
<p style="font-family:'DM Sans',sans-serif; font-size:clamp(13px,2vw,16px);
|
| 533 |
+
color:rgba(203,213,225,0.85); margin:0 auto 18px; max-width:640px; line-height:1.6;">
|
| 534 |
+
Implementasi <em>Named Entity Recognition</em> pada Kumpulan<br>
|
| 535 |
+
Laporan-laporan Keuangan Bahasa Indonesia
|
| 536 |
+
</p>
|
| 537 |
|
| 538 |
+
<div style="display:inline-flex; align-items:center; gap:8px; background:rgba(0,0,0,0.25);
|
| 539 |
+
border:1px solid rgba(255,255,255,0.15); border-radius:20px;
|
| 540 |
+
padding:6px 16px 6px 8px;">
|
| 541 |
+
<span style="background:linear-gradient(135deg,#1e40af,#6d28d9); border-radius:12px;
|
| 542 |
+
padding:3px 10px; font-size:11px; font-weight:700; color:#fff;
|
| 543 |
+
letter-spacing:0.05em;">MODEL</span>
|
| 544 |
+
<span style="font-family:monospace; font-size:13px; color:#a5b4fc;">
|
| 545 |
+
cahya/NusaBert-ner-v1.3</span>
|
| 546 |
+
</div>
|
| 547 |
+
</div>
|
| 548 |
+
"""
|
| 549 |
|
| 550 |
+
# Build Gradio UI
|
| 551 |
+
with gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:
|
|
|
|
|
|
|
| 552 |
|
| 553 |
+
gr.HTML(HERO_HTML)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 554 |
|
| 555 |
+
with gr.Tabs(elem_classes="tab-nav"):
|
| 556 |
+
|
| 557 |
+
with gr.Tab("Analisis Teks"):
|
| 558 |
+
|
| 559 |
+
with gr.Row():
|
| 560 |
+
with gr.Column(scale=1):
|
| 561 |
+
gr.HTML('<div class="section-heading">Contoh Teks</div>')
|
| 562 |
+
example_btns = []
|
| 563 |
+
for title, body in EXAMPLES:
|
| 564 |
+
btn = gr.Button(f"π {title}\n\n{body[:120]}β¦", elem_classes="example-btn")
|
| 565 |
+
example_btns.append((btn, body))
|
| 566 |
+
|
| 567 |
+
with gr.Column(scale=2):
|
| 568 |
+
gr.HTML('<div class="section-heading">Input Teks</div>')
|
| 569 |
+
text_input = gr.Textbox(
|
| 570 |
+
lines=9,
|
| 571 |
+
placeholder="Ketik atau tempel teks laporan keuangan di siniβ¦",
|
| 572 |
+
label="",
|
| 573 |
+
show_label=False,
|
| 574 |
+
)
|
| 575 |
+
analyze_btn = gr.Button("π Lakukan Analisis", elem_id="analyze-btn")
|
| 576 |
|
| 577 |
+
gr.HTML('<div class="section-heading" style="margin:8px 0 4px;">Hasil Analisis Entitas</div>')
|
| 578 |
+
text_output = gr.HTML(
|
| 579 |
+
value="<p style='color:#64748b; font-size:14px; font-family:DM Sans,sans-serif;"
|
| 580 |
+
"padding:20px; text-align:center;'>β¬ Masukkan teks lalu klik <strong>Lakukan Analisis</strong>.</p>"
|
| 581 |
+
)
|
| 582 |
+
|
| 583 |
+
with gr.Tab("Analisis PDF"):
|
| 584 |
+
|
| 585 |
+
gr.HTML(
|
| 586 |
+
'<p style="color:#94a3b8; font-family:DM Sans,sans-serif; font-size:13px; '
|
| 587 |
+
'margin:0 0 16px;">'
|
| 588 |
+
'Unggah PDF laporan keuangan (maks. <strong style="color:#a78bfa;">5 halaman</strong>). '
|
| 589 |
+
'Entitas yang terdeteksi akan diberi warna berbeda di dalam PDF.</p>'
|
| 590 |
+
)
|
| 591 |
+
|
| 592 |
+
with gr.Row():
|
| 593 |
+
with gr.Column(scale=1):
|
| 594 |
+
gr.HTML('<div class="section-heading">Unggah PDF</div>')
|
| 595 |
+
pdf_input = gr.File(
|
| 596 |
+
label="",
|
| 597 |
+
file_types=[".pdf"],
|
| 598 |
+
type="filepath",
|
| 599 |
+
)
|
| 600 |
+
analyze_pdf_btn = gr.Button("π¨ Highlight Entitas PDF", elem_id="analyze-pdf-btn")
|
| 601 |
+
|
| 602 |
+
with gr.Column(scale=1):
|
| 603 |
+
gr.HTML('<div class="section-heading">Download Hasil PDF</div>')
|
| 604 |
+
pdf_output = gr.File(
|
| 605 |
+
label="",
|
| 606 |
+
file_types=[".pdf"],
|
| 607 |
+
interactive=False,
|
| 608 |
+
)
|
| 609 |
+
|
| 610 |
+
# Warning / error area
|
| 611 |
+
pdf_warning = gr.HTML(value="")
|
| 612 |
+
|
| 613 |
+
gr.HTML('<div class="section-heading" style="margin:16px 0 4px;">Keterangan Warna Entitas</div>')
|
| 614 |
+
pdf_legend = gr.HTML(
|
| 615 |
+
value="<p style='color:#64748b; font-size:14px; font-family:DM Sans,sans-serif;"
|
| 616 |
+
"padding:16px; text-align:center;'>β¬ Unggah PDF lalu klik tombol Highlight.</p>"
|
| 617 |
+
)
|
| 618 |
+
|
| 619 |
+
gr.HTML('<div id="footer">NER for Financial Statements Β· NusaBERT Β· Bahasa Indonesia Β· 2024</div>')
|
| 620 |
+
|
| 621 |
+
# Wiring
|
| 622 |
for btn, body in example_btns:
|
| 623 |
btn.click(fn=lambda b=body: b, inputs=[], outputs=text_input)
|
| 624 |
|
| 625 |
+
analyze_btn.click(fn=run_ner, inputs=text_input, outputs=text_output)
|
| 626 |
+
text_input.submit(fn=run_ner, inputs=text_input, outputs=text_output)
|
| 627 |
|
| 628 |
+
def handle_pdf(pdf_file):
|
| 629 |
+
out_path, legend_html, warn_html = run_ner_pdf(pdf_file)
|
| 630 |
+
return out_path, legend_html, warn_html
|
| 631 |
+
|
| 632 |
+
analyze_pdf_btn.click(
|
| 633 |
+
fn=handle_pdf,
|
| 634 |
+
inputs=pdf_input,
|
| 635 |
+
outputs=[pdf_output, pdf_legend, pdf_warning],
|
| 636 |
+
)
|
| 637 |
|
| 638 |
if __name__ == "__main__":
|
| 639 |
+
demo.launch()
|