Spaces:
Sleeping
Sleeping
Initial commit for hoax detector
Browse files
app.py
CHANGED
@@ -13,15 +13,15 @@ def detect_hoax(text):
|
|
13 |
vec = vectorizer.transform([text])
|
14 |
result = model.predict(vec)[0]
|
15 |
if result == 1:
|
16 |
-
return
|
17 |
else:
|
18 |
-
return
|
19 |
|
20 |
-
def
|
21 |
if not context:
|
22 |
-
return "Mohon
|
23 |
result = qa_pipe(question=message, context=context)
|
24 |
-
return result["answer"]
|
25 |
|
26 |
def ner(text):
|
27 |
entities = ner_pipe(text)
|
@@ -31,30 +31,33 @@ def ner(text):
|
|
31 |
}
|
32 |
for ent in entities:
|
33 |
color = color_map.get(ent["entity_group"], "#eee")
|
34 |
-
styled += f"<mark style='background-color:{color}; padding:
|
35 |
return styled
|
36 |
|
37 |
# --- UI Gradio ---
|
38 |
-
with gr.Blocks(
|
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 |
-
|
46 |
-
|
47 |
-
|
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 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
with gr.Tab("NER"):
|
60 |
ner_btn = gr.Button("Ekstrak Entitas")
|
|
|
13 |
vec = vectorizer.transform([text])
|
14 |
result = model.predict(vec)[0]
|
15 |
if result == 1:
|
16 |
+
return "<div style='background-color:#e74c3c; color:white; padding:10px; border-radius:5px'>HOAX</div>"
|
17 |
else:
|
18 |
+
return "<div style='background-color:#27ae60; color:white; padding:10px; border-radius:5px'>BUKAN HOAX</div>"
|
19 |
|
20 |
+
def qa_manual(message, history, context):
|
21 |
if not context:
|
22 |
+
return history + [[message, "Mohon isi teks berita terlebih dahulu."]]
|
23 |
result = qa_pipe(question=message, context=context)
|
24 |
+
return history + [[message, result["answer"]]]
|
25 |
|
26 |
def ner(text):
|
27 |
entities = ner_pipe(text)
|
|
|
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() as demo:
|
39 |
gr.Markdown("## Hoax Detector App")
|
40 |
|
|
|
41 |
context_input = gr.Textbox(label="Teks Berita / Konteks", lines=5, placeholder="Masukkan teks berita di sini...")
|
42 |
|
43 |
with gr.Tab("Deteksi Hoaks"):
|
44 |
+
detect_btn = gr.Button("DETEKSI")
|
45 |
+
hoax_output = gr.HTML()
|
46 |
+
detect_btn.click(fn=detect_hoax, inputs=context_input, outputs=hoax_output)
|
|
|
47 |
|
48 |
with gr.Tab("QA"):
|
49 |
#gr.Markdown("### Tanya Jawab Berdasarkan Teks Berita")
|
50 |
+
qa_question = gr.Textbox(placeholder="Tulis pertanyaan...", label="Pertanyaan")
|
51 |
+
qa_btn = gr.Button("KIRIM")
|
52 |
+
qa_history = gr.Chatbot(label="Riwayat Tanya Jawab")
|
53 |
+
qa_state = gr.State([])
|
54 |
+
|
55 |
+
qa_btn.click(
|
56 |
+
fn=qa_manual,
|
57 |
+
inputs=[qa_question, qa_state, context_input],
|
58 |
+
outputs=[qa_history],
|
59 |
+
show_progress=False
|
60 |
+
).then(fn=lambda h: h, inputs=qa_history, outputs=qa_state)
|
61 |
|
62 |
with gr.Tab("NER"):
|
63 |
ner_btn = gr.Button("Ekstrak Entitas")
|