File size: 2,118 Bytes
36c0029
 
 
 
 
9c95d2e
 
36c0029
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c95d2e
 
 
36c0029
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ba2d0e
 
 
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
import streamlit as st
from streamlit_option_menu import option_menu
from markup import app_intro
import langchain
from query_data import chat_chain
from memory import msgs



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():    
    st.header("🗣️ Chat with the AI about the ingested documents! 📚")

    for i, msg in enumerate(msgs.messages):
        name = "user" if i % 2 == 0 else "assistant"
        st.chat_message(name).markdown(msg.content)

    if user_input := st.chat_input("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)

    if st.sidebar.button("Clear Chat History"):
        msgs.clear()


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()