File size: 18,387 Bytes
7e75ba5
70ab729
417a6fb
 
 
a5e5dd6
73fa91c
 
 
 
 
 
a5e5dd6
 
 
7e75ba5
417a6fb
 
70ab729
417a6fb
 
 
7e75ba5
a5e5dd6
 
 
 
 
 
73fa91c
 
 
 
 
 
 
 
 
 
 
 
a5e5dd6
 
 
73fa91c
 
 
 
 
 
 
 
 
 
 
a5e5dd6
70ab729
a5e5dd6
 
70ab729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5e5dd6
 
 
 
70ab729
 
a5e5dd6
70ab729
 
 
 
 
 
 
 
 
 
 
 
a5e5dd6
70ab729
 
 
a5e5dd6
70ab729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5e5dd6
73fa91c
 
 
 
 
 
 
 
 
 
 
a5e5dd6
417a6fb
7e75ba5
a5e5dd6
70ab729
a5e5dd6
 
417a6fb
a5e5dd6
 
417a6fb
7e75ba5
417a6fb
70ab729
 
 
73fa91c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70ab729
 
 
 
 
 
 
417a6fb
 
 
 
 
70ab729
 
 
417a6fb
 
70ab729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417a6fb
70ab729
417a6fb
70ab729
 
 
 
 
 
 
73fa91c
 
 
 
 
 
 
 
 
 
 
70ab729
 
 
 
 
 
 
 
 
73fa91c
 
 
 
 
 
 
 
 
 
 
 
 
70ab729
7e75ba5
417a6fb
70ab729
a5e5dd6
 
73fa91c
 
 
 
 
 
 
 
 
 
 
70ab729
 
 
 
 
 
 
 
 
 
a5e5dd6
 
 
70ab729
 
 
a5e5dd6
 
70ab729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417a6fb
70ab729
417a6fb
70ab729
 
 
 
73fa91c
 
70ab729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e75ba5
a5e5dd6
 
70ab729
 
a5e5dd6
 
 
 
 
 
 
 
70ab729
73fa91c
 
 
 
 
 
 
70ab729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55b6074
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import streamlit as st
from transformers import pipeline
from arabic_reshaper import reshape
from bidi.algorithm import get_display
import torch
import os
import importlib.util

# التحقق من وجود مكتبة sentencepiece
if importlib.util.find_spec("sentencepiece") is None:
    st.error("مكتبة sentencepiece غير مثبتة. يرجى تثبيتها باستخدام الأمر: pip install sentencepiece")
    st.stop()

# تكوين متغيرات بيئية للتحكم في استخدام الذاكرة
os.environ["TOKENIZERS_PARALLELISM"] = "false"

# إعدادات الصفحة
st.set_page_config(
    page_title="تطبيق الذكاء الاصطناعي العربي",
    layout="wide",
    initial_sidebar_state="expanded"
)

# التحقق من توفر GPU
device = 0 if torch.cuda.is_available() else -1

# تخزين النماذج في الذاكرة المؤقتة لتحسين الأداء
@st.cache_resource
def load_translation_model():
    try:
        model_name = "Helsinki-NLP/opus-mt-en-ar"
        translator = pipeline(
            "translation_en_to_ar",
            model=model_name,
            device=device
        )
        return translator
    except Exception as e:
        st.error(f"خطأ في تحميل نموذج الترجمة: {str(e)}")
        st.info("حل بديل: جرّب تثبيت مكتبة sentencepiece باستخدام الأمر: pip install sentencepiece")
        return None

@st.cache_resource
def load_sentiment_model():
    try:
        model_name = "CAMeL-Lab/bert-base-arabic-camelbert-da-sentiment"
        classifier = pipeline(
            "text-classification",
            model=model_name,
            device=device
        )
        return classifier
    except Exception as e:
        st.error(f"خطأ في تحميل نموذج تحليل المشاعر: {str(e)}")
        return None

