RangGaraga commited on
Commit
08a4e58
Β·
verified Β·
1 Parent(s): c611062

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -77
app.py CHANGED
@@ -1,13 +1,13 @@
1
- import os
2
- import fitz
3
- import tempfile
4
  import gradio as gr
5
  from transformers import pipeline
 
 
 
6
 
7
- # Load model
8
  ner = pipeline("ner", model="cahya/NusaBert-ner-v1.3", aggregation_strategy="simple")
9
 
10
- # Label mapping
11
  LABEL_MAP = {
12
  "CRD": "Kardinal",
13
  "DAT": "Tanggal",
@@ -30,7 +30,7 @@ LABEL_MAP = {
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
@@ -79,7 +79,7 @@ LABEL_HEX = {
79
 
80
  MAX_PDF_PAGES = 5
81
 
82
- # Example texts
83
  EXAMPLES = [
84
  (
85
  "Contoh 1 – RUPS & Dana Cadangan",
@@ -123,7 +123,7 @@ EXAMPLES = [
123
  ]
124
 
125
 
126
- # Helper clean entity word
127
  def clean_word(word: str) -> str:
128
  return word.replace("▁", " ").replace("##", "").strip()
129
 
@@ -133,7 +133,7 @@ def get_label_id(raw_label: str) -> str:
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>"
@@ -195,7 +195,7 @@ def run_ner(text: str):
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
 
@@ -259,7 +259,7 @@ def run_ner_pdf(pdf_file):
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))
@@ -270,13 +270,13 @@ def run_ner_pdf(pdf_file):
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)
@@ -501,6 +501,14 @@ textarea:focus {
501
  font-family: 'DM Sans', sans-serif !important;
502
  }
503
 
 
 
 
 
 
 
 
 
504
  /* ── Footer ── */
505
  #footer {
506
  text-align: center;
@@ -547,78 +555,76 @@ HERO_HTML = """
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
 
 
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import fitz # PyMuPDF
4
+ import tempfile
5
+ import os
6
 
7
+ # ─── Load model ───────────────────────────────────────────────────────────────
8
  ner = pipeline("ner", model="cahya/NusaBert-ner-v1.3", aggregation_strategy="simple")
9
 
10
+ # ─── Label mapping ────────────────────────────────────────────────────────────
11
  LABEL_MAP = {
12
  "CRD": "Kardinal",
13
  "DAT": "Tanggal",
 
30
  "LAN": "Bahasa",
31
  }
32
 
33
+ # ─── Color palette per label (RGB 0–1) ────────────────────────────────────────
34
  # Each label gets a distinct, vivid but elegant highlight color
35
  LABEL_COLORS = {
36
  "PER": (1.0, 0.75, 0.75), # rose
 
79
 
80
  MAX_PDF_PAGES = 5
81
 
82
+ # ─── Example texts ────────────────────────────────────────────────────────────
83
  EXAMPLES = [
84
  (
85
  "Contoh 1 – RUPS & Dana Cadangan",
 
123
  ]
124
 
125
 
126
+ # ─── Helper: clean entity word ────────────────────────────────────────────────
127
  def clean_word(word: str) -> str:
128
  return word.replace("▁", " ").replace("##", "").strip()
129
 
 
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>"
 
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
 
 
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))
 
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)
 
501
  font-family: 'DM Sans', sans-serif !important;
502
  }
503
 
504
+ /* ── Centered single column ── */
505
+ #center-col {
506
+ max-width: 780px !important;
507
+ margin: 0 auto !important;
508
+ width: 100% !important;
509
+ padding: 0 8px !important;
510
+ }
511
+
512
  /* ── Footer ── */
513
  #footer {
514
  text-align: center;
 
555
  </div>
556
  """
557
 
558
+ # ─── Build Gradio UI ───────────────────────────────────────────────────────────
559
  with gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:
560
 
561
  gr.HTML(HERO_HTML)
562
 
563
  with gr.Tabs(elem_classes="tab-nav"):
564
+
565
+ # ════════════════════ TAB 1 β€” TEXT INPUT ════════════════════
566
+ with gr.Tab("πŸ“ Analisis Teks"):
567
+
568
+ with gr.Column(elem_id="center-col"):
569
+ gr.HTML('<div class="section-heading">Contoh Teks</div>')
570
+ example_btns = []
571
+ for title, body in EXAMPLES:
572
+ btn = gr.Button(f"πŸ“Œ {title}\n\n{body[:120]}…", elem_classes="example-btn")
573
+ example_btns.append((btn, body))
574
+
575
+ gr.HTML('<div class="section-heading" style="margin-top:20px;">Input Teks</div>')
576
+ text_input = gr.Textbox(
577
+ lines=9,
578
+ placeholder="Ketik atau tempel teks laporan keuangan di sini…",
579
+ label="",
580
+ show_label=False,
581
+ )
582
+ analyze_btn = gr.Button("πŸ” Lakukan Analisis", elem_id="analyze-btn")
583
+
584
+ gr.HTML('<div class="section-heading" style="margin-top:20px;">Hasil Analisis Entitas</div>')
585
+ text_output = gr.HTML(
586
+ value="<p style='color:#64748b; font-size:14px; font-family:DM Sans,sans-serif;"
587
+ "padding:20px; text-align:center;'>⬆ Masukkan teks lalu klik <strong>Lakukan Analisis</strong>.</p>"
588
+ )
589
+
590
+ # ════════════════════ TAB 2 β€” PDF ════════════════════
591
+ with gr.Tab("πŸ“„ Analisis PDF"):
592
+
593
+ with gr.Column(elem_id="center-col"):
594
+ gr.HTML(
595
+ '<p style="color:#94a3b8; font-family:DM Sans,sans-serif; font-size:13px; '
596
+ 'margin:0 0 16px;">'
597
+ '⚑ Unggah PDF laporan keuangan (maks. <strong style="color:#a78bfa;">5 halaman</strong>). '
598
+ 'Entitas yang terdeteksi akan diberi warna berbeda di dalam PDF.</p>'
599
+ )
600
+
601
+ gr.HTML('<div class="section-heading">Unggah PDF</div>')
602
+ pdf_input = gr.File(
603
+ label="",
604
+ file_types=[".pdf"],
605
+ type="filepath",
606
+ )
607
+ analyze_pdf_btn = gr.Button("🎨 Highlight Entitas PDF", elem_id="analyze-pdf-btn")
608
+
609
+ # Warning / error area
610
+ pdf_warning = gr.HTML(value="")
611
+
612
+ gr.HTML('<div class="section-heading" style="margin-top:20px;">Download Hasil PDF</div>')
613
+ pdf_output = gr.File(
614
+ label="",
615
+ file_types=[".pdf"],
616
+ interactive=False,
617
+ )
618
+
619
+ gr.HTML('<div class="section-heading" style="margin-top:20px;">Keterangan Warna Entitas</div>')
620
+ pdf_legend = gr.HTML(
621
+ value="<p style='color:#64748b; font-size:14px; font-family:DM Sans,sans-serif;"
622
+ "padding:16px; text-align:center;'>⬆ Unggah PDF lalu klik tombol Highlight.</p>"
623
+ )
 
 
624
 
625
  gr.HTML('<div id="footer">NER for Financial Statements Β· NusaBERT Β· Bahasa Indonesia Β· 2024</div>')
626
 
627
+ # ─── Wiring ──────────────────────────────────────────────────────────────
628
  for btn, body in example_btns:
629
  btn.click(fn=lambda b=body: b, inputs=[], outputs=text_input)
630