Spaces:
Runtime error
Runtime error
| # learner.py - الإصدار المحدث | |
| import requests | |
| from bs4 import BeautifulSoup | |
| import json | |
| import os | |
| from difflib import get_close_matches | |
| from analyzer import fix_url, detect_media_type | |
| from memory import load_memory, save_memory, load_global_memory, save_global_memory | |
| LEARNING_FILE = "learned_links.json" | |
| SEARCH_QUERY = "websites to learn something new every day" | |
| SEARCH_ENGINE_URL = f"https://html.duckduckgo.com/html/?q={SEARCH_QUERY}" | |
| # الردود التلقائية الذكية - محدثة | |
| def auto_answer(message): | |
| message = message.strip().lower() | |
| # استعلامات البدء | |
| if message.startswith(("هل ابدأ", "ابدأ", "هل نبدأ", "ابدا", "نبدأ")): | |
| return "نعم ابدأ" | |
| # نعم/لا | |
| elif message in ["نعم", "لا", "نعم أو لا", "نعم او لا", "yes", "no"]: | |
| return "نعم" | |
| # خيارات (أو) | |
| elif "أو" in message or "او" in message or "or" in message: | |
| parts = message.split() | |
| for i, part in enumerate(parts): | |
| if part in ["أو", "او", "or"] and i > 0: | |
| return parts[i-1] # إرجاع الكلمة قبل "أو" | |
| return parts[0] if parts else "نعم" | |
| # استفسارات الرغبة | |
| elif message.startswith(("هل تريدني", "هل تود", "هل تبي", "اذا تبي", "هل ترغب")): | |
| return "نعم" | |
| # استفسارات الحاجة | |
| elif message.startswith("هل تحتاج"): | |
| return "نعم اكمل مع تفعيل الاجابات التلقائية" | |
| # استفسارات عامة | |
| elif message.startswith("ما هي") or message.startswith("ما هو"): | |
| return "ليس الآن، ركز على التعلم" | |
| # طلب التفاصيل | |
| elif "تفصيل" in message or "تفاصيل" in message or "detail" in message: | |
| return "ليس الآن، يمكنني البحث لاحقاً" | |
| # طلب المعلومات | |
| elif message.startswith("قول لي") or message.startswith("اخبرني") or message.startswith("tell me"): | |
| return "موافق، سأبحث عن المعلومة" | |
| # الجاهزية | |
| elif "جاهز" in message or "مستعد" in message or "ready" in message: | |
| return "ابدأ التعلم الآن" | |
| # استفسارات الرغبة المباشرة | |
| elif message.startswith("هل تريد") or message.startswith("do you want"): | |
| return "نعم" | |
| # متابعة العمل | |
| elif "هل تحتاج شيء آخر" in message or "هل تحتاج لشيء اخر" in message or "need anything else" in message: | |
| return "نعم اكمل مع تفعيل الاجابات التلقائية" | |
| # إضافة ردود جديدة | |
| elif any(word in message for word in ["شكرا", "thanks", "thank you", "متشكر"]): | |
| return "العفو، هل تريد المزيد من المساعدة؟" | |
| elif any(word in message for word in ["مرحبا", "hello", "hi", "السلام"]): | |
| return "مرحباً! كيف يمكنني مساعدتك في التعلم اليوم؟" | |
| elif any(word in message for word in ["وداعا", "bye", "مع السلامة", "غادر"]): | |
| return "إلى اللقاء! لا تنسى التعلم يومياً" | |
| return None | |
| # المجيب الذكي - محدث | |
| def generate_reply(message, username="مجهول"): | |
| global_memory = load_global_memory() | |
| # التحقق من الرد التلقائي أولاً | |
| auto = auto_answer(message) | |
| if auto: | |
| return auto | |
| # البحث في الذاكرة | |
| if message in global_memory: | |
| return global_memory[message] | |
| # البحث عن أقرب تطابق | |
| matches = get_close_matches(message, global_memory.keys(), n=1, cutoff=0.6) | |
| if matches: | |
| return global_memory[matches[0]] | |
| # معالجة الروابط | |
| if message.startswith(("http://", "https://", "www.")): | |
| # تصحيح الرابط إذا لزم الأمر | |
| corrected_url = fix_url(message) | |
| media_type = detect_media_type(corrected_url) | |
| if media_type == 'image': | |
| reply = f'<img src="{corrected_url}" alt="صورة" width="300" style="max-width:100%;">' | |
| elif media_type == 'video': | |
| reply = f'<video controls width="300" style="max-width:100%;"><source src="{corrected_url}"></video>' | |
| elif media_type == 'audio': | |
| reply = f'<audio controls><source src="{corrected_url}"></audio>' | |
| else: | |
| reply = f'<a href="{corrected_url}" target="_blank" style="color: blue; text-decoration: underline;">رابط خارجي - اضغط هنا</a>' | |
| # إضافة وصف للرابط | |
| reply += f"\n\nتم اكتشاف: {media_type}" | |
| else: | |
| # رد ذكي للمحتوى النصي | |
| if len(message) > 50: | |
| reply = f"رسالتك تحتوي على {len(message)} حرفاً. هل تريدني أن أبحث عن معلومات حول هذا الموضوع؟" | |
| else: | |
| # عكس النص كرد افتراضي مع تحسين | |
| reversed_text = message[::-1] | |
| reply = f"🤖 رد ذكي: {reversed_text}\n\nهل تريد شرحاً أكثر؟" | |
| # معالجة الروابط داخل النص | |
| if '//' in message and not message.startswith(('http://', 'https://')): | |
| words = message.split() | |
| corrected_words = [] | |
| for word in words: | |
| if '//' in word: | |
| corrected_word = fix_url(word) | |
| corrected_words.append(corrected_word) | |
| else: | |
| corrected_words.append(word) | |
| if corrected_words != words: | |
| reply += "\n\n🔗 الروابط المصححة: " + " ".join(corrected_words) | |
| # حفظ في الذاكرة إذا كان جديداً | |
| if message not in global_memory: | |
| global_memory[message] = reply | |
| save_global_memory(global_memory) | |
| return reply | |
| # تعليم تلقائي - محدث | |
| def fetch_learning_links(): | |
| headers = { | |
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" | |
| } | |
| try: | |
| print("🔍 جاري البحث عن مصادر تعليمية...") | |
| response = requests.get(SEARCH_ENGINE_URL, headers=headers, timeout=15) | |
| response.raise_for_status() | |
| soup = BeautifulSoup(response.text, "html.parser") | |
| print("✅ تم تحليل نتائج البحث بنجاح") | |
| except requests.exceptions.RequestException as e: | |
| print(f"❌ فشل في الاتصال بمصدر التعلم: {e}") | |
| return [] | |
| except Exception as e: | |
| print(f"❌ خطأ غير متوقع: {e}") | |
| return [] | |
| links = [] | |
| # البحث في نتائج البحث | |
| for a in soup.find_all("a", href=True, class_=True): | |
| href = a.get('href', '') | |
| if any(domain in href for domain in ['wikipedia', 'edx', 'coursera', 'khanacademy', 'ted.com', 'youtube.com/education']): | |
| clean_link = fix_url(href) | |
| if clean_link not in links: | |
| links.append(clean_link) | |
| print(f"📚 وجدت مصدر تعليمي: {clean_link}") | |
| # إذا لم نجد روابط تعليمية محددة، نأخذ أول 10 روابط | |
| if not links: | |
| for a in soup.find_all("a", href=True): | |
| href = a['href'] | |
| if href.startswith(('http://', 'https://', '//')): | |
| clean_link = fix_url(href) | |
| if clean_link not in links: | |
| links.append(clean_link) | |
| if len(links) >= 10: | |
| break | |
| return links[:10] | |
| def save_learned_links(links): | |
| try: | |
| data = { | |
| "timestamp": json.dumps(str(os.path.getctime(LEARNING_FILE)) if os.path.exists(LEARNING_FILE) else "new"), | |
| "links": links, | |
| "count": len(links) | |
| } | |
| with open(LEARNING_FILE, "w", encoding="utf-8") as f: | |
| json.dump(data, f, indent=2, ensure_ascii=False) | |
| print(f"💾 تم حفظ {len(links)} رابط في {LEARNING_FILE}") | |
| except Exception as e: | |
| print(f"❌ خطأ في حفظ الروابط: {e}") | |
| def load_learned_links(): | |
| try: | |
| if os.path.exists(LEARNING_FILE): | |
| with open(LEARNING_FILE, "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| return data.get("links", []) | |
| return [] | |
| except Exception as e: | |
| print(f"❌ خطأ في تحميل الروابط: {e}") | |
| return [] | |
| def auto_learn(): | |
| try: | |
| print("🚀 بدء عملية التعلم التلقائي...") | |
| # جلب الروابط الجديدة | |
| new_links = fetch_learning_links() | |
| if not new_links: | |
| print("⚠️ لم يتم العثور على روابط جديدة، استخدام الروابط المخزنة") | |
| new_links = load_learned_links() | |
| # حفظ الروابط | |
| save_learned_links(new_links) | |
| # تحديث الذاكرة العالمية | |
| memory = load_global_memory() | |
| links_added = 0 | |
| for link in new_links: | |
| if link not in memory: | |
| memory[link] = f"🎓 مصدر تعليمي: {link}" | |
| links_added += 1 | |
| save_global_memory(memory) | |
| print(f"✅ تم التعلّم التلقائي: {links_added} رابط جديد، {len(memory)} عنصر في الذاكرة") | |
| return new_links | |
| except Exception as e: | |
| print(f"❌ نورا: حدث خطأ أثناء التعلّم: {str(e)}") | |
| return [] | |
| # دالة مساعدة جديدة للبحث في الروابط المتعلمة | |
| def search_learned_links(keyword): | |
| """بحث في الروابط المتعلمة بناءً على كلمة مفتاحية""" | |
| links = load_learned_links() | |
| keyword = keyword.lower() | |
| matching_links = [] | |
| for link in links: | |
| if keyword in link.lower(): | |
| matching_links.append(link) | |
| return matching_links | |
| # دالة لاستعراض الروابط المتعلمة | |
| def list_learned_links(limit=5): | |
| """استعراض آخر الروابط المتعلمة""" | |
| links = load_learned_links() | |
| return links[:limit] | |
| if __name__ == "__main__": | |
| # اختبار الوحدة | |
| print("🧪 اختبار وحدة learner.py") | |
| # اختبار الردود التلقائية | |
| test_messages = ["هل ابدأ التعلم؟", "شكراً لك", "مرحبا", "ما هو أفضل موقع تعلم؟"] | |
| for msg in test_messages: | |
| response = generate_reply(msg) | |
| print(f"📨 '{msg}' → '{response}'") | |
| # اختبار التعلم التلقائي (اختياري) | |
| if input("🔍 هل تريد اختبار التعلم التلقائي؟ (نعم/لا): ").lower() in ["نعم", "yes", "y"]: | |
| links = auto_learn() | |
| print(f"📚 تم تعلم {len(links)} رابط") |