| | import gradio as gr
|
| | import skops.io as sio
|
| | from pathlib import Path
|
| | import logging
|
| |
|
| |
|
| | logging.basicConfig(level=logging.INFO)
|
| |
|
| |
|
| | 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}")
|
| |
|
| | raise gr.Error(f"Failed to load model: {e}")
|
| |
|
| |
|
| | 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:
|
| |
|
| |
|
| | probabilities = pipeline.predict_proba([text])[0]
|
| |
|
| |
|
| | 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)}
|
| |
|
| |
|
| | 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"
|
| | )
|
| |
|
| |
|
| | if __name__ == "__main__":
|
| | iface.launch() |