RangGaraga commited on
Commit
4da7434
Β·
verified Β·
1 Parent(s): c353c3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +224 -436
app.py CHANGED
@@ -4,209 +4,136 @@ import fitz
4
  import tempfile
5
  import os
6
  import re
 
7
 
8
  # Load model
9
  ner = pipeline("ner", model="cahya/NusaBert-ner-v1.3", aggregation_strategy="simple")
10
 
11
  # Label mapping
12
  LABEL_MAP = {
13
- "CRD": "Kardinal",
14
- "DAT": "Tanggal",
15
- "EVT": "Peristiwa",
16
- "FAC": "Fasilitas",
17
- "GPE": "Entitas Geopolitik",
18
- "LAW": "Peraturan / Undang-Undang",
19
- "LOC": "Lokasi",
20
- "MON": "Uang",
21
- "NOR": "Organisasi Politik",
22
- "ORD": "Ordinal",
23
- "ORG": "Organisasi",
24
- "PER": "Orang",
25
- "PRC": "Persentase",
26
- "PRD": "Produk",
27
- "QTY": "Kuantitas",
28
- "REG": "Agama",
29
- "TIM": "Waktu",
30
- "WOA": "Karya Seni",
31
- "LAN": "Bahasa",
32
  }
33
 
34
- # RTF color index (1-based, index 0 = auto)
35
- # Urutan ini harus cocok dengan color table di RTF header
36
  RTF_COLOR_TABLE = [
37
- (255, 191, 191), # 1 PER - rose
38
- (174, 221, 255), # 2 ORG - sky blue
39
- (141, 199, 255), # 3 NOR - cornflower
40
- (184, 255, 184), # 4 LOC - mint green
41
- (153, 242, 204), # 5 GPE - seafoam
42
- (217, 255, 165), # 6 FAC - lime
43
- (255, 229, 140), # 7 DAT - amber
44
- (255, 204, 102), # 8 TIM - orange-gold
45
- (204, 255, 218), # 9 MON - pale green
46
- (242, 204, 255), # 10 CRD - lavender
47
- (224, 191, 255), # 11 ORD - violet
48
- (255, 242, 178), # 12 PRC - pale yellow
49
- (199, 242, 242), # 13 QTY - cyan-light
50
- (255, 186, 186), # 14 LAW - salmon
51
- (255, 217, 165), # 15 EVT - peach
52
- (191, 223, 255), # 16 PRD - baby blue
53
- (230, 218, 255), # 17 REG - periwinkle
54
- (255, 230, 218), # 18 WOA - blush
55
- (204, 255, 242), # 19 LAN - aqua
56
  ]
57
 
58
- # Label β†’ RTF color index (1-based)
59
  LABEL_RTF_COLOR_IDX = {
60
- "PER": 1, "ORG": 2, "NOR": 3, "LOC": 4, "GPE": 5,
61
- "FAC": 6, "DAT": 7, "TIM": 8, "MON": 9, "CRD": 10,
62
- "ORD": 11, "PRC": 12, "QTY": 13, "LAW": 14, "EVT": 15,
63
- "PRD": 16, "REG": 17, "WOA": 18, "LAN": 19,
64
  }
65
 
66
- # CSS hex for legend table
67
  LABEL_HEX = {
68
- "PER": "#FFBFBF", "ORG": "#AEDDFF", "NOR": "#8DC7FF",
69
- "LOC": "#B8FFB8", "GPE": "#99F2CC", "FAC": "#D9FFA5",
70
- "DAT": "#FFE58C", "TIM": "#FFCC66", "MON": "#CCFFDA",
71
- "CRD": "#F2CCFF", "ORD": "#E0BFFF", "PRC": "#FFF2B2",
72
- "QTY": "#C7F2F2", "LAW": "#FFBABA", "EVT": "#FFD9A5",
73
- "PRD": "#BFDFFF", "REG": "#E6DAFF", "WOA": "#FFE6DA",
74
- "LAN": "#CCFFF2",
75
  }
76
 
77
  MAX_PDF_PAGES = 5
 
 
 
78
 
79
  EXAMPLES = [
80
- (
81
- "Contoh 1 – RUPS & Dana Cadangan",
82
- "Berdasarkan Rapat Umum Pemegang Saham (RUPS) pada tanggal 24 Juni 2024 yang disahkan "
83
- "oleh notaris Ashoya Ratam, S.H., M.Kn., Risalah No.124/VI/2024, Perusahaan memutuskan "
84
- "antara lain menyisihkan 5% dari laba bersih untuk tahun yang berakhir 31 Desember 2023 "
85
- "atau sebesar Rp5.299.075.507 sebagai dana cadangan jaminan.",
86
- ),
87
- (
88
- "Contoh 2 – Akta Jual Beli Saham PEFINDO",
89
- "Berdasarkan Akta Notaris Melinda, S.Sos., S.H., M.Kn dengan No. 17 tanggal 21 Januari "
90
- "2025, Perusahaan dan Dana Pensiun Pertamina telah menandatangani Akta Jual Beli saham "
91
- "dan Perusahaan telah melakukan pembayaran penuh untuk pembelian 5.170 lembar saham "
92
- "PEFINDO yang dimiliki Dana Pensiun Pertamina. Dengan demikian total kepemilikan saham "
93
- "Perusahaan pada tanggal 21 Januari 2025 menjadi sebanyak 37.548 lembar saham atau sama "
94
- "dengan 31,92% kepemilikan di PEFINDO.",
95
- ),
96
- (
97
- "Contoh 3 – Fasilitas Kredit Bank Permata",
98
- "Pada tanggal 12 Desember 2022, PEI, entitas anak, dan PT Bank Permata Tbk "
99
- "menandatangani perjanjian fasilitas money market dengan fasilitas kredit maksimum "
100
- "sebesar Rp50.000.000.000. Pinjaman ini digunakan untuk keperluan stand by facility "
101
- "dengan jangka waktu penarikan antara 3 (tiga) hari sampai dengan 3 (tiga) bulan "
102
- "semenjak tanggal penarikan pinjaman dilakukan.",
103
- ),
104
- (
105
- "Contoh 4 – Dividen PEFINDO Biro Kredit",
106
- "Berdasarkan Rapat Umum Pemegang Saham Tahunan tanggal 28 Juni 2024, pemegang saham "
107
- "PEFINDO Biro Kredit menyetujui pembagian dividen untuk Perusahaan sebesar "
108
- "Rp6.637.962.683.",
109
- ),
110
- (
111
- "Contoh 5 – Regulasi Bursa Karbon",
112
- "Peraturan Presiden RI No. 98 Tahun 2021 tentang Penyelenggaraan Nilai Ekonomi Karbon "
113
- "untuk Pencapaian Target Kontribusi yang Ditetapkan Secara Nasional dan Pengendalian "
114
- "Emisi Gas Rumah Kaca dalam Pembangunan Nasional mengatur mengenai mekanisme pencapaian "
115
- "NDC. Undang-undang RI No. 4 Tahun 2023 tentang Pengembangan dan Penguatan Sektor "
116
- "Keuangan menegaskan bahwa tugas pengaturan dan pengawasan bursa karbon dilakukan oleh "
117
- "Otoritas Jasa Keuangan.",
118
- ),
119
  ]
120
 
121
 
122
- # Helpers
 
123
  def clean_word(word: str) -> str:
124
  return word.replace("▁", " ").replace("##", "").strip()
125
 
126
-
127
  def get_label_id(raw_label: str) -> str:
128
- label_id = raw_label.replace("B-", "").replace("I-", "").replace("B_", "").replace("I_", "")
129
  return label_id.split("-")[-1].upper().strip()
130
 
131
-
132
  def escape_rtf(text: str) -> str:
133
- """Escape karakter khusus RTF dan encode non-ASCII ke \\uN\\' hex."""
134
  out = []
135
  for ch in text:
136
- if ch == '\\':
137
- out.append('\\\\')
138
- elif ch == '{':
139
- out.append('\\{')
140
- elif ch == '}':
141
- out.append('\\}')
142
- elif ord(ch) > 127:
143
- # RTF unicode escape: \uN? (? = fallback char)
144
- out.append(f'\\u{ord(ch)}?')
145
- else:
146
- out.append(ch)
147
  return ''.join(out)
