RangGaraga commited on
Commit
c353c3c
·
verified ·
1 Parent(s): d8d5d31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +180 -158
app.py CHANGED
@@ -1,8 +1,9 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- import fitz
4
  import tempfile
5
  import os
 
6
 
7
  # Load model
8
  ner = pipeline("ner", model="cahya/NusaBert-ner-v1.3", aggregation_strategy="simple")
@@ -30,55 +31,51 @@ LABEL_MAP = {
30
  "LAN": "Bahasa",
31
  }
32
 
33
- # Color palette per label (RGB 0–1)
34
- LABEL_COLORS = {
35
- "PER": (1.0, 0.75, 0.75), # rose
36
- "ORG": (0.68, 0.85, 1.0), # sky blue
37
- "NOR": (0.55, 0.78, 1.0), # cornflower
38
- "LOC": (0.72, 1.0, 0.72), # mint green
39
- "GPE": (0.60, 0.95, 0.80), # seafoam
40
- "FAC": (0.85, 1.0, 0.65), # lime
41
- "DAT": (1.0, 0.90, 0.55), # amber
42
- "TIM": (1.0, 0.80, 0.40), # orange-gold
43
- "MON": (0.80, 1.0, 0.85), # pale green
44
- "CRD": (0.95, 0.80, 1.0), # lavender
45
- "ORD": (0.88, 0.75, 1.0), # violet
46
- "PRC": (1.0, 0.95, 0.70), # pale yellow
47
- "QTY": (0.78, 0.95, 0.95), # cyan-light
48
- "LAW": (1.0, 0.72, 0.72), # salmon
49
- "EVT": (1.0, 0.85, 0.65), # peach
50
- "PRD": (0.75, 0.90, 1.0), # baby blue
51
- "REG": (0.90, 0.85, 1.0), # periwinkle
52
- "WOA": (1.0, 0.90, 0.85), # blush
53
- "LAN": (0.80, 1.0, 0.95), # aqua
 
 
 
 
 
 
 
 
 
54
  }
55
 
56
- # CSS hex equivalents for the legend table
57
  LABEL_HEX = {
58
- "PER": "#FFBFBF",
59
- "ORG": "#AEDDFF",
60
- "NOR": "#8DC7FF",
61
- "LOC": "#B8FFB8",
62
- "GPE": "#99F2CC",
63
- "FAC": "#D9FF A5",
64
- "DAT": "#FFE58C",
65
- "TIM": "#FFCC66",
66
- "MON": "#CCFFDA",
67
- "CRD": "#F2CCFF",
68
- "ORD": "#E0BFFF",
69
- "PRC": "#FFF2B2",
70
- "QTY": "#C7F2F2",
71
- "LAW": "#FFBABA",
72
- "EVT": "#FFD9A5",
73
- "PRD": "#BFDFFF",
74
- "REG": "#E6DAFF",
75
- "WOA": "#FFE6DA",
76
  "LAN": "#CCFFF2",
77
  }
78
 
79
  MAX_PDF_PAGES = 5
80
 
