File size: 2,674 Bytes
03fb699
af7b60b
03fb699
af7b60b
 
03fb699
af7b60b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45fd788
af7b60b
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import gradio as gr
import pickle

# Load the model at startup
MODEL_PATH = 'logreg_model.pkl'

try:
    with open(MODEL_PATH, 'rb') as f:
        model = pickle.load(f)
    print("Model loaded successfully!")
except FileNotFoundError:
    print(f"Error: {MODEL_PATH} not found. Please ensure the model file is in the same directory.")
    model = None

def detect_hoax(text):
    """

    Detect if the input text is a hoax or legitimate news.



    Args:

        text (str): Indonesian news text to analyze



    Returns:

        str: Prediction result with confidence

    """
    if model is None:
        return "Error: Model not loaded. Please check the model file."

    if not text.strip():
        return "Please enter some text to analyze."

    try:
        # Make prediction
        prediction = model.predict([text])[0]
        probabilities = model.predict_proba([text])[0]

        # Interpret results
        label = "Hoax" if prediction == 1 else "Legitimate"
        confidence = probabilities[prediction]

        # Format output
        result = f"**Prediction: {label}**\n\n"
        result += f"**Confidence: {confidence:.1%}**\n\n"
        result += f"**Probabilities:**\n"
        result += f"- Legitimate: {probabilities[0]:.1%}\n"
        result += f"- Hoax: {probabilities[1]:.1%}\n\n"
        result += "*Note: This is an AI prediction. Always verify with human expertise and multiple sources.*"

        return result

    except Exception as e:
        return f"Error during prediction: {str(e)}"

# Create Gradio interface
demo = gr.Interface(
    fn=detect_hoax,
    inputs=gr.Textbox(
        label="Indonesian News Text",
        placeholder="Enter Indonesian news article text here...",
        lines=5
    ),
    outputs=gr.Markdown(label="Detection Result"),
    title="IndoHoaxDetector",
    description="**Stylistic Analysis Tool**: Detects if Indonesian news text is written in a hoax-like style using machine learning. This analyzes writing patterns and sensationalism, **not factual accuracy**. Results indicate writing style similarity to known hoaxes, not truth verification.",
    examples=[
        ["Presiden mengumumkan program bantuan sosial untuk masyarakat miskin di seluruh Indonesia."],
        ["Ditemukan cara ampuh menghilangkan stres hanya dengan minum air putih 2 liter sehari."],
        ["Harga bahan pokok naik drastis akibat bencana alam di beberapa daerah."],
        ["Vaksin COVID-19 ternyata mengandung bahan kimia berbahaya yang disembunyikan pemerintah."]
    ],
    theme=gr.themes.Soft()
)

if __name__ == "__main__":
    demo.launch()