Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -110,6 +110,57 @@ def highlight_html(text: str, entity_map: dict) -> str:
|
|
| 110 |
)
|
| 111 |
|
| 112 |
# NER Teks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
def run_ner_file(upload_file):
|
| 114 |
if upload_file is None:
|
| 115 |
return "<p style='color:#94a3b8;font-style:italic;padding:16px;'>Unggah file terlebih dahulu.</p>", ""
|
|
@@ -225,7 +276,7 @@ HERO_HTML = """
|
|
| 225 |
|
| 226 |
|
| 227 |
# Gradio UI
|
| 228 |
-
with gr.Blocks(title="NER for Financial Statements"
|
| 229 |
|
| 230 |
gr.HTML(HERO_HTML)
|
| 231 |
|
|
@@ -276,4 +327,4 @@ with gr.Blocks(title="NER for Financial Statements", css=CUSTOM_CSS) as demo:
|
|
| 276 |
)
|
| 277 |
|
| 278 |
if __name__ == "__main__":
|
| 279 |
-
demo.launch()
|
|
|
|
| 110 |
)
|
| 111 |
|
| 112 |
# NER Teks
|
| 113 |
+
def run_ner(text: str):
|
| 114 |
+
if not text or not text.strip():
|
| 115 |
+
return "<p style='color:#94a3b8;font-style:italic;padding:16px;'>Masukkan teks terlebih dahulu.</p>"
|
| 116 |
+
results = ner(text.strip())
|
| 117 |
+
if not results:
|
| 118 |
+
return "<p style='color:#94a3b8;font-style:italic;padding:16px;'>Tidak ada entitas yang ditemukan.</p>"
|
| 119 |
+
|
| 120 |
+
rows_html = ""
|
| 121 |
+
row_num = 1
|
| 122 |
+
seen_words = set()
|
| 123 |
+
for ent in results:
|
| 124 |
+
raw_label = ent["entity_group"]
|
| 125 |
+
label_id = get_label_id(raw_label)
|
| 126 |
+
label_idn = LABEL_MAP.get(label_id, raw_label)
|
| 127 |
+
word = clean_word(ent["word"])
|
| 128 |
+
if not word: continue
|
| 129 |
+
word_key = word.lower()
|
| 130 |
+
if word_key in seen_words: continue
|
| 131 |
+
seen_words.add(word_key)
|
| 132 |
+
hex_color = LABEL_HEX.get(label_id, "#e2e8f0")
|
| 133 |
+
score = f"{ent['score']:.2%}"
|
| 134 |
+
row_bg = "#f8faff" if row_num % 2 == 0 else "#ffffff"
|
| 135 |
+
rows_html += f"""
|
| 136 |
+
<tr style="background:{row_bg};">
|
| 137 |
+
<td style="padding:9px 14px;border-bottom:1px solid #e8edf5;text-align:center;color:#64748b;font-size:12px;">{row_num}</td>
|
| 138 |
+
<td style="padding:9px 14px;border-bottom:1px solid #e8edf5;font-weight:600;color:#1e293b;">{word}</td>
|
| 139 |
+
<td style="padding:9px 14px;border-bottom:1px solid #e8edf5;">
|
| 140 |
+
<span style="background:{hex_color};padding:3px 10px;border-radius:20px;font-size:12px;font-weight:600;color:#1e293b;">{label_idn}</span>
|
| 141 |
+
</td>
|
| 142 |
+
<td style="padding:9px 14px;border-bottom:1px solid #e8edf5;font-size:12px;color:#64748b;text-align:center;">{score}</td>
|
| 143 |
+
</tr>"""
|
| 144 |
+
row_num += 1
|
| 145 |
+
|
| 146 |
+
if not rows_html:
|
| 147 |
+
return "<p style='color:#94a3b8;font-style:italic;padding:16px;'>Tidak ada entitas yang ditemukan.</p>"
|
| 148 |
+
|
| 149 |
+
return f"""
|
| 150 |
+
<div style="overflow-x:auto;margin-top:4px;border-radius:12px;border:1px solid #e2e8f0;box-shadow:0 2px 12px rgba(0,0,0,0.06);">
|
| 151 |
+
<table style="width:100%;border-collapse:collapse;font-size:14px;font-family:'Segoe UI',sans-serif;">
|
| 152 |
+
<thead>
|
| 153 |
+
<tr style="background:linear-gradient(135deg,#1e40af,#6d28d9);">
|
| 154 |
+
<th style="padding:12px 14px;color:#fff;width:55px;font-weight:600;font-size:12px;letter-spacing:0.05em;">NO</th>
|
| 155 |
+
<th style="padding:12px 14px;color:#fff;text-align:left;font-weight:600;font-size:12px;letter-spacing:0.05em;">KATA / FRASA</th>
|
| 156 |
+
<th style="padding:12px 14px;color:#fff;text-align:left;font-weight:600;font-size:12px;letter-spacing:0.05em;">ENTITAS</th>
|
| 157 |
+
<th style="padding:12px 14px;color:#fff;width:90px;font-weight:600;font-size:12px;letter-spacing:0.05em;">SKOR</th>
|
| 158 |
+
</tr>
|
| 159 |
+
</thead>
|
| 160 |
+
<tbody>{rows_html}</tbody>
|
| 161 |
+
</table>
|
| 162 |
+
</div>"""
|
| 163 |
+
|
| 164 |
def run_ner_file(upload_file):
|
| 165 |
if upload_file is None:
|
| 166 |
return "<p style='color:#94a3b8;font-style:italic;padding:16px;'>Unggah file terlebih dahulu.</p>", ""
|
|
|
|
| 276 |
|
| 277 |
|
| 278 |
# Gradio UI
|
| 279 |
+
with gr.Blocks(title="NER for Financial Statements") as demo:
|
| 280 |
|
| 281 |
gr.HTML(HERO_HTML)
|
| 282 |
|
|
|
|
| 327 |
)
|
| 328 |
|
| 329 |
if __name__ == "__main__":
|
| 330 |
+
demo.launch(css=CUSTOM_CSS)
|