File size: 1,921 Bytes
21a48a4
 
 
 
 
 
 
 
 
 
df672d0
 
 
 
 
21a48a4
 
df672d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21a48a4
df672d0
 
 
 
 
 
 
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
import sys
import os

# Dynamically add the 'src' directory to the Python path
current_dir = os.path.dirname(os.path.abspath(__file__))
src_dir = os.path.join(current_dir, 'src')
if src_dir not in sys.path:
    sys.path.insert(0, src_dir)

# Import modules from the 'src' directory
from src import utils
from src.llm import EmotionalChatbot
from src.log import logger

# Streamlit UI Setup
import streamlit as st

st.set_page_config(
    page_title="Emotional Intelligence Bot",
    page_icon="🤖",
    layout="wide"
)

# Initialize session state for chat history
if "chat_history" not in st.session_state:
    st.session_state.chat_history = []

try:
    # Initialize the chatbot
    chatbot = EmotionalChatbot(chat_history=st.session_state.chat_history)
except Exception as e:
    logger.critical("Failed to initialize the chatbot", exc_info=True)
    st.error("Unable to initialize the bot. Check logs for more details.")

st.markdown(utils.styles(), unsafe_allow_html=True)

st.header(":rainbow[Lumina] - :blue[EI Bot] 🤗")

st.sidebar.markdown(utils.sidebar_markdown())

# Display chat history
for message in st.session_state.chat_history:
    for role, content in message.items():
        with st.chat_message(role):
            st.write(content)

# User input
user_input = st.chat_input("Chat With Lumina...")
if user_input:
    with st.chat_message("user"):
        st.write(user_input)

    # Process the user input and generate bot response
    try:
        with st.spinner("Thinking..."):
            response = chatbot.generate_response(user_input)
            message = {'human': user_input, 'AI': response}
            st.session_state.chat_history.append(message)
            with st.chat_message("assistant"):
                st.write(response)
    except Exception as e:
        logger.error(f"Error generating bot response: {e}")
        st.error("An error occurred. Check logs for more details.")