import flet as ft import speech_recognition as sr import threading # قاعدة بيانات مؤقتة لحفظ الأوامر المتكررة class CommandMemory: def __init__(self): self.learned_commands = {} def learn(self, command): self.learned_commands[command] = self.learned_commands.get(command, 0) + 1 print(f"[تعلم] '{command}' - عدد مرات الاستخدام: {self.learned_commands[command]}") def count(self, command): return self.learned_commands.get(command, 0) # الذكاء الأساسي للتعامل مع الأوامر class NoraBrain: def __init__(self, memory: CommandMemory): self.memory = memory def reply(self, command): command = command.strip().lower() if "فتح" in command and "الكاميرا" in command: return "جارٍ فتح الكاميرا الآن." elif "تشغيل" in command and "الموسيقى" in command: return "تشغيل الموسيقى بأعلى جودة." elif "اتصال" in command: return "من الشخص الذي ترغب في الاتصال به؟" elif self.memory.count(command) > 0: return f"لقد استخدمت هذا الأمر {self.memory.count(command)} مرّات." else: return "عذرًا، لم أفهم الأمر. هل يمكنك المحاولة بصيغة أخرى؟" # مسؤول عن التعامل مع الصوت وتشغيل الردود class VoiceController: def __init__(self, brain: NoraBrain, page: ft.Page): self.recognizer = sr.Recognizer() self.microphone = sr.Microphone() self.page = page self.brain = brain def listen_loop(self): with self.microphone as source: self.recognizer.adjust_for_ambient_noise(source) print("[النظام] تم ضبط حساسية الميكروفون.") while True: try: with self.microphone as source: print("[استماع] في انتظار أمر صوتي...") audio = self.recognizer.listen(source) command = self.recognizer.recognize_google(audio, language="ar-AR").lower() print(f"[أمر صوتي] تم التقاط: {command}") self.brain.memory.learn(command) response = self.brain.reply(command) self.page.call_from_thread(lambda: self.page.add(ft.Text(f"أمر صوتي: {command}", color="blue"))) self.page.call_from_thread(lambda: self.page.add(ft.Text(f"رد نورا: {response}", color="green"))) except Exception as e: error_text = f"[خطأ] مشكلة في التعرف على الصوت: {e}" print(error_text) self.page.call_from_thread(lambda: self.page.add(ft.Text(error_text, color="red"))) # الواجهة الرئيسية def main(page: ft.Page): page.title = "نورا فون - مساعد صوتي عربي" page.vertical_alignment = ft.MainAxisAlignment.START page.padding = 20 page.scroll = "auto" page.add(ft.Text("مرحبًا بك في تطبيق نورا فون!", size=24, weight=ft.FontWeight.BOLD, color="purple")) page.add(ft.Text("تحدث بالأوامر الصوتية للتحكم بالتطبيق.\nمثال: 'افتح الكاميرا' أو 'شغّل الموسيقى'.", size=16)) memory = CommandMemory() brain = NoraBrain(memory) controller = VoiceController(brain, page) threading.Thread(target=controller.listen_loop, daemon=True).start() # تشغيل التطبيق ft.app(target=main, view=ft.AppView.WEB_BROWSER)