Spaces:
Sleeping
Sleeping
Update app.py
Browse files4 unmodified lines5678910111213141569 unmodified lines858687888990919277 unmodified lines17017117217317417517617746 unmodified lines2242252262272282292302312322332342351 unmodified line2372382392402412422439 unmodified lines25325425525625725825961 unmodified lines3213223233243253263273283293303313323333341 unmodified line33633733833934034134229 unmodified lines37237337437537637737837938038138236 unmodified lines41942042142242342442542641 unmodified lines4684694704714724734749 unmodified lines4844854864874884894904914924 unmodified linesimport osimport reimport time# Load model + warmup agar request pertama tidak lambatimport time as _tprint("[STARTUP] Loading NER model...")_t0 = _t.time()ner = pipeline("ner", model="cahya/NusaBert-ner-v1.3", aggregation_strategy="simple")print(f"[STARTUP] Model loaded in {_t.time()-_t0:.2f}s β running warmup...")69 unmodified lines]# ββ Helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef clean_word(word: str) -> str:return word.replace("β", " ").replace("##", "").strip()77 unmodified lines</div>"""# ββ NER Teks βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef run_ner(text: str):if not text or not text.strip():return "<p style='color:#94a3b8; font-style:italic; padding:16px;'>Masukkan teks terlebih dahulu.</p>"46 unmodified lines</div>"""# ββ NER PDF β RTF βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef run_ner_pdf(pdf_file):# return: (out_path, legend_html, warn_html, debug_html)if pdf_file is None:return None, "", "<p style='color:#94a3b8;font-style:italic;padding:16px;'>Unggah file PDF terlebih dahulu.</p>", ""pdf_path = pdf_file if isinstance(pdf_file, str) else pdf_file.namedoc = fitz.open(pdf_path)1 unmodified lineif page_count > MAX_PDF_PAGES:doc.close()return None, "", f"""<div style="background:#fff1f2;border:1.5px solid #fda4af;border-radius:12px;padding:16px 20px;display:flex;align-items:center;gap:12px;"><span style="font-size:24px;">β οΈ</span>9 unmodified linesdebug_lines = []t_total = time.time()# ββ LANGKAH 1: Ekstrak teks ββt0 = time.time()pages_text = [page.get_text() for page in doc]doc.close()61 unmodified linest1 = time.time()debug_lines.append(f"β <b>Langkah 5</b> β Highlight & build RTF content: <b>{t1-t0:.4f}s</b>")# ββ LANGKAH 6: Tulis file .rtf ββt0 = time.time()rtf_full = wrap_rtf(rtf_content)out_fd, out_path = tempfile.mkstemp(suffix=".rtf")with os.fdopen(out_fd, "w", encoding="ascii", errors="replace") as f:f.write(rtf_full)t1 = time.time()debug_lines.append(f"β <b>Langkah 6</b> β Tulis file RTF: <b>{t1-t0:.4f}s</b> | {len(rtf_full):,} bytes")t_end = time.time()debug_lines.append(f"β± <b>TOTAL WAKTU: {t_end-t_total:.2f}s</b>")1 unmodified linedebug_html = _build_debug_html(debug_lines)found_labels: set[str] = set(entity_map.values())# ββ Legend ββlegend_rows = ""for label_id in sorted(found_labels):label_idn = LABEL_MAP.get(label_id, label_id)29 unmodified lines</div></div>"""return out_path, legend_html, "", debug_html# ββ CSS βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββCUSTOM_CSS = """@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700;900&family=DM+Sans:wght@400;500;600&display=swap');.gradio-container{max-width:100%!important;padding:0!important;background:linear-gradient(160deg,#0f172a 0%,#1e1b4b 40%,#0f172a 100%)!important;min-height:100vh;}36 unmodified lines"""# ββ Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββwith gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:gr.HTML(HERO_HTML)41 unmodified lineswith gr.Column(visible=False) as pdf_results_col:gr.HTML('<div class="section-heading" style="margin-top:20px;">Download Hasil RTF</div>')pdf_output = gr.File(label="", file_types=[".rtf"], interactive=False)gr.HTML('<div class="section-heading" style="margin-top:20px;">Keterangan Warna Entitas</div>')pdf_legend = gr.HTML(value="")# Debug panel β selalu tampil setelah proses9 unmodified linestext_input.submit(fn=run_ner, inputs=text_input, outputs=text_output)def handle_pdf(pdf_file):out_path, legend_html, warn_html, debug_html = run_ner_pdf(pdf_file)show = gr.Column(visible=out_path is not None)return out_path, legend_html, warn_html, debug_html, showanalyze_pdf_btn.click(fn=handle_pdf,4 unmodified lines56789101112131469 unmodified lines8485868788899077 unmodified lines16816917017117217317446 unmodified lines2212222232242252262272282292301 unmodified line2322332342352362372389 unmodified lines24824925025125225325461 unmodified lines3163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583591 unmodified line36136236336436536636729 unmodified lines39739839940040140240340440536 unmodified lines44244344444544644744841 unmodified lines4904914924934944954969 unmodified lines5065075085095105115125135145155165175185195205215225235244 unmodified linesimport osimport reimport timeimport base64# Load model + warmup agar request pertama tidak lambatimport time as _t_t0 = _t.time()ner = pipeline("ner", model="cahya/NusaBert-ner-v1.3", aggregation_strategy="simple")print(f"[STARTUP] Model loaded in {_t.time()-_t0:.2f}s β running warmup...")69 unmodified lines]# Helpersdef clean_word(word: str) -> str:return word.replace("β", " ").replace("##", "").strip()77 unmodified lines</div>"""# NER Teksdef run_ner(text: str):if not text or not text.strip():return "<p style='color:#94a3b8; font-style:italic; padding:16px;'>Masukkan teks terlebih dahulu.</p>"46 unmodified lines</div>"""# NER PDF β RTFdef run_ner_pdf(pdf_file):if pdf_file is None:return "", "", "<p style='color:#94a3b8;font-style:italic;padding:16px;'>Unggah file PDF terlebih dahulu.</p>", ""pdf_path = pdf_file if isinstance(pdf_file, str) else pdf_file.namedoc = fitz.open(pdf_path)1 unmodified lineif page_count > MAX_PDF_PAGES:doc.close()return "", "", f"""<div style="background:#fff1f2;border:1.5px solid #fda4af;border-radius:12px;padding:16px 20px;display:flex;align-items:center;gap:12px;"><span style="font-size:24px;">β οΈ</span>9 unmodified linesdebug_lines = []t_total = time.time()# LANGKAH 1: Ekstrak tekst0 = time.time()pages_text = [page.get_text() for page in doc]doc.close()61 unmodified linest1 = time.time()debug_lines.append(f"β <b>Langkah 5</b> β Highlight & build RTF content: <b>{t1-t0:.4f}s</b>")# ββ LANGKAH 6: Build Download Link ββt0 = time.time()rtf_full = wrap_rtf(rtf_content)rtf_bytes = rtf_full.encode("ascii",errors="replace")b64 = base64.b64encode(rtf_bytes).decode()download_html = f"""<div style="padding:12px 0;"><ahref="data:application/rtf;base64,{b64}"download="hasil_ner.rtf"style="display:inline-block;background:linear-gradient(135deg,#1e40af,#6d28d9);color:white;text-decoration:none;padding:12px 20px;border-radius:10px;font-weight:600;font-family:'DM Sans',sans-serif;">π₯ Download Hasil RTF</a></div>"""t1 = time.time()debug_lines.append(f"β <b>Langkah 6</b> β Build Download Link: <b>{t1-t0:.4f}s</b>")t_end = time.time()debug_lines.append(f"β± <b>TOTAL WAKTU: {t_end-t_total:.2f}s</b>")1 unmodified linedebug_html = _build_debug_html(debug_lines)found_labels: set[str] = set(entity_map.values())# Legendlegend_rows = ""for label_id in sorted(found_labels):label_idn = LABEL_MAP.get(label_id, label_id)29 unmodified lines</div></div>"""return download_html, legend_html, "", debug_html# CSSCUSTOM_CSS = """@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700;900&family=DM+Sans:wght@400;500;600&display=swap');.gradio-container{max-width:100%!important;padding:0!important;background:linear-gradient(160deg,#0f172a 0%,#1e1b4b 40%,#0f172a 100%)!important;min-height:100vh;}36 unmodified lines"""# Gradio UIwith gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:gr.HTML(HERO_HTML)41 unmodified lineswith gr.Column(visible=False) as pdf_results_col:gr.HTML('<div class="section-heading" style="margin-top:20px;">Download Hasil RTF</div>')pdf_output = gr.HTML(value="")gr.HTML('<div class="section-heading" style="margin-top:20px;">Keterangan Warna Entitas</div>')pdf_legend = gr.HTML(value="")# Debug panel β selalu tampil setelah proses9 unmodified linestext_input.submit(fn=run_ner, inputs=text_input, outputs=text_output)def handle_pdf(pdf_file):download_html, legend_html, warn_html, debug_html = run_ner_pdf(pdf_file)show = gr.Column(visible=bool(download_html))return (download_html,legend_html,warn_html,debug_html,show)analyze_pdf_btn.click(fn=handle_pdf,
app.py+60-28