Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
# ---
|
| 13 |
-
#
|
| 14 |
-
HF_TOKEN = "
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
# 2.
|
| 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 |
-
|
| 26 |
-
|
| 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 |
-
"تعريف": "البيانات الضخمة
|
| 50 |
-
"ا
|
| 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 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
return response
|
| 86 |
-
except:
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 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()
|
|
|
|
|
|
|
|
|