File size: 2,520 Bytes
9de6c4e
 
 
 
39e40d8
e578822
9de6c4e
 
 
 
 
 
 
39e40d8
 
2cbde77
39e40d8
2cbde77
9de6c4e
2cbde77
9de6c4e
2cbde77
9de6c4e
2cbde77
9de6c4e
 
 
39e40d8
 
 
 
 
 
2cbde77
39e40d8
9de6c4e
 
2cbde77
39e40d8
9de6c4e
 
 
 
2cbde77
 
 
9de6c4e
 
39e40d8
2cbde77
 
 
 
 
 
 
 
 
 
 
9de6c4e
 
 
39e40d8
 
9de6c4e
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import gradio as gr
import joblib
from transformers import pipeline

# Load model dan pipeline
model = joblib.load("ensemble_model.pkl")
vectorizer = joblib.load("vectorizer.pkl")
qa_pipe = pipeline("question-answering", model="Rifky/IndoBERT-QA")
ner_pipe = pipeline("ner", model="cahya/bert-base-indonesian-NER", aggregation_strategy="simple")

# --- Fungsi ---
def detect_hoax(text):
    vec = vectorizer.transform([text])
    result = model.predict(vec)[0]
    if result == 1:
        return "<div style='background-color:#e74c3c; color:white; padding:10px; border-radius:5px'>HOAX</div>"
    else:
        return "<div style='background-color:#27ae60; color:white; padding:10px; border-radius:5px'>BUKAN HOAX</div>"

def qa_manual(message, history, context):
    if not context:
        return history + [[message, "Mohon isi teks berita terlebih dahulu."]]
    result = qa_pipe(question=message, context=context)
    return history + [[message, result["answer"]]]

def ner(text):
    entities = ner_pipe(text)
    styled = ""
    color_map = {
        "PER": "#ffd1dc", "ORG": "#d1e0ff", "LOC": "#d1ffd1", "MISC": "#fdfd96"
    }
    for ent in entities:
        color = color_map.get(ent["entity_group"], "#eee")
        styled += f"<mark style='background-color:{color}; padding:2px; margin:2px'>{ent['word']} <small>({ent['entity_group']})</small></mark> "
    return styled

# --- UI Gradio ---
with gr.Blocks() as demo:
    gr.Markdown("## Hoax Detector App")

    context_input = gr.Textbox(label="Teks Berita / Konteks", lines=5, placeholder="Masukkan teks berita di sini...")

    with gr.Tab("Deteksi Hoaks"):
        detect_btn = gr.Button("DETEKSI")
        hoax_output = gr.HTML()
        detect_btn.click(fn=detect_hoax, inputs=context_input, outputs=hoax_output)

    with gr.Tab("QA"):
        #gr.Markdown("### Tanya Jawab Berdasarkan Teks Berita")
        qa_question = gr.Textbox(placeholder="Tulis pertanyaan...", label="Pertanyaan")
        qa_btn = gr.Button("KIRIM")
        qa_history = gr.Chatbot(label="Riwayat Tanya Jawab")
        qa_state = gr.State([])

        qa_btn.click(
            fn=qa_manual,
            inputs=[qa_question, qa_state, context_input],
            outputs=[qa_history],
            show_progress=False
        ).then(fn=lambda h: h, inputs=qa_history, outputs=qa_state)

    with gr.Tab("NER"):
        ner_btn = gr.Button("Ekstrak Entitas")
        ner_result = gr.HTML()
        ner_btn.click(fn=ner, inputs=context_input, outputs=ner_result)

demo.launch()