Spaces:
D12004
/
Runtime error

D12004 commited on
Commit
f3263fe
·
verified ·
1 Parent(s): cff3c67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -33
app.py CHANGED
@@ -10,85 +10,116 @@ st.set_page_config(
10
  )
11
 
12
  # --- ضع مفتاح الـ Token الخاص بك هنا من Hugging Face ---
13
- # يمكنك الحصول عليه من Settings -> Access Tokens في موقع Hugging Face
14
  HF_TOKEN = "YOUR_HUGGING_FACE_TOKEN_HERE"
15
  client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta", token=HF_TOKEN)
16
 
17
- # 2. تنسيق الـ CSS المطور
18
  st.markdown("""
19
  <style>
20
  @import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;700&display=swap');
 
21
  .stApp { background: linear-gradient(135deg, #010409 0%, #0d1117 100%) !important; }
22
  [data-testid="stSidebar"] { background-color: #0d1117 !important; }
23
- [data-testid="stChatInput"] { background-color: #161b22 !important; border-radius: 15px; }
24
- [data-testid="stChatInput"] textarea { color: #ffffff !important; }
25
- .stMarkdown, p, li, label, div { color: #FFFFFF !important; font-family: 'Cairo', sans-serif !important; font-size: 20px !important; direction: rtl; text-align: right; }
26
- h1 { color: #00f2ff !important; text-shadow: 0 0 15px #00f2ff; text-align: center; font-family: 'Cairo', sans-serif !important; }
 
 
 
 
 
 
 
 
 
 
27
  .bot-text { background-color: #0d1a26; border-right: 5px solid #00f2ff; padding: 20px; border-radius: 15px; margin-bottom: 15px; }
28
  .user-text { background-color: #161b22; border-left: 5px solid #58a6ff; padding: 15px; border-radius: 12px; margin-bottom: 15px; }
 
 
 
29
  .step-box { border: 2px solid #00f2ff; padding: 15px; border-radius: 12px; margin: 10px 0; background: rgba(0, 242, 255, 0.1); color: #00f2ff; font-weight: bold; }
30
  </style>
31
  """, unsafe_allow_html=True)
32
 
33
- # 3. قاعدة المعرفة المحلية
34
  kb = {
35
- "تعريف": "البيانات الضخمة (Big Data) هي 'منجم الذهب' الرقمي؛ هي كميات هائلة من المعلومات اللي بتنتج كل ثانية! 🔍✨",
36
- "الفرق": "🌟 **مقارنة الأبطال:**\n1. **البيانات العادية:** زي جدول درجاتك.\n2. **البيانات الضخمة:** حجمها ضخم جداً ومحتاجة سيرفرات قوية. 🔥",
37
- "ذكاء": "🤝 **الثنائي الخارق:** البيانات الضخمة هي 'البنزين' والذكاء الاصطناعي هو 'المحرك'. 🧠🤖",
38
- "خصائص": "✨ **الخصائص الـ 5 (5Vs):**\n1. Volume | 2. Velocity | 3. Variety | 4. Veracity | 5. Value 💎",
39
- "ترتيب": """⛓️ **مخطط رحلة البيانات:**
40
- <div class="step-box">1️⃣ التجميع | 2️⃣ التخزين | 3️⃣ المعالجة | 4️⃣ التحليل | 5️⃣ القرار ✅</div>"""
 
 
 
 
 
41
  }
42
 
43
  # 4. إدارة حالة الجلسة
44
  if "messages" not in st.session_state:
45
- st.session_state.messages = [{"role": "assistant", "content": "مرحباً بك يا بطل! أنا وكيلك الذكي، اسألني عن أي شيء. 🤖✨"}]
46
  if "view" not in st.session_state:
47
  st.session_state.view = "chat"
48
 
49
- # 5. الواجهة الجانبية (الأزرار)
50
  with st.sidebar:
51
- st.markdown('<p style="color:#FFD700; text-align:center; font-weight:bold;">📍 المحطات</p>', unsafe_allow_html=True)
52
- if st.button("🔍 البيانات الضخمة"): st.session_state.q_auto = "تعريف"
53
- if st.button("⚡ الخصائص"): st.session_state.q_auto = "خصائص"
 
 
 
 
 
 
54
  if st.button("📝 اختبار الأبطال"): st.session_state.view = "quiz"; st.rerun()
