import gradio as gr import skops.io as sio from pathlib import Path import logging # --- 1. إعدادات التسجيل (Logging) --- logging.basicConfig(level=logging.INFO) # --- 2. تحميل الـ Pipeline --- PIPELINE_PATH = Path("fake_news_pipeline.skops") pipeline = None try: logging.info(f"Loading pipeline from {PIPELINE_PATH}...") pipeline = sio.load(PIPELINE_PATH, trusted=True) logging.info("Pipeline loaded successfully.") except Exception as e: logging.error(f"Error loading pipeline: {e}") # هذا سيظهر الخطأ على واجهة Gradio إذا فشل التحميل raise gr.Error(f"Failed to load model: {e}") # --- 3. دالة التنبؤ (أصبحت أبسط) --- def predict_news(text: str): """ دالة للتنبؤ بما إذا كان النص "Fake" أو "True" باستخدام الـ Pipeline. """ if not text: return {"Fake": 0, "True": 0} if pipeline is None: return {"Error": "Model is not loaded."} try: # الـ Pipeline يتولى (transform) و (predict) في خطوة واحدة # predict_proba يُرجع [[prob_0, prob_1]] probabilities = pipeline.predict_proba([text])[0] # 0 = Fake, 1 = True (بناءً على ملف التدريب) output_labels = { "Fake": float(probabilities[0]), "True": float(probabilities[1]) } return output_labels except Exception as e: logging.error(f"Error during prediction: {e}") return {"Error": str(e)} # --- 4. واجهة Gradio (كما هي) --- example_fake = "Donald Trump Sends Out Embarrassing New Year’s Eve Message; This is Disturbing" example_true = "WASHINGTON (Reuters) - The head of a conservative Republican faction in the U.S. Congress, who voted this month for a huge expansion of the national debt to pay for tax cuts, called himself a “fiscal conservative” on Sunday..." iface = gr.Interface( fn=predict_news, inputs=gr.Textbox(lines=10, label="أدخل نص الخبر هنا", placeholder="...اكتب نص المقال..."), outputs=gr.Label(num_top_classes=2, label="النتيجة"), title="🤖 Fake News Detector ", description="هذا النموذج هو كاشف للأخبار الكاذبة (باستخدام Pipeline) تم تدريبه باستخدام Logistic Regression و TF-IDF. أدخل نص مقال إخباري لمعرفة تصنيفه (True أو Fake).", examples=[ [example_fake], [example_true] ], allow_flagging="never" ) # --- 5. تشغيل التطبيق --- if __name__ == "__main__": iface.launch()