81
- # Example texts
82
  EXAMPLES = [
83
  (
84
  "Contoh 1 – RUPS & Dana Cadangan",
@@ -122,7 +119,7 @@ EXAMPLES = [
122
  ]
123
 
124
 
125
- # Helper clean entity word
126
  def clean_word(word: str) -> str:
127
  return word.replace("▁", " ").replace("##", "").strip()
128
 
@@ -132,7 +129,98 @@ def get_label_id(raw_label: str) -> str:
132
  return label_id.split("-")[-1].upper().strip()
133
 
134
 
135
- # Text NER
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  def run_ner(text: str):
137
  if not text or not text.strip():
138
  return "<p style='color:#94a3b8; font-style:italic; padding:16px;'>Masukkan teks terlebih dahulu.</p>"
@@ -143,7 +231,7 @@ def run_ner(text: str):
143
 
144
  rows_html = ""
145
  row_num = 1
146
- seen_words = set() # ← tambah ini
147
 
148
  for ent in results:
149
  raw_label = ent["entity_group"]
@@ -152,7 +240,6 @@ def run_ner(text: str):
152
  word = clean_word(ent["word"])
153
  if not word:
154
  continue
155
-
156
  word_key = word.lower()
157
  if word_key in seen_words:
158
  continue
@@ -160,11 +247,10 @@ def run_ner(text: str):
160
 
161
  hex_color = LABEL_HEX.get(label_id, "#e2e8f0")
162
  score = f"{ent['score']:.2%}"
163
- even = row_num % 2 == 0
164
- row_bg = "#f8faff" if even else "#ffffff"
165
 
166
  rows_html += f"""
167
- <tr style="background:{row_bg}; transition:background 0.15s;">
168
  <td style="padding:9px 14px; border-bottom:1px solid #e8edf5; text-align:center;
169
  color:#64748b; font-size:12px;">{row_num}</td>
170
  <td style="padding:9px 14px; border-bottom:1px solid #e8edf5; font-weight:600;
@@ -181,7 +267,7 @@ def run_ner(text: str):
181
  if not rows_html:
182
  return "<p style='color:#94a3b8; font-style:italic; padding:16px;'>Tidak ada entitas yang ditemukan.</p>"
183
 
184
- table_html = f"""
185
  <div style="overflow-x:auto; margin-top:4px; border-radius:12px;
186
  border:1px solid #e2e8f0; box-shadow:0 2px 12px rgba(0,0,0,0.06);">
187
  <table style="width:100%; border-collapse:collapse; font-size:14px;
@@ -201,9 +287,10 @@ def run_ner(text: str):
201
  <tbody>{rows_html}</tbody>
202
  </table>
203
  </div>"""
204
- return table_html
205
 
206
- # PDF NER
 
 
207
  def run_ner_pdf(pdf_file):
208
  if pdf_file is None:
209
  return (
@@ -216,9 +303,10 @@ def run_ner_pdf(pdf_file):
216
  doc = fitz.open(pdf_path)
217
  page_count = len(doc)
218
 
 
219
  if page_count > MAX_PDF_PAGES:
220
  doc.close()
221
- warn_html = f"""
222
  <div style="background:#fff1f2; border:1.5px solid #fda4af; border-radius:12px;
223
  padding:16px 20px; display:flex; align-items:center; gap:12px;">
224
  <span style="font-size:24px;">⚠️</span>
@@ -231,66 +319,48 @@ def run_ner_pdf(pdf_file):
231
  </p>
232
  </div>
233
  </div>"""
234
- return None, "", warn_html
235
-
236
- pages_words = []
237
- full_text_parts = []
238
 
 
 
239
  for page in doc:
240
- raw_words = page.get_text("words")
241
- pages_words.append(raw_words)
242
- full_text_parts.append(" ".join(w[4] for w in raw_words))
243
 
244
- full_text = " ".join(full_text_parts)
 
 
245
  ner_results = ner(full_text.strip())
246
 
247
  if not ner_results:
248
- doc.close()
249
  return None, "", "<p style='color:#94a3b8; padding:16px;'>Tidak ada entitas yang ditemukan dalam PDF.</p>"
250
 
 
251
  entity_map: dict[str, str] = {}
252
  for ent in ner_results:
253
  word = clean_word(ent["word"])
254
  if len(word) < 2:
255
  continue
256
  label_id = get_label_id(ent["entity_group"])
257
- if word.lower() not in entity_map:
258
- entity_map[word.lower()] = label_id
 
 
259
 
260
  if not entity_map:
261
- doc.close()
262
  return None, "", "<p style='color:#94a3b8; padding:16px;'>Tidak ada entitas yang ditemukan dalam PDF.</p>"
263
 
264
- found_labels: set[str] = set()
 
265
 
266
- for page_idx, page in enumerate(doc):
267
- raw_words = pages_words[page_idx]
268
 
269
- for w in raw_words:
270
- word_text = w[4]
271
- word_lower = word_text.lower().strip()
272
 
273
- if not word_lower or len(word_lower) < 2:
274
- continue
275
-
276
- # O(1) lookup — langsung cek di dict
277
- label_id = entity_map.get(word_lower)
278
- if label_id is None:
279
- continue
280
-
281
- # Koordinat sudah ada, langsung buat Rect dan highlight
282
- rect = fitz.Rect(w[0], w[1], w[2], w[3])
283
- color = LABEL_COLORS.get(label_id, (0.9, 0.9, 0.9))
284
- annot = page.add_highlight_annot(rect)
285
- annot.set_colors(stroke=list(color))
286
- annot.update()
287
- found_labels.add(label_id)
288
-
289
- # Simpan PDF hasil
290
- out_fd, out_path = tempfile.mkstemp(suffix=".pdf")
291
- os.close(out_fd)
292
- doc.save(out_path)
293
- doc.close()
294
 
295
  # Legend
296
  legend_rows = ""
@@ -314,7 +384,8 @@ def run_ner_pdf(pdf_file):
314
  <div style="margin-top:4px;">
315
  <p style="font-size:13px; color:#64748b; margin-bottom:10px;">
316
  Ditemukan <strong style='color:#1e40af;'>{len(found_labels)}</strong> jenis entitas
317
- di <strong style='color:#1e40af;'>{page_count}</strong> halaman PDF.
 
318
  </p>
319
  <div style="overflow-x:auto; border-radius:12px; border:1px solid #e2e8f0;
320
  box-shadow:0 2px 12px rgba(0,0,0,0.06);">
@@ -335,20 +406,18 @@ def run_ner_pdf(pdf_file):
335
  </div>"""
336
 
337
  return out_path, legend_html, ""
338
-
339
- # Custom CSS
 
340
  CUSTOM_CSS = """
341
  @import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700;900&family=DM+Sans:wght@400;500;600&display=swap');
342
 
343
- /* ── Root & Background ── */
344
  .gradio-container {
345
  max-width: 100% !important;
346
  padding: 0 !important;
347
  background: linear-gradient(160deg, #0f172a 0%, #1e1b4b 40%, #0f172a 100%) !important;
348
  min-height: 100vh;
349
  }
350
-
351
- /* ── Hero header ── */
352
  #hero-header {
353
  background: linear-gradient(135deg, #1e40af 0%, #6d28d9 50%, #be185d 100%);
354
  padding: 40px 48px 36px;
@@ -363,18 +432,6 @@ CUSTOM_CSS = """
363
  position: absolute; inset: 0;
364
  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");
365
  }
366
-
367
- /* ── Section cards ── */
368
- .section-card {
369
- background: rgba(255,255,255,0.04) !important;
370
- border: 1px solid rgba(255,255,255,0.10) !important;
371
- border-radius: 16px !important;
372
- padding: 24px !important;
373
- margin: 0 32px 24px !important;
374
- backdrop-filter: blur(10px);
375
- }
376
-
377
- /* ── Tabs ── */
378
  .tab-nav {
379
  background: rgba(255,255,255,0.05) !important;
380
  border-radius: 12px !important;
@@ -397,8 +454,6 @@ CUSTOM_CSS = """
397
  color: #ffffff !important;
398
  box-shadow: 0 4px 12px rgba(109,40,217,0.4) !important;
399
  }
400
-
401
- /* ── Labels ── */
402
  label span {
403
  color: #cbd5e1 !important;
404
  font-family: 'DM Sans', sans-serif !important;
@@ -407,8 +462,6 @@ label span {
407
  letter-spacing: 0.07em !important;
408
  text-transform: uppercase !important;
409
  }
410
-
411
- /* ── Textbox ── */
412
  textarea, .gr-textbox textarea {
413
  background: rgba(15,23,42,0.7) !important;
414
  border: 1.5px solid rgba(255,255,255,0.12) !important;
@@ -423,8 +476,6 @@ textarea:focus {
423
  border-color: #6d28d9 !important;
424
  outline: none !important;
425
  }
426
-
427
- /* ── Example buttons ── */
428
  .example-btn {
429
  display: block !important;
430
  width: 100% !important;
@@ -449,8 +500,6 @@ textarea:focus {
449
  color: #e2e8f0 !important;
450
  transform: translateX(3px);
451
  }
452
-
453
- /* ── Analyze button ── */
454
  #analyze-btn, #analyze-pdf-btn {
455
  background: linear-gradient(135deg, #1e40af, #6d28d9) !important;
456
  color: white !important;
@@ -469,8 +518,6 @@ textarea:focus {
469
  transform: translateY(-2px) !important;
470
  box-shadow: 0 6px 24px rgba(109,40,217,0.50) !important;
471
  }
472
-
473
- /* ── Section headings ── */
474
  .section-heading {
475
  font-family: 'DM Sans', sans-serif;
476
  font-weight: 700;
@@ -491,31 +538,12 @@ textarea:focus {
491
  background: linear-gradient(90deg, #6d28d9, #be185d);
492
  border-radius: 2px;
493
  }
494
-
495
- /* ── Output HTML container ── */
496
- .output-box {
497
- background: rgba(15,23,42,0.6) !important;
498
- border: 1px solid rgba(255,255,255,0.10) !important;
499
- border-radius: 12px !important;
500
- padding: 16px !important;
501
- min-height: 80px;
502
- }
503
-
504
- /* Placeholder teks di output */
505
- .output-box p, #center-col p {
506
- color: #94a3b8 !important;
507
- }
508
-
509
-
510
- /* ── File upload ── */
511
  .gr-file {
512
  background: rgba(15,23,42,0.7) !important;
513
  border: 1.5px dashed rgba(99,102,241,0.40) !important;
514
  border-radius: 10px !important;
515
  color: #94a3b8 !important;
516
  }
517
-
518
- /* ── Download button ── */
519
  .gr-file-download {
520
  background: rgba(30,64,175,0.2) !important;
521
  border: 1px solid rgba(99,102,241,0.4) !important;
@@ -523,16 +551,12 @@ textarea:focus {
523
  color: #a5b4fc !important;
524
  font-family: 'DM Sans', sans-serif !important;
525
  }
526
-
527
- /* ── Centered single column ── */
528
  #center-col {
529
  max-width: 780px !important;
530
  margin: 0 auto !important;
531
  width: 100% !important;
532
  padding: 0 8px !important;
533
  }
534
-
535
- /* ── Footer ── */
536
  #footer {
537
  text-align: center;
538
  padding: 20px;
@@ -541,11 +565,12 @@ textarea:focus {
541
  font-size: 12px;
542
  letter-spacing: 0.04em;
543
  }
544
-
545
- /* ── Markdown headings in dark ── */
546
  .gradio-container h3 {
547
  color: #e2e8f0 !important;
548
  }
 
 
 
549
  """
550
 
551
  HERO_HTML = """
@@ -553,19 +578,16 @@ HERO_HTML = """
553
  <p style="font-family:'DM Sans',sans-serif; font-size:12px; font-weight:700;
554
  letter-spacing:0.20em; text-transform:uppercase; color:rgba(165,180,252,0.8);
555
  margin:0 0 10px;">Tugas Kelompok · NLP &amp; Text Mining</p>
556
-
557
  <h1 style="font-family:'Playfair Display',serif; font-size:clamp(32px,5vw,56px);
558
  font-weight:900; color:#ffffff; margin:0 0 8px; line-height:1.1;
559
  text-shadow: 0 2px 20px rgba(109,40,217,0.5);">
560
  NER <span style="color:#a78bfa;">for</span> Financial Statements
561
  </h1>
562
-
563
  <p style="font-family:'DM Sans',sans-serif; font-size:clamp(13px,2vw,16px);
564
  color:rgba(203,213,225,0.85); margin:0 auto 18px; max-width:640px; line-height:1.6;">
565
  Implementasi Named Entity Recognition pada Kumpulan<br>
566
  Laporan-laporan Keuangan Bahasa Indonesia
567
  </p>
568
-
569
  <div style="display:inline-flex; align-items:center; gap:8px; background:rgba(0,0,0,0.25);
570
  border:1px solid rgba(255,255,255,0.15); border-radius:20px;
571
  padding:6px 16px 6px 8px;">
@@ -578,7 +600,8 @@ HERO_HTML = """
578
  </div>
579
  """
580
 
581
- # Build Gradio UI
 
582
  with gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:
583
 
584
  gr.HTML(HERO_HTML)
@@ -587,7 +610,6 @@ with gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:
587
 
588
  # TAB 1 — TEXT INPUT
589
  with gr.Tab("Analisis Teks"):
590
-
591
  with gr.Column(elem_id="center-col"):
592
  gr.HTML('<div class="section-heading">Contoh Teks</div>')
593
  example_btns = []
@@ -610,15 +632,16 @@ with gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:
610
  "padding:20px; text-align:center;'>Masukkan teks lalu klik Lakukan Analisis.</p>"
611
  )
612
 
613
- # TAB 2 — PDF
614
  with gr.Tab("Analisis PDF"):
615
-
616
  with gr.Column(elem_id="center-col"):
617
  gr.HTML(
618
  '<p style="color:#94a3b8; font-family:DM Sans,sans-serif; font-size:13px; '
619
  'margin:0 0 16px;">'
620
  'Unggah PDF laporan keuangan (maks. <strong style="color:#a78bfa;">5 halaman</strong>). '
621
- 'Entitas yang terdeteksi akan diberi warna berbeda di dalam PDF.</p>'
 
 
622
  )
623
 
624
  gr.HTML('<div class="section-heading">Unggah PDF</div>')
@@ -627,16 +650,15 @@ with gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:
627
  file_types=[".pdf"],
628
  type="filepath",
629
  )
