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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
|
5 |
+
# تحميل نموذج أخف للغة العربية
|
6 |
+
print("جاري تحميل النموذج الخفيف...")
|
7 |
+
MODEL_NAME = "aubmindlab/aragpt2-medium" # نموذج أخف من AraT5
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, low_cpu_mem_usage=True)
|
10 |
+
print("تم تحميل النموذج بنجاح!")
|
11 |
+
|
12 |
+
# القائمة المنسدلة للمشاعر
|
13 |
+
emotions = [
|
14 |
+
"سعيد", "حزين", "غاضب", "متحمس", "خائف", "متفائل", "متشائم",
|
15 |
+
"مندهش", "مشتاق", "ممتن", "فخور", "مرتبك", "محبط", "هادئ"
|
16 |
+
]
|
17 |
+
|
18 |
+
def generate_text(emotion, length_choice):
|
19 |
+
# تحويل اختيار الطول إلى رقم
|
20 |
+
length_options = {"قصير": 80, "متوسط": 150, "طويل": 250}
|
21 |
+
max_length = length_options.get(length_choice, 150)
|
22 |
+
|
23 |
+
# إعداد المطالبة بناءً على المشاعر
|
24 |
+
prompt = f"أشعر بأنني {emotion} لأن"
|
25 |
+
|
26 |
+
# تجهيز المدخلات
|
27 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
28 |
+
|
29 |
+
# توليد النص بإعدادات أخف
|
30 |
+
with torch.no_grad():
|
31 |
+
output = model.generate(
|
32 |
+
input_ids,
|
33 |
+
max_length=max_length,
|
34 |
+
num_return_sequences=1,
|
35 |
+
temperature=0.8,
|
36 |
+
top_k=40,
|
37 |
+
top_p=0.9,
|
38 |
+
no_repeat_ngram_size=2,
|
39 |
+
pad_token_id=tokenizer.eos_token_id
|
40 |
+
)
|
41 |
+
|
42 |
+
# تحويل النتيجة إلى نص
|
43 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
44 |
+
|
45 |
+
# تنظيف النص وإضافة علامات ترقيم إذا لزم الأمر
|
46 |
+
if not any(generated_text.endswith(x) for x in [".", "!", "؟", "،", "؛"]):
|
47 |
+
generated_text += "."
|
48 |
+
|
49 |
+
return generated_text
|
50 |
+
|
51 |
+
# تعريف CSS المخصص للواجهة
|
52 |
+
css = """
|
53 |
+
:root {
|
54 |
+
--rose-gold: #bd8c7d;
|
55 |
+
--rose-gold-light: #d1a193;
|
56 |
+
--black: #000000;
|
57 |
+
--dark-gray: #121212;
|
58 |
+
}
|
59 |
+
|
60 |
+
body {
|
61 |
+
background-color: var(--black) !important;
|
62 |
+
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" opacity="0.08"><text x="10" y="30" font-family="Arial" font-size="20" fill="%23bd8c7d">أ</text><text x="40" y="50" font-family="Arial" font-size="25" fill="%23bd8c7d">ب</text><text x="70" y="20" font-family="Arial" font-size="15" fill="%23bd8c7d">ت</text><text x="20" y="70" font-family="Arial" font-size="30" fill="%23bd8c7d">ث</text><text x="60" y="80" font-family="Arial" font-size="20" fill="%23bd8c7d">ج</text></svg>');
|
63 |
+
}
|
64 |
+
|
65 |
+
.gradio-container {
|
66 |
+
margin: 0 auto;
|
67 |
+
background-color: var(--black) !important;
|
68 |
+
max-width: 850px !important;
|
69 |
+
}
|
70 |
+
|
71 |
+
.main {
|
72 |
+
background-color: var(--dark-gray) !important;
|
73 |
+
border: 2px solid var(--rose-gold) !important;
|
74 |
+
border-radius: 15px !important;
|
75 |
+
box-shadow: 0 0 15px rgba(189, 140, 125, 0.3) !important;
|
76 |
+
padding: 20px !important;
|
77 |
+
position: relative;
|
78 |
+
overflow: hidden;
|
79 |
+
}
|
80 |
+
|
81 |
+
.main::before {
|
82 |
+
content: "";
|
83 |
+
position: absolute;
|
84 |
+
top: 0;
|
85 |
+
left: 0;
|
86 |
+
right: 0;
|
87 |
+
height: 5px;
|
88 |
+
background: linear-gradient(90deg, var(--black), var(--rose-gold), var(--black));
|
89 |
+
}
|
90 |
+
|
91 |
+
h1, h2, h3, p, span, label {
|
92 |
+
color: var(--rose-gold) !important;
|
93 |
+
font-family: 'Amiri', 'Noto Sans Arabic', sans-serif !important;
|
94 |
+
}
|
95 |
+
|
96 |
+
.title {
|
97 |
+
text-align: center;
|
98 |
+
position: relative;
|
99 |
+
padding-bottom: 15px;
|
100 |
+
}
|
101 |
+
|
102 |
+
.title::after {
|
103 |
+
content: "";
|
104 |
+
position: absolute;
|
105 |
+
bottom: -5px;
|
106 |
+
left: 50%;
|
107 |
+
transform: translateX(-50%);
|
108 |
+
font-size: 24px;
|
109 |
+
color: var(--rose-gold);
|
110 |
+
opacity: 0.7;
|
111 |
+
}
|
112 |
+
|
113 |
+
.prose {
|
114 |
+
color: var(--rose-gold-light) !important;
|
115 |
+
}
|
116 |
+
|
117 |
+
textarea, .output-text, .input-text {
|
118 |
+
color: var(--rose-gold) !important;
|
119 |
+
background-color: var(--dark-gray) !important;
|
120 |
+
border: 1px solid var(--rose-gold) !important;
|
121 |
+
border-radius: 8px !important;
|
122 |
+
font-family: 'Amiri', 'Noto Sans Arabic', sans-serif !important;
|
123 |
+
font-size: 18px !important;
|
124 |
+
line-height: 1.6 !important;
|
125 |
+
padding: 15px !important;
|
126 |
+
box-shadow: inset 0 0 8px rgba(189, 140, 125, 0.1) !important;
|
127 |
+
}
|
128 |
+
|
129 |
+
button {
|
130 |
+
background-color: var(--rose-gold) !important;
|
131 |
+
color: var(--black) !important;
|
132 |
+
border: none !important;
|
133 |
+
border-radius: 8px !important;
|
134 |
+
font-family: 'Amiri', 'Noto Sans Arabic', sans-serif !important;
|
135 |
+
font-weight: bold !important;
|
136 |
+
transition: all 0.3s ease !important;
|
137 |
+
padding: 10px 20px !important;
|
138 |
+
letter-spacing: 1px !important;
|
139 |
+
}
|
140 |
+
|
141 |
+
button:hover {
|
142 |
+
background-color: var(--rose-gold-light) !important;
|
143 |
+
box-shadow: 0 0 10px rgba(189, 140, 125, 0.6) !important;
|
144 |
+
transform: translateY(-2px);
|
145 |
+
}
|
146 |
+
|
147 |
+
select, .dropdown {
|
148 |
+
background-color: var(--dark-gray) !important;
|
149 |
+
color: var(--rose-gold) !important;
|
150 |
+
border: 1px solid var(--rose-gold) !important;
|
151 |
+
border-radius: 8px !important;
|
152 |
+
font-family: 'Amiri', 'Noto Sans Arabic', sans-serif !important;
|
153 |
+
}
|
154 |
+
|
155 |
+
.footer {
|
156 |
+
border-top: 1px solid var(--rose-gold) !important;
|
157 |
+
color: var(--rose-gold) !important;
|
158 |
+
margin-top: 30px;
|
159 |
+
padding-top: 15px;
|
160 |
+
text-align: center;
|
161 |
+
position: relative;
|
162 |
+
}
|
163 |
+
|
164 |
+
.footer::before {
|
165 |
+
content: "♱";
|
166 |
+
position: absolute;
|
167 |
+
top: -15px;
|
168 |
+
left: 50%;
|
169 |
+
transform: translateX(-50%);
|
170 |
+
background-color: var(--dark-gray);
|
171 |
+
padding: 0 15px;
|
172 |
+
color: var(--rose-gold);
|
173 |
+
font-size: 18px;
|
174 |
+
}
|
175 |
+
|
176 |
+
/* زخارف إضافية */
|
177 |
+
.corner-decoration {
|
178 |
+
position: absolute;
|
179 |
+
font-size: 24px;
|
180 |
+
color: var(--rose-gold);
|
181 |
+
opacity: 0.4;
|
182 |
+
}
|
183 |
+
|
184 |
+
.top-right {
|
185 |
+
top: 15px;
|
186 |
+
right: 15px;
|
187 |
+
}
|
188 |
+
|
189 |
+
.top-left {
|
190 |
+
top: 15px;
|
191 |
+
left: 15px;
|
192 |
+
}
|
193 |
+
|
194 |
+
.bottom-right {
|
195 |
+
bottom: 15px;
|
196 |
+
right: 15px;
|
197 |
+
}
|
198 |
+
|
199 |
+
.bottom-left {
|
200 |
+
bottom: 15px;
|
201 |
+
left: 15px;
|
202 |
+
}
|
203 |
+
|
204 |
+
.output-container {
|
205 |
+
position: relative;
|
206 |
+
padding: 10px;
|
207 |
+
border-radius: 10px;
|
208 |
+
border: 1px solid var(--rose-gold);
|
209 |
+
background-color: rgba(0, 0, 0, 0.3);
|
210 |
+
}
|
211 |
+
|
212 |
+
.output-container::before {
|
213 |
+
content: "۞";
|
214 |
+
font-size: 24px;
|
215 |
+
color: var(--rose-gold);
|
216 |
+
display: block;
|
217 |
+
text-align: center;
|
218 |
+
margin-bottom: 10px;
|
219 |
+
opacity: 0.5;
|
220 |
+
}
|
221 |
+
"""
|
222 |
+
|
223 |
+
# إنشاء واجهة المستخدم
|
224 |
+
with gr.Blocks(css=css) as demo:
|
225 |
+
with gr.Column(elem_classes=["main"]):
|
226 |
+
gr.HTML("""
|
227 |
+
<div class="corner-decoration top-right">٭</div>
|
228 |
+
<div class="corner-decoration top-left">٭</div>
|
229 |
+
<div class="corner-decoration bottom-right">٭</div>
|
230 |
+
<div class="corner-decoration bottom-left">٭</div>
|
231 |
+
|
232 |
+
<div class="title">
|
233 |
+
<h1>❁ اكتبلي ❁</h1>
|
234 |
+
<h2>مولد النصوص العربية بالمشاعر</h2>
|
235 |
+
<p>أداة لتوليد نصوص عربية إبداعية بناءً على المشاعر التي تختارها</p>
|
236 |
+
</div>
|
237 |
+
""")
|
238 |
+
|
239 |
+
with gr.Row():
|
240 |
+
with gr.Column(scale=1):
|
241 |
+
emotion = gr.Dropdown(
|
242 |
+
choices=emotions,
|
243 |
+
label="اختر المشاعر",
|
244 |
+
value="سعيد"
|
245 |
+
)
|
246 |
+
length = gr.Radio(
|
247 |
+
choices=["قصير", "متوسط", "طويل"],
|
248 |
+
label="طول النص",
|
249 |
+
value="متوسط"
|
250 |
+
)
|
251 |
+
generate_button = gr.Button("✨ توليد النص ✨")
|
252 |
+
|
253 |
+
with gr.Column(scale=2):
|
254 |
+
with gr.Box(elem_classes=["output-container"]):
|
255 |
+
output = gr.Textbox(
|
256 |
+
label="النص المولّد",
|
257 |
+
lines=12,
|
258 |
+
elem_classes=["output-text"]
|
259 |
+
)
|
260 |
+
|
261 |
+
gr.HTML("""
|
262 |
+
<div class="footer">
|
263 |
+
<p>❁ اكتبلي - إبداع بلا حدود ❁</p>
|
264 |
+
</div>
|
265 |
+
""")
|
266 |
+
|
267 |
+
generate_button.click(
|
268 |
+
fn=generate_text,
|
269 |
+
inputs=[emotion, length],
|
270 |
+
outputs=output
|
271 |
+
)
|
272 |
+
|
273 |
+
# تشغيل التطبيق
|
274 |
+
demo.launch()
|