File size: 2,744 Bytes
03a5e6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()