rizkims commited on
Commit
39e40d8
·
1 Parent(s): 6e4098c

Initial commit for hoax detector

Browse files
Files changed (1) hide show
  1. app.py +28 -17
app.py CHANGED
@@ -2,52 +2,63 @@ import gradio as gr
2
  import joblib
3
  from transformers import pipeline
4
 
5
- # Load model hoax detector
6
- model = joblib.load("ensemble_model.pkl")
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
- return "HOAX" if result[0] == 1 else "Bukan Hoax"
 
 
 
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['answer']
24
 
25
  def ner(text):
26
  entities = ner_pipe(text)
27
- return "\n".join([f"{e['word']} ({e['entity_group']})" for e in entities])
 
 
 
 
 
 
 
28
 
29
  # --- UI Gradio ---
30
- with gr.Blocks() as demo:
31
- gr.Markdown("## Deteksi Hoaks, QA (Chat), dan NER")
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
- hoax_output = gr.Textbox(label="Output Deteksi")
38
- hoax_btn = gr.Button("Deteksi")
39
- hoax_btn.click(fn=detect_hoax, inputs=context_input, outputs=hoax_output)
 
40
 
41
  with gr.Tab("QA"):
42
- gr.Markdown("Tanyakan apapun berdasarkan teks berita di atas:")
43
  qa_chatbot = gr.ChatInterface(
44
  fn=lambda msg, hist: qa_chat(msg, hist, context_input.value),
45
- title="Tanya Jawab Berbasis Teks",
 
 
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
- ner_btn.click(fn=ner, inputs=context_input, outputs=ner_output)
 
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()