File size: 1,792 Bytes
484b4e0
 
13b81ea
 
484b4e0
 
 
 
11f324c
 
 
484b4e0
 
 
11f324c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484b4e0
11f324c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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})