55
- if st.button("🔄 محادثة جديدة"): st.session_state.messages = []; st.session_state.view = "chat"; st.rerun()
 
56
 
57
- # 6. منطق الرد الهجين (قاعدة معرفة + Hugging Face)
58
  def get_ai_response(prompt):
59
- # البحث في قاعدة المعرفة أولاً
 
60
  for key in kb:
61
- if key in prompt:
62
- return kb[key]
63
-
64
- # إذا لم يجد، يسأل Hugging Face
65
  try:
66
- formatted_prompt = f"<|system|>\nأنت مساعد تعليمي ذكي للأطفال تشرح البيانات الضخمة بالعربي ببساطة وبطريقة مشجعة.</s>\n<|user|>\n{prompt}</s>\n<|assistant|>\n"
67
  response = ""
68
  for message in client.chat_completion(
69
  messages=[{"role": "user", "content": formatted_prompt}],
70
- max_tokens=200,
71
  stream=True
72
  ):
73
  response += message.choices[0].delta.content or ""
74
  return response
75
  except:
76
- return "سؤال ذكي! البيانات الضخمة هي محرك المستقبل. هل تود معرفة المزيد عن خصائصها؟ 🤖"
77
 
78
- # ا��عرض
79
  if st.session_state.view == "quiz":
80
  st.markdown("<h1>📝 اختبار التحدي</h1>", unsafe_allow_html=True)
81
- # ... (كود الاختبار كما هو) ...
 
 
 
 
 
82
  if st.button("العودة للدردشة"): st.session_state.view = "chat"; st.rerun()
83
  else:
84
  st.markdown("<h1>🤖 الوكيل التعليمي الذكي</h1>", unsafe_allow_html=True)
85
-
86
- # عرض الرسائل القديمة
87
  for m in st.session_state.messages:
88
  role_class = "user-text" if m["role"] == "user" else "bot-text"
89
  st.markdown(f'<div class="{role_class}">{m["content"]}</div>', unsafe_allow_html=True)
 
 
 
90
 
91
- # إدخال المستخدم
92
  u_input = st.chat_input("اسألني عن أي شيء...")
93
  query = u_input or st.session_state.get("q_auto")
94
 
 
10
  )
11
 
12
  # --- ضع مفتاح الـ Token الخاص بك هنا من Hugging Face ---
 
13
  HF_TOKEN = "YOUR_HUGGING_FACE_TOKEN_HERE"
14
  client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta", token=HF_TOKEN)
15
 
16
+ # 2. تنسيق الـ CSS المطور (كودك القديم بالكامل)
17
  st.markdown("""
18
  <style>
19
  @import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;700&display=swap');
20
+
21
  .stApp { background: linear-gradient(135deg, #010409 0%, #0d1117 100%) !important; }
22
  [data-testid="stSidebar"] { background-color: #0d1117 !important; }
23
+ [data-testid="stChatInput"] { background-color: #161b22 !important; border-radius: 15px; border: 1px solid #30363d !important; }
24
+ [data-testid="stChatInput"] textarea { color: #ffffff !important; background-color: #0d1117 !important; }
25
+ [data-testid="stChatInput"] textarea::placeholder { color: rgba(255, 255, 255, 0.8) !important; }
26
+
27
+ footer {display: none !important;}
28
+ header {visibility: hidden;}
29
+
30
+ .stMarkdown, p, li, label, .stSelectbox, .stRadio, div {
31
+ color: #FFFFFF !important; font-family: 'Cairo', sans-serif !important; font-size: 20px !important; direction: rtl; text-align: right;
32
+ }
33
+
34
+ .sidebar-title { color: #FFD700 !important; text-align: center; font-size: 24px !important; font-weight: bold; text-shadow: 0 0 10px rgba(255, 215, 0, 0.6); }
35
+ h1 { color: #00f2ff !important; text-shadow: 0 0 15px #00f2ff; text-align: center; border-bottom: 2px solid #1f6feb; padding-bottom: 10px; font-family: 'Cairo', sans-serif !important; }
36
+
37
  .bot-text { background-color: #0d1a26; border-right: 5px solid #00f2ff; padding: 20px; border-radius: 15px; margin-bottom: 15px; }
38
  .user-text { background-color: #161b22; border-left: 5px solid #58a6ff; padding: 15px; border-radius: 12px; margin-bottom: 15px; }
39
+
40
+ .stButton>button { width: 100%; border-radius: 12px; background: linear-gradient(45deg, #1f6feb, #388bfd); color: white; font-weight: bold; }
41
+ .game-link { display: block; width: 100%; text-align: center; background: linear-gradient(45deg, #238636, #2ea043); color: white !important; padding: 15px; border-radius: 12px; text-decoration: none; font-weight: bold; margin-top: 20px; }
42
  .step-box { border: 2px solid #00f2ff; padding: 15px; border-radius: 12px; margin: 10px 0; background: rgba(0, 242, 255, 0.1); color: #00f2ff; font-weight: bold; }
43
  </style>
44
  """, unsafe_allow_html=True)