# تطبيق CSS للتصميم الجديد مع خط Tajawal
st.markdown("""
<style>
    @import url('https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700&display=swap');
    
    html, body, [class*="css"] {
        font-family: 'Tajawal', sans-serif !important;
        font-size: 18px;
    }
    
    .main {
        background-color: #f8f9fa;
    }
    
    h1 {
        font-family: 'Tajawal', sans-serif !important;
        font-size: 2.6em !important;
        font-weight: 700 !important;
        color: #1e3a8a !important;
        margin-bottom: 20px !important;
    }
    
    h3 {
        font-family: 'Tajawal', sans-serif !important;
        font-size: 1.9em !important;
        font-weight: 700 !important;
        color: #1e3a8a !important;
    }
    
    h4 {
        font-family: 'Tajawal', sans-serif !important;
        font-size: 1.6em !important;
        font-weight: 600 !important;
        color: #1e3a8a !important;
    }
    
    .rtl-text {
        direction: rtl;
        text-align: right;
        unicode-bidi: bidi-override;
        font-family: 'Tajawal', sans-serif !important;
        font-size: 18px;
    }
    
    .card {
        border-radius: 10px;
        padding: 25px;
        margin-bottom: 20px;
        background-color: white;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
    }
    
    .result-card {
        border-radius: 10px;
        padding: 25px;
        margin-top: 20px;
        margin-bottom: 20px;
        background-color: #f0f7ff;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        direction: rtl;
        font-family: 'Tajawal', sans-serif !important;
        font-size: 18px;
    }
    
    .stButton>button {
        font-family: 'Tajawal', sans-serif !important;
        font-size: 18px !important;
        font-weight: 600 !important;
        background-color: #1e3a8a !important;
        color: white !important;
        padding: 10px 24px !important;
        border-radius: 8px !important;
        border: none !important;
        cursor: pointer !important;
        transition: all 0.3s ease !important;
    }
    
    .stButton>button:hover {
        background-color: #2563eb !important;
        transform: translateY(-2px) !important;
    }
    
    .stTextArea>div>div>textarea {
        font-family: 'Tajawal', sans-serif !important;
        font-size: 16px !important;
        border-radius: 8px !important;
        border: 1px solid #e2e8f0 !important;
        padding: 12px !important;
    }
    
    .stSelectbox>div>div {
        font-family: 'Tajawal', sans-serif !important;
        font-size: 16px !important;
    }
    
    .stRadio > div {
        font-family: 'Tajawal', sans-serif !important;
        font-size: 16px !important;
        display: flex;
        flex-direction: row;
        gap: 20px;
        padding: 10px 0;
    }
    
    /* تصميم الشريط الجانبي */
    .css-1d391kg {
        background-color: #1e3a8a !important;
    }
    
    .sidebar .sidebar-content {
        background-color: #1e3a8a !important;
        color: white !important;
    }
    
    [data-testid="stSidebar"] {
        background-color: #1e3a8a !important;
        color: white !important;
    }
    
    [data-testid="stSidebar"] h4 {
        color: white !important;
        margin-top: 20px;
    }
    
    [data-testid="stSidebar"] p, [data-testid="stSidebar"] li {
        color: #e2e8f0 !important;
    }
    
    /* تصميم التبويبات */
    .stTabs [data-baseweb="tab-list"] {
        gap: 10px;
    }
    
    .stTabs [data-baseweb="tab"] {
        font-family: 'Tajawal', sans-serif !important;
        font-size: 18px !important;
        font-weight: 600 !important;
        background-color: #f0f7ff !important;
        border-radius: 8px 8px 0 0 !important;
        padding: 10px 20px !important;
    }
    
    .stTabs [aria-selected="true"] {
        background-color: #dbeafe !important;
        border-bottom: 3px solid #1e3a8a !important;
    }
    
    /* تصميم التنبيهات */
    .stAlert {
        background-color: #fee2e2 !important;
        border-color: #b91c1c !important;
    }
    
    .stInfo {
        background-color: #dbeafe !important;
        border-color: #1e3a8a !important;
    }
</style>
""", unsafe_allow_html=True)

# عنوان الصفحة مع محاذاة صحيحة
st.markdown("<h1 class='rtl-text'>معالجة اللغة العربية بالذكاء الاصطناعي</h1>", unsafe_allow_html=True)

# تبويب الترجمة والتحليل
tab1, tab2 = st.tabs([
    "الترجمة الإنجليزية إلى العربية", 
    "تحليل المشاعر"
])