148
 
149
-
150
  def build_rtf_color_table() -> str:
151
- """Bangun color table RTF dari RTF_COLOR_TABLE."""
152
- entries = "\\red0\\green0\\blue0;" # index 0 = hitam (auto)
153
- for r, g, b in RTF_COLOR_TABLE:
154
  entries += f"\\red{r}\\green{g}\\blue{b};"
155
  return "{\\colortbl ;" + entries + "}"
156
 
157
-
158
  def text_to_rtf_with_highlights(text: str, entity_map: dict) -> str:
159
  sorted_entities = sorted(entity_map.items(), key=lambda x: len(x[0]), reverse=True)
160
-
161
- # Bangun list
162
  spans = []
163
- text_lower = text.lower()
164
  used = [False] * len(text)
165
-
166
  for entity_lower, label_id in sorted_entities:
167
  if not entity_lower:
168
  continue
169
  pattern = re.compile(re.escape(entity_lower), re.IGNORECASE)
170
  for m in pattern.finditer(text):
171
  s, e = m.start(), m.end()
172
- # Skip kalau ada overlap dengan span yang sudah dipilih
173
  if any(used[i] for i in range(s, e)):
174
  continue
175
  spans.append((s, e, label_id))
176
  for i in range(s, e):
177
  used[i] = True
178
-
179
- # Urutkan spans by position
180
  spans.sort(key=lambda x: x[0])
181
-
182
- # Bangun RTF content token per token
183
  rtf_parts = []
184
  cursor = 0
185
  for s, e, label_id in spans:
186
- # Teks biasa sebelum span
187
  if cursor < s:
188
  rtf_parts.append(escape_rtf(text[cursor:s]))
189
- # Teks highlight
190
  color_idx = LABEL_RTF_COLOR_IDX.get(label_id, 1)
191
  highlighted = escape_rtf(text[s:e])
192
  rtf_parts.append(f"{{\\highlight{color_idx} {highlighted}}}")
193
  cursor = e
194
-
195
- # Sisa teks setelah span terakhir
196
  if cursor < len(text):
197
  rtf_parts.append(escape_rtf(text[cursor:]))
198
-
199
  return "".join(rtf_parts)
200
 
201
-
202
  def wrap_rtf(content: str) -> str:
203
- """Bungkus konten dengan header & footer RTF lengkap."""
204
  color_table = build_rtf_color_table()
205
- # \\line = line break dalam RTF; kita ganti newline teks dengan \\line\n
206
- content_lines = content.replace("\r\n", "\n").replace("\r", "\n")
207
- # Ganti newline dengan RTF paragraph break
208
- content_rtf = content_lines.replace("\n", "\\par\n")
209
-
210
  return (
211
  "{\\rtf1\\ansi\\ansicpg1252\\deff0\n"
212
  "{\\fonttbl{\\f0\\froman\\fcharset0 Times New Roman;}}\n"
@@ -218,13 +145,28 @@ def wrap_rtf(content: str) -> str:
218
  "}"
219
  )
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
 
222
  # ── NER Teks ─────────────────────────────────────────────────────────────────
223
 
224
  def run_ner(text: str):
225
  if not text or not text.strip():
226
  return "<p style='color:#94a3b8; font-style:italic; padding:16px;'>Masukkan teks terlebih dahulu.</p>"
227
-
228
  results = ner(text.strip())
229
  if not results:
230
  return "<p style='color:#94a3b8; font-style:italic; padding:16px;'>Tidak ada entitas yang ditemukan.</p>"
@@ -232,35 +174,26 @@ def run_ner(text: str):
232
  rows_html = ""
233
  row_num = 1
234
  seen_words = set()
235
-
236
  for ent in results:
237
  raw_label = ent["entity_group"]
238
  label_id = get_label_id(raw_label)
239
  label_idn = LABEL_MAP.get(label_id, raw_label)
240
  word = clean_word(ent["word"])
241
- if not word:
242
- continue
243
  word_key = word.lower()
244
- if word_key in seen_words:
245
- continue
246
  seen_words.add(word_key)
247
-
248
  hex_color = LABEL_HEX.get(label_id, "#e2e8f0")
249
  score = f"{ent['score']:.2%}"
250
  row_bg = "#f8faff" if row_num % 2 == 0 else "#ffffff"
251
-
252
  rows_html += f"""
253
  <tr style="background:{row_bg};">
254
- <td style="padding:9px 14px; border-bottom:1px solid #e8edf5; text-align:center;
255
- color:#64748b; font-size:12px;">{row_num}</td>
256
- <td style="padding:9px 14px; border-bottom:1px solid #e8edf5; font-weight:600;
257
- color:#1e293b;">{word}</td>
258
- <td style="padding:9px 14px; border-bottom:1px solid #e8edf5;">
259
- <span style="background:{hex_color}; padding:3px 10px; border-radius:20px;
260
- font-size:12px; font-weight:600; color:#1e293b;">{label_idn}</span>
261
  </td>
262
- <td style="padding:9px 14px; border-bottom:1px solid #e8edf5; font-size:12px;
263
- color:#64748b; text-align:center;">{score}</td>
264
  </tr>"""
265
  row_num += 1
266
 
@@ -268,20 +201,14 @@ def run_ner(text: str):
268
  return "<p style='color:#94a3b8; font-style:italic; padding:16px;'>Tidak ada entitas yang ditemukan.</p>"
269
 
270
  return f"""
271
- <div style="overflow-x:auto; margin-top:4px; border-radius:12px;
272
- border:1px solid #e2e8f0; box-shadow:0 2px 12px rgba(0,0,0,0.06);">
273
- <table style="width:100%; border-collapse:collapse; font-size:14px;
274
- font-family:'Segoe UI', sans-serif;">
275
  <thead>
276
- <tr style="background:linear-gradient(135deg, #1e40af, #6d28d9);">
277
- <th style="padding:12px 14px; color:#fff; width:55px; font-weight:600;
278
- font-size:12px; letter-spacing:0.05em;">NO</th>
279
- <th style="padding:12px 14px; color:#fff; text-align:left; font-weight:600;
280
- font-size:12px; letter-spacing:0.05em;">KATA / FRASA</th>
281
- <th style="padding:12px 14px; color:#fff; text-align:left; font-weight:600;
282
- font-size:12px; letter-spacing:0.05em;">ENTITAS</th>
283
- <th style="padding:12px 14px; color:#fff; width:90px; font-weight:600;
284
- font-size:12px; letter-spacing:0.05em;">SKOR</th>
285
  </tr>
286
  </thead>
287
  <tbody>{rows_html}</tbody>
@@ -292,112 +219,144 @@ def run_ner(text: str):
292
  # ── NER PDF β†’ RTF ─────────────────────────────────────────────────────────────
293
 
294
  def run_ner_pdf(pdf_file):
 
295
  if pdf_file is None:
296
- return (
297
- None,
298
- "",
299
- "<p style='color:#94a3b8; font-style:italic; padding:16px;'>Unggah file PDF terlebih dahulu.</p>",
300
- )
301
 
302
  pdf_path = pdf_file if isinstance(pdf_file, str) else pdf_file.name
303
  doc = fitz.open(pdf_path)
304
  page_count = len(doc)
305
 
306
- # Cek batas halaman
307
  if page_count > MAX_PDF_PAGES:
308
  doc.close()
309
  return None, "", f"""
310
- <div style="background:#fff1f2; border:1.5px solid #fda4af; border-radius:12px;
311
- padding:16px 20px; display:flex; align-items:center; gap:12px;">
312
  <span style="font-size:24px;">⚠️</span>
313
  <div>
314
- <p style="margin:0; font-weight:700; color:#be123c; font-size:15px;">
315
- PDF Terlalu Banyak Halaman</p>
316
- <p style="margin:4px 0 0; color:#9f1239; font-size:13px;">
317
  File Anda memiliki <strong>{page_count} halaman</strong>.
318
- Maksimal yang diizinkan adalah <strong>{MAX_PDF_PAGES} halaman</strong>.
319
  </p>
320
  </div>
