File size: 2,979 Bytes
2b08015
 
 
 
 
 
 
 
 
1fa6011
 
2b08015
 
 
1fa6011
 
2b08015
 
 
 
 
 
1fa6011
2b08015
 
1fa6011
 
 
 
 
 
2b08015
 
 
1fa6011
 
 
 
 
2b08015
1fa6011
2b08015
 
 
 
1fa6011
2b08015
1fa6011
2b08015
f2f8ae8
2b08015
1fa6011
2b08015
 
 
1fa6011
2b08015
 
 
 
 
1fa6011
2b08015
 
 
 
 
 
 
 
1fa6011
2b08015
 
 
 
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
69
70
71
72
73
74
75
76

import gradio as gr
from transformers import pipeline
import os

# --- 1. تحميل المودل الخاص بك من الهَب ---
try:
    fill_mask_pipeline = pipeline(
        "fill-mask",
        model="alomari7/ara-bert-okaz-style",
        tokenizer="alomari7/ara-bert-okaz-style"
    )
    print("✅ تم تحميل المودل بنجاح!")
except Exception as e:
    # [تم التعديل] طباعة الخطأ قبل رفع Gr.Error
    print(f"❌ خطأ فادح أثناء تحميل المودل: {e}") # This line uses 'e'
    raise gr.Error(f"فشل تحميل المودل: {e}. يرجى مراجعة سجلات Space.")

# --- 2. تعريف دالة التنبؤ ---
def predict_okaz_style(sentence):
    if "[MASK]" not in sentence:
        return "❌ خطأ: لم أجد الكلمة [MASK] في الجملة. الرجاء إضافتها (بحروف كبيرة)."

    try:
        predictions = fill_mask_pipeline(sentence)

        output = f"الجملة المدخلة: {sentence}\n"
        output += "=========================\n"
        output += "✨ أفضل 5 تخمينات للمودل:\n"
        output += "=========================\n"

        for pred in predictions:
            token_str = pred['token_str']
            score = pred['score']
            full_sentence = pred['sequence']

            output += f"التخمين: {token_str} (الثقة: {score:.2%})\n"
            output += f"  👈 {full_sentence}\n\n"

        return output

    except Exception as e:
        return f"❌ حدث خطأ داخلي أثناء المعالجة: {e}"

# --- 3. إطلاق واجهة Gradio ---
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown(
        """
        # 🤖 متنبئ الكلمات بأسلوب عكاظ
        هذا تطبيق ويب لتجربة المودل (alomari7/ara-bert-okaz-style) الذي تم تدريبه على 143 مقالاً من صحيفة عكاظ.
        اكتب جملة واستخدم `[MASK]` (بحروف كبيرة) مكان الكلمة التي تريد من المودل تخمينها.
        """
    )
    with gr.Column():
        input_text = gr.Textbox(
            label="أدخل جملة بأسلوب (عكاظ)",
            placeholder="مثال: أكد وزير [MASK] أن المشروع يسير وفق الخطة.",
            lines=3
        )
        output_text = gr.Textbox(label="نتيجة التخمين", lines=15)
        submit_btn = gr.Button("🚀 خمن الكلمة")

    gr.Examples(
        examples=[
            ["أكد وزير [MASK] أن المشروع يسير وفق الخطة."],
            ["فاز نادي [MASK] في المباراة التي أقيمت بجدة."],
            ["أعلنت الهيئة عن [MASK] جديد للمستثمرين."]
        ],
        inputs=input_text
    )

    submit_btn.click(fn=predict_okaz_style, inputs=input_text, outputs=output_text)

# إطلاق التطبيق
demo.launch()