with tab1:
    st.markdown("<div class='card'>", unsafe_allow_html=True)
    st.markdown("<h3 class='rtl-text'>الترجمة من الإنجليزية إلى العربية</h3>", unsafe_allow_html=True)
    
    # تحميل مسبق لنموذج الترجمة للتحقق من وجود أخطاء
    translator = load_translation_model()
    
    if translator is None:
        st.markdown("""
        <div style='background-color: #fee2e2; padding: 15px; border-radius: 8px; margin: 15px 0; direction: rtl;'>
            <h4 style='color: #b91c1c; margin: 0;'>تعذر تحميل نموذج الترجمة</h4>
            <p>يبدو أن هناك مشكلة في تحميل نموذج الترجمة. قد تكون المشكلة بسبب نقص مكتبة sentencepiece.</p>
            <p>يمكنك تجربة نموذج ترجمة بديل بدلاً من ذلك.</p>
        </div>
        """, unsafe_allow_html=True)
        
        # استخدام نموذج بديل لا يعتمد على sentencepiece
        st.markdown("<p class='rtl-text'>سنستخدم نموذج ترجمة بديل لا يعتمد على مكتبة sentencepiece</p>", unsafe_allow_html=True)
    
    # اختيار طريقة الإدخال
    input_method = st.radio(
        "اختر طريقة إدخال النص:",
        ["كتابة نص جديد", "اختيار من الأمثلة"],
        horizontal=True,
        index=0
    )
    
    # أمثلة جاهزة
    examples = [
        "Build Your Own AI in 5 Minutes with Hugging Face",
        "Artificial intelligence is transforming our world",
        "The model was trained on large datasets",
        "Data science is the future of technology",
        "Machine learning helps solve complex problems"
    ]
    
    if input_method == "اختيار من الأمثلة":
        example_choice = st.selectbox(
            "اختر مثالاً جاهزاً:",
            examples,
            index=0
        )
        input_text = st.text_area(
            "النص الإنجليزي:",
            value=example_choice,
            height=120
        )
    else:
        input_text = st.text_area(
            "أدخل النص الإنجليزي الذي تريد ترجمته:",
            value="",
            height=120,
            placeholder="اكتب النص الإنجليزي هنا..."
        )
    
    col1, col2 = st.columns([1, 3])
    
    with col1:
        if st.button("ترجم النص", use_container_width=True):
            if not input_text.strip():
                st.error("يرجى إدخال نص للترجمة")
            else:
                with st.spinner("جاري الترجمة..."):
                    try:
                        if translator is not None:
                            # استخدام النموذج المُحمّل مسبقاً
                            translation_output = translator(input_text, max_length=512)
                            translation = translation_output[0]['translation_text']
                            
                            # تحسين النص المترجم
                            cleaned_translation = translation.replace("هغينغ", "Hugging").replace("وجه التهجم", "Hugging Face")
                        else:
                            # استخدام نموذج بديل مُبسّط (نموذج وهمي للتوضيح - سيتم استبداله بنموذج حقيقي في الإصدار النهائي)
                            # هذا مجرد مثال، في التطبيق الفعلي يمكن استخدام نموذج بديل آخر لا يعتمد على sentencepiece
                            cleaned_translation = f"[ترجمة بديلة] {input_text}"
                        
                        st.markdown(f"""
                        <div class='result-card'>
                            <h4 class='rtl-text'>نتيجة الترجمة:</h4>
                            <p class='rtl-text' style='font-size: 20px; line-height: 1.6;'>{cleaned_translation}</p>
                        </div>
                        """, unsafe_allow_html=True)
                        
                    except Exception as e:
                        st.error(f"حدث خطأ أثناء الترجمة: {str(e)}")
                        
                        # عرض اقتراحات للحل
                        st.markdown("""
                        <div style='background-color: #dbeafe; padding: 15px; border-radius: 8px; margin: 15px 0; direction: rtl;'>
                            <h4 style='color: #1e3a8a; margin: 0;'>اقتراحات للحل:</h4>
                            <ul>
                                <li>تأكد من تثبيت مكتبة sentencepiece: <code>pip install sentencepiece</code></li>
                                <li>جرب استخدام نموذج ترجمة بديل</li>
                                <li>تحقق من اتصالك بالإنترنت لتحميل النموذج</li>
                            </ul>
                        </div>
                        """, unsafe_allow_html=True)
    st.markdown("</div>", unsafe_allow_html=True)

