Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
with gr.Tab("Analisis PDF"):
|
| 584 |
-
|
| 585 |
-
gr.
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 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 |
|