321
- </div>"""
322
 
323
- # Ekstrak teks per halaman
324
- pages_text = []
325
- for page in doc:
326
- pages_text.append(page.get_text())
327
- doc.close()
328
 
 
 
 
 
329
  full_text = "\n\n".join(pages_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
330
 
331
- # Masukkan ke model NER
332
- ner_results = ner(full_text.strip())
333
-
334
- if not ner_results:
335
- return None, "", "<p style='color:#94a3b8; padding:16px;'>Tidak ada entitas yang ditemukan dalam PDF.</p>"
336
 
337
- # Bangun entity_map dari output model ─
 
338
  entity_map: dict[str, str] = {}
339
- for ent in ner_results:
340
  word = clean_word(ent["word"])
341
- if len(word) < 2:
342
- continue
343
  label_id = get_label_id(ent["entity_group"])
344
  w_lower = word.lower()
345
- # Simpan; kalau sudah ada dan baru lebih panjang β†’ update
346
- if w_lower not in entity_map or len(w_lower) > len(list(entity_map.keys())[-1]):
347
  entity_map[w_lower] = label_id
 
 
348
 
349
  if not entity_map:
350
- return None, "", "<p style='color:#94a3b8; padding:16px;'>Tidak ada entitas yang ditemukan dalam PDF.</p>"
 
351
 
352
- # Cocokkan entitas dengan teks & beri highlight RTF
 
353
  rtf_content = text_to_rtf_with_highlights(full_text, entity_map)
 
 
354
 
355
- # Bungkus jadi file rtf dan simpan
 
356
  rtf_full = wrap_rtf(rtf_content)
357
-
358
  out_fd, out_path = tempfile.mkstemp(suffix=".rtf")
359
  with os.fdopen(out_fd, "w", encoding="ascii", errors="replace") as f:
360
  f.write(rtf_full)
 
 
361
 
362
- # Hitung label yang muncul
 
 
 
363
  found_labels: set[str] = set(entity_map.values())
364
 
365
- # Legend
366
  legend_rows = ""
367
  for label_id in sorted(found_labels):
368
  label_idn = LABEL_MAP.get(label_id, label_id)
369
  hex_color = LABEL_HEX.get(label_id, "#e2e8f0")
370
  legend_rows += f"""
371
  <tr>
372
- <td style="padding:8px 14px; border-bottom:1px solid #e8edf5;">
373
- <span style="display:inline-block; width:24px; height:24px; border-radius:6px;
374
- background:{hex_color}; vertical-align:middle;
375
- border:1px solid rgba(0,0,0,0.1);"></span>
376
  </td>
377
- <td style="padding:8px 14px; border-bottom:1px solid #e8edf5; font-weight:600;
378
- color:#1e293b; font-size:13px;">{label_id}</td>
379
- <td style="padding:8px 14px; border-bottom:1px solid #e8edf5; color:#475569;
380
- font-size:13px;">{label_idn}</td>
381
  </tr>"""
382
 
383
  legend_html = f"""
384
  <div style="margin-top:4px;">
385
- <p style="font-size:13px; color:#64748b; margin-bottom:10px;">
386
  Ditemukan <strong style='color:#1e40af;'>{len(found_labels)}</strong> jenis entitas
387
  dari <strong style='color:#1e40af;'>{page_count}</strong> halaman PDF.
388
- File dapat dibuka di <strong>Microsoft Word</strong> atau <strong>LibreOffice Writer</strong>.
389
  </p>
390
- <div style="overflow-x:auto; border-radius:12px; border:1px solid #e2e8f0;
391
- box-shadow:0 2px 12px rgba(0,0,0,0.06);">
392
- <table style="width:100%; border-collapse:collapse; font-family:'Segoe UI',sans-serif;">
393
  <thead>
394
- <tr style="background:linear-gradient(135deg, #1e40af, #6d28d9);">
395
- <th style="padding:11px 14px; color:#fff; font-size:12px;
396
- letter-spacing:0.05em; width:50px;">WARNA</th>
397
- <th style="padding:11px 14px; color:#fff; font-size:12px;
398
- letter-spacing:0.05em; text-align:left;">KODE</th>
399
- <th style="padding:11px 14px; color:#fff; font-size:12px;
400
- letter-spacing:0.05em; text-align:left;">NAMA ENTITAS</th>
401
  </tr>
402
  </thead>
403
  <tbody>{legend_rows}</tbody>
@@ -405,210 +364,61 @@ def run_ner_pdf(pdf_file):
405
  </div>
406
  </div>"""
407
 
408
- return out_path, legend_html, ""
 
409
 
 
410
 
411
- # CSS
412
  CUSTOM_CSS = """
413
  @import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700;900&family=DM+Sans:wght@400;500;600&display=swap');
414
-
415
- .gradio-container {
416
- max-width: 100% !important;
417
- padding: 0 !important;
418
- background: linear-gradient(160deg, #0f172a 0%, #1e1b4b 40%, #0f172a 100%) !important;
419
- min-height: 100vh;
420
- }
421
- #hero-header {
422
- background: linear-gradient(135deg, #1e40af 0%, #6d28d9 50%, #be185d 100%);
423
- padding: 40px 48px 36px;
424
- margin: 0 0 24px 0;
425
- border-radius: 16px;
426
- text-align: center;
427
- position: relative;
428
- overflow: hidden;
429
- }
430
- #hero-header::before {
431
- content: '';
432
- position: absolute; inset: 0;
433
- 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");
434
- }
435
- .tab-nav {
436
- background: rgba(255,255,255,0.05) !important;
437
- border-radius: 12px !important;
438
- padding: 4px !important;
439
- border: 1px solid rgba(255,255,255,0.1) !important;
440
- margin: 0 !important;
441
- }
442
- .tab-nav button {
443
- background: transparent !important;
444
- color: #94a3b8 !important;
445
- border-radius: 8px !important;
446
- padding: 10px 24px !important;
447
- font-family: 'DM Sans', sans-serif !important;
448
- font-weight: 600 !important;
449
- font-size: 14px !important;
450
- transition: all 0.2s !important;
451
- }
452
- .tab-nav button.selected {
453
- background: linear-gradient(135deg, #1e40af, #6d28d9) !important;
454
- color: #ffffff !important;
455
- box-shadow: 0 4px 12px rgba(109,40,217,0.4) !important;
456
- }
457
- label span {
458
- color: #cbd5e1 !important;
459
- font-family: 'DM Sans', sans-serif !important;
460
- font-weight: 600 !important;
461
- font-size: 12px !important;
462
- letter-spacing: 0.07em !important;
463
- text-transform: uppercase !important;
464
- }
465
- textarea, .gr-textbox textarea {
466
- background: rgba(15,23,42,0.7) !important;
467
- border: 1.5px solid rgba(255,255,255,0.12) !important;
468
- border-radius: 10px !important;
469
- color: #e2e8f0 !important;
470
- font-family: 'DM Sans', sans-serif !important;
471
- font-size: 14px !important;
472
- padding: 14px !important;
473
- transition: border 0.2s !important;
474
- }
475
- textarea:focus {
476
- border-color: #6d28d9 !important;
477
- outline: none !important;
478
- }
479
- .example-btn {
480
- display: block !important;
481
- width: 100% !important;
482
- text-align: left !important;
483
- padding: 12px 16px !important;
484
- margin-bottom: 8px !important;
485
- background: rgba(30,64,175,0.15) !important;
486
- border: 1px solid rgba(99,102,241,0.30) !important;
487
- border-radius: 10px !important;
488
- cursor: pointer !important;
489
- font-size: 13px !important;
490
- line-height: 1.6 !important;
491
- color: #cbd5e1 !important;
492
- white-space: normal !important;
493
- height: auto !important;
494
- font-family: 'DM Sans', sans-serif !important;
495
- transition: all 0.2s !important;
496
- }
497
- .example-btn:hover {
498
- background: rgba(109,40,217,0.25) !important;
499
- border-color: #6d28d9 !important;
500
- color: #e2e8f0 !important;
501
- transform: translateX(3px);
502
- }
503
- #analyze-btn, #analyze-pdf-btn {
504
- background: linear-gradient(135deg, #1e40af, #6d28d9) !important;
505
- color: white !important;
506
- font-weight: 700 !important;
507
- font-size: 15px !important;
508
- border-radius: 10px !important;
509
- padding: 12px 0 !important;
510
- margin-top: 8px !important;
511
- font-family: 'DM Sans', sans-serif !important;
512
- letter-spacing: 0.03em !important;
513
- box-shadow: 0 4px 16px rgba(109,40,217,0.35) !important;
514
- transition: all 0.2s !important;
515
- border: none !important;
516
- }
517
- #analyze-btn:hover, #analyze-pdf-btn:hover {
518
- transform: translateY(-2px) !important;
519
- box-shadow: 0 6px 24px rgba(109,40,217,0.50) !important;
520
- }
521
- .section-heading {
522
- font-family: 'DM Sans', sans-serif;
523
- font-weight: 700;
524
- font-size: 11px;
525
- letter-spacing: 0.12em;
526
- text-transform: uppercase;
527
- color: #ffffff !important;
528
- margin-bottom: 12px;
529
- display: flex;
530
- align-items: center;
531
- gap: 8px;
532
- }
533
- .section-heading::before {
534
- content: '';
535
- display: inline-block;
536
- width: 18px;
537
- height: 2px;
538
- background: linear-gradient(90deg, #6d28d9, #be185d);
539
- border-radius: 2px;
540
- }
541
- .gr-file {
542
- background: rgba(15,23,42,0.7) !important;
543
- border: 1.5px dashed rgba(99,102,241,0.40) !important;
544
- border-radius: 10px !important;
545
- color: #94a3b8 !important;
546
- }
547
- .gr-file-download {
548
- background: rgba(30,64,175,0.2) !important;
549
- border: 1px solid rgba(99,102,241,0.4) !important;
550
- border-radius: 10px !important;
551
- color: #a5b4fc !important;
552
- font-family: 'DM Sans', sans-serif !important;
553
- }
554
- #center-col {
555
- max-width: 780px !important;
556
- margin: 0 auto !important;
557
- width: 100% !important;
558
- padding: 0 8px !important;
559
- }
560
- #footer {
561
- text-align: center;
562
- padding: 20px;
563
- color: rgba(148,163,184,0.5);
564
- font-family: 'DM Sans', sans-serif;
565
- font-size: 12px;
566
- letter-spacing: 0.04em;
567
- }
568
- .gradio-container h3 {
569
- color: #e2e8f0 !important;
570
- }
571
- #center-col p {
572
- color: #94a3b8 !important;
573
- }
574
  """
575
 
576
  HERO_HTML = """
