alomari7's picture
Upload app.py with huggingface_hub
1fa6011 verified
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()