File size: 2,722 Bytes
23b8b85 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# main.py
import streamlit as st
from typing import Optional, Dict, List
from config import Config
from chat_service import ChatService
from chat_manager import ChatManager
from chat_state import ChatState
from ui_components import UIComponents
def main():
config = Config()
chat_service = ChatService(config.GROQ_API_KEY)
chat_manager = ChatManager(config.SYSTEM_PROMPT)
# Initialize session state
if "chat_state" not in st.session_state:
st.session_state.chat_state = ChatState.initialize()
chat_state = st.session_state.chat_state
ui = UIComponents()
# Setup UI
ui.setup_page(config)
ui.render_header()
ui.render_sidebar(chat_state, chat_manager, chat_service)
# Main chat area
current_chat = chat_state.temp_chat or (
chat_state.chat_history[chat_state.current_chat_id]
if chat_state.current_chat_id
else None
)
if current_chat:
for message in current_chat[1:]:
with st.chat_message(
message["role"], avatar="🧑🎓" if message["role"] == "user" else "🤖"
):
st.write(message["content"])
else:
st.markdown(
"<div style=\"color: black;\">.انقر على 'محادثة جديدة' لبدء محادثة مع المرشد التعليمي 👈</div>",
unsafe_allow_html=True,
)
ui.render_footer()
# Chat input
user_input = st.chat_input("...اكتب سؤالك هنا")
if user_input:
if chat_service.is_arabic(user_input):
if not current_chat:
chat_state.temp_chat = chat_manager.create_new_chat()
current_chat = chat_state.temp_chat
current_chat.append({"role": "user", "content": user_input})
with st.chat_message("user", avatar="🧑🎓"):
st.write(user_input)
with st.chat_message("assistant", avatar="🤖"):
with st.spinner("جاري التفكير... ⏳"):
assistant_response = chat_service.get_groq_response(current_chat)
st.write(assistant_response)
current_chat.append({"role": "assistant", "content": assistant_response})
if chat_state.temp_chat:
new_chat_id = chat_manager.save_chat(
chat_state.temp_chat, chat_state.chat_history
)
if new_chat_id:
chat_state.current_chat_id = new_chat_id
chat_state.temp_chat = None
else:
st.error("عذرًا، يرجى إدخال النص باللغة العربية فقط. 🚫")
if __name__ == "__main__":
main()
|