Spaces:
Runtime error

D12004 commited on
Commit
b494bc1
·
verified ·
1 Parent(s): 8e0cf0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -83
app.py CHANGED
@@ -2,110 +2,82 @@ import streamlit as st
2
  import pandas as pd
3
  from huggingface_hub import InferenceClient
4
 
5
- # 1. إعدادات الصفحة الأساسية
6
- st.set_page_config(
7
- page_title="Big Data Hero 🤖",
8
- layout="wide",
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');
22
-
23
  .stApp { background: linear-gradient(135deg, #010409 0%, #0d1117 100%) !important; }
24
  [data-testid="stSidebar"] { background-color: #0d1117 !important; }
25
- [data-testid="stChatInput"] { background-color: #161b22 !important; border-radius: 15px; border: 1px solid #30363d !important; }
26
- [data-testid="stChatInput"] textarea { color: #ffffff !important; background-color: #0d1117 !important; }
27
- [data-testid="stChatInput"] textarea::placeholder { color: rgba(255, 255, 255, 0.8) !important; }
28
-
29
- footer {display: none !important;}
30
- header {visibility: hidden;}
31
-
32
- .stMarkdown, p, li, label, .stSelectbox, .stRadio, div {
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")
104
-
105
- if query:
106
- st.session_state.messages.append({"role": "user", "content": query})
107
- with st.spinner("جاري التفكير..."):
108
- ans = get_ai_response(query)
109
- st.session_state.messages.append({"role": "assistant", "content": ans})
110
- if "q_auto" in st.session_state: del st.session_state["q_auto"]
111
- st.rerun()
 
2
  import pandas as pd
3
  from huggingface_hub import InferenceClient
4
 
5
+ # 1. إعدادات الصفحة
6
+ st.set_page_config(page_title="Big Data Hero 🤖", layout="wide", initial_sidebar_state="collapsed")
 
 
 
 
7
 
8
+ # --- ⚠️ الجزء الأهم: التوكن ⚠️ ---
9
+ # تأكدي أنكِ وضعتِ التوكن الخاص بكِ هنا بين علامات التنصيص
10
+ HF_TOKEN = "hf_XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
11
 
12
+ # التأكد من عمل التوكن
13
+ try:
14
+ client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta", token=HF_TOKEN)
15
+ except:
16
+ st.error("خطأ في الاتصال بالتوكن، تأكدي من الرمز!")
17
 
18
+ # 2. التنسيق (CSS)
19
  st.markdown("""
20
  <style>
21
  @import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;700&display=swap');
 
22
  .stApp { background: linear-gradient(135deg, #010409 0%, #0d1117 100%) !important; }
23
  [data-testid="stSidebar"] { background-color: #0d1117 !important; }
24
+ .stMarkdown, p, li, label, div { color: #FFFFFF !important; font-family: 'Cairo', sans-serif !important; font-size: 20px !important; direction: rtl; text-align: right; }
25
+ h1 { color: #00f2ff !important; text-shadow: 0 0 15px #00f2ff; text-align: center; font-family: 'Cairo', sans-serif !important; }
 
 
 
 
 
 
 
 
 
 
 
 
26
  .bot-text { background-color: #0d1a26; border-right: 5px solid #00f2ff; padding: 20px; border-radius: 15px; margin-bottom: 15px; }
27
  .user-text { background-color: #161b22; border-left: 5px solid #58a6ff; padding: 15px; border-radius: 12px; margin-bottom: 15px; }
28
+ .stChatInput { bottom: 20px !important; }
 
 
29
  </style>
30
  """, unsafe_allow_html=True)
31
 
32
+ # 3. قاعدة المعرفة اليدوية
33
  kb = {
34
+ "تعريف": "البيانات الضخمة هي منجم الذهب الرقمي! 🔍✨",
35
+ "خصائص": "الخصائص هي: Volume, Velocity, Variety, Veracity, Value 💎",
36
+ "ترتيب": "رحلة البيانات: تجميع، تخزين، معالجة، تحليل، قرار ✅"
 
 
 
37
  }
38
 
39
+ # 4. حالة الجلسة
40
+ if "messages" not in st.session_state:
41
+ st.session_state.messages = [{"role": "assistant", "content": "أهلاً بك يا بطل! أنا وكيلك الذكي، اسألني عن أي شيء. 🤖✨"}]
 
 
42
 
43
+ # 5. منطق الرد الذكي (ChatGPT بديل)
 
 
 
 
 
 
 
 
 
 
 
44
  def get_ai_response(prompt):
45
  q_low = prompt.lower()
46
+ # التحقق من قاعدة المعرفة أولاً
47
  for key in kb:
48
  if key in q_low: return kb[key]
49
 
50
+ # إذا لم يجد، يستخدم الذكاء الاصطناعي
51
  try:
52
+ # صياغة الطلب للذكاء الاصطناعي ليتقمص الشخصية
53
+ formatted_prompt = f"System: أنت مساعد تعليمي ذكي للأطفال تشرح بأسلوب بسيط وممتع بالعربي. User: {prompt}"
54
+
55
+ response = client.text_generation(
56
+ formatted_prompt,
57
+ max_new_tokens=150,
58
+ temperature=0.7
59
+ )
60
  return response
61
+ except Exception as e:
62
+ # هذا الرد الذي ظهر لكِ في الصورة، مما يعني وجود مشكلة في الاتصال (التوكن)
63
+ return f"عذراً يا بطل، حدث خطأ فني بسيط! تأكد من اتصال التوكن. 🤖"
64
+
65
+ # 6. العرض
66
+ st.sidebar.title("📍 المحطات")
67
+ if st.sidebar.button("🔄 محادثة جديدة"):
68
+ st.session_state.messages = [{"role": "assistant", "content": "أهلاً بك مجدداً!"}]
69
+ st.rerun()
70
+
71
+ st.markdown("<h1>🤖 الوكيل التعليمي الذكي</h1>", unsafe_allow_html=True)
72
+
73
+ for m in st.session_state.messages:
74
+ role_class = "user-text" if m["role"] == "user" else "bot-text"
75
+ st.markdown(f'<div class="{role_class}">{m["content"]}</div>', unsafe_allow_html=True)
76
+
77
+ query = st.chat_input("اسألني عن أي شيء...")
78
+ if query:
79
+ st.session_state.messages.append({"role": "user", "content": query})
80
+ with st.spinner("جاري التفكير..."):
81
+ ans = get_ai_response(query)
82
+ st.session_state.messages.append({"role": "assistant", "content": ans})
83
+ st.rerun()