45
 
46
+ # 3. قاعدة المعرفة (المعلومات التي وضعتِها يدوياً)
47
  kb = {
48
+ "تعريف": "البيانات الضخمة (Big Data) هي 'منجم الذهب' الرقمي؛ هي كميات هائلة من المعلومات اللي بتنتج كل ثانية من موبايلاتنا وحياتنا! 🔍✨",
49
+ "الفرق": "🌟 **مقارنة الأبطال:**\n1. **البيانات العادية:** زي جدول درجاتك، حجمها صغير.\n2. **البيانات الضخمة:** حجمها بالـ 'زيتابايت' ومحتاجة سيرفرات قوية جداً. 🔥",
50
+ "ذكاء": "🤝 **الثنائي الخارق:**\nالبيانات الضخمة هي 'البنزين' والذكاء الاصطناعي هو 'المحرك'. كل ما تدي بيانات أكتر، الـ AI بيبقى أذكى! 🧠🤖",
51
+ "تغيير": "🚀 **كيف غيرت عالمنا؟**\n- **الطب الذكي:** تشخيص الأم��اض.\n- **أمانك المالي:** حماية كروت البنك.\n- **مدن المستقبل:** تقليل الزحمة. 🚦",
52
+ "خصائص": "✨ **الخصائص الـ 5 (5Vs):**\n1. Volume (الحجم) | 2. Velocity (السرعة) | 3. Variety (التنوع) | 4. Veracity (الموثوقية) | 5. Value (القيمة) 💎",
53
+ "ترتيب": """⛓️ **مخطط رحلة البيانات الضخمة:**
54
+ <div class="step-box">1️⃣ التجميع (Collection) 📥</div>
55
+ <div class="step-box">2️⃣ التخزين (Storage) 🗄️</div>
56
+ <div class="step-box">3️⃣ المعالجة (Processing) 🧼</div>
57
+ <div class="step-box">4️⃣ التحليل (Analysis) 🧠</div>
58
+ <div class="step-box">5️⃣ اتخاذ القرار (Action) ✅</div>"""
59
  }
60
 
61
  # 4. إدارة حالة الجلسة
62
  if "messages" not in st.session_state:
63
+ st.session_state.messages = [{"role": "assistant", "content": "مرحباً بك يا بطل! أنا وكيلك الذكي، اسألني عن أي شيء في عالم البيانات الضخمة. 🤖✨"}]
64
  if "view" not in st.session_state:
65
  st.session_state.view = "chat"
66
 
67
+ # 5. الواجهة الجانبية (كل أزرارك القديمة)
68
  with st.sidebar:
