Spaces:
Runtime error
Runtime error
File size: 1,836 Bytes
9de6c4e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import gradio as gr
import joblib
from transformers import pipeline
# Load model hoax detector
model = joblib.load("model_hoax.pkl")
vectorizer = joblib.load("vectorizer.pkl")
# Load QA dan NER pipeline
qa_pipe = pipeline("question-answering", model="Rifky/IndoBERT-QA")
ner_pipe = pipeline("ner", model="cahya/bert-base-indonesian-NER", aggregation_strategy="simple")
# --- Fungsi ---
def detect_hoax(text):
vec = vectorizer.transform([text])
result = model.predict(vec)
return "HOAX" if result[0] == 1 else "Bukan Hoax"
def qa_chat(message, history, context):
if not context:
return "Mohon masukkan teks berita di kolom atas terlebih dahulu."
result = qa_pipe(question=message, context=context)
return result['answer']
def ner(text):
entities = ner_pipe(text)
return "\n".join([f"{e['word']} ({e['entity_group']})" for e in entities])
# --- UI Gradio ---
with gr.Blocks() as demo:
gr.Markdown("## Deteksi Hoaks, QA (Chat), dan NER")
# Shared input
context_input = gr.Textbox(label="Teks Berita / Konteks", lines=5, placeholder="Masukkan teks berita di sini...")
with gr.Tab("Deteksi Hoaks"):
hoax_output = gr.Textbox(label="Output Deteksi")
hoax_btn = gr.Button("Deteksi")
hoax_btn.click(fn=detect_hoax, inputs=context_input, outputs=hoax_output)
with gr.Tab("QA"):
gr.Markdown("Tanyakan apapun berdasarkan teks berita di atas:")
qa_chatbot = gr.ChatInterface(
fn=lambda msg, hist: qa_chat(msg, hist, context_input.value),
title="Tanya Jawab Berbasis Teks",
)
with gr.Tab("NER"):
ner_output = gr.Textbox(label="Hasil Ekstraksi Entitas", lines=5)
ner_btn = gr.Button("Ekstrak Entitas")
ner_btn.click(fn=ner, inputs=context_input, outputs=ner_output)
demo.launch()
|