577
  <div id="hero-header">
578
- <p style="font-family:'DM Sans',sans-serif; font-size:12px; font-weight:700;
579
- letter-spacing:0.20em; text-transform:uppercase; color:rgba(165,180,252,0.8);
580
- margin:0 0 10px;">Tugas Kelompok Β· NLP &amp; Text Mining</p>
581
- <h1 style="font-family:'Playfair Display',serif; font-size:clamp(32px,5vw,56px);
582
- font-weight:900; color:#ffffff; margin:0 0 8px; line-height:1.1;
583
- text-shadow: 0 2px 20px rgba(109,40,217,0.5);">
584
  NER <span style="color:#a78bfa;">for</span> Financial Statements
585
  </h1>
586
- <p style="font-family:'DM Sans',sans-serif; font-size:clamp(13px,2vw,16px);
587
- color:rgba(203,213,225,0.85); margin:0 auto 18px; max-width:640px; line-height:1.6;">
588
- Implementasi Named Entity Recognition pada Kumpulan<br>
589
- Laporan-laporan Keuangan Bahasa Indonesia
590
  </p>
591
- <div style="display:inline-flex; align-items:center; gap:8px; background:rgba(0,0,0,0.25);
592
- border:1px solid rgba(255,255,255,0.15); border-radius:20px;
593
- padding:6px 16px 6px 8px;">
594
- <span style="background:linear-gradient(135deg,#1e40af,#6d28d9); border-radius:12px;
595
- padding:3px 10px; font-size:11px; font-weight:700; color:#fff;
596
- letter-spacing:0.05em;">MODEL</span>
597
- <span style="font-family:monospace; font-size:13px; color:#a5b4fc;">
598
- cahya/NusaBert-ner-v1.3</span>
599
  </div>
600
  </div>
