rizkims commited on
Commit
9de6c4e
·
1 Parent(s): 4fc17c5

Initial commit for hoax detector

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+ from transformers import pipeline
4
+
5
+ # Load model hoax detector
6
+ model = joblib.load("model_hoax.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()