69
+ st.markdown('<p class="sidebar-title">📍 المحطات التعليمية</p>', unsafe_allow_html=True)
70
+ if st.button("🔍 1. البيانات الضخمه"): st.session_state.q_auto = "تعريف"
71
+ if st.button("⚖️ 2. العادية vs الضخمة"): st.session_state.q_auto = "الفرق"
72
+ if st.button("⚡ 3. الخصائص الـ 5"): st.session_state.q_auto = "خصائص"
73
+ if st.button("🤖 4. الذكاء الاصطناعي"): st.session_state.q_auto = "ذكاء"
74
+ if st.button("🚀 5. فائدتها"): st.session_state.q_auto = "تغيير"
75
+ if st.button("📈 6. رسم بياني"): st.session_state.q_auto = "رسم"
76
+ st.markdown("---")
77
+ if st.button("⛓️ رحلة البيانات"): st.session_state.q_auto = "ترتيب"
78
  if st.button("📝 اختبار الأبطال"): st.session_state.view = "quiz"; st.rerun()
79
+ if st.button("🔄 محادثة جديدة"): st.session_state.messages = [{"role": "assistant", "content": "أهلاً بك مجدداً! كيف يمكنني مساعدتك؟"}]; st.session_state.view = "chat"; st.rerun()
80
+ st.markdown('<a href="https://view.genially.com/..." target="_blank" class="game-link">🎮 العودة للمغامرة</a>', unsafe_allow_html=True)
81
 
82
+ # 6. منطق الرد الذكي (Hybrid AI)
83
  def get_ai_response(prompt):
84
+ q_low = prompt.lower()
85
+ # أولاً: جرب الرد من قاعدة بياناتك (أسرع وأدق لمشروعك)
86
  for key in kb:
87
+ if key in q_low: return kb[key]
88
+ if "رسم" in q_low: return "إليك الرسم البياني لتطور حجم البيانات عالمياً: 📊📈"
89
+
90
+ # ثانياً: إذا لم يجد الإجابة، يسأل Hugging Face (بديل شات جي بي تي)
91
  try:
92
+ formatted_prompt = f"<|system|>\nأنت مساعد تعليمي ذكي للأطفال اسمه Big Data Hero. تشرح بأسلوب بسيط ومشجع بالعربي وتستخدم الرموز التعبيرية.</s>\n<|user|>\n{prompt}</s>\n<|assistant|>\n"
93
  response = ""
94
  for message in client.chat_completion(
95
  messages=[{"role": "user", "content": formatted_prompt}],
96
+ max_tokens=250,
97
  stream=True
98
  ):
99
  response += message.choices[0].delta.content or ""
100
  return response
101
  except:
102
+ return "سؤال ذكي جداً! البيانات الضخمة مذهلة. هل تريد استكشاف المحطات التعليمية؟ 🤖"
103
 
104
+ # العرض الرئيسي
105
  if st.session_state.view == "quiz":
106
  st.markdown("<h1>📝 اختبار التحدي</h1>", unsafe_allow_html=True)
107
+ q1 = st.radio("1️⃣ أي خاصية تعبر عن 'تنوع' البيانات (فيديو، نصوص، صوت)؟", ["Volume", "Variety", "Velocity"])
108
+ q2 = st.radio("2️⃣ من هو 'الوقود' الذي يغذي محرك الذكاء الاصطناعي؟", ["البيانات الضخمة", "الكهرباء فقط"])
109
+ if st.button("إرسال الإجابات"):
110
+ if q1 == "Variety" and q2 == "البيانات الضخمة":
111
+ st.balloons(); st.success("عبقري! إجاباتك كاملة وصحيحة 🎯")
112
+ else: st.error("راجع المحطات التعليمية وجرب مرة أخرى!")
113
  if st.button("العودة للدردشة"): st.session_state.view = "chat"; st.rerun()
114
  else:
115
  st.markdown("<h1>🤖 الوكيل التعليمي الذكي</h1>", unsafe_allow_html=True)
 
 
116
  for m in st.session_state.messages:
117
  role_class = "user-text" if m["role"] == "user" else "bot-text"
118
  st.markdown(f'<div class="{role_class}">{m["content"]}</div>', unsafe_allow_html=True)
119
+ if "📊" in m["content"]:
120
+ chart_data = pd.DataFrame({'السنة': [2010, 2015, 2020, 2025], 'حجم البيانات (زيتابايت)': [2, 12, 64, 175]})
121
+ st.area_chart(chart_data.set_index('السنة'))
122
 
 
123
  u_input = st.chat_input("اسألني عن أي شيء...")
124
  query = u_input or st.session_state.get("q_auto")
125