601
  """
602
 
603
 
604
- # Gradio UI
 
605
  with gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:
606
 
607
  gr.HTML(HERO_HTML)
608
 
609
  with gr.Tabs(elem_classes="tab-nav"):
610
 
611
- # TAB 1 β€” TEXT INPUT
612
  with gr.Tab("Analisis Teks"):
613
  with gr.Column(elem_id="center-col"):
614
  gr.HTML('<div class="section-heading">Contoh Teks</div>')
@@ -618,54 +428,32 @@ with gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:
618
  example_btns.append((btn, body))
619
 
620
  gr.HTML('<div class="section-heading" style="margin-top:20px;">Input Teks</div>')
621
- text_input = gr.Textbox(
622
- lines=9,
623
- placeholder="Ketik atau tempel teks laporan keuangan di sini…",
624
- label="",
625
- show_label=False,
626
- )
627
  analyze_btn = gr.Button("Lakukan Analisis", elem_id="analyze-btn")
628
 
629
  gr.HTML('<div class="section-heading" style="margin-top:20px;">Hasil Analisis Entitas</div>')
630
- text_output = gr.HTML(
631
- value="<p style='color:#64748b; font-size:14px; font-family:DM Sans,sans-serif;"
632
- "padding:20px; text-align:center;'>Masukkan teks lalu klik Lakukan Analisis.</p>"
633
- )
634
 
635
- # TAB 2 β€” PDF β†’ RTF
636
  with gr.Tab("Analisis PDF"):
637
  with gr.Column(elem_id="center-col"):
638
  gr.HTML(
639
- '<p style="color:#94a3b8; font-family:DM Sans,sans-serif; font-size:13px; '
640
- 'margin:0 0 16px;">'
641
  'Unggah PDF laporan keuangan (maks. <strong style="color:#a78bfa;">5 halaman</strong>). '
642
- 'Entitas yang terdeteksi akan diberi stabilo warna di file '
643
- '<strong style="color:#a78bfa;">.rtf</strong> '
644
- 'β€” bisa dibuka di <strong>Microsoft Word</strong> atau <strong>LibreOffice Writer</strong>.</p>'
645
  )
646
-
647
  gr.HTML('<div class="section-heading">Unggah PDF</div>')
648
- pdf_input = gr.File(
649
- label="",
650
- file_types=[".pdf"],
651
- type="filepath",
652
- )
653
  analyze_pdf_btn = gr.Button("Highlight Entitas β†’ Download RTF", elem_id="analyze-pdf-btn")
654
-
655
  pdf_warning = gr.HTML(value="")
656
 
657
  with gr.Column(visible=False) as pdf_results_col:
658
  gr.HTML('<div class="section-heading" style="margin-top:20px;">Download Hasil RTF</div>')
659
- pdf_output = gr.File(
660
- label="",
661
- file_types=[".rtf"],
662
- interactive=False,
663
- )
664
  gr.HTML('<div class="section-heading" style="margin-top:20px;">Keterangan Warna Entitas</div>')
665
- pdf_legend = gr.HTML(
666
- value="<p style='color:#64748b; font-size:14px; font-family:DM Sans,sans-serif;"
667
- "padding:16px; text-align:center;'>⬆ Unggah PDF lalu klik tombol Highlight.</p>"
668
- )
669
 
670
  gr.HTML('<div id="footer">NER for Financial Statements 2026</div>')
671
 
@@ -677,14 +465,14 @@ with gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:
677
  text_input.submit(fn=run_ner, inputs=text_input, outputs=text_output)
678
 
679
  def handle_pdf(pdf_file):
680
- out_path, legend_html, warn_html = run_ner_pdf(pdf_file)
681
  show = gr.Column(visible=out_path is not None)
682
- return out_path, legend_html, warn_html, show
683
 
684
  analyze_pdf_btn.click(
685
  fn=handle_pdf,
686
  inputs=pdf_input,
687
- outputs=[pdf_output, pdf_legend, pdf_warning, pdf_results_col],
688
  )
689
 
690
  if __name__ == "__main__":
 
4
  import tempfile
5
  import os
6
  import re
7
+ import time
8
 
9
  # Load model
10
  ner = pipeline("ner", model="cahya/NusaBert-ner-v1.3", aggregation_strategy="simple")
11
 
12
  # Label mapping
13
  LABEL_MAP = {
14
+ "CRD": "Kardinal", "DAT": "Tanggal", "EVT": "Peristiwa",
15
+ "FAC": "Fasilitas", "GPE": "Entitas Geopolitik", "LAW": "Peraturan / Undang-Undang",
16
+ "LOC": "Lokasi", "MON": "Uang", "NOR": "Organisasi Politik",
17
+ "ORD": "Ordinal", "ORG": "Organisasi", "PER": "Orang",
18
+ "PRC": "Persentase", "PRD": "Produk", "QTY": "Kuantitas",
19
+ "REG": "Agama", "TIM": "Waktu", "WOA": "Karya Seni", "LAN": "Bahasa",
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  }
21
 
 
 
22
  RTF_COLOR_TABLE = [
23
+ (255,191,191),(174,221,255),(141,199,255),(184,255,184),(153,242,204),
24
+ (217,255,165),(255,229,140),(255,204,102),(204,255,218),(242,204,255),
25
+ (224,191,255),(255,242,178),(199,242,242),(255,186,186),(255,217,165),
26
+ (191,223,255),(230,218,255),(255,230,218),(204,255,242),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  ]
28
 
 
29
  LABEL_RTF_COLOR_IDX = {
30
+ "PER":1,"ORG":2,"NOR":3,"LOC":4,"GPE":5,"FAC":6,"DAT":7,"TIM":8,
31
+ "MON":9,"CRD":10,"ORD":11,"PRC":12,"QTY":13,"LAW":14,"EVT":15,
32
+ "PRD":16,"REG":17,"WOA":18,"LAN":19,
 
33
  }
34
 
 
35
  LABEL_HEX = {
36
+ "PER":"#FFBFBF","ORG":"#AEDDFF","NOR":"#8DC7FF","LOC":"#B8FFB8",
37
+ "GPE":"#99F2CC","FAC":"#D9FFA5","DAT":"#FFE58C","TIM":"#FFCC66",
38
+ "MON":"#CCFFDA","CRD":"#F2CCFF","ORD":"#E0BFFF","PRC":"#FFF2B2",
39
+ "QTY":"#C7F2F2","LAW":"#FFBABA","EVT":"#FFD9A5","PRD":"#BFDFFF",
40
+ "REG":"#E6DAFF","WOA":"#FFE6DA","LAN":"#CCFFF2",
 
 
41
  }
42
 
43
  MAX_PDF_PAGES = 5
44
+ # NusaBERT max 512 token β‰ˆ ~400 kata β‰ˆ ~2500 karakter aman
45
+ MAX_CHUNK_CHARS = 2000
46
+ OVERLAP_CHARS = 150
47
 
48
  EXAMPLES = [
49
+ ("Contoh 1 – RUPS & Dana Cadangan",
50
+ "Berdasarkan Rapat Umum Pemegang Saham (RUPS) pada tanggal 24 Juni 2024 yang disahkan "
51
+ "oleh notaris Ashoya Ratam, S.H., M.Kn., Risalah No.124/VI/2024, Perusahaan memutuskan "
52
+ "antara lain menyisihkan 5% dari laba bersih untuk tahun yang berakhir 31 Desember 2023 "
53
+ "atau sebesar Rp5.299.075.507 sebagai dana cadangan jaminan."),
54
+ ("Contoh 2 – Akta Jual Beli Saham PEFINDO",
55
+ "Berdasarkan Akta Notaris Melinda, S.Sos., S.H., M.Kn dengan No. 17 tanggal 21 Januari "
56
+ "2025, Perusahaan dan Dana Pensiun Pertamina telah menandatangani Akta Jual Beli saham "
57
+ "dan Perusahaan telah melakukan pembayaran penuh untuk pembelian 5.170 lembar saham "
58
+ "PEFINDO yang dimiliki Dana Pensiun Pertamina. Dengan demikian total kepemilikan saham "
59
+ "Perusahaan pada tanggal 21 Januari 2025 menjadi sebanyak 37.548 lembar saham atau sama "
60
+ "dengan 31,92% kepemilikan di PEFINDO."),
61
+ ("Contoh 3 – Fasilitas Kredit Bank Permata",
62
+ "Pada tanggal 12 Desember 2022, PEI, entitas anak, dan PT Bank Permata Tbk "
63
+ "menandatangani perjanjian fasilitas money market dengan fasilitas kredit maksimum "
64
+ "sebesar Rp50.000.000.000. Pinjaman ini digunakan untuk keperluan stand by facility "
65
+ "dengan jangka waktu penarikan antara 3 (tiga) hari sampai dengan 3 (tiga) bulan "
66
+ "semenjak tanggal penarikan pinjaman dilakukan."),
67
+ ("Contoh 4 – Dividen PEFINDO Biro Kredit",
68
+ "Berdasarkan Rapat Umum Pemegang Saham Tahunan tanggal 28 Juni 2024, pemegang saham "
69
+ "PEFINDO Biro Kredit menyetujui pembagian dividen untuk Perusahaan sebesar Rp6.637.962.683."),
70
+ ("Contoh 5 – Regulasi Bursa Karbon",
71
+ "Peraturan Presiden RI No. 98 Tahun 2021 tentang Penyelenggaraan Nilai Ekonomi Karbon "
72
+ "untuk Pencapaian Target Kontribusi yang Ditetapkan Secara Nasional dan Pengendalian "
73
+ "Emisi Gas Rumah Kaca dalam Pembangunan Nasional mengatur mengenai mekanisme pencapaian "
74
+ "NDC. Undang-undang RI No. 4 Tahun 2023 tentang Pengembangan dan Penguatan Sektor "
75
+ "Keuangan menegaskan bahwa tugas pengaturan dan pengawasan bursa karbon dilakukan oleh "
76
+ "Otoritas Jasa Keuangan."),
 
 
 
 
 
 
 
 
 
 
 
77
  ]
78
 
79
 
80
+ # ── Helpers ──────────────────────────────────────────────────────────────────
81
+
82
  def clean_word(word: str) -> str:
83
  return word.replace("▁", " ").replace("##", "").strip()
84
 
 
85
  def get_label_id(raw_label: str) -> str:
86
+ label_id = raw_label.replace("B-","").replace("I-","").replace("B_","").replace("I_","")
87
  return label_id.split("-")[-1].upper().strip()
88
 
 
89
  def escape_rtf(text: str) -> str:
 
90
  out = []
91
  for ch in text:
92
+ if ch == '\\': out.append('\\\\')
93
+ elif ch == '{': out.append('\\{')
94
+ elif ch == '}': out.append('\\}')
95
+ elif ord(ch) > 127: out.append(f'\\u{ord(ch)}?')
96
+ else: out.append(ch)
 
 
 
 
 
 
97
  return ''.join(out)
98
 
 
99
  def build_rtf_color_table() -> str:
100
+ entries = "\\red0\\green0\\blue0;"
101
+ for r,g,b in RTF_COLOR_TABLE:
 
102
  entries += f"\\red{r}\\green{g}\\blue{b};"
103
  return "{\\colortbl ;" + entries + "}"
104
 
 
105
  def text_to_rtf_with_highlights(text: str, entity_map: dict) -> str:
106
  sorted_entities = sorted(entity_map.items(), key=lambda x: len(x[0]), reverse=True)
 
 
107
  spans = []
 
108
  used = [False] * len(text)
 
109
  for entity_lower, label_id in sorted_entities:
110
  if not entity_lower:
111
  continue
112
  pattern = re.compile(re.escape(entity_lower), re.IGNORECASE)
113
  for m in pattern.finditer(text):
114
  s, e = m.start(), m.end()
 
115
  if any(used[i] for i in range(s, e)):
116
  continue
117
  spans.append((s, e, label_id))
118
  for i in range(s, e):
119
  used[i] = True
 
 
120
  spans.sort(key=lambda x: x[0])
 
 
121
  rtf_parts = []
122
  cursor = 0
123
  for s, e, label_id in spans:
 
124
  if cursor < s:
125
  rtf_parts.append(escape_rtf(text[cursor:s]))
 
126
  color_idx = LABEL_RTF_COLOR_IDX.get(label_id, 1)
127
  highlighted = escape_rtf(text[s:e])
128
  rtf_parts.append(f"{{\\highlight{color_idx} {highlighted}}}")
129
  cursor = e
 
 
130
  if cursor < len(text):
131
  rtf_parts.append(escape_rtf(text[cursor:]))
 
132
  return "".join(rtf_parts)
133
 
 
134
  def wrap_rtf(content: str) -> str:
 
135
  color_table = build_rtf_color_table()
136
+ content_rtf = content.replace("\r\n","\n").replace("\r","\n").replace("\n","\\par\n")
 
 
 
 
137
  return (
138
  "{\\rtf1\\ansi\\ansicpg1252\\deff0\n"
139
  "{\\fonttbl{\\f0\\froman\\fcharset0 Times New Roman;}}\n"
 
145
  "}"
146
  )
147
 
148
+ def _build_debug_html(lines: list) -> str:
149
+ rows = "".join(
150
+ f'<div style="padding:6px 10px; border-bottom:1px solid rgba(255,255,255,0.07); '
151
+ f'font-size:13px; color:#cbd5e1; font-family:monospace;">{line}</div>'
152
+ for line in lines
153
+ )
154
+ return f"""
155
+ <div style="margin-top:12px; background:rgba(15,23,42,0.85); border:1px solid rgba(99,102,241,0.35);
156
+ border-radius:12px; overflow:hidden;">
157
+ <div style="background:linear-gradient(135deg,#1e40af,#6d28d9); padding:8px 14px;
158
+ font-size:11px; font-weight:700; letter-spacing:0.1em; color:#fff;">
159
+ πŸ” DEBUG β€” WAKTU PER LANGKAH
160
+ </div>
161
+ {rows}
162
+ </div>"""
163
+
164
 
165
  # ── NER Teks ─────────────────────────────────────────────────────────────────
166
 
167
  def run_ner(text: str):
168
  if not text or not text.strip():
169
  return "<p style='color:#94a3b8; font-style:italic; padding:16px;'>Masukkan teks terlebih dahulu.</p>"
 
170
  results = ner(text.strip())
171
  if not results:
172
  return "<p style='color:#94a3b8; font-style:italic; padding:16px;'>Tidak ada entitas yang ditemukan.</p>"
 
174
  rows_html = ""
175
  row_num = 1
176
  seen_words = set()
 
177
  for ent in results:
178
  raw_label = ent["entity_group"]
179
  label_id = get_label_id(raw_label)
180
  label_idn = LABEL_MAP.get(label_id, raw_label)
181
  word = clean_word(ent["word"])
182
+ if not word: continue
 
183
  word_key = word.lower()
184
+ if word_key in seen_words: continue
 
185
  seen_words.add(word_key)
 
186
  hex_color = LABEL_HEX.get(label_id, "#e2e8f0")
187
  score = f"{ent['score']:.2%}"
188
  row_bg = "#f8faff" if row_num % 2 == 0 else "#ffffff"
 
189
  rows_html += f"""
