Spaces:
Running
Running
Paul Magee
Refactor Streamlit chatbot application into app.py, enhancing UI and feedback management. Removed originalapp.py and Public_Chat.py. Added environment variable fixes for cloud deployment. Improved message handling and session state management.
8274ba3
""" | |
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() |