import streamlit as st from openai import OpenAI import time import json # إعداد الصفحة st.set_page_config(page_title="Genisi - Chatbot", page_icon="🤖", layout="wide") # اختيار الوضع (فاتح/داكن) من الشريط الجانبي with st.sidebar: theme = st.radio("🎨 الوضع", ["فاتح", "داكن"], index=0) # تطبيق الأنماط حسب الوضع المختار if theme == "فاتح": bg_color = "#FFFFFF" # أبيض كامل للخلفية text_color = "#000000" # أسود كامل للنصوص border_color = "#E0E0E0" # رمادي فاتح جداً للحدود فقط secondary_bg = "#FFFFFF" # نفس الأبيض else: # داكن bg_color = "#000000" # أسود كامل للخلفية text_color = "#FFFFFF" # أبيض كامل للنصوص border_color = "#333333" # رمادي غامق جداً للحدود فقط secondary_bg = "#000000" # نفس الأسود # CSS بسيط جداً - لون واحد فقط st.markdown(f""" """, unsafe_allow_html=True) # مفتاح API API_KEY = "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" # تهيئة العميل client = OpenAI( base_url="https://integrate.api.nvidia.com/v1", api_key=API_KEY ) # تهيئة حالة المحادثات if "conversations" not in st.session_state: st.session_state.conversations = [] if "current_chat_id" not in st.session_state: st.session_state.current_chat_id = None if "messages" not in st.session_state: st.session_state.messages = [] # محاولة تحميل المحادثات من localStorage st.components.v1.html(""" """, height=0) # الشريط الجانبي - قائمة المحادثات with st.sidebar: st.markdown(f'

🤖 Genisi

', unsafe_allow_html=True) st.markdown(f'

by AnesNT

', unsafe_allow_html=True) # زر محادثة جديدة if st.button("➕ محادثة جديدة", use_container_width=True): new_chat = { "id": str(time.time()), "title": f"محادثة جديدة {len(st.session_state.conversations) + 1}", "messages": [], "created_at": time.strftime("%Y-%m-%d %H:%M") } st.session_state.conversations.append(new_chat) st.session_state.current_chat_id = new_chat["id"] st.session_state.messages = [] st.components.v1.html(f""" """, height=0) st.rerun() st.markdown("---") # عرض قائمة المحادثات if st.session_state.conversations: for conv in reversed(st.session_state.conversations): # تحديد عنوان المحادثة title = conv["title"] if conv["messages"] and len(conv["messages"]) > 0: first_msg = conv["messages"][0]["content"][:30] title = first_msg + "..." if len(first_msg) >= 30 else first_msg # تاريخ المحادثة created_at = conv.get("created_at", "") # تنسيق المحادثة col1, col2, col3 = st.columns([5, 1, 1]) with col1: if st.button(f"💬 {title}", key=f"chat_{conv['id']}", use_container_width=True): st.session_state.current_chat_id = conv["id"] st.session_state.messages = conv["messages"] st.rerun() with col2: if st.button("📋", key=f"copy_{conv['id']}", help="نسخ المحادثة"): chat_text = "\n".join([f"{m['role']}: {m['content']}" for m in conv["messages"]]) st.components.v1.html(f""" """, height=0) with col3: if st.button("🗑️", key=f"delete_{conv['id']}", help="حذف المحادثة"): st.session_state.conversations = [c for c in st.session_state.conversations if c["id"] != conv["id"]] st.components.v1.html(f""" """, height=0) if conv["id"] == st.session_state.current_chat_id: st.session_state.current_chat_id = None st.session_state.messages = [] st.rerun() # عرض تاريخ المحادثة if created_at: st.markdown(f'

{created_at}

', unsafe_allow_html=True) else: st.markdown(f'

لا توجد محادثات سابقة

', unsafe_allow_html=True) # المحتوى الرئيسي st.markdown(f'
🤖 Genisi - AI Assistant by AnesNT
', unsafe_allow_html=True) # عرض المحادثة الحالية for msg in st.session_state.messages: with st.chat_message(msg["role"]): st.markdown(msg["content"]) # مربع الإدخال if prompt := st.chat_input("اكتب سؤالك هنا..."): # عرض سؤال المستخدم st.chat_message("user").markdown(prompt) st.session_state.messages.append({"role": "user", "content": prompt}) # تحديث المحادثة الحالية if st.session_state.current_chat_id: for conv in st.session_state.conversations: if conv["id"] == st.session_state.current_chat_id: conv["messages"] = st.session_state.messages break else: # إنشاء محادثة جديدة new_chat = { "id": str(time.time()), "title": prompt[:30] + "..." if len(prompt) > 30 else prompt, "messages": st.session_state.messages, "created_at": time.strftime("%Y-%m-%d %H:%M") } st.session_state.conversations.append(new_chat) st.session_state.current_chat_id = new_chat["id"] st.components.v1.html(f""" """, height=0) # رد المساعد with st.chat_message("assistant"): msg_placeholder = st.empty() full = "" try: # استدعاء API completion = client.chat.completions.create( model="moonshotai/kimi-k2-instruct", messages=st.session_state.messages, temperature=0.5, stream=True ) # تجميع الرد for chunk in completion: if chunk.choices and chunk.choices[0].delta.content: full += chunk.choices[0].delta.content msg_placeholder.markdown(full + "▌") time.sleep(0.01) msg_placeholder.markdown(full) st.session_state.messages.append({"role": "assistant", "content": full}) # تحديث المحادثة الحالية for conv in st.session_state.conversations: if conv["id"] == st.session_state.current_chat_id: conv["messages"] = st.session_state.messages st.components.v1.html(f""" """, height=0) break except Exception as e: st.error(f"حدث خطأ: {str(e)}") # معلومات المطور st.markdown(f'
Genisi is an AI can make mistakes
', unsafe_allow_html=True)