190
  <tr style="background:{row_bg};">
191
+ <td style="padding:9px 14px;border-bottom:1px solid #e8edf5;text-align:center;color:#64748b;font-size:12px;">{row_num}</td>
192
+ <td style="padding:9px 14px;border-bottom:1px solid #e8edf5;font-weight:600;color:#1e293b;">{word}</td>
193
+ <td style="padding:9px 14px;border-bottom:1px solid #e8edf5;">
194
+ <span style="background:{hex_color};padding:3px 10px;border-radius:20px;font-size:12px;font-weight:600;color:#1e293b;">{label_idn}</span>
 
 
 
195
  </td>
196
+ <td style="padding:9px 14px;border-bottom:1px solid #e8edf5;font-size:12px;color:#64748b;text-align:center;">{score}</td>
 
197
  </tr>"""
198
  row_num += 1
199
 
 
201
  return "<p style='color:#94a3b8; font-style:italic; padding:16px;'>Tidak ada entitas yang ditemukan.</p>"
202
 
203
  return f"""
204
+ <div style="overflow-x:auto;margin-top:4px;border-radius:12px;border:1px solid #e2e8f0;box-shadow:0 2px 12px rgba(0,0,0,0.06);">
205
+ <table style="width:100%;border-collapse:collapse;font-size:14px;font-family:'Segoe UI',sans-serif;">
 
 
206
  <thead>
207
+ <tr style="background:linear-gradient(135deg,#1e40af,#6d28d9);">
208
+ <th style="padding:12px 14px;color:#fff;width:55px;font-weight:600;font-size:12px;letter-spacing:0.05em;">NO</th>
209
+ <th style="padding:12px 14px;color:#fff;text-align:left;font-weight:600;font-size:12px;letter-spacing:0.05em;">KATA / FRASA</th>
210
+ <th style="padding:12px 14px;color:#fff;text-align:left;font-weight:600;font-size:12px;letter-spacing:0.05em;">ENTITAS</th>
211
+ <th style="padding:12px 14px;color:#fff;width:90px;font-weight:600;font-size:12px;letter-spacing:0.05em;">SKOR</th>
 
 
 
 
212
  </tr>
213
  </thead>
214
  <tbody>{rows_html}</tbody>
 
219
  # ── NER PDF β†’ RTF ─────────────────────────────────────────────────────────────
220
 
221
  def run_ner_pdf(pdf_file):
222
+ # return: (out_path, legend_html, warn_html, debug_html)
223
  if pdf_file is None:
224
+ return None, "", "<p style='color:#94a3b8;font-style:italic;padding:16px;'>Unggah file PDF terlebih dahulu.</p>", ""
 
 
 
 
225
 
226
  pdf_path = pdf_file if isinstance(pdf_file, str) else pdf_file.name
227
  doc = fitz.open(pdf_path)
228
  page_count = len(doc)
229
 
 
230
  if page_count > MAX_PDF_PAGES:
231
  doc.close()
232
  return None, "", f"""
233
+ <div style="background:#fff1f2;border:1.5px solid #fda4af;border-radius:12px;
234
+ padding:16px 20px;display:flex;align-items:center;gap:12px;">
235
  <span style="font-size:24px;">⚠️</span>
236
  <div>
237
+ <p style="margin:0;font-weight:700;color:#be123c;font-size:15px;">PDF Terlalu Banyak Halaman</p>
238
+ <p style="margin:4px 0 0;color:#9f1239;font-size:13px;">
 
239
  File Anda memiliki <strong>{page_count} halaman</strong>.
240
+ Maksimal <strong>{MAX_PDF_PAGES} halaman</strong>.
241
  </p>
242
  </div>
243
+ </div>""", ""
244
 
245
+ debug_lines = []
246
+ t_total = time.time()
 
 
 
247
 
248
+ # ── LANGKAH 1: Ekstrak teks ──
249
+ t0 = time.time()
250
+ pages_text = [page.get_text() for page in doc]
251
+ doc.close()
252
  full_text = "\n\n".join(pages_text)
