File size: 2,560 Bytes
8274ba3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
"""
Streamlit web interface for the chatbot application.
"""
import streamlit as st
from chatbot import Chatbot
from utils.logging_config import setup_logging
import config

# Setup logging
logger = setup_logging()

# Set page config
st.set_page_config(
    page_title="Document Chatbot",
    page_icon="πŸ“š",
    layout="wide"
)

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

# Initialize chatbot
if "chatbot" not in st.session_state:
    with st.spinner("Initializing chatbot..."):
        # Get configuration from config module
        chatbot_config = config.get_chatbot_config()
        
        # Initialize chatbot
        logger.info("Initializing chatbot...")
        st.session_state.chatbot = Chatbot(chatbot_config)
        
        # Load documents and create index
        documents = st.session_state.chatbot.load_documents()
        st.session_state.chatbot.create_index(documents)
        st.session_state.chatbot.initialize_query_engine()
        logger.info("Chatbot initialized successfully")

# Title and description
st.title("πŸ“š Document Chatbot")
st.markdown("""
This chatbot can answer questions about your documents. 
Ask any question about the content in your documents!
""")

# Display chat messages
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# Chat input
if prompt := st.chat_input("What would you like to know?"):
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)

    # Get chatbot response
    with st.chat_message("assistant"):
        with st.spinner("Thinking..."):
            logger.info(f"User query: {prompt}")
            response = st.session_state.chatbot.query(prompt)
            st.markdown(response)
            st.session_state.messages.append({"role": "assistant", "content": response})
            logger.info("Response provided to user")

# Sidebar with information
with st.sidebar:
    st.title("About")
    st.markdown("""
    This chatbot uses:
    - LlamaIndex for document processing
    - Claude 3 Sonnet for answering questions
    - Streamlit for the interface
    
    The chatbot has access to your documents in the `data` folder.
    """)
    
    # Add a clear chat button
    if st.button("Clear Chat History"):
        st.session_state.messages = []
        logger.info("Chat history cleared")
        st.rerun()