Spaces:
Runtime error
Runtime error
Initial commit for hoax detector
Browse files
app.py
CHANGED
@@ -2,52 +2,63 @@ import gradio as gr
|
|
2 |
import joblib
|
3 |
from transformers import pipeline
|
4 |
|
5 |
-
# Load model
|
6 |
-
model = joblib.load("
|
7 |
vectorizer = joblib.load("vectorizer.pkl")
|
8 |
-
|
9 |
-
# Load QA dan NER pipeline
|
10 |
qa_pipe = pipeline("question-answering", model="Rifky/IndoBERT-QA")
|
11 |
ner_pipe = pipeline("ner", model="cahya/bert-base-indonesian-NER", aggregation_strategy="simple")
|
12 |
|
13 |
# --- Fungsi ---
|
14 |
def detect_hoax(text):
|
15 |
vec = vectorizer.transform([text])
|
16 |
-
result = model.predict(vec)
|
17 |
-
|
|
|
|
|
|
|
18 |
|
19 |
def qa_chat(message, history, context):
|
20 |
if not context:
|
21 |
return "Mohon masukkan teks berita di kolom atas terlebih dahulu."
|
22 |
result = qa_pipe(question=message, context=context)
|
23 |
-
return result[
|
24 |
|
25 |
def ner(text):
|
26 |
entities = ner_pipe(text)
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
# --- UI Gradio ---
|
30 |
-
with gr.Blocks() as demo:
|
31 |
-
gr.Markdown("##
|
32 |
|
33 |
# Shared input
|
34 |
context_input = gr.Textbox(label="Teks Berita / Konteks", lines=5, placeholder="Masukkan teks berita di sini...")
|
35 |
|
36 |
with gr.Tab("Deteksi Hoaks"):
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
|
41 |
with gr.Tab("QA"):
|
42 |
-
gr.Markdown("
|
43 |
qa_chatbot = gr.ChatInterface(
|
44 |
fn=lambda msg, hist: qa_chat(msg, hist, context_input.value),
|
45 |
-
title="
|
|
|
|
|
46 |
)
|
47 |
|
48 |
with gr.Tab("NER"):
|
49 |
-
ner_output = gr.Textbox(label="Hasil Ekstraksi Entitas", lines=5)
|
50 |
ner_btn = gr.Button("Ekstrak Entitas")
|
51 |
-
|
|
|
52 |
|
53 |
demo.launch()
|
|
|
2 |
import joblib
|
3 |
from transformers import pipeline
|
4 |
|
5 |
+
# Load model dan pipeline
|
6 |
+
model = joblib.load("model_hoax.pkl")
|
7 |
vectorizer = joblib.load("vectorizer.pkl")
|
|
|
|
|
8 |
qa_pipe = pipeline("question-answering", model="Rifky/IndoBERT-QA")
|
9 |
ner_pipe = pipeline("ner", model="cahya/bert-base-indonesian-NER", aggregation_strategy="simple")
|
10 |
|
11 |
# --- Fungsi ---
|
12 |
def detect_hoax(text):
|
13 |
vec = vectorizer.transform([text])
|
14 |
+
result = model.predict(vec)[0]
|
15 |
+
if result == 1:
|
16 |
+
return gr.update(value="HOAX", visible=True), gr.update(visible=True, elem_classes="hoax-red")
|
17 |
+
else:
|
18 |
+
return gr.update(value="Bukan Hoax", visible=True), gr.update(visible=True, elem_classes="hoax-green")
|
19 |
|
20 |
def qa_chat(message, history, context):
|
21 |
if not context:
|
22 |
return "Mohon masukkan teks berita di kolom atas terlebih dahulu."
|
23 |
result = qa_pipe(question=message, context=context)
|
24 |
+
return result["answer"]
|
25 |
|
26 |
def ner(text):
|
27 |
entities = ner_pipe(text)
|
28 |
+
styled = ""
|
29 |
+
color_map = {
|
30 |
+
"PER": "#ffd1dc", "ORG": "#d1e0ff", "LOC": "#d1ffd1", "MISC": "#fdfd96"
|
31 |
+
}
|
32 |
+
for ent in entities:
|
33 |
+
color = color_map.get(ent["entity_group"], "#eee")
|
34 |
+
styled += f"<mark style='background-color:{color}; padding: 2px; margin:2px'>{ent['word']} <small>({ent['entity_group']})</small></mark> "
|
35 |
+
return styled
|
36 |
|
37 |
# --- UI Gradio ---
|
38 |
+
with gr.Blocks(css=".hoax-red {color: white; background-color: #e74c3c; padding: 10px; border-radius: 5px;} .hoax-green {color: white; background-color: #27ae60; padding: 10px; border-radius: 5px;}") as demo:
|
39 |
+
gr.Markdown("## Hoax Detector App")
|
40 |
|
41 |
# Shared input
|
42 |
context_input = gr.Textbox(label="Teks Berita / Konteks", lines=5, placeholder="Masukkan teks berita di sini...")
|
43 |
|
44 |
with gr.Tab("Deteksi Hoaks"):
|
45 |
+
hoax_btn = gr.Button("DETEKSI")
|
46 |
+
hoax_result = gr.Textbox(visible=False, label="Hasil Deteksi", interactive=False, show_label=False)
|
47 |
+
hoax_result_style = gr.HTML(visible=False)
|
48 |
+
hoax_btn.click(fn=detect_hoax, inputs=context_input, outputs=[hoax_result, hoax_result_style])
|
49 |
|
50 |
with gr.Tab("QA"):
|
51 |
+
#gr.Markdown("### Tanya Jawab Berdasarkan Teks Berita")
|
52 |
qa_chatbot = gr.ChatInterface(
|
53 |
fn=lambda msg, hist: qa_chat(msg, hist, context_input.value),
|
54 |
+
title="",
|
55 |
+
textbox=gr.Textbox(placeholder="Tanyakan sesuatu...", label=None),
|
56 |
+
submit_btn="KIRIM"
|
57 |
)
|
58 |
|
59 |
with gr.Tab("NER"):
|
|
|
60 |
ner_btn = gr.Button("Ekstrak Entitas")
|
61 |
+
ner_result = gr.HTML()
|
62 |
+
ner_btn.click(fn=ner, inputs=context_input, outputs=ner_result)
|
63 |
|
64 |
demo.launch()
|