Rayan545454 commited on
Commit
5d481eb
·
verified ·
1 Parent(s): 5b6b7f5

import re

Browse files

import gradio as gr

# --- دالة لتطبيع النص العربي (إزالة التشكيل + توحيد الحروف) ---
def normalize_ar(text: str) -> str:
if not text:
return ""
text = text.lower()
text = re.sub(r'[\u064B-\u0652\u0640]', '', text) # إزالة الحركات + التطويل
text = re.sub(r'[إأآٱ]', 'ا', text)
text = text.replace('ى','ي').replace('ؤ','و').replace('ئ','ي').replace('ة','ه')
return text.strip()

# --- قاعدة الأسئلة والأجوبة الثابتة ---
QA = [
{
"patterns": ["كم سنه المملكه", "كم سنه المملكة السعودية", "عمر المملكه", "كم عمر السعوديه", "كم سنه السعوديه"],
"answer": "أربعة فصول."
},
{
"patterns": ["صاحب اول الملك", "اول ملك", "من اول ملك", "من صاحب اول الملك"],
"answer": "عبدالعزيز آل سعود."
},
{
"patterns": ["متى التاريخ توحيد المملكه", "متى تاريخ توحيد المملكه", "في اي سنه تم توحيد المملكه", "متى التوحيد", "تاريخ التوحيد"],
"answer": "1932."
},
{
"patterns": ["متى اليوم الوطني", "اليوم الوطني السعودي", "ماهو اليوم الوطني", "تاريخ اليوم الوطني"],
"answer": "٩٤."
}
]

# --- دالة البحث عن الجواب ---
def find_answer(question: str) -> str:
nq = normalize_ar(question)

for item in QA:
for p in item["patterns"]:
np = normalize_ar(p)
if nq == np or nq in np or np in nq:
return item["answer"]

return "لم أفهم السؤال. جرّب: «كم سنه المملكة السعودية؟» أو «صاحب أول الملك؟» أو «متى التاريخ توحيد المملكة؟» أو «متى اليوم الوطني السعودي؟»."

# --- واجهة Gradio ---
with gr.Blocks(title="سؤال وجواب – اليوم الوطني السعودي") as demo:
gr.Markdown("## 🇸🇦 روبوت سؤال وجواب عن اليوم الوطني السعودي\nاسأل أحد الأسئلة الأربعة التالية:")
gr.Markdown("1. كم سنه المملكة السعودية؟\n2. صاحب أول الملك؟\n3. متى التاريخ توحيد المملكة؟\n4. متى اليوم الوطني السعودي؟")

chat = gr.Chatbot()
msg = gr.Textbox(placeholder="اكتب سؤالك هنا...", label="سؤالك")

def respond(user_message, history):
bot_message = find_answer(user_message)
history.append((user_message, bot_message))
return history, ""

msg.submit(respond, [msg, chat], [chat, msg])

# للتشغيل محلياً: demo.launch()
# في HuggingFace Space يشتغل تلقائياً
if __name__ == "__main__":
demo.launch() - Initial Deployment

Files changed (2) hide show
  1. README.md +7 -5
  2. index.html +209 -18
README.md CHANGED
@@ -1,10 +1,12 @@
1
  ---
2
- title: Zzzz
3
- emoji: 💻
4
- colorFrom: red
5
- colorTo: purple
6
  sdk: static
7
  pinned: false
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: zzzz
3
+ emoji: 🐳
4
+ colorFrom: green
5
+ colorTo: gray
6
  sdk: static
7
  pinned: false
8
+ tags:
9
+ - deepsite
10
  ---
