import streamlit as st import time import random import datetime from utils import ( analyze_sentiment, generate_ai_response, get_mock_users, get_mock_rooms, get_initial_messages ) # --- PAGE CONFIGURATION --- st.set_page_config( page_title="چت روم هوشمند", page_icon="🤖", layout="wide", initial_sidebar_state="expanded" ) # --- CSS STYLING (Ported from PHP/HTML) --- st.markdown(""" """, unsafe_allow_html=True) # --- SESSION STATE INITIALIZATION --- if 'logged_in' not in st.session_state: st.session_state.logged_in = False if 'username' not in st.session_state: st.session_state.username = "" if 'role' not in st.session_state: st.session_state.role = "user" # or 'admin' if 'current_room' not in st.session_state: st.session_state.current_room = 1 if 'messages' not in st.session_state: st.session_state.messages = get_initial_messages() if 'users' not in st.session_state: st.session_state.users = get_mock_users() # --- HELPER FUNCTIONS --- def render_message(msg): is_own = msg['user_id'] == st.session_state.username # Simulating ID check msg_class = "own" if is_own else "other" sender_name = "شما" if is_own else msg['username'] # Admin delete button logic admin_html = "" if st.session_state.role == 'admin' and not is_own: admin_html = f"""
حذف پیام
""" time_str = msg['time'] return f"""
{sender_name} {time_str}
{msg['text']}
{admin_html}
""" def get_room_name(room_id): rooms = get_mock_rooms() for r in rooms: if r['id'] == room_id: return r['name'] return "اتاق ناشناس" # --- PAGES --- def login_page(): st.markdown("""
🤖

چت روم هوشمند

ورود به دنیای هوش مصنوعی

Built with anycoder
""", unsafe_allow_html=True) with st.container(): col1, col2, col3 = st.columns([1, 2, 1]) with col2: # Hidden Login Form logic with st.form("login_form", clear_on_submit=True): username_input = st.text_input("نام کاربری", placeholder="نام کاربری خود را وارد کنید") password_input = st.text_input("رمز عبور", type="password", placeholder="رمز عبور") submit_btn = st.form_submit_button("ورود به سیستم", use_container_width=True) # Simple auth simulation if submit_btn: if username_input and password_input: st.session_state.logged_in = True st.session_state.username = username_input # Simulate admin login if username is admin st.session_state.role = 'admin' if username_input.lower() == 'admin' else 'user' st.rerun() else: st.error("لطفاً نام کاربری و رمز عبور را وارد کنید.") st.markdown("
حساب کاربری ندارید؟ ثبت نام کنید
", unsafe_allow_html=True) def chat_page(): # Layout sidebar, main = st.columns([1, 3]) current_room_id = st.session_state.current_room room_name = get_room_name(current_room_id) # --- SIDEBAR --- with sidebar: st.markdown(f""" ", unsafe_allow_html=True) # Close sidebar # Logout if st.button("خروج از سیستم", use_container_width=True, key="logout_btn"): st.session_state.logged_in = False st.session_state.messages = [] # clear session st.rerun() # --- MAIN CHAT AREA --- with main: st.markdown(f"""

{room_name}

گفتگوی آزاد و محترمانه

{f'' if st.session_state.role == 'admin' else ''}
""", unsafe_allow_html=True) # Messages Container with st.container(): msg_container = st.container() # Filter messages for current room room_messages = [m for m in st.session_state.messages if m['room_id'] == current_room_id] with msg_container: st.markdown('
', unsafe_allow_html=True) if not room_messages: st.markdown("""

هنوز هیچ پیامی نیست. اولین نفر باشید!

""", unsafe_allow_html=True) else: for msg in room_messages: st.markdown(render_message(msg), unsafe_allow_html=True) st.markdown("
", unsafe_allow_html=True) # Input Area with st.form("chat_form", clear_on_submit=True): col_input, col_send = st.columns([5, 1]) with col_input: user_input = st.text_input("message_input", placeholder="پیام خود را بنویسید...", label_visibility="collapsed") with col_send: submit = st.form_submit_button("ارسال", use_container_width=True) # Emoji Bar (Custom HTML buttons) st.markdown("""
""", unsafe_allow_html=True) # JS to handle emoji clicks (using streamlit's set_query_param or similar trick is hard, # so we stick to Python handling for simplicity here, or just visual buttons) # For a functional emoji click in Streamlit without components, we can use st.session_state emojis = ["😊", "😂", "❤️", "👍"] cols = st.columns(4) for i, emoji in enumerate(emojis): if cols[i].button(emoji, key=f"emoji_{i}"): st.session_state.temp_input = user_input + emoji st.rerun() if submit and user_input: # Add User Message new_msg = { "id": int(time.time() * 1000), "room_id": current_room_id, "user_id": st.session_state.username, "username": st.session_state.username, "text": user_input, "time": datetime.datetime.now().strftime("%H:%M"), "is_deleted": False } st.session_state.messages.append(new_msg) st.rerun() # Simulate AI/Bot Response time.sleep(0.5) # Simulate thinking response_text = generate_ai_response(user_input) bot_msg = { "id": int(time.time() * 1000) + 1, "room_id": current_room_id, "user_id": "ai_bot", "username": "دستیار هوشمند", "text": response_text, "time": datetime.datetime.now().strftime("%H:%M"), "is_deleted": False } st.session_state.messages.append(bot_msg) st.rerun() st.markdown("
", unsafe_allow_html=True) # Close main-chat-area # --- MAIN NAVIGATION LOGIC --- # Use temp_input for emoji appending if 'temp_input' not in st.session_state: st.session_state.temp_input = "" def main(): if not st.session_state.logged_in: login_page() else: chat_page() if __name__ == "__main__": main()