IndoHoaxDetector / example.py
theonegareth's picture
Improve IndoHoaxDetector repo: add comprehensive README, model card, examples, evaluation script, tests, license, and fix app.py for hoax detection
af7b60b
#!/usr/bin/env python3
"""
Example script for using IndoHoaxDetector model
This script demonstrates how to load the model and make predictions on Indonesian news text.
"""
import pickle
def load_model(model_path='logreg_model.pkl'):
"""Load the trained logistic regression model."""
with open(model_path, 'rb') as f:
model = pickle.load(f)
return model
def predict_hoax(text, model):
"""
Predict if the given text is a hoax or legitimate news.
Args:
text (str): Indonesian news text to classify
model: Loaded sklearn model
Returns:
dict: Prediction results with label and confidence
"""
# 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]
return {
'prediction': label,
'confidence': confidence,
'probabilities': {
'legitimate': probabilities[0],
'hoax': probabilities[1]
}
}
def main():
"""Main function to demonstrate model usage."""
# Load the model
print("Loading IndoHoaxDetector model...")
model = load_model()
# Example texts (Indonesian news snippets)
example_texts = [
"Presiden mengumumkan kebijakan baru untuk ekonomi nasional hari ini di Jakarta.",
"Alien mendarat di Monas dan bertemu dengan presiden secara rahasia.",
"Harga bahan pokok naik 50% akibat cuaca ekstrem di beberapa daerah.",
"Minum air kelapa bisa menyembuhkan semua penyakit termasuk kanker stadium 4."
]
print("\n" + "="*60)
print("IndoHoaxDetector Predictions")
print("="*60)
for i, text in enumerate(example_texts, 1):
print(f"\nExample {i}:")
print(f"Text: {text[:100]}{'...' if len(text) > 100 else ''}")
result = predict_hoax(text, model)
print(f"Prediction: {result['prediction']}")
print(".4f")
print("\n" + "="*60)
print("Note: This is a demonstration. Always verify predictions with human expertise.")
print("="*60)
if __name__ == "__main__":
main()