rizkims commited on
Commit
2cbde77
·
1 Parent(s): e578822

Initial commit for hoax detector

Browse files
Files changed (1) hide show
  1. app.py +21 -18
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 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)
@@ -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: 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")
 
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")