Spaces:
Sleeping
Sleeping
Commit
·
164603c
1
Parent(s):
273d1df
إضافة محلل النصوص العربية
Browse files- README.md +48 -6
- app.py +183 -0
- requirements.txt +8 -0
README.md
CHANGED
@@ -1,14 +1,56 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
-
short_description: test
|
12 |
---
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: محلل النصوص العربية
|
3 |
+
emoji: 🤖
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: green
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.50.2
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
+
short_description: 'test '
|
12 |
---
|
13 |
|
14 |
+
# 🤖 المحلل الذكي للنصوص العربية
|
15 |
+
|
16 |
+
تطبيق ذكي لتحليل النصوص العربية باستخدام تقنيات الذكاء الاصطناعي المتقدمة.
|
17 |
+
|
18 |
+
## ✨ المميزات
|
19 |
+
|
20 |
+
- 📊 تحليل المشاعر في النص (إيجابي، سلبي، محايد)
|
21 |
+
- 🎯 تحديد المواضيع الرئيسية
|
22 |
+
- 📝 تلخيص النص تلقائياً
|
23 |
+
- 💡 اقتراح ردود مناسبة
|
24 |
+
- 🔍 معالجة متقدمة للغة العربية
|
25 |
+
|
26 |
+
## 🛠️ التقنيات المستخدمة
|
27 |
+
|
28 |
+
- نموذج `aragpt2-base` لتوليد النصوص
|
29 |
+
- نموذج `CAMeL-Lab/bert-base-arabic-camelbert-msa` لتحليل المشاعر
|
30 |
+
- مكتبة `ArabertPreprocessor` لمعالجة النصوص العربية
|
31 |
+
- واجهة مستخدم تفاعلية باستخدام `Gradio`
|
32 |
+
|
33 |
+
## 📋 كيفية الاستخدام
|
34 |
+
|
35 |
+
1. أدخل النص العربي في مربع النص
|
36 |
+
2. اختر ما إذا كنت تريد تلخيص النص
|
37 |
+
3. اختر ما إذا كنت تريد اقتراح رد
|
38 |
+
4. انتظر النتائج التي ستظهر في الأسفل
|
39 |
+
|
40 |
+
## 🌟 مثال
|
41 |
+
|
42 |
+
```
|
43 |
+
النص: أعلنت وزارة التعليم عن خطة جديدة لتطوير المناهج الدراسية
|
44 |
+
|
45 |
+
النتيجة:
|
46 |
+
- الموضوع: تعليم
|
47 |
+
- المشاعر: إيجابي
|
48 |
+
- ملخص: إعلان خطة تطوير المناهج
|
49 |
+
- رد مقترح: خطوة إيجابية نحو تحسين جودة التعليم
|
50 |
+
```
|
51 |
+
|
52 |
+
## 📄 الترخيص
|
53 |
+
|
54 |
+
هذا المشروع مرخص تحت رخصة MIT.
|
55 |
+
|
56 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import (
|
4 |
+
AutoTokenizer,
|
5 |
+
AutoModelForCausalLM,
|
6 |
+
AutoModelForSequenceClassification,
|
7 |
+
pipeline
|
8 |
+
)
|
9 |
+
from datasets import load_dataset
|
10 |
+
import numpy as np
|
11 |
+
from arabert.preprocess import ArabertPreprocessor
|
12 |
+
import re
|
13 |
+
import os
|
14 |
+
|
15 |
+
# تعيين توكن Hugging Face من متغير بيئي
|
16 |
+
HF_TOKEN = os.getenv('HF_TOKEN')
|
17 |
+
if not HF_TOKEN:
|
18 |
+
raise ValueError("يرجى تعيين متغير البيئة HF_TOKEN")
|
19 |
+
|
20 |
+
# تهيئة النماذج
|
21 |
+
print("جاري تهيئة النماذج...")
|
22 |
+
base_model_name = "aubmindlab/aragpt2-base"
|
23 |
+
sentiment_model_name = "CAMeL-Lab/bert-base-arabic-camelbert-msa"
|
24 |
+
|
25 |
+
# تهيئة المعالجات
|
26 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name, use_auth_token=HF_TOKEN)
|
27 |
+
model = AutoModelForCausalLM.from_pretrained(base_model_name, use_auth_token=HF_TOKEN)
|
28 |
+
arabert_prep = ArabertPreprocessor(model_name="aubmindlab/bert-base-arabertv2")
|
29 |
+
|
30 |
+
# إعداد معالجات النصوص
|
31 |
+
text_generator = pipeline(
|
32 |
+
'text-generation',
|
33 |
+
model=model,
|
34 |
+
tokenizer=tokenizer,
|
35 |
+
device=0 if torch.cuda.is_available() else -1
|
36 |
+
)
|
37 |
+
|
38 |
+
sentiment_analyzer = pipeline(
|
39 |
+
'sentiment-analysis',
|
40 |
+
model=sentiment_model_name,
|
41 |
+
tokenizer=AutoTokenizer.from_pretrained(sentiment_model_name),
|
42 |
+
device=0 if torch.cuda.is_available() else -1
|
43 |
+
)
|
44 |
+
|
45 |
+
def clean_arabic_text(text):
|
46 |
+
# إزالة الأسطر الجديدة والمسافات الزائدة
|
47 |
+
text = ' '.join(text.split())
|
48 |
+
# معالجة النص باستخدام ArabertPreprocessor
|
49 |
+
text = arabert_prep.preprocess(text)
|
50 |
+
# إزالة الرموز غير المرغوب فيها
|
51 |
+
text = re.sub(r'[^\u0600-\u06FF\s]', ' ', text)
|
52 |
+
return text
|
53 |
+
|
54 |
+
def analyze_sentiment(text):
|
55 |
+
try:
|
56 |
+
result = sentiment_analyzer(text)[0]
|
57 |
+
if result['label'] == 'positive':
|
58 |
+
return "إيجابي", result['score']
|
59 |
+
elif result['label'] == 'negative':
|
60 |
+
return "سلبي", result['score']
|
61 |
+
else:
|
62 |
+
return "محايد", result['score']
|
63 |
+
except:
|
64 |
+
return "محايد", 0.5
|
65 |
+
|
66 |
+
def summarize_text(text, max_length=100):
|
67 |
+
try:
|
68 |
+
summary = text_generator(
|
69 |
+
f"لخص النص التالي: {text}",
|
70 |
+
max_length=max_length,
|
71 |
+
num_return_sequences=1,
|
72 |
+
no_repeat_ngram_size=2,
|
73 |
+
do_sample=True,
|
74 |
+
top_k=50,
|
75 |
+
top_p=0.95,
|
76 |
+
temperature=0.7
|
77 |
+
)[0]['generated_text']
|
78 |
+
return summary
|
79 |
+
except:
|
80 |
+
return "لم نتمكن من تلخيص النص"
|
81 |
+
|
82 |
+
def suggest_response(text):
|
83 |
+
try:
|
84 |
+
response = text_generator(
|
85 |
+
f"اقترح رداً مناسباً على النص التالي: {text}",
|
86 |
+
max_length=150,
|
87 |
+
num_return_sequences=1,
|
88 |
+
no_repeat_ngram_size=2,
|
89 |
+
do_sample=True,
|
90 |
+
top_k=50,
|
91 |
+
top_p=0.95,
|
92 |
+
temperature=0.7
|
93 |
+
)[0]['generated_text']
|
94 |
+
return response
|
95 |
+
except:
|
96 |
+
return "لم نتمكن من توليد رد مناسب"
|
97 |
+
|
98 |
+
def detect_topics(text):
|
99 |
+
topics = {
|
100 |
+
"سياسة": ["حكومة", "وزير", "برلمان", "رئيس", "انتخابات"],
|
101 |
+
"اقتصاد": ["اقتصاد", "سوق", "بورصة", "أسهم", "استثمار"],
|
102 |
+
"رياضة": ["كرة", "مباراة", "فريق", "لاعب", "بطولة"],
|
103 |
+
"تكنولوجيا": ["تقنية", "إنترنت", "تطبيق", "برمجة", "ذكاء اصطناعي"],
|
104 |
+
"ثقافة": ["فن", "أدب", "مسرح", "سينما", "موسيقى"]
|
105 |
+
}
|
106 |
+
|
107 |
+
text_lower = text.lower()
|
108 |
+
detected = []
|
109 |
+
for topic, keywords in topics.items():
|
110 |
+
if any(keyword in text_lower for keyword in keywords):
|
111 |
+
detected.append(topic)
|
112 |
+
|
113 |
+
return detected if detected else ["عام"]
|
114 |
+
|
115 |
+
def analyze_text(text, include_summary=True, include_response=True):
|
116 |
+
if not text.strip():
|
117 |
+
return "الرجاء إدخال نص للتحليل"
|
118 |
+
|
119 |
+
try:
|
120 |
+
# تنظيف النص
|
121 |
+
cleaned_text = clean_arabic_text(text)
|
122 |
+
|
123 |
+
# تحليل المشاعر
|
124 |
+
sentiment, confidence = analyze_sentiment(cleaned_text)
|
125 |
+
|
126 |
+
# تحديد المواضيع
|
127 |
+
topics = detect_topics(cleaned_text)
|
128 |
+
|
129 |
+
# إنشاء التقرير
|
130 |
+
report = f"""🔍 تحليل النص:
|
131 |
+
|
132 |
+
📝 النص الأصلي:
|
133 |
+
{text}
|
134 |
+
|
135 |
+
📊 التحليل الأساسي:
|
136 |
+
• المشاعر: {sentiment} (الثقة: {confidence:.1%})
|
137 |
+
• المواضيع: {', '.join(topics)}
|
138 |
+
"""
|
139 |
+
|
140 |
+
# إضافة التلخيص إذا مطلوب
|
141 |
+
if include_summary:
|
142 |
+
summary = summarize_text(cleaned_text)
|
143 |
+
report += f"\n✨ ملخص النص:\n{summary}"
|
144 |
+
|
145 |
+
# إضافة الرد المقترح إذا مطلوب
|
146 |
+
if include_response:
|
147 |
+
response = suggest_response(cleaned_text)
|
148 |
+
report += f"\n💡 الر�� المقترح:\n{response}"
|
149 |
+
|
150 |
+
return report
|
151 |
+
|
152 |
+
except Exception as e:
|
153 |
+
return f"⚠️ حدث خطأ أثناء التحليل: {str(e)}"
|
154 |
+
|
155 |
+
# إنشاء واجهة المستخدم
|
156 |
+
demo = gr.Interface(
|
157 |
+
fn=analyze_text,
|
158 |
+
inputs=[
|
159 |
+
gr.Textbox(
|
160 |
+
label="أدخل النص هنا",
|
161 |
+
placeholder="اكتب نصاً عربياً هنا للتحليل...",
|
162 |
+
lines=5
|
163 |
+
),
|
164 |
+
gr.Checkbox(label="تضمين ملخص للنص", default=True),
|
165 |
+
gr.Checkbox(label="تضمين رد مقترح", default=True)
|
166 |
+
],
|
167 |
+
outputs=gr.Textbox(label="نتائج التحليل", lines=12),
|
168 |
+
title="🤖 المحلل الذكي للنصوص العربية",
|
169 |
+
description="""نموذج متقدم لتحليل النصوص العربية وتوليد الردود
|
170 |
+
✨ المميزات:
|
171 |
+
• تحليل المشاعر في النص
|
172 |
+
• تحديد المواضيع الرئيسية
|
173 |
+
• تلخيص النص
|
174 |
+
• اقتراح ردود مناسبة
|
175 |
+
• معالجة متقدمة للغة العربية
|
176 |
+
""",
|
177 |
+
theme="default"
|
178 |
+
)
|
179 |
+
|
180 |
+
# تشغيل الواجهة
|
181 |
+
if __name__ == "__main__":
|
182 |
+
print("جاري تشغيل النموذج...")
|
183 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers==4.30.2
|
2 |
+
torch==2.0.1
|
3 |
+
gradio==3.35.2
|
4 |
+
datasets==2.13.0
|
5 |
+
scikit-learn==1.2.2
|
6 |
+
numpy==1.24.3
|
7 |
+
arabert==1.0.1
|
8 |
+
regex==2023.5.5
|