Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,11 +9,13 @@ st.set_page_config(
|
|
| 9 |
initial_sidebar_state="collapsed"
|
| 10 |
)
|
| 11 |
|
| 12 |
-
# --- ضع مفتاح الـ Token الخاص بك هنا م
|
| 13 |
-
|
|
|
|
|
|
|
| 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');
|
|
@@ -31,94 +33,71 @@ st.markdown("""
|
|
| 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;
|
| 35 |
-
h1 { color: #00f2ff !important; text-shadow: 0 0 15px #00f2ff; text-align: center; border-bottom: 2px solid #1f6feb; padding-bottom: 10px;
|
| 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. **البيانات العادية:** زي جدول درجاتك
|
| 50 |
-
"ذكاء": "🤝 **الثنائي الخارق:**
|
| 51 |
-
"
|
| 52 |
-
"خصائص": "✨ **الخصائص الـ 5 (5Vs):**\n1. Volume (الحجم) | 2. Velocity (السرعة) | 3. Variety (التنوع) | 4. Veracity (الموثوقية) | 5. Value (القيمة) 💎",
|
| 53 |
"ترتيب": """⛓️ **مخطط رحلة البيانات الضخمة:**
|
| 54 |
-
<div class="step-box">1️⃣ التجميع
|
| 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("🔄 محادثة جديدة"):
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
-
# 6.
|
| 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. ت
|
| 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("
|
| 108 |
-
q2 = st.radio("2️⃣ من هو 'الوقود' الذي يغذي محرك الذكاء الاصطناعي؟", ["البيانات الضخمة", "الكهرباء فقط"])
|
| 109 |
if st.button("إرسال الإجابات"):
|
| 110 |
-
if q1 == "Variety"
|
| 111 |
-
|
| 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")
|
|
|
|
| 9 |
initial_sidebar_state="collapsed"
|
| 10 |
)
|
| 11 |
|
| 12 |
+
# --- ضع مفتاح الـ Token الخاص بك هنا مباشرة ليعمل الشات ---
|
| 13 |
+
# انسخي التوكن من حسابك في Hugging Face وضعيها بين القوسين
|
| 14 |
+
HF_TOKEN = "ضع_التوكن_الخاص_بك_هنا_hf_..."
|
| 15 |
+
|
| 16 |
client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta", token=HF_TOKEN)
|
| 17 |
|
| 18 |
+
# 2. تنسيق الـ CSS (كل تنسيقاتك القديمة والألوان)
|
| 19 |
st.markdown("""
|
| 20 |
<style>
|
| 21 |
@import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;700&display=swap');
|
|
|
|
| 33 |
color: #FFFFFF !important; font-family: 'Cairo', sans-serif !important; font-size: 20px !important; direction: rtl; text-align: right;
|
| 34 |
}
|
| 35 |
|
| 36 |
+
.sidebar-title { color: #FFD700 !important; text-align: center; font-size: 24px !important; font-weight: bold; }
|
| 37 |
+
h1 { color: #00f2ff !important; text-shadow: 0 0 15px #00f2ff; text-align: center; border-bottom: 2px solid #1f6feb; padding-bottom: 10px; }
|
| 38 |
|
| 39 |
.bot-text { background-color: #0d1a26; border-right: 5px solid #00f2ff; padding: 20px; border-radius: 15px; margin-bottom: 15px; }
|
| 40 |
.user-text { background-color: #161b22; border-left: 5px solid #58a6ff; padding: 15px; border-radius: 12px; margin-bottom: 15px; }
|
| 41 |
|
| 42 |
.stButton>button { width: 100%; border-radius: 12px; background: linear-gradient(45deg, #1f6feb, #388bfd); color: white; font-weight: bold; }
|
|
|
|
| 43 |
.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; }
|
| 44 |
</style>
|
| 45 |
""", unsafe_allow_html=True)
|
| 46 |
|
| 47 |
+
# 3. قاعدة المعرفة (المعلومات اليدوية)
|
| 48 |
kb = {
|
| 49 |
+
"تعريف": "البيانات الضخمة (Big Data) هي 'منجم الذهب' الرقمي؛ هي كميات هائلة من المعلومات! 🔍✨",
|
| 50 |
+
"الفرق": "🌟 **مقارنة الأبطال:**\n1. **البيانات العادية:** زي جدول درجاتك.\n2. **البيانات الضخمة:** حجمها بالـ 'زيتابايت'. 🔥",
|
| 51 |
+
"ذكاء": "🤝 **الثنائي الخارق:** البيانات الضخمة هي 'البنزين' والذكاء الاصطناعي هو 'المحرك'. 🧠🤖",
|
| 52 |
+
"خصائص": "✨ **الخصائص الـ 5 (5Vs):** Volume, Velocity, Variety, Veracity, Value 💎",
|
|
|
|
| 53 |
"ترتيب": """⛓️ **مخطط رحلة البيانات الضخمة:**
|
| 54 |
+
<div class="step-box">1️⃣ التجميع | 2️⃣ التخزين | 3️⃣ المعالجة | 4️⃣ التحليل | 5️⃣ القرار ✅</div>"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
}
|
| 56 |
|
| 57 |
# 4. إدارة حالة الجلسة
|
| 58 |
if "messages" not in st.session_state:
|
| 59 |
+
st.session_state.messages = [{"role": "assistant", "content": "مرحباً بك يا بطل! أنا وكيلك الذكي، اسألني عن أي شيء. 🤖✨"}]
|
| 60 |
if "view" not in st.session_state:
|
| 61 |
st.session_state.view = "chat"
|
| 62 |
|
| 63 |
+
# 5. القائمة الجانبية
|
| 64 |
with st.sidebar:
|
| 65 |
st.markdown('<p class="sidebar-title">📍 المحطات التعليمية</p>', unsafe_allow_html=True)
|
| 66 |
if st.button("🔍 1. البيانات الضخمه"): st.session_state.q_auto = "تعريف"
|
|
|
|
| 67 |
if st.button("⚡ 3. الخصائص الـ 5"): st.session_state.q_auto = "خصائص"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
if st.button("⛓️ رحلة البيانات"): st.session_state.q_auto = "ترتيب"
|
| 69 |
if st.button("📝 اختبار الأبطال"): st.session_state.view = "quiz"; st.rerun()
|
| 70 |
+
if st.button("🔄 محادثة جديدة"):
|
| 71 |
+
st.session_state.messages = [{"role": "assistant", "content": "أهلاً بك مجدداً!"}]
|
| 72 |
+
st.session_state.view = "chat"; st.rerun()
|
| 73 |
|
| 74 |
+
# 6. دالة الرد الذكي (تتصل بـ Hugging Face)
|
| 75 |
def get_ai_response(prompt):
|
| 76 |
q_low = prompt.lower()
|
|
|
|
| 77 |
for key in kb:
|
| 78 |
if key in q_low: return kb[key]
|
|
|
|
| 79 |
|
|
|
|
| 80 |
try:
|
| 81 |
+
formatted_prompt = f"<|system|>\nأنت مساعد تعليمي ذكي للأطفال اسمه Big Data Hero. تجيب بالعربي ببساطة.</s>\n<|user|>\n{prompt}</s>\n<|assistant|>\n"
|
| 82 |
response = ""
|
| 83 |
+
for message in client.chat_completion(messages=[{"role": "user", "content": formatted_prompt}], max_tokens=200, stream=True):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
response += message.choices[0].delta.content or ""
|
| 85 |
return response
|
| 86 |
except:
|
| 87 |
+
return "سؤال رائع! هل تريد معرفة المزيد عن عالم البيانات؟ 🤖"
|
| 88 |
|
| 89 |
+
# --- العرض الرئيسي ---
|
| 90 |
if st.session_state.view == "quiz":
|
| 91 |
st.markdown("<h1>📝 اختبار التحدي</h1>", unsafe_allow_html=True)
|
| 92 |
+
q1 = st.radio("أي خاصية تعبر عن 'تنوع' البيانات؟", ["Volume", "Variety", "Velocity"])
|
|
|
|
| 93 |
if st.button("إرسال الإجابات"):
|
| 94 |
+
if q1 == "Variety": st.balloons(); st.success("ممتاز!")
|
| 95 |
+
if st.button("العودة"): st.session_state.view = "chat"; st.rerun()
|
|
|
|
|
|
|
| 96 |
else:
|
| 97 |
st.markdown("<h1>🤖 الوكيل التعليمي الذكي</h1>", unsafe_allow_html=True)
|
| 98 |
for m in st.session_state.messages:
|
| 99 |
role_class = "user-text" if m["role"] == "user" else "bot-text"
|
| 100 |
st.markdown(f'<div class="{role_class}">{m["content"]}</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
u_input = st.chat_input("اسألني عن أي شيء...")
|
| 103 |
query = u_input or st.session_state.get("q_auto")
|