File size: 2,291 Bytes
7f8ded9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
858592d
 
7f8ded9
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import logging
from utils import MEMORY, DocumentLoader, check_password
from chat import config_retrieval_chain
from streamlit.external.langchain import StreamlitCallbackHandler

logging.basicConfig(encoding="utf-8", level=logging.INFO)
LOGGER = logging.getLogger()

def main_RAG_ui():
    use_chunk = st.sidebar.slider(
        'Chunk Size',
        500, 2000, (1000)
    )
    use_temperature = st.sidebar.slider(
        'Temperature 🦄',
        0.0, 1.0, (0.1))
    use_compression = st.checkbox("Compression🛠️(on uploaded document)", value=False)
    use_ddg_search = st.checkbox("Search on DuckDuckGO🦆(does not use document)", value=False)

    
    CONV_CHAIN = config_retrieval_chain(
        uploaded_files,
        use_compression=use_compression,
        use_chunksize=use_chunk,
        use_temperature=use_temperature,
        use_zeroshoot=use_ddg_search
    )
    if st.sidebar.button("Clear History🦭"):
        MEMORY.chat_memory.clear()
    if len(MEMORY.chat_memory.messages) == 0:
        st.chat_message("assistant").markdown("Ask me something🤖")
    avatars = {"human": "user", "ai": "assistant"}

    if user_query := st.chat_input(placeholder="Say something🐻"):
        st.chat_message("user").write(user_query)
        container = st.empty()
        stream_handler = StreamlitCallbackHandler(container)
        with st.chat_message("assistant"):
            if use_ddg_search:
                response = CONV_CHAIN.invoke(
                    {"input": user_query}, {"callbacks": [stream_handler]}
                )
                st.write(response["output"])
            else:
                params = {
                    "question": user_query,
                    "chat_history": MEMORY.chat_memory.messages,
                }
                response = CONV_CHAIN.run(params, callbacks=[stream_handler])
                if response:
                    container.markdown(response)


#if not check_password():
#    st.stop()
st.title("👻START CHAT👻")

uploaded_files = st.sidebar.file_uploader(
    label="Upload a file🐣",
    type=list(DocumentLoader.supported_extensions.keys()),
    accept_multiple_files=True
)

if not uploaded_files:
    st.info("Upload a file to start🐣")
    st.stop()

main_RAG_ui()