Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
ner = pipeline("ner", model="cahya/NusaBert-ner-v1.3",
|
| 6 |
|
| 7 |
-
#
|
| 8 |
LABEL_MAP = {
|
| 9 |
"CRD": "Kardinal",
|
| 10 |
"DAT": "Tanggal",
|
|
@@ -27,7 +27,7 @@ LABEL_MAP = {
|
|
| 27 |
"LAN": "Bahasa",
|
| 28 |
}
|
| 29 |
|
| 30 |
-
#
|
| 31 |
EXAMPLES = [
|
| 32 |
(
|
| 33 |
"Contoh 1 β RUPS & Dana Cadangan",
|
|
@@ -70,7 +70,7 @@ EXAMPLES = [
|
|
| 70 |
),
|
| 71 |
]
|
| 72 |
|
| 73 |
-
#
|
| 74 |
def run_ner(text: str):
|
| 75 |
if not text or not text.strip():
|
| 76 |
return "<p style='color:gray;'>Masukkan teks terlebih dahulu.</p>"
|
|
@@ -114,7 +114,7 @@ def run_ner(text: str):
|
|
| 114 |
return table_html
|
| 115 |
|
| 116 |
|
| 117 |
-
#
|
| 118 |
CUSTOM_CSS = """
|
| 119 |
/* Kontainer utama */
|
| 120 |
.gradio-container { max-width: 860px !important; margin: 0 auto; }
|
|
@@ -158,48 +158,48 @@ CUSTOM_CSS = """
|
|
| 158 |
#analyze-btn:hover { background: #2563eb !important; }
|
| 159 |
"""
|
| 160 |
|
| 161 |
-
#
|
| 162 |
with gr.Blocks(css=CUSTOM_CSS, title="NusaBert NER") as demo:
|
| 163 |
|
| 164 |
gr.Markdown(
|
| 165 |
-
"#
|
| 166 |
"Model: **cahya/NusaBert-ner-v1.3** Β· Masukkan teks Bahasa Indonesia lalu klik **Lakukan Analisis**.",
|
| 167 |
elem_id="app-title",
|
| 168 |
)
|
| 169 |
|
| 170 |
with gr.Row():
|
| 171 |
-
#
|
| 172 |
with gr.Column(scale=1):
|
| 173 |
-
gr.Markdown("###
|
| 174 |
example_btns = []
|
| 175 |
for title, body in EXAMPLES:
|
| 176 |
btn = gr.Button(f"**{title}**\n\n{body}", elem_classes="example-btn")
|
| 177 |
example_btns.append((btn, body))
|
| 178 |
|
| 179 |
-
#
|
| 180 |
with gr.Column(scale=2):
|
| 181 |
-
gr.Markdown("###
|
| 182 |
text_input = gr.Textbox(
|
| 183 |
lines=8,
|
| 184 |
placeholder="Ketik atau tempel teks di siniβ¦",
|
| 185 |
label="",
|
| 186 |
show_label=False,
|
| 187 |
)
|
| 188 |
-
analyze_btn = gr.Button("
|
| 189 |
|
| 190 |
-
gr.Markdown("###
|
| 191 |
output_html = gr.HTML(
|
| 192 |
value="<p style='color:gray; font-size:14px;'>Hasil analisis akan muncul di sini.</p>"
|
| 193 |
)
|
| 194 |
|
| 195 |
-
#
|
| 196 |
for btn, body in example_btns:
|
| 197 |
btn.click(fn=lambda b=body: b, inputs=[], outputs=text_input)
|
| 198 |
|
| 199 |
-
#
|
| 200 |
analyze_btn.click(fn=run_ner, inputs=text_input, outputs=output_html)
|
| 201 |
|
| 202 |
-
#
|
| 203 |
text_input.submit(fn=run_ner, inputs=text_input, outputs=output_html)
|
| 204 |
|
| 205 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load model
|
| 5 |
+
ner = pipeline("ner", model="cahya/NusaBert-ner-v1.3", aggregation_strategy="simple")
|
| 6 |
|
| 7 |
+
# Label mapping
|
| 8 |
LABEL_MAP = {
|
| 9 |
"CRD": "Kardinal",
|
| 10 |
"DAT": "Tanggal",
|
|
|
|
| 27 |
"LAN": "Bahasa",
|
| 28 |
}
|
| 29 |
|
| 30 |
+
# Contoh teks
|
| 31 |
EXAMPLES = [
|
| 32 |
(
|
| 33 |
"Contoh 1 β RUPS & Dana Cadangan",
|
|
|
|
| 70 |
),
|
| 71 |
]
|
| 72 |
|
| 73 |
+
# Fungsi inferensi
|
| 74 |
def run_ner(text: str):
|
| 75 |
if not text or not text.strip():
|
| 76 |
return "<p style='color:gray;'>Masukkan teks terlebih dahulu.</p>"
|
|
|
|
| 114 |
return table_html
|
| 115 |
|
| 116 |
|
| 117 |
+
# CSS kustom
|
| 118 |
CUSTOM_CSS = """
|
| 119 |
/* Kontainer utama */
|
| 120 |
.gradio-container { max-width: 860px !important; margin: 0 auto; }
|
|
|
|
| 158 |
#analyze-btn:hover { background: #2563eb !important; }
|
| 159 |
"""
|
| 160 |
|
| 161 |
+
# Bangun UI
|
| 162 |
with gr.Blocks(css=CUSTOM_CSS, title="NusaBert NER") as demo:
|
| 163 |
|
| 164 |
gr.Markdown(
|
| 165 |
+
"#NusaBert NER β Pengenalan Entitas\n"
|
| 166 |
"Model: **cahya/NusaBert-ner-v1.3** Β· Masukkan teks Bahasa Indonesia lalu klik **Lakukan Analisis**.",
|
| 167 |
elem_id="app-title",
|
| 168 |
)
|
| 169 |
|
| 170 |
with gr.Row():
|
| 171 |
+
# Kolom kiri: contoh teks
|
| 172 |
with gr.Column(scale=1):
|
| 173 |
+
gr.Markdown("###Contoh Teks")
|
| 174 |
example_btns = []
|
| 175 |
for title, body in EXAMPLES:
|
| 176 |
btn = gr.Button(f"**{title}**\n\n{body}", elem_classes="example-btn")
|
| 177 |
example_btns.append((btn, body))
|
| 178 |
|
| 179 |
+
# Kolom kanan: input & output
|
| 180 |
with gr.Column(scale=2):
|
| 181 |
+
gr.Markdown("###Input Teks")
|
| 182 |
text_input = gr.Textbox(
|
| 183 |
lines=8,
|
| 184 |
placeholder="Ketik atau tempel teks di siniβ¦",
|
| 185 |
label="",
|
| 186 |
show_label=False,
|
| 187 |
)
|
| 188 |
+
analyze_btn = gr.Button("Lakukan Analisis", elem_id="analyze-btn")
|
| 189 |
|
| 190 |
+
gr.Markdown("###Hasil Analisis Entitas")
|
| 191 |
output_html = gr.HTML(
|
| 192 |
value="<p style='color:gray; font-size:14px;'>Hasil analisis akan muncul di sini.</p>"
|
| 193 |
)
|
| 194 |
|
| 195 |
+
# Hubungkan tombol contoh β input field
|
| 196 |
for btn, body in example_btns:
|
| 197 |
btn.click(fn=lambda b=body: b, inputs=[], outputs=text_input)
|
| 198 |
|
| 199 |
+
# Hubungkan tombol analisis
|
| 200 |
analyze_btn.click(fn=run_ner, inputs=text_input, outputs=output_html)
|
| 201 |
|
| 202 |
+
# Enter di textbox juga memicu analisis
|
| 203 |
text_input.submit(fn=run_ner, inputs=text_input, outputs=output_html)
|
| 204 |
|
| 205 |
if __name__ == "__main__":
|