Spaces:
Runtime error
Runtime error
import gradio as gr | |
import joblib | |
from transformers import pipeline | |
# Load model hoax detector | |
model = joblib.load("ensemble_model.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() | |