11
 
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
index.html CHANGED
@@ -1,19 +1,210 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
+ <!DOCTYPE html>
2
+ <html lang="ar" dir="rtl">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>روبوت سؤال وجواب - اليوم الوطني السعودي</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <script>
9
+ tailwind.config = {
10
+ theme: {
11
+ extend: {
12
+ colors: {
13
+ saudiGreen: '#245C36',
14
+ saudiWhite: '#FFFFFF',
15
+ },
16
+ fontFamily: {
17
+ arabic: ['"Noto Sans Arabic"', 'sans-serif'],
18
+ }
19
+ }
20
+ }
21
+ }
22
+ </script>
23
+ <link rel="preconnect" href="https://fonts.googleapis.com">
24
+ <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
25
+ <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Arabic:wght@300;400;500;600;700&display=swap" rel="stylesheet">
26
+ <link href="https://unpkg.com/aos@2.3.1/dist/aos.css" rel="stylesheet">
27
+ <script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
28
+ <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
29
+ <script src="https://unpkg.com/feather-icons"></script>
30
+ <style>
31
+ .chat-bubble {
32
+ border-radius: 1.5rem;
33
+ }
34
+ .user-bubble {
35
+ border-bottom-right-radius: 0;
36
+ }
37
+ .bot-bubble {
38
+ border-bottom-left-radius: 0;
39
+ }
40
+ .palm-icon {
41
+ filter: drop-shadow(0 4px 3px rgb(0 0 0 / 0.07)) drop-shadow(0 2px 2px rgb(0 0 0 / 0.06));
42
+ }
43
+ </style>
44
+ </head>
45
+ <body class="font-arabic bg-gradient-to-b from-saudiGreen to-green-900 min-h-screen text-saudiWhite">
46
+ <!-- Header with Saudi Flag Theme -->
47
+ <header class="container mx-auto px-4 py-8">
48
+ <div class="flex flex-col items-center justify-center" data-aos="fade-down">
49
+ <div class="bg-saudiWhite p-4 rounded-full shadow-xl mb-6">
50
+ <div class="bg-saudiGreen p-4 rounded-full text-saudiWhite flex items-center justify-center palm-icon">
51
+ <svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
52
+ <path d="M13 2a1 1 0 1 0-2 0v7.268a2 2 0 0 1-1 1.732V2a1 1 0 1 0-2 0v9a2 2 0 0 1-1 1.732V2a1 1 0 1 0-2 0v11a4 4 0 0 0 4 4h1a4 4 0 0 0 4-4V2Z"></path>
53
+ </svg>
54
+ </div>
55
+ </div>
56
+ <h1 class="text-3xl md:text-4xl font-bold text-center mb-2">روبوت سؤال وجواب</h1>
57
+ <h2 class="text-xl md:text-2xl font-medium text-center text-yellow-200">اليوم الوطني السعودي</h2>
58
+ </div>
59
+ </header>
60
+
61
+ <!-- Main Content -->
62
+ <main class="container mx-auto px-4 py-6 max-w-3xl">
63
+ <div class="bg-white/10 backdrop-blur-sm rounded-2xl shadow-lg overflow-hidden">
64
+ <!-- Chat Container -->
65
+ <div id="chat-container" class="h-96 overflow-y-auto p-4 space-y-4">
66
+ <!-- Welcome Message -->
67
+ <div data-aos="fade-up" class="flex justify-start">
68
+ <div class="bg-saudiWhite/10 chat-bubble bot-bubble max-w-xs md:max-w-md p-4 text-saudiWhite">
69
+ <p>مرحباً! أنا روبوت اليوم الوطني السعودي. يمكنك أن تسألني:</p>
70
+ <ul class="list-disc list-inside mt-2 space-y-1">
71
+ <li>كم سنه المملكة السعودية؟</li>
72
+ <li>صاحب أول الملك؟</li>
73
+ <li>متى التاريخ توحيد المملكة؟</li>
74
+ <li>متى اليوم الوطني السعودي؟</li>
75
+ </ul>
76
+ </div>
77
+ </div>
78
+ </div>
79
+
80
+ <!-- Input Area -->
81
+ <div class="border-t border-white/20 p-4">
82
+ <form id="chat-form" class="flex gap-2">
83
+ <input
84
+ id="user-input"
85
+ type="text"
86
+ placeholder="اكتب سؤالك هنا..."
87
+ class="flex-1 bg-white/20 backdrop-blur-sm rounded-full px-4 py-3 text-saudiWhite placeholder-white/60 focus:outline-none focus:ring-2 focus:ring-green-300"
88
+ >
89
+ <button
90
+ type="submit"
91
+ class="bg-green-600 hover:bg-green-700 text-saudiWhite rounded-full p-3 transition-colors duration-200 flex items-center justify-center"
92
+ >
93
+ <i data-feather="send"></i>
94
+ </button>
95
+ </form>
96
+ </div>
97
+ </div>
98
+ </main>
99
+
100
+ <!-- Footer -->
101
+ <footer class="container mx-auto px-4 py-8 text-center text-sm text-white/70">
102
+ <p>بمناسبة اليوم الوطني السعودي ٩٤ - 🇸🇦</p>
103
+ </footer>
104
+
105
+ <script>
106
+ // Initialize animations and icons
107
+ AOS.init({
108
+ duration: 800,
109
+ easing: 'ease-out-quad'
110
+ });
111
+ feather.replace();
112
+
113
+ // Chat functionality
114
+ const chatForm = document.getElementById('chat-form');
115
+ const chatContainer = document.getElementById('chat-container');
116
+ const userInput = document.getElementById('user-input');
117
+
118
+ // Question-Answer data
119
+ const qaData = [
120
+ {
121
+ patterns: ["كم سنه المملكه", "كم سنه المملكة السعودية", "عمر المملكه", "كم عمر السعوديه", "كم سنه السعوديه"],
122
+ answer: "أربعة فصول."
123
+ },
124
+ {
125
+ patterns: ["صاحب اول الملك", "اول ملك", "من اول ملك", "من صاحب اول الملك"],
126
+ answer: "الملك عبدالعزيز آل سعود رحمه الله."
127
+ },
128
+ {
129
+ patterns: ["متى التاريخ توحيد المملكه", "متى تاريخ توحيد المملكه", "في اي سنه تم توحيد المملكه", "متى التوحيد", "تاريخ التوحيد"],
130
+ answer: "تم توحيد المملكة العربية السعودية في عام ١٩٣٢م."
131
+ },
132
+ {
133
+ patterns: ["متى اليوم الوطني", "اليوم الوطني السعودي", "ماهو اليوم الوطني", "تاريخ اليوم الوطني"],
134
+ answer: "اليوم الوطني السعودي في ٢٣ سبتمبر من كل عام ويحتفل هذا العام بالذكرى ٩٤."
135
+ }
136
+ ];
137
+
138
+ // Normalize Arabic text
139
+ function normalizeAr(text) {
140
+ if (!text) return "";
141
+ text = text.toLowerCase();
142
+ text = text.replace(/[\u064B-\u0652\u0640]/g, ''); // Remove diacritics
143
+ text = text.replace(/[إأآٱ]/g, 'ا');
144
+ text = text.replace(/ى/g, 'ي').replace(/ؤ/g, 'و').replace(/ئ/g, 'ي').replace(/ة/g, 'ه');
145
+ return text.trim();
146
+ }
147
+
148
+ // Find answer
149
+ function findAnswer(question) {
150
+ const nq = normalizeAr(question);
151
+
152
+ for (const item of qaData) {
153
+ for (const p of item.patterns) {
154
+ const np = normalizeAr(p);
155
+ if (nq === np || nq.includes(np) || np.includes(nq)) {
156
+ return item.answer;
157
+ }
158
+ }
159
+ }
160
+
161
+ return "لم أفهم السؤال. جرّب أحد الأسئلة التالية:<br>1. كم سنه المملكة السعودية؟<br>2. صاحب أول الملك؟<br>3. متى التاريخ توحيد المملكة؟<br>4. متى اليوم الوطني السعودي؟";
162
+ }
163
+
164
+ // Add message to chat
165
+ function addMessage(message, isUser) {
166
+ const messageDiv = document.createElement('div');
167
+ messageDiv.className = `flex ${isUser ? 'justify-end' : 'justify-start'}`;
168
+
169
+ const bubble = document.createElement('div');
170
+ bubble.className = `chat-bubble ${isUser ? 'user-bubble bg-green-600' : 'bot-bubble bg-saudiWhite/10'} max-w-xs md:max-w-md p-4 text-saudiWhite`;
171
+ bubble.innerHTML = message;
172
+
173
+ messageDiv.appendChild(bubble);
174
+ chatContainer.appendChild(messageDiv);
175
+ chatContainer.scrollTop = chatContainer.scrollHeight;
176
+ }
177
+
178
+ // Handle form submission
179
+ chatForm.addEventListener('submit', function(e) {
180
+ e.preventDefault();
181
+ const question = userInput.value.trim();
182
+
183
+ if (question) {
184
+ addMessage(question, true);
185
+ userInput.value = '';
186
+
187
+ setTimeout(() => {
188
+ const answer = findAnswer(question);
189
+ addMessage(answer, false);
190
+ }, 600);
191
+ }
192
+ });
193
+
194
+ // Simple animation for chat bubbles
195
+ function animateChat() {
196
+ anime({
197
+ targets: '.chat-bubble',
198
+ scale: [0.9, 1],
199
+ opacity: [0, 1],
200
+ duration: 400,
201
+ easing: 'easeOutQuad',
202
+ delay: anime.stagger(100)
203
+ });
204
+ }
205
+
206
+ // Initial animation
207
+ setTimeout(animateChat, 500);
208
+ </script>
209
+ </body>
210
  </html>