File size: 2,361 Bytes
36c0029
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import streamlit as st
from streamlit_option_menu import option_menu
from markup import app_intro
import langchain
from langchain.cache import InMemoryCache
from query_data import chat_chain

langchain.llm_cache = InMemoryCache()

def tab1():
    st.header("CIMA Chatbot")
    col1, col2 = st.columns([1, 2])
    with col1:
        st.image("image.jpg", use_column_width=True)
    with col2:
        st.markdown(app_intro(), unsafe_allow_html=True)


metadata_list = []
unique_metadata_list = []
seen = set()

def tab4():    
    if "messages" not in st.session_state:
        st.session_state.messages = []

    st.header("🗣️ Chat with the AI about the ingested documents! 📚")

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

    if user_input := st.chat_input("User Input"):
        st.session_state.messages.append({"role": "user", "content": user_input})

        with st.chat_message("user"):
            st.markdown(user_input)

        with st.spinner("Generating Response..."):

            with st.chat_message("assistant"):
                response = chat_chain({"question": user_input})

                answer = response['answer']
                source_documents = response['source_documents']

                for doc in source_documents:
                    if hasattr(doc, 'metadata'):
                        metadata = doc.metadata
                        metadata_list.append(metadata)

                for metadata in metadata_list:
                    metadata_tuple = tuple(metadata.items())
                    if metadata_tuple not in seen:
                        unique_metadata_list.append(metadata)
                        seen.add(metadata_tuple)

                st.write(answer)
                st.write(unique_metadata_list)
                
            st.session_state.messages.append({"role": "assistant", "content": answer})


def main():
    st.set_page_config(page_title="CIMA Chat", page_icon=":memo:", layout="wide")
    tabs = ["Intro", "Chat"]

    with st.sidebar:

        current_tab = option_menu("Select a Tab", tabs, menu_icon="cast")

    tab_functions = {
    "Intro": tab1,
    "Chat": tab4,
    }

    if current_tab in tab_functions:
        tab_functions[current_tab]()

if __name__ == "__main__":
    main()