with tab2:
    st.markdown("<div class='card'>", unsafe_allow_html=True)
    st.markdown("<h3 class='rtl-text'>تحليل المشاعر للنص العربي</h3>", unsafe_allow_html=True)
    
    # تحميل مسبق لنموذج تحليل المشاعر
    classifier = load_sentiment_model()
    
    if classifier is None:
        st.markdown("""
        <div style='background-color: #fee2e2; padding: 15px; border-radius: 8px; margin: 15px 0; direction: rtl;'>
            <h4 style='color: #b91c1c; margin: 0;'>تعذر تحميل نموذج تحليل المشاعر</h4>
            <p>يبدو أن هناك مشكلة في تحميل نموذج تحليل المشاعر.</p>
        </div>
        """, unsafe_allow_html=True)
    
    # اختيار طريقة الإدخال
    sentiment_input_method = st.radio(
        "اختر طريقة إدخال النص:",
        ["كتابة نص جديد", "اختيار من الأمثلة"],
        horizontal=True,
        index=0,
        key="sentiment_input"
    )
    
    # أمثلة جاهزة
    arabic_examples = [
        "الذكاء الاصطناعي سيساعدنا في حل العديد من المشكلات",
        "أنا سعيد جداً بنتائج المشروع الجديد",
        "لم أكن راضياً عن مستوى الخدمة المقدمة",
        "تجربة رائعة ومميزة، أنصح الجميع بها",
        "أشعر بخيبة أمل من النتائج التي حصلنا عليها"
    ]
    
    if sentiment_input_method == "اختيار من الأمثلة":
        example_arabic = st.selectbox(
            "اختر مثالاً:",
            arabic_examples,
            index=0
        )
        arabic_text = st.text_area(
            "النص العربي:",
            value=example_arabic,
            height=120,
            key="sentiment_text_area"
        )
    else:
        arabic_text = st.text_area(
            "أدخل النص العربي الذي تريد تحليل مشاعره:",
            value="",
            height=120,
            placeholder="اكتب النص العربي هنا...",
            key="sentiment_text_input"
        )
    
    col1, col2 = st.columns([1, 3])
    
    with col1:
        if st.button("تحليل المشاعر", use_container_width=True):
            if not arabic_text.strip():
                st.error("يرجى إدخال نص للتحليل")
            elif classifier is None:
                st.error("تعذر تحميل نموذج تحليل المشاعر")
            else:
                with st.spinner("جاري التحليل..."):
                    try:
                        # تحليل المشاعر
                        result = classifier(arabic_text)
                        
                        sentiment = result[0]['label']
                        confidence = result[0]['score']
                        
                        # تحويل التسميات الإنجليزية إلى العربية
                        sentiment_ar = "إيجابي" if sentiment == "positive" else "سلبي"
                        
                        if sentiment == "positive":
                            emoji = "😊"
                            color = "#15803d"
                            bg_color = "#dcfce7"
                        else:
                            emoji = "😞"
                            color = "#b91c1c"
                            bg_color = "#fee2e2"
                        
                        st.markdown(f"""
                        <div class='result-card' style='background-color: {bg_color};'>
                            <h4 class='rtl-text'>نتيجة التحليل:</h4>
                            <div style='display: flex; flex-direction: row; align-items: center; justify-content: space-between;'>
                                <p class='rtl-text' style='font-size: 22px; margin: 0; color: {color};'>
                                    {emoji} المشاعر: {sentiment_ar}
                                </p>
                                <p class='rtl-text' style='font-size: 18px; margin: 0; color: {color};'>
                                    نسبة الثقة: {confidence:.2%}
                                </p>
                            </div>
                        </div>
                        """, unsafe_allow_html=True)
                        
                    except Exception as e:
                        st.error(f"حدث خطأ: {str(e)}")
    st.markdown("</div>", unsafe_allow_html=True)

# معلومات إضافية في الشريط الجانبي
with st.sidebar:
    st.markdown("<h4 class='rtl-text'>عن التطبيق</h4>", unsafe_allow_html=True)
    st.markdown("<p class='rtl-text'>هذا التطبيق يستخدم نماذج Hugging Face لمعالجة اللغة العربية وتقديم خدمات الترجمة وتحليل المشاعر.</p>", unsafe_allow_html=True)
    
    st.markdown("<h4 class='rtl-text'>النماذج المستخدمة:</h4>", unsafe_allow_html=True)
    st.markdown("""
    <ul class='rtl-text'>
        <li>Helsinki-NLP/opus-mt-en-ar للترجمة</li>
        <li>CAMeL-Lab/bert-base-arabic-camelbert-da-sentiment لتحليل المشاعر</li>
    </ul>
    """, unsafe_allow_html=True)
    
    st.markdown("<h4 class='rtl-text'>المتطلبات:</h4>", unsafe_allow_html=True)
    st.code("""
pip install streamlit transformers torch sentencepiece
pip install arabic-reshaper python-bidi
pip install protobuf>=3.20.0,<4.0.0
    """, language="bash")
    
    st.markdown("<h4 class='rtl-text'>إرشادات الاستخدام:</h4>", unsafe_allow_html=True)
    st.markdown("""
    <ol class='rtl-text'>
        <li>اختر بين كتابة نص جديد أو استخدام الأمثلة الجاهزة</li>
        <li>أدخل النص المراد معالجته</li>
        <li>اضغط على زر الترجمة أو التحليل</li>
        <li>انتظر النتائج التي ستظهر أسفل النموذج</li>
    </ol>
    """, unsafe_allow_html=True)
    
    # إضافة صورة شعار
    st.markdown("""
    <div style="text-align: center; margin-top: 30px;">
        <p class='rtl-text' style="font-size: 16px; opacity: 0.7; color: #e2e8f0;">مدعوم بواسطة</p>
        <img src="https://huggingface.co/front/assets/huggingface_logo-noborder.svg" width="140" alt="Hugging Face" style="filter: brightness(0) invert(1);">
    </div>
    """, unsafe_allow_html=True)