File size: 6,333 Bytes
164603c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6948096
 
164603c
 
6948096
 
 
 
 
 
164603c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257c23e
 
 
 
 
 
 
 
164603c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97c4c9c
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
import gradio as gr
import torch
from transformers import (
    AutoTokenizer, 
    AutoModelForCausalLM, 
    AutoModelForSequenceClassification,
    pipeline
)
from datasets import load_dataset
import numpy as np
import re
import os

# تعيين توكن Hugging Face من متغير بيئي
HF_TOKEN = os.getenv('HF_TOKEN')
if not HF_TOKEN:
    raise ValueError("يرجى تعيين متغير البيئة HF_TOKEN")

# تهيئة النماذج
print("جاري تهيئة النماذج...")
base_model_name = "aubmindlab/aragpt2-base"
sentiment_model_name = "CAMeL-Lab/bert-base-arabic-camelbert-msa"

# تهيئة المعالجات
tokenizer = AutoTokenizer.from_pretrained(base_model_name, use_auth_token=HF_TOKEN)
model = AutoModelForCausalLM.from_pretrained(base_model_name, use_auth_token=HF_TOKEN)

# إعداد معالجات النصوص
text_generator = pipeline(
    'text-generation',
    model=model,
    tokenizer=tokenizer,
    device=0 if torch.cuda.is_available() else -1
)

sentiment_analyzer = pipeline(
    'sentiment-analysis',
    model=sentiment_model_name,
    tokenizer=AutoTokenizer.from_pretrained(sentiment_model_name),
    device=0 if torch.cuda.is_available() else -1
)

def clean_arabic_text(text):
    # إزالة الأسطر الجديدة والمسافات الزائدة
    text = ' '.join(text.split())
    # إزالة التشكيل
    text = re.sub(r'[\u064B-\u065F\u0670]', '', text)
    # إزالة الرموز غير المرغوب فيها
    text = re.sub(r'[^\u0600-\u06FF\s]', ' ', text)
    # توحيد الألف والياء
    text = re.sub('[إأآا]', 'ا', text)
    text = re.sub('[ىي]', 'ي', text)
    # إزالة التكرار
    text = re.sub(r'(.)\1+', r'\1', text)
    return text.strip()

def analyze_sentiment(text):
    try:
        result = sentiment_analyzer(text)[0]
        if result['label'] == 'positive':
            return "إيجابي", result['score']
        elif result['label'] == 'negative':
            return "سلبي", result['score']
        else:
            return "محايد", result['score']
    except:
        return "محايد", 0.5

def summarize_text(text, max_length=100):
    try:
        summary = text_generator(
            f"لخص النص التالي: {text}",
            max_length=max_length,
            num_return_sequences=1,
            no_repeat_ngram_size=2,
            do_sample=True,
            top_k=50,
            top_p=0.95,
            temperature=0.7
        )[0]['generated_text']
        return summary
    except:
        return "لم نتمكن من تلخيص النص"

def suggest_response(text):
    try:
        response = text_generator(
            f"اقترح رداً مناسباً على النص التالي: {text}",
            max_length=150,
            num_return_sequences=1,
            no_repeat_ngram_size=2,
            do_sample=True,
            top_k=50,
            top_p=0.95,
            temperature=0.7
        )[0]['generated_text']
        return response
    except:
        return "لم نتمكن من توليد رد مناسب"

def detect_topics(text):
    topics = {
        "سياسة": ["حكومة", "وزير", "برلمان", "رئيس", "انتخابات"],
        "اقتصاد": ["اقتصاد", "سوق", "بورصة", "أسهم", "استثمار"],
        "رياضة": ["كرة", "مباراة", "فريق", "لاعب", "بطولة"],
        "تكنولوجيا": ["تقنية", "إنترنت", "تطبيق", "برمجة", "ذكاء اصطناعي"],
        "ثقافة": ["فن", "أدب", "مسرح", "سينما", "موسيقى"]
    }
    
    text_lower = text.lower()
    detected = []
    for topic, keywords in topics.items():
        if any(keyword in text_lower for keyword in keywords):
            detected.append(topic)
    
    return detected if detected else ["عام"]

def analyze_text(text, include_summary=True, include_response=True):
    if not text.strip():
        return "الرجاء إدخال نص للتحليل"
    
    try:
        # تنظيف النص
        cleaned_text = clean_arabic_text(text)
        
        # تحليل المشاعر
        sentiment, confidence = analyze_sentiment(cleaned_text)
        
        # تحديد المواضيع
        topics = detect_topics(cleaned_text)
        
        # إنشاء التقرير
        report = f"""🔍 تحليل النص:

📝 النص الأصلي:
{text}

📊 التحليل الأساسي:
• المشاعر: {sentiment} (الثقة: {confidence:.1%})
• المواضيع: {', '.join(topics)}
"""
        
        # إضافة التلخيص إذا مطلوب
        if include_summary:
            summary = summarize_text(cleaned_text)
            report += f"\n✨ ملخص النص:\n{summary}"
        
        # إضافة الرد المقترح إذا مطلوب
        if include_response:
            response = suggest_response(cleaned_text)
            report += f"\n💡 الرد المقترح:\n{response}"
        
        return report
        
    except Exception as e:
        return f"⚠️ حدث خطأ أثناء التحليل: {str(e)}"

# إنشاء واجهة المستخدم
demo = gr.Interface(
    fn=analyze_text,
    inputs=[
        gr.Textbox(
            label="أدخل النص هنا",
            placeholder="اكتب نصاً عربياً هنا للتحليل...",
            lines=5
        ),
        gr.Checkbox(
            label="تضمين ملخص للنص",
            value=True
        ),
        gr.Checkbox(
            label="تضمين رد مقترح",
            value=True
        )
    ],
    outputs=gr.Textbox(label="نتائج التحليل", lines=12),
    title="🤖 المحلل الذكي للنصوص العربية",
    description="""نموذج متقدم لتحليل النصوص العربية وتوليد الردود
    ✨ المميزات:
    • تحليل المشاعر في النص
    • تحديد المواضيع الرئيسية
    • تلخيص النص
    • اقتراح ردود مناسبة
    • معالجة متقدمة للغة العربية
    """,
    theme="default"
)

# تشغيل الواجهة
if __name__ == "__main__":
    print("جاري تشغيل النموذج...")
    demo.launch()