Download app.py from Shabatry/README: direct link, hf CLI and curl.
- Browser
- Download file 2.7 kB
-
https://huggingface.co/spaces/Shabatry/README/resolve/main/app.py
- Command line
-
hf download hf://spaces/Shabatry/README/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Shabatry/README/resolve/main/app.py
2.7 kB
| import gradio as gr | |
| import easyocr | |
| import numpy as np | |
| import os | |
| # متغير عالمي لتخزين القارئ | |
| reader = None | |
| def load_reader(): | |
| """ | |
| دالة لتحميل النموذج فقط عند الحاجة (Lazy Loading) | |
| لتجنب تعليق التطبيق عند البدء | |
| """ | |
| global reader | |
| if reader is None: | |
| print("جاري تحميل نماذج اللغة لأول مرة...") | |
| # تحديد مجلد محلي لتجنب مشاكل التصاريح | |
| model_dir = os.path.join(os.getcwd(), "models") | |
| os.makedirs(model_dir, exist_ok=True) | |
| # تحميل القارئ | |
| reader = easyocr.Reader( | |
| ['ar', 'en'], | |
| gpu=False, | |
| model_storage_directory=model_dir, | |
| download_enabled=True | |
| ) | |
| return reader | |
| def extract_text_from_image(image): | |
| """ | |
| دالة تقوم باستقبال الصورة من واجهة جراديو واستخراج النص منها | |
| """ | |
| if image is None: | |
| return "من فضلك قم برفع صورة أولاً." | |
| try: | |
| # استدعاء دالة التحميل هنا | |
| active_reader = load_reader() | |
| # EasyOCR يستطيع قراءة مصفوفات Numpy مباشرة | |
| result = active_reader.readtext(image, detail=0, paragraph=True) | |
| full_text = "\n".join(result) | |
| return full_text if full_text.strip() else "لم يتم العثور على نص واضح في الصورة." | |
| except Exception as e: | |
| return f"حدث خطأ أثناء المعالجة: {str(e)}\n\n(حاول مرة أخرى، قد يكون خطأ مؤقتاً في التحميل)" | |
| # تنسيق الواجهة | |
| custom_css = """ | |
| .gradio-container {font-family: 'Tajawal', sans-serif;} | |
| """ | |
| # إنشاء التطبيق (تحديث التوافق مع Gradio 6.0+) | |
| # تم إزالة theme و css و allow_flagging من هنا | |
| demo = gr.Interface( | |
| fn=extract_text_from_image, | |
| inputs=gr.Image(label="ارفع الصورة هنا", type="numpy"), | |
| outputs=gr.Textbox(label="النص المستخرج", lines=15, text_align="right"), | |
| title="مستخرج النصوص العربي الذكي 🤖", | |
| description="قم برفع صورة تحتوي على جداول أو نصوص عربية، وسيقوم الذكاء الاصطناعي بقراءتها.", | |
| flagging_mode="never" # بديل allow_flagging في الإصدارات الجديدة | |
| ) | |
| # تشغيل التطبيق | |
| if __name__ == "__main__": | |
| # تم نقل الثيم والـ CSS إلى هنا بناءً على طلب الإصدار الجديد 6.0 | |
| demo.launch() |