Spaces:
Build error
Build error
| import os | |
| import streamlit as st | |
| # load environment variables | |
| from dotenv import load_dotenv | |
| from huggingface_hub import login | |
| import utils | |
| from build_model import load_model | |
| load_dotenv() | |
| login(token=os.getenv("HUGGINGFACE_TOKEN")) | |
| st.title("Buzzbot") | |
| # Initialize retriever and model | |
| if "retriever" not in st.session_state: | |
| st.session_state["retriever"] = utils.build_retriever() | |
| if "model" not in st.session_state: | |
| st.session_state["model"] = load_model() | |
| if "conversation" not in st.session_state: | |
| st.session_state["conversation"] = utils.Conversation() | |
| # Initialize chat history | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| # Display chat messages from history on app rerun | |
| for message in st.session_state.messages: | |
| with st.chat_message(message["role"]): | |
| st.markdown(message["content"]) | |
| if message["role"] == "assistant": | |
| st.caption(message["source_docs"]) | |
| # Accept user input | |
| if user_input := st.chat_input("What is up?"): | |
| # Add user message to chat history | |
| st.session_state.messages.append({"role": "user", "content": user_input, "source_docs": None}) | |
| # Display user message in chat message container | |
| with st.chat_message("user"): | |
| st.markdown(user_input) | |
| # Display assistant response in chat message container | |
| with st.chat_message("assistant"): | |
| with st.spinner(""): | |
| answer, source_docs = utils.ask_question( | |
| user_input, st.session_state.conversation, st.session_state.model, st.session_state.retriever | |
| ) | |
| st.write(answer) | |
| # for source_doc in source_docs: | |
| st.caption(source_docs) | |
| st.session_state.messages.append({"role": "assistant", "content": answer, "source_docs": source_docs}) | |