253
+ char_count = len(full_text)
254
+ t1 = time.time()
255
+ debug_lines.append(f"βœ… <b>Langkah 1</b> β€” Ekstrak teks PDF: <b>{t1-t0:.3f}s</b> | {char_count:,} karakter | {page_count} halaman")
256
+
257
+ # ── LANGKAH 2: Chunking ──
258
+ t0 = time.time()
259
+ chunks = []
260
+ start = 0
261
+ while start < len(full_text):
262
+ end = min(start + MAX_CHUNK_CHARS, len(full_text))
263
+ chunks.append(full_text[start:end])
264
+ if end == len(full_text):
265
+ break
266
+ start = end - OVERLAP_CHARS
267
+ t1 = time.time()
268
+ debug_lines.append(f"βœ… <b>Langkah 2</b> β€” Chunking teks: <b>{t1-t0:.4f}s</b> | {len(chunks)} chunk @ {MAX_CHUNK_CHARS} karakter")
269
+
270
+ # ── LANGKAH 3: NER model (ini yang biasanya paling lama) ──
271
+ t0 = time.time()
272
+ all_ner_results = []
273
+ chunk_times = []
274
+ for i, chunk in enumerate(chunks):
275
+ tc = time.time()
276
+ chunk_results = ner(chunk.strip())
277
+ chunk_times.append(time.time() - tc)
278
+ all_ner_results.extend(chunk_results)
279
+ t1 = time.time()
280
+ avg_chunk = sum(chunk_times)/len(chunk_times) if chunk_times else 0
281
+ debug_lines.append(
282
+ f"βœ… <b>Langkah 3</b> β€” NER model: <b>{t1-t0:.2f}s</b> total | "
283
+ f"{len(chunks)} chunk | avg/chunk: {avg_chunk:.2f}s | {len(all_ner_results)} entitas raw"
284
+ )
285
+ # Detail per chunk
286
+ for i, ct in enumerate(chunk_times):
287
+ debug_lines.append(f"&nbsp;&nbsp;&nbsp;↳ chunk {i+1}/{len(chunks)}: {ct:.2f}s ({len(chunks[i])} karakter)")
288
 
289
+ if not all_ner_results:
290
+ dh = _build_debug_html(debug_lines)
291
+ return None, "", "<p style='color:#94a3b8;padding:16px;'>Tidak ada entitas ditemukan.</p>", dh
 
 
292
 
293
+ # ── LANGKAH 4: Bangun entity_map ──
294
+ t0 = time.time()
295
  entity_map: dict[str, str] = {}
296
+ for ent in all_ner_results:
297
  word = clean_word(ent["word"])
298
+ if len(word) < 2: continue
 
299
  label_id = get_label_id(ent["entity_group"])
300
  w_lower = word.lower()
301
+ if w_lower not in entity_map:
 
302
  entity_map[w_lower] = label_id
303
+ t1 = time.time()
304
+ debug_lines.append(f"βœ… <b>Langkah 4</b> β€” Bangun entity_map: <b>{t1-t0:.4f}s</b> | {len(entity_map)} entitas unik")
305
 
306
  if not entity_map:
307
+ dh = _build_debug_html(debug_lines)
308
+ return None, "", "<p style='color:#94a3b8;padding:16px;'>Tidak ada entitas ditemukan.</p>", dh
309
 
310
+ # ── LANGKAH 5: Highlight β†’ RTF content ──
311
+ t0 = time.time()
312
  rtf_content = text_to_rtf_with_highlights(full_text, entity_map)
313
+ t1 = time.time()
314
+ debug_lines.append(f"βœ… <b>Langkah 5</b> β€” Highlight & build RTF content: <b>{t1-t0:.4f}s</b>")
315
 
316
+ # ── LANGKAH 6: Tulis file .rtf ──
317
+ t0 = time.time()
318
  rtf_full = wrap_rtf(rtf_content)
 
319
  out_fd, out_path = tempfile.mkstemp(suffix=".rtf")
320
  with os.fdopen(out_fd, "w", encoding="ascii", errors="replace") as f:
321
  f.write(rtf_full)
322
+ t1 = time.time()
323
+ debug_lines.append(f"βœ… <b>Langkah 6</b> β€” Tulis file RTF: <b>{t1-t0:.4f}s</b> | {len(rtf_full):,} bytes")
324
 
325
+ t_end = time.time()
326
+ debug_lines.append(f"⏱ <b>TOTAL WAKTU: {t_end-t_total:.2f}s</b>")
327
+
328
+ debug_html = _build_debug_html(debug_lines)
329
  found_labels: set[str] = set(entity_map.values())
330
 
331
+ # ── Legend ──
332
  legend_rows = ""
333
  for label_id in sorted(found_labels):
334
  label_idn = LABEL_MAP.get(label_id, label_id)
335
  hex_color = LABEL_HEX.get(label_id, "#e2e8f0")
336
  legend_rows += f"""
337
  <tr>
338
+ <td style="padding:8px 14px;border-bottom:1px solid #e8edf5;">
339
+ <span style="display:inline-block;width:24px;height:24px;border-radius:6px;
340
+ background:{hex_color};vertical-align:middle;border:1px solid rgba(0,0,0,0.1);"></span>
 
341
  </td>
342
+ <td style="padding:8px 14px;border-bottom:1px solid #e8edf5;font-weight:600;color:#1e293b;font-size:13px;">{label_id}</td>
343
+ <td style="padding:8px 14px;border-bottom:1px solid #e8edf5;color:#475569;font-size:13px;">{label_idn}</td>
 
 
344
  </tr>"""
345
 
346
  legend_html = f"""
347
  <div style="margin-top:4px;">
348
+ <p style="font-size:13px;color:#64748b;margin-bottom:10px;">
349
  Ditemukan <strong style='color:#1e40af;'>{len(found_labels)}</strong> jenis entitas
350
  dari <strong style='color:#1e40af;'>{page_count}</strong> halaman PDF.
351
+ Buka file <strong>.rtf</strong> di <strong>Microsoft Word</strong> atau <strong>LibreOffice Writer</strong>.
352
  </p>
353
+ <div style="overflow-x:auto;border-radius:12px;border:1px solid #e2e8f0;box-shadow:0 2px 12px rgba(0,0,0,0.06);">
354
+ <table style="width:100%;border-collapse:collapse;font-family:'Segoe UI',sans-serif;">
 
355
  <thead>
356
+ <tr style="background:linear-gradient(135deg,#1e40af,#6d28d9);">
357
+ <th style="padding:11px 14px;color:#fff;font-size:12px;letter-spacing:0.05em;width:50px;">WARNA</th>
358
+ <th style="padding:11px 14px;color:#fff;font-size:12px;letter-spacing:0.05em;text-align:left;">KODE</th>
359
+ <th style="padding:11px 14px;color:#fff;font-size:12px;letter-spacing:0.05em;text-align:left;">NAMA ENTITAS</th>
 
 
 
360
  </tr>
361
  </thead>
362
  <tbody>{legend_rows}</tbody>
 
364
  </div>
365
  </div>"""
366
 
367
+ return out_path, legend_html, "", debug_html
368
+
369
 
