|
|
import gradio as gr |
|
|
import pandas as pd |
|
|
from transformers import pipeline, AutoTokenizer, AutoModelForTokenClassification |
|
|
import os |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MODEL_ID = "Dawgggggg/modelNER" |
|
|
|
|
|
|
|
|
|
|
|
tokenizer = None |
|
|
model = None |
|
|
ner_pipeline = None |
|
|
|
|
|
try: |
|
|
print(f"Loading tokenizer from {MODEL_ID}...") |
|
|
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) |
|
|
print("Tokenizer loaded.") |
|
|
|
|
|
print(f"Loading model from {MODEL_ID}...") |
|
|
model = AutoModelForTokenClassification.from_pretrained(MODEL_ID) |
|
|
print("Model loaded.") |
|
|
|
|
|
print("Creating NER pipeline...") |
|
|
ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="simple") |
|
|
print("NER pipeline created successfully.") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"Error loading model or tokenizer: {e}") |
|
|
|
|
|
|
|
|
def dummy_pipeline(text): |
|
|
print(f"Model not loaded. Error: {e}") |
|
|
return [] |
|
|
ner_pipeline = dummy_pipeline |
|
|
|
|
|
|
|
|
def predict_ner_all(text): |
|
|
|
|
|
if ner_pipeline is None or isinstance(ner_pipeline, type(lambda:0)): |
|
|
return ( |
|
|
pd.DataFrame(columns=["Entitas", "Kata", "Skor", "Mulai", "Akhir"]), |
|
|
"Error: Model tidak dapat dimuat. Cek log.", |
|
|
[] |
|
|
) |
|
|
|
|
|
if not text: |
|
|
return ( |
|
|
pd.DataFrame(columns=["Entitas", "Kata", "Skor", "Mulai", "Akhir"]), |
|
|
"", |
|
|
[] |
|
|
) |
|
|
|
|
|
try: |
|
|
results = ner_pipeline(text) |
|
|
except Exception as e: |
|
|
print(f"Error during NER pipeline execution: {e}") |
|
|
return ( |
|
|
pd.DataFrame(columns=["Entitas", "Kata", "Skor", "Mulai", "Akhir"]), |
|
|
f"Error saat menjalankan NER: {e}", |
|
|
[] |
|
|
) |
|
|
|
|
|
|
|
|
df_results = [] |
|
|
for res in results: |
|
|
df_results.append( |
|
|
{ |
|
|
"Entitas": res["entity_group"], |
|
|
"Kata": res["word"], |
|
|
"Skor": f"{res['score']:.2f}", |
|
|
"Mulai": res["start"], |
|
|
"Akhir": res["end"] |
|
|
} |
|
|
) |
|
|
df_output = pd.DataFrame(df_results) |
|
|
|
|
|
|
|
|
highlighted_chunks = [] |
|
|
current_index = 0 |
|
|
|
|
|
if results: |
|
|
for res in results: |
|
|
|
|
|
if res["start"] > current_index: |
|
|
highlighted_chunks.append((text[current_index:res["start"]], None)) |
|
|
|
|
|
|
|
|
highlighted_chunks.append((res["word"], res["entity_group"], res["score"])) |
|
|
|
|
|
|
|
|
current_index = res["end"] |
|
|
|
|
|
|
|
|
if current_index < len(text): |
|
|
highlighted_chunks.append((text[current_index:], None)) |
|
|
else: |
|
|
|
|
|
highlighted_chunks.append((text, None)) |
|
|
|
|
|
return df_output, text, highlighted_chunks |
|
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as demo: |
|
|
gr.Markdown("# Demo NER Informasi Obat dan Dosis") |
|
|
gr.Markdown("Model ini dirancang untuk mengekstraksi informasi obat, dosis, dan unit dari teks medis berbahasa Indonesia.") |
|
|
|
|
|
with gr.Row(): |
|
|
text_input = gr.Textbox( |
|
|
lines=2, |
|
|
placeholder="Masukkan kalimat di sini, contoh: Diberikan parasetamol 5.6 mg/dL setiap 8 jam.", |
|
|
label="Kalimat Input" |
|
|
) |
|
|
submit_button = gr.Button("Deteksi Entitas") |
|
|
|
|
|
|
|
|
with gr.Row(): |
|
|
df_output = gr.Dataframe( |
|
|
headers=["Entitas", "Kata", "Skor", "Mulai", "Akhir"], |
|
|
wrap=True, |
|
|
label="Hasil Deteksi (Tabel)" |
|
|
) |
|
|
|
|
|
|
|
|
with gr.Row(): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
highlighted_output = gr.HighlightedText( |
|
|
label="Deteksi Entitas (Teks Disorot)", |
|
|
color_map={ |
|
|
"DRUG": "red", |
|
|
"RESULT": "blue", |
|
|
"UNIT": "green", |
|
|
|
|
|
|
|
|
}, |
|
|
show_legend=True |
|
|
) |
|
|
|
|
|
|
|
|
submit_button.click( |
|
|
fn=predict_ner_all, |
|
|
inputs=text_input, |
|
|
|
|
|
outputs=[df_output, text_input, highlighted_output] |
|
|
) |
|
|
|
|
|
|
|
|
gr.Examples( |
|
|
examples=[ |
|
|
["Diberikan parasetamol 5.6 mg/dL setiap 8 jam."], |
|
|
["Pasien diberi insulin 10 unit sebelum makan."], |
|
|
["Resepnya adalah amoxicillin 500 mg tiga kali sehari."] |
|
|
], |
|
|
inputs=text_input |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
demo.launch() |