Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,10 @@ import tempfile
|
|
| 5 |
import os
|
| 6 |
import re
|
| 7 |
import time
|
| 8 |
-
|
| 9 |
|
| 10 |
# Load model + warmup agar request pertama tidak lambat
|
| 11 |
import time as _t
|
| 12 |
-
print("[STARTUP] Loading NER model...")
|
| 13 |
_t0 = _t.time()
|
| 14 |
ner = pipeline("ner", model="cahya/NusaBert-ner-v1.3", aggregation_strategy="simple")
|
| 15 |
print(f"[STARTUP] Model loaded in {_t.time()-_t0:.2f}s β running warmup...")
|
|
@@ -85,8 +84,7 @@ EXAMPLES = [
|
|
| 85 |
]
|
| 86 |
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
|
| 90 |
def clean_word(word: str) -> str:
|
| 91 |
return word.replace("β", " ").replace("##", "").strip()
|
| 92 |
|
|
@@ -170,8 +168,7 @@ def _build_debug_html(lines: list) -> str:
|
|
| 170 |
</div>"""
|
| 171 |
|
| 172 |
|
| 173 |
-
#
|
| 174 |
-
|
| 175 |
def run_ner(text: str):
|
| 176 |
if not text or not text.strip():
|
| 177 |
return "<p style='color:#94a3b8; font-style:italic; padding:16px;'>Masukkan teks terlebih dahulu.</p>"
|
|
@@ -224,12 +221,10 @@ def run_ner(text: str):
|
|
| 224 |
</div>"""
|
| 225 |
|
| 226 |
|
| 227 |
-
#
|
| 228 |
-
|
| 229 |
def run_ner_pdf(pdf_file):
|
| 230 |
-
# return: (out_path, legend_html, warn_html, debug_html)
|
| 231 |
if pdf_file is None:
|
| 232 |
-
|
| 233 |
|
| 234 |
pdf_path = pdf_file if isinstance(pdf_file, str) else pdf_file.name
|
| 235 |
doc = fitz.open(pdf_path)
|
|
@@ -237,7 +232,7 @@ def run_ner_pdf(pdf_file):
|
|
| 237 |
|
| 238 |
if page_count > MAX_PDF_PAGES:
|
| 239 |
doc.close()
|
| 240 |
-
return
|
| 241 |
<div style="background:#fff1f2;border:1.5px solid #fda4af;border-radius:12px;
|
| 242 |
padding:16px 20px;display:flex;align-items:center;gap:12px;">
|
| 243 |
<span style="font-size:24px;">β οΈ</span>
|
|
@@ -253,7 +248,7 @@ def run_ner_pdf(pdf_file):
|
|
| 253 |
debug_lines = []
|
| 254 |
t_total = time.time()
|
| 255 |
|
| 256 |
-
#
|
| 257 |
t0 = time.time()
|
| 258 |
pages_text = [page.get_text() for page in doc]
|
| 259 |
doc.close()
|
|
@@ -321,14 +316,44 @@ def run_ner_pdf(pdf_file):
|
|
| 321 |
t1 = time.time()
|
| 322 |
debug_lines.append(f"β
<b>Langkah 5</b> β Highlight & build RTF content: <b>{t1-t0:.4f}s</b>")
|
| 323 |
|
| 324 |
-
# ββ LANGKAH 6:
|
| 325 |
t0 = time.time()
|
|
|
|
| 326 |
rtf_full = wrap_rtf(rtf_content)
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
t1 = time.time()
|
| 331 |
-
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
t_end = time.time()
|
| 334 |
debug_lines.append(f"β± <b>TOTAL WAKTU: {t_end-t_total:.2f}s</b>")
|
|
@@ -336,7 +361,7 @@ def run_ner_pdf(pdf_file):
|
|
| 336 |
debug_html = _build_debug_html(debug_lines)
|
| 337 |
found_labels: set[str] = set(entity_map.values())
|
| 338 |
|
| 339 |
-
#
|
| 340 |
legend_rows = ""
|
| 341 |
for label_id in sorted(found_labels):
|
| 342 |
label_idn = LABEL_MAP.get(label_id, label_id)
|
|
@@ -372,11 +397,9 @@ def run_ner_pdf(pdf_file):
|
|
| 372 |
</div>
|
| 373 |
</div>"""
|
| 374 |
|
| 375 |
-
return
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
# ββ CSS βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 379 |
|
|
|
|
| 380 |
CUSTOM_CSS = """
|
| 381 |
@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700;900&family=DM+Sans:wght@400;500;600&display=swap');
|
| 382 |
.gradio-container{max-width:100%!important;padding:0!important;background:linear-gradient(160deg,#0f172a 0%,#1e1b4b 40%,#0f172a 100%)!important;min-height:100vh;}
|
|
@@ -419,8 +442,7 @@ HERO_HTML = """
|
|
| 419 |
"""
|
| 420 |
|
| 421 |
|
| 422 |
-
#
|
| 423 |
-
|
| 424 |
with gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:
|
| 425 |
|
| 426 |
gr.HTML(HERO_HTML)
|
|
@@ -468,7 +490,7 @@ with gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:
|
|
| 468 |
|
| 469 |
with gr.Column(visible=False) as pdf_results_col:
|
| 470 |
gr.HTML('<div class="section-heading" style="margin-top:20px;">Download Hasil RTF</div>')
|
| 471 |
-
pdf_output = gr.
|
| 472 |
gr.HTML('<div class="section-heading" style="margin-top:20px;">Keterangan Warna Entitas</div>')
|
| 473 |
pdf_legend = gr.HTML(value="")
|
| 474 |
# Debug panel β selalu tampil setelah proses
|
|
@@ -484,9 +506,19 @@ with gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:
|
|
| 484 |
text_input.submit(fn=run_ner, inputs=text_input, outputs=text_output)
|
| 485 |
|
| 486 |
def handle_pdf(pdf_file):
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
|
| 491 |
analyze_pdf_btn.click(
|
| 492 |
fn=handle_pdf,
|
|
|
|
| 5 |
import os
|
| 6 |
import re
|
| 7 |
import time
|
| 8 |
+
import base64
|
| 9 |
|
| 10 |
# Load model + warmup agar request pertama tidak lambat
|
| 11 |
import time as _t
|
|
|
|
| 12 |
_t0 = _t.time()
|
| 13 |
ner = pipeline("ner", model="cahya/NusaBert-ner-v1.3", aggregation_strategy="simple")
|
| 14 |
print(f"[STARTUP] Model loaded in {_t.time()-_t0:.2f}s β running warmup...")
|
|
|
|
| 84 |
]
|
| 85 |
|
| 86 |
|
| 87 |
+
# Helpers
|
|
|
|
| 88 |
def clean_word(word: str) -> str:
|
| 89 |
return word.replace("β", " ").replace("##", "").strip()
|
| 90 |
|
|
|
|
| 168 |
</div>"""
|
| 169 |
|
| 170 |
|
| 171 |
+
# NER Teks
|
|
|
|
| 172 |
def run_ner(text: str):
|
| 173 |
if not text or not text.strip():
|
| 174 |
return "<p style='color:#94a3b8; font-style:italic; padding:16px;'>Masukkan teks terlebih dahulu.</p>"
|
|
|
|
| 221 |
</div>"""
|
| 222 |
|
| 223 |
|
| 224 |
+
# NER PDF β RTF
|
|
|
|
| 225 |
def run_ner_pdf(pdf_file):
|
|
|
|
| 226 |
if pdf_file is None:
|
| 227 |
+
return "", "", "<p style='color:#94a3b8;font-style:italic;padding:16px;'>Unggah file PDF terlebih dahulu.</p>", ""
|
| 228 |
|
| 229 |
pdf_path = pdf_file if isinstance(pdf_file, str) else pdf_file.name
|
| 230 |
doc = fitz.open(pdf_path)
|
|
|
|
| 232 |
|
| 233 |
if page_count > MAX_PDF_PAGES:
|
| 234 |
doc.close()
|
| 235 |
+
return "", "", f"""
|
| 236 |
<div style="background:#fff1f2;border:1.5px solid #fda4af;border-radius:12px;
|
| 237 |
padding:16px 20px;display:flex;align-items:center;gap:12px;">
|
| 238 |
<span style="font-size:24px;">β οΈ</span>
|
|
|
|
| 248 |
debug_lines = []
|
| 249 |
t_total = time.time()
|
| 250 |
|
| 251 |
+
# LANGKAH 1: Ekstrak teks
|
| 252 |
t0 = time.time()
|
| 253 |
pages_text = [page.get_text() for page in doc]
|
| 254 |
doc.close()
|
|
|
|
| 316 |
t1 = time.time()
|
| 317 |
debug_lines.append(f"β
<b>Langkah 5</b> β Highlight & build RTF content: <b>{t1-t0:.4f}s</b>")
|
| 318 |
|
| 319 |
+
# ββ LANGKAH 6: Build Download Link ββ
|
| 320 |
t0 = time.time()
|
| 321 |
+
|
| 322 |
rtf_full = wrap_rtf(rtf_content)
|
| 323 |
+
|
| 324 |
+
rtf_bytes = rtf_full.encode(
|
| 325 |
+
"ascii",
|
| 326 |
+
errors="replace"
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
+
b64 = base64.b64encode(
|
| 330 |
+
rtf_bytes
|
| 331 |
+
).decode()
|
| 332 |
+
|
| 333 |
+
download_html = f"""
|
| 334 |
+
<div style="padding:12px 0;">
|
| 335 |
+
<a
|
| 336 |
+
href="data:application/rtf;base64,{b64}"
|
| 337 |
+
download="hasil_ner.rtf"
|
| 338 |
+
style="
|
| 339 |
+
display:inline-block;
|
| 340 |
+
background:linear-gradient(135deg,#1e40af,#6d28d9);
|
| 341 |
+
color:white;
|
| 342 |
+
text-decoration:none;
|
| 343 |
+
padding:12px 20px;
|
| 344 |
+
border-radius:10px;
|
| 345 |
+
font-weight:600;
|
| 346 |
+
font-family:'DM Sans',sans-serif;
|
| 347 |
+
">
|
| 348 |
+
π₯ Download Hasil RTF
|
| 349 |
+
</a>
|
| 350 |
+
</div>
|
| 351 |
+
"""
|
| 352 |
t1 = time.time()
|
| 353 |
+
|
| 354 |
+
debug_lines.append(
|
| 355 |
+
f"β
<b>Langkah 6</b> β Build Download Link: <b>{t1-t0:.4f}s</b>"
|
| 356 |
+
)
|
| 357 |
|
| 358 |
t_end = time.time()
|
| 359 |
debug_lines.append(f"β± <b>TOTAL WAKTU: {t_end-t_total:.2f}s</b>")
|
|
|
|
| 361 |
debug_html = _build_debug_html(debug_lines)
|
| 362 |
found_labels: set[str] = set(entity_map.values())
|
| 363 |
|
| 364 |
+
# Legend
|
| 365 |
legend_rows = ""
|
| 366 |
for label_id in sorted(found_labels):
|
| 367 |
label_idn = LABEL_MAP.get(label_id, label_id)
|
|
|
|
| 397 |
</div>
|
| 398 |
</div>"""
|
| 399 |
|
| 400 |
+
return download_html, legend_html, "", debug_html
|
|
|
|
|
|
|
|
|
|
| 401 |
|
| 402 |
+
# CSS
|
| 403 |
CUSTOM_CSS = """
|
| 404 |
@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700;900&family=DM+Sans:wght@400;500;600&display=swap');
|
| 405 |
.gradio-container{max-width:100%!important;padding:0!important;background:linear-gradient(160deg,#0f172a 0%,#1e1b4b 40%,#0f172a 100%)!important;min-height:100vh;}
|
|
|
|
| 442 |
"""
|
| 443 |
|
| 444 |
|
| 445 |
+
# Gradio UI
|
|
|
|
| 446 |
with gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:
|
| 447 |
|
| 448 |
gr.HTML(HERO_HTML)
|
|
|
|
| 490 |
|
| 491 |
with gr.Column(visible=False) as pdf_results_col:
|
| 492 |
gr.HTML('<div class="section-heading" style="margin-top:20px;">Download Hasil RTF</div>')
|
| 493 |
+
pdf_output = gr.HTML(value="")
|
| 494 |
gr.HTML('<div class="section-heading" style="margin-top:20px;">Keterangan Warna Entitas</div>')
|
| 495 |
pdf_legend = gr.HTML(value="")
|
| 496 |
# Debug panel β selalu tampil setelah proses
|
|
|
|
| 506 |
text_input.submit(fn=run_ner, inputs=text_input, outputs=text_output)
|
| 507 |
|
| 508 |
def handle_pdf(pdf_file):
|
| 509 |
+
download_html, legend_html, warn_html, debug_html = run_ner_pdf(pdf_file)
|
| 510 |
+
|
| 511 |
+
show = gr.Column(
|
| 512 |
+
visible=bool(download_html)
|
| 513 |
+
)
|
| 514 |
+
|
| 515 |
+
return (
|
| 516 |
+
download_html,
|
| 517 |
+
legend_html,
|
| 518 |
+
warn_html,
|
| 519 |
+
debug_html,
|
| 520 |
+
show
|
| 521 |
+
)
|
| 522 |
|
| 523 |
analyze_pdf_btn.click(
|
| 524 |
fn=handle_pdf,
|