370
+ # ── CSS ───────────────────────────────────────────────────────────────────────
371
 
 
372
  CUSTOM_CSS = """
373
  @import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700;900&family=DM+Sans:wght@400;500;600&display=swap');
374
+ .gradio-container{max-width:100%!important;padding:0!important;background:linear-gradient(160deg,#0f172a 0%,#1e1b4b 40%,#0f172a 100%)!important;min-height:100vh;}
375
+ #hero-header{background:linear-gradient(135deg,#1e40af 0%,#6d28d9 50%,#be185d 100%);padding:40px 48px 36px;margin:0 0 24px;border-radius:16px;text-align:center;position:relative;overflow:hidden;}
376
+ #hero-header::before{content:'';position:absolute;inset:0;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");}
377
+ .tab-nav{background:rgba(255,255,255,0.05)!important;border-radius:12px!important;padding:4px!important;border:1px solid rgba(255,255,255,0.1)!important;margin:0!important;}
378
+ .tab-nav button{background:transparent!important;color:#94a3b8!important;border-radius:8px!important;padding:10px 24px!important;font-family:'DM Sans',sans-serif!important;font-weight:600!important;font-size:14px!important;transition:all 0.2s!important;}
379
+ .tab-nav button.selected{background:linear-gradient(135deg,#1e40af,#6d28d9)!important;color:#ffffff!important;box-shadow:0 4px 12px rgba(109,40,217,0.4)!important;}
380
+ label span{color:#cbd5e1!important;font-family:'DM Sans',sans-serif!important;font-weight:600!important;font-size:12px!important;letter-spacing:0.07em!important;text-transform:uppercase!important;}
381
+ textarea,.gr-textbox textarea{background:rgba(15,23,42,0.7)!important;border:1.5px solid rgba(255,255,255,0.12)!important;border-radius:10px!important;color:#e2e8f0!important;font-family:'DM Sans',sans-serif!important;font-size:14px!important;padding:14px!important;transition:border 0.2s!important;}
382
+ textarea:focus{border-color:#6d28d9!important;outline:none!important;}
383
+ .example-btn{display:block!important;width:100%!important;text-align:left!important;padding:12px 16px!important;margin-bottom:8px!important;background:rgba(30,64,175,0.15)!important;border:1px solid rgba(99,102,241,0.30)!important;border-radius:10px!important;cursor:pointer!important;font-size:13px!important;line-height:1.6!important;color:#cbd5e1!important;white-space:normal!important;height:auto!important;font-family:'DM Sans',sans-serif!important;transition:all 0.2s!important;}
384
+ .example-btn:hover{background:rgba(109,40,217,0.25)!important;border-color:#6d28d9!important;color:#e2e8f0!important;transform:translateX(3px);}
385
+ #analyze-btn,#analyze-pdf-btn{background:linear-gradient(135deg,#1e40af,#6d28d9)!important;color:white!important;font-weight:700!important;font-size:15px!important;border-radius:10px!important;padding:12px 0!important;margin-top:8px!important;font-family:'DM Sans',sans-serif!important;letter-spacing:0.03em!important;box-shadow:0 4px 16px rgba(109,40,217,0.35)!important;transition:all 0.2s!important;border:none!important;}
386
+ #analyze-btn:hover,#analyze-pdf-btn:hover{transform:translateY(-2px)!important;box-shadow:0 6px 24px rgba(109,40,217,0.50)!important;}
387
+ .section-heading{font-family:'DM Sans',sans-serif;font-weight:700;font-size:11px;letter-spacing:0.12em;text-transform:uppercase;color:#ffffff!important;margin-bottom:12px;display:flex;align-items:center;gap:8px;}
388
+ .section-heading::before{content:'';display:inline-block;width:18px;height:2px;background:linear-gradient(90deg,#6d28d9,#be185d);border-radius:2px;}
389
+ .gr-file{background:rgba(15,23,42,0.7)!important;border:1.5px dashed rgba(99,102,241,0.40)!important;border-radius:10px!important;color:#94a3b8!important;}
390
+ .gr-file-download{background:rgba(30,64,175,0.2)!important;border:1px solid rgba(99,102,241,0.4)!important;border-radius:10px!important;color:#a5b4fc!important;font-family:'DM Sans',sans-serif!important;}
391
+ #center-col{max-width:780px!important;margin:0 auto!important;width:100%!important;padding:0 8px!important;}
392
+ #footer{text-align:center;padding:20px;color:rgba(148,163,184,0.5);font-family:'DM Sans',sans-serif;font-size:12px;letter-spacing:0.04em;}
393
+ .gradio-container h3{color:#e2e8f0!important;}
394
+ #center-col p{color:#94a3b8!important;}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
395
  """
396
 
397
  HERO_HTML = """
398
  <div id="hero-header">
399
+ <p style="font-family:'DM Sans',sans-serif;font-size:12px;font-weight:700;letter-spacing:0.20em;text-transform:uppercase;color:rgba(165,180,252,0.8);margin:0 0 10px;">Tugas Kelompok Β· NLP &amp; Text Mining</p>
400
+ <h1 style="font-family:'Playfair Display',serif;font-size:clamp(32px,5vw,56px);font-weight:900;color:#ffffff;margin:0 0 8px;line-height:1.1;text-shadow:0 2px 20px rgba(109,40,217,0.5);">
 
 
 
 
401
  NER <span style="color:#a78bfa;">for</span> Financial Statements
402
  </h1>
403
+ <p style="font-family:'DM Sans',sans-serif;font-size:clamp(13px,2vw,16px);color:rgba(203,213,225,0.85);margin:0 auto 18px;max-width:640px;line-height:1.6;">
404
+ Implementasi Named Entity Recognition pada Kumpulan<br>Laporan-laporan Keuangan Bahasa Indonesia
 
 
405
  </p>
406
+ <div style="display:inline-flex;align-items:center;gap:8px;background:rgba(0,0,0,0.25);border:1px solid rgba(255,255,255,0.15);border-radius:20px;padding:6px 16px 6px 8px;">
407
+ <span style="background:linear-gradient(135deg,#1e40af,#6d28d9);border-radius:12px;padding:3px 10px;font-size:11px;font-weight:700;color:#fff;letter-spacing:0.05em;">MODEL</span>
408
+ <span style="font-family:monospace;font-size:13px;color:#a5b4fc;">cahya/NusaBert-ner-v1.3</span>
 
 
 
 
 
409
  </div>
410
  </div>
411
  """
412
 
413
 
414
+ # ── Gradio UI ─────────────────────────────────────────────────────────────────
415
+
416
  with gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:
417
 
418
  gr.HTML(HERO_HTML)
419
 
420
  with gr.Tabs(elem_classes="tab-nav"):
421
 
 
422
  with gr.Tab("Analisis Teks"):
423
  with gr.Column(elem_id="center-col"):
424
  gr.HTML('<div class="section-heading">Contoh Teks</div>')
 
428
  example_btns.append((btn, body))
429
 
430
  gr.HTML('<div class="section-heading" style="margin-top:20px;">Input Teks</div>')
431
+ text_input = gr.Textbox(lines=9, placeholder="Ketik atau tempel teks laporan keuangan di sini…", label="", show_label=False)
 
 
 
 
 
432
  analyze_btn = gr.Button("Lakukan Analisis", elem_id="analyze-btn")
433
 
434
  gr.HTML('<div class="section-heading" style="margin-top:20px;">Hasil Analisis Entitas</div>')
435
+ text_output = gr.HTML(value="<p style='color:#64748b;font-size:14px;font-family:DM Sans,sans-serif;padding:20px;text-align:center;'>Masukkan teks lalu klik Lakukan Analisis.</p>")
 
 
 
436
 
 
437
  with gr.Tab("Analisis PDF"):
438
  with gr.Column(elem_id="center-col"):
439
  gr.HTML(
440
+ '<p style="color:#94a3b8;font-family:DM Sans,sans-serif;font-size:13px;margin:0 0 16px;">'
 
441
  'Unggah PDF laporan keuangan (maks. <strong style="color:#a78bfa;">5 halaman</strong>). '
442
+ 'Output: file <strong style="color:#a78bfa;">.rtf</strong> dengan stabilo warna β€” '
443
+ 'bisa dibuka di <strong>Microsoft Word</strong> atau <strong>LibreOffice</strong>.</p>'
 
444
  )
 
445
  gr.HTML('<div class="section-heading">Unggah PDF</div>')
446
+ pdf_input = gr.File(label="", file_types=[".pdf"], type="filepath")
 
 
 
 
447
  analyze_pdf_btn = gr.Button("Highlight Entitas β†’ Download RTF", elem_id="analyze-pdf-btn")
 
448
  pdf_warning = gr.HTML(value="")
449
 
450
  with gr.Column(visible=False) as pdf_results_col:
451
  gr.HTML('<div class="section-heading" style="margin-top:20px;">Download Hasil RTF</div>')
452
+ pdf_output = gr.File(label="", file_types=[".rtf"], interactive=False)
 
 
 
 
453
  gr.HTML('<div class="section-heading" style="margin-top:20px;">Keterangan Warna Entitas</div>')
454
+ pdf_legend = gr.HTML(value="")
455
+ # Debug panel β€” selalu tampil setelah proses
456
+ pdf_debug = gr.HTML(value="")
 
457
 
458
  gr.HTML('<div id="footer">NER for Financial Statements 2026</div>')
459
 
 
465
  text_input.submit(fn=run_ner, inputs=text_input, outputs=text_output)
466
 
467
  def handle_pdf(pdf_file):
468
+ out_path, legend_html, warn_html, debug_html = run_ner_pdf(pdf_file)
469
  show = gr.Column(visible=out_path is not None)
470
+ return out_path, legend_html, warn_html, debug_html, show
471
 
472
  analyze_pdf_btn.click(
473
  fn=handle_pdf,
474
  inputs=pdf_input,
475
+ outputs=[pdf_output, pdf_legend, pdf_warning, pdf_debug, pdf_results_col],
476
  )
477
 
478
  if __name__ == "__main__":