Skip to content

Commit 053d9a2

Browse files
Create ذكاء اصطناعي
ذكاء اصطناعي
1 parent d74a963 commit 053d9a2

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

ذكاء اصطناعي

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import os
2+
import tkinter as tk
3+
from tkinter import scrolledtext, messagebox
4+
from google import genai
5+
from google.genai import types
6+
7+
# --- إعدادات الذكاء الاصطناعي (كما في الكود الأصلي) ---
8+
MODEL = 'gemini-2.5-flash'
9+
SYSTEM_INSTRUCTION = "أنت مساعد ذكاء اصطناعي ودود ومفيد، تجيب على جميع الأسئلة باللغة العربية الفصحى. كن مختصراً وواضحاً."
10+
client = None
11+
chat = None
12+
13+
def setup_ai():
14+
"""تهيئة اتصال Gemini وبدء جلسة المحادثة."""
15+
global client, chat
16+
try:
17+
# محاولة تهيئة العميل
18+
client = genai.Client()
19+
20+
# إنشاء جلسة المحادثة
21+
chat = client.chats.create(
22+
model=MODEL,
23+
config=types.GenerateContentConfig(
24+
system_instruction=SYSTEM_INSTRUCTION
25+
)
26+
)
27+
return True
28+
except Exception as e:
29+
messagebox.showerror("خطأ في التهيئة",
30+
f"فشل في إعداد الذكاء الاصطناعي. تأكد من تعيين متغير البيئة GEMINI_API_KEY. التفاصيل: {e}")
31+
return False
32+
33+
# --- وظائف واجهة المستخدم (GUI Functions) ---
34+
35+
def send_message():
36+
"""إرسال رسالة المستخدم وعرض رد الروبوت."""
37+
user_input = user_entry.get()
38+
if not user_input.strip():
39+
return
40+
41+
# مسح مربع الإدخال
42+
user_entry.delete(0, tk.END)
43+
44+
# عرض رسالة المستخدم
45+
chat_history.config(state=tk.NORMAL)
46+
chat_history.insert(tk.END, f"أنت: {user_input}\n", 'user')
47+
chat_history.config(state=tk.DISABLED)
48+
chat_history.see(tk.END)
49+
50+
# الحصول على الرد من Gemini
51+
try:
52+
response = chat.send_message(user_input)
53+
54+
# عرض رد الروبوت
55+
chat_history.config(state=tk.NORMAL)
56+
chat_history.insert(tk.END, f"الروبوت: {response.text}\n\n", 'bot')
57+
chat_history.config(state=tk.DISABLED)
58+
chat_history.see(tk.END)
59+
60+
except Exception as e:
61+
chat_history.config(state=tk.NORMAL)
62+
chat_history.insert(tk.END, f"❌ خطأ: حدثت مشكلة أثناء الاتصال.\n\n", 'error')
63+
chat_history.config(state=tk.DISABLED)
64+
messagebox.showerror("خطأ", f"حدث خطأ أثناء معالجة الرسالة: {e}")
65+
66+
# --- إعداد الواجهة الرسومية (GUI Setup) ---
67+
68+
# تهيئة نافذة Tkinter
69+
root = tk.Tk()
70+
root.title("🤖 روبوت المحادثة العربي")
71+
root.geometry("600x500")
72+
root.option_add('*Font', 'Arial 12') # تعيين الخط الافتراضي
73+
74+
# تعيين الواجهة لتدعم اللغة العربية (من اليمين لليسار)
75+
root.tk_setPalette(background='#f0f0f0')
76+
77+
# مربع عرض المحادثة (ScrolledText)
78+
chat_history = scrolledtext.ScrolledText(root, wrap=tk.WORD, state=tk.DISABLED,
79+
bg='white', bd=1, relief=tk.FLAT)
80+
chat_history.pack(pady=10, padx=10, fill=tk.BOTH, expand=True)
81+
82+
# إضافة وسوم للألوان (Tags for colors)
83+
chat_history.tag_config('user', foreground='blue')
84+
chat_history.tag_config('bot', foreground='black', font=('Arial', 12, 'bold'))
85+
chat_history.tag_config('error', foreground='red')
86+
87+
# إطار الإدخال والزر
88+
input_frame = tk.Frame(root, padx=10, pady=5)
89+
input_frame.pack(fill=tk.X)
90+
91+
# مربع إدخال المستخدم
92+
user_entry = tk.Entry(input_frame, bd=1, relief=tk.SOLID, justify='right') # محاذاة لليمين
93+
user_entry.pack(side=tk.LEFT, fill=tk.X, expand=True, ipady=5)
94+
user_entry.bind("<Return>", lambda event: send_message()) # إرسال عند الضغط على Enter
95+
96+
# زر الإرسال
97+
send_button = tk.Button(input_frame, text="إرسال", command=send_message, bg='#4CAF50', fg='white',
98+
relief=tk.FLAT)
99+
send_button.pack(side=tk.LEFT, padx=(5, 0), ipady=5)
100+
101+
# --- بدء التطبيق ---
102+
103+
# 1. تهيئة الذكاء الاصطناعي عند بدء التشغيل
104+
if setup_ai():
105+
# رسالة ترحيب أولى
106+
chat_history.config(state=tk.NORMAL)
107+
chat_history.insert(tk.END, "🤖 الروبوت: مرحباً! كيف يمكنني مساعدتك اليوم؟\n\n", 'bot')
108+
chat_history.config(state=tk.DISABLED)
109+
110+
# 2. تشغيل الحلقة الرئيسية للواجهة
111+
root.mainloop()
112+
pip install pyinstaller
113+
pyinstaller --onefile --windowed tkinter_chatbot.py

0 commit comments

Comments
 (0)