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()