630
- analyze_pdf_btn = gr.Button("Highlight Entitas PDF", elem_id="analyze-pdf-btn")
631
 
632
- # Warning / error area
633
  pdf_warning = gr.HTML(value="")
634
 
635
- with gr.Column(visible=False, elem_id="pdf-results-col") as pdf_results_col:
636
- gr.HTML('<div class="section-heading" style="margin-top:20px;">Download Hasil PDF</div>')
637
  pdf_output = gr.File(
638
  label="",
639
- file_types=[".pdf"],
640
  interactive=False,
641
  )
642
  gr.HTML('<div class="section-heading" style="margin-top:20px;">Keterangan Warna Entitas</div>')
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ 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")
 
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",
 
119
  ]
120
 
121
 
122
+ # Helpers
123
  def clean_word(word: str) -> str:
124
  return word.replace("▁", " ").replace("##", "").strip()
125
 
 
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"
213
+ f"{color_table}\n"
214
+ "\\widowctrl\\wpaper12240\\wpaperh15840\n"
215
+ "\\margl1800\\margr1800\\margt1440\\margb1440\n"
216
+ "\\f0\\fs24\\sl360\\slmult1\n"
217
+ f"{content_rtf}"
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>"
 
231
 
232
  rows_html = ""
233
  row_num = 1
234
+ seen_words = set()
235
 
236
  for ent in results:
237
  raw_label = ent["entity_group"]
 
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
 
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;
 
267
  if not rows_html:
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;
 
287
  <tbody>{rows_html}</tbody>
288
  </table>
289
  </div>"""
 
290
 
291
+
292
+ # ── NER PDF → RTF ─────────────────────────────────────────────────────────────
293
+
294
  def run_ner_pdf(pdf_file):
295
  if pdf_file is None:
296
  return (
 
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>
 
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 = ""
 
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);">
 
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;
 
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;
 
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;
 
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;
 
476
  border-color: #6d28d9 !important;
477
  outline: none !important;
478
  }
 
 
479
  .example-btn {
480
  display: block !important;
481
  width: 100% !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;
 
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;
 
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;
 
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;
 
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 = """
 
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;">
 
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)
 
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>')
615
  example_btns = []
 
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>')
 
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>')