Download app.py from So66/Te: direct link, hf CLI and curl.
- Browser
- Download file 7.14 kB
-
https://huggingface.co/spaces/So66/Te/resolve/main/app.py
- Command line
-
hf download hf://spaces/So66/Te/app.py
-
curl -L -o app.py https://huggingface.co/spaces/So66/Te/resolve/main/app.py
7.14 kB
| في هذا الكود لماذا يتم تكرار كتابة الرسائل القديمة في كل رد import streamlit as st | |
| import json | |
| import time | |
| from datetime import datetime | |
| # تحميل البيانات من ملف JSON | |
| with open("qa_data.json", "r", encoding="utf-8") as f: | |
| qa_data = json.load(f) | |
| # تعريف مخصص عن المساعد | |
| ASSISTANT_PROFILE = { | |
| "name": "CME", | |
| "description": """ | |
| أنا مساعد ذكي اسمي CME، صممت خصيصاً ل: | |
| - تقديم معلومات دقيقة من قاعدة البيانات | |
| - الإجابة على استفساراتك المحددة | |
| - مساعدتك في العثور على المعلومات | |
| جميع إجاباتي مبنية على معلومات موثوقة. | |
| """, | |
| "keywords": ["من انت", "ما هو اسمك", "تعريفك", "مهمتك", "وظيفتك", "ما الذي تفعله"] | |
| } | |
| # إعداد الصفحة | |
| st.set_page_config( | |
| page_title=ASSISTANT_PROFILE["name"], | |
| page_icon="🤖", | |
| layout="centered" | |
| ) | |
| # تنسيقات CSS المخصصة | |
| st.markdown(""" | |
| <style> | |
| .stApp { | |
| background-color: #000000; | |
| } | |
| h1, .stChatInput, .stTextInput>div>div>input { | |
| color: white !important; | |
| } | |
| .stMarkdown { | |
| color: white; | |
| } | |
| .assistant-bubble { | |
| border-radius: 10px; | |
| padding: 10px; | |
| margin: 10px 0; | |
| max-width: 100%; | |
| } | |
| .user-bubble { | |
| background-color: #2B2B2B; | |
| border-radius: 10px; | |
| padding: 10px; | |
| margin: 10px 0; | |
| max-width: 100%; | |
| } | |
| .typing-cursor { | |
| display: inline-block; | |
| width: 8px; | |
| height: 16px; | |
| background: white; | |
| animation: blink 1s infinite; | |
| } | |
| @keyframes blink { | |
| 0%, 100% { opacity: 1; } | |
| 50% { opacity: 0; } | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # تهيئة حالة المحادثة | |
| if "chat_history" not in st.session_state: | |
| st.session_state.chat_history = [ | |
| {"role": "system", "content": f"أنت مساعد اسمه {ASSISTANT_PROFILE['name']} يعتمد على قاعدة بيانات محددة."} | |
| ] | |
| if "displayed_messages" not in st.session_state: | |
| st.session_state.displayed_messages = set() | |
| # دالة للكتابة التدريجية | |
| def typewriter_effect(text, speed=0.03, message_id=None): | |
| container = st.empty() | |
| current_text = "" | |
| for char in str(text): | |
| current_text += char | |
| container.markdown( | |
| f''' | |
| <div class="assistant-bubble" style="float: right; clear: both; text-align: right;"> | |
| {current_text}<span class="typing-cursor"></span> | |
| </div> | |
| ''', | |
| unsafe_allow_html=True | |
| ) | |
| time.sleep(speed) | |
| # حفظ الرسالة النهائية | |
| final_markup = f''' | |
| <div class="assistant-bubble" style="float: right; clear: both; text-align: right;"> | |
| {text} | |
| </div> | |
| ''' | |
| container.markdown(final_markup, unsafe_allow_html=True) | |
| # تسجيل أن الرسالة قد تم عرضها | |
| if message_id: | |
| st.session_state.displayed_messages.add(message_id) | |
| return container | |
| # دالة لعرض الرسائل | |
| def display_message(role, content, message_id=None): | |
| if not message_id: | |
| message_id = f"{role}_{hash(content)}" | |
| if role == 'user': | |
| st.markdown( | |
| f'<div class="user-bubble" style="float: right; clear: both; text-align: right;">{content}</div>', | |
| unsafe_allow_html=True | |
| ) | |
| st.session_state.displayed_messages.add(message_id) | |
| else: | |
| if message_id not in st.session_state.displayed_messages: | |
| typewriter_effect(content, message_id=message_id) | |
| else: | |
| st.markdown( | |
| f'<div class="assistant-bubble" style="float: right; clear: both; text-align: right;">{content}</div>', | |
| unsafe_allow_html=True | |
| ) | |
| # دالة الكشف عن العربية بالأحرف | |
| def is_arabic(text, threshold=0.4): | |
| arabic_chars = set('ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىي ') | |
| text_chars = set(text) | |
| if not text_chars: | |
| return False | |
| arabic_ratio = len(arabic_chars.intersection(text_chars)) / len(text_chars) | |
| return arabic_ratio > threshold | |
| # دالة للكشف عن الأسئلة الشخصية | |
| def is_asking_about_me(text): | |
| text = text.lower() | |
| return any(keyword.lower() in text for keyword in ASSISTANT_PROFILE["keywords"]) | |
| # دالة البحث في قاعدة البيانات | |
| def answer_question(user_question): | |
| # بحث مطابقة كاملة | |
| for item in qa_data: | |
| if item["question"].strip().lower() == user_question.strip().lower(): | |
| return item["answer"] | |
| # بحث جزئي | |
| user_words = user_question.lower().split() | |
| best_match = None | |
| highest_score = 0 | |
| for item in qa_data: | |
| question_words = item["question"].lower().split() | |
| matches = sum(1 for word in user_words if word in question_words) | |
| score = matches / len(question_words) | |
| if score > highest_score: | |
| highest_score = score | |
| best_match = item | |
| if best_match and highest_score > 0.5: | |
| return best_match["answer"] | |
| if is_asking_about_me(user_question): | |
| return f"مرحباً! {ASSISTANT_PROFILE['description']}\n\nتاريخ اليوم: {datetime.now().strftime('%Y-%m-%d')}" | |
| return "عذراً، لم أتمكن من العثور على إجابة لسؤالك في قاعدة البيانات." | |
| # واجهة المستخدم | |
| st.title(f"{ASSISTANT_PROFILE['name']} مساعدك الذكي") | |
| # عرض سجل المحادثة | |
| for message in st.session_state.chat_history: | |
| message_id = f"{message['role']}_{hash(message['content'])}" | |
| display_message(message["role"], message["content"], message_id) | |
| # استقبال المدخلات | |
| user_prompt = st.chat_input("اكتب سؤالك هنا...") | |
| if user_prompt: | |
| # التحقق من اللغة العربية | |
| if not is_arabic(user_prompt): | |
| error_msg = "⚠️ يرجى استخدام اللغة العربية فقط" | |
| display_message('assistant', error_msg, "error_msg") | |
| st.session_state.chat_history.append({"role": "assistant", "content": error_msg}) | |
| else: | |
| # عرض رسالة المستخدم | |
| display_message('user', user_prompt, f"user_{len(st.session_state.chat_history)}") | |
| st.session_state.chat_history.append({"role": "user", "content": user_prompt}) | |
| # البحث عن الإجابة | |
| with st.spinner("🔍 يبحث في قاعدة البيانات..."): | |
| answer = answer_question(user_prompt) | |
| display_message('assistant', answer, f"assistant_{len(st.session_state.chat_history)}") | |
| st.session_state.chat_history.append({"role": "assistant", "content": answer}) |