File size: 3,318 Bytes
9a9ff62
 
 
 
3630a6a
 
5f8dde6
3630a6a
 
4761e32
 
 
 
 
 
 
 
 
 
 
ac53508
b52ddd3
3630a6a
c1f0bbe
4761e32
 
 
 
 
 
d9b3134
4761e32
 
 
 
 
 
 
 
 
 
 
3630a6a
5f8dde6
3630a6a
5f8dde6
 
 
 
 
 
 
 
 
 
 
 
 
 
4761e32
 
 
9a9ff62
4761e32
9a9ff62
 
 
 
 
4761e32
9a9ff62
 
 
 
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
import streamlit as st
from model import Web_qa
import time

from htmlTemplates import css, bot_template, user_template, source_template

st.set_page_config(page_title="Chat with ATrad",page_icon=":currency_exchange:")
st.write(css, unsafe_allow_html=True)

def main():
    # Set up the layout --------------------------------------------------------------
    st.sidebar.title("Guideline")
    st.sidebar.markdown("""
        1. Type your message in the chat box on the right.
        2. Hit Enter or click the send button to send your message.
        3. Chat bot responses will appear below.
        4. Source documents will be displayed in the sidebar.
    """)

    # Button to connect to Google link ------------------------------------------------
    
    st.sidebar.markdown('<a href="https://drive.google.com/drive/folders/13v6LsaYH9wEwvqVtlLG1U4OiUHgZ7hY4?usp=sharing" target="_blank" style="display: inline-block;'
                        'background-color: #475063; color: white; padding: 10px 20px; text-align: center;border: 1px solid white;'
                        'text-decoration: none; cursor: pointer; border-radius: 5px;">Sources</a>',
                        unsafe_allow_html=True)

    st.title("ATrad Chat App")

    # Chat area -----------------------------------------------------------------------

    user_input = st.text_input("", key="user_input",placeholder="Type your question here...")
    # JavaScript code to submit the form on Enter key press
    js_submit = f"""
        document.addEventListener("keydown", function(event) {{
            if (event.code === "Enter" && !event.shiftKey) {{
                document.querySelector(".stTextInput").dispatchEvent(new Event("submit"));
            }}
        }});
    """
    st.markdown(f'<script>{js_submit}</script>', unsafe_allow_html=True)
    if st.button("Send"):
        if user_input:
            
            with st.spinner('Waiting for response...'):
            
                # Add bot response here (you can replace this with your bot logic)
                response, metadata, source_documents = generate_bot_response(user_input)
                st.write(user_template.replace(
                "{{MSG}}", user_input), unsafe_allow_html=True)
                st.write(bot_template.replace(
                "{{MSG}}",  response ), unsafe_allow_html=True)
            
                # Source documents
                print("metadata", metadata)
                st.sidebar.title("Source Documents")
                for i, doc in enumerate(source_documents, 1):
                    tit=metadata[i-1]["source"].split("\\")[-1]
                    with st.sidebar.expander(f"{tit}"):
                        st.write(doc)  # Assuming the Document object can be directly written to display its content

def generate_bot_response(user_input):
    # Simple bot logic (replace with your actual bot logic)
    start_time = time.time()
    print(f"User Input: {user_input}")
    res = Web_qa(user_input)
    response = res['result']
    metadata = [i.metadata for i in res.get("source_documents", [])]
    end_time = time.time()
    response_time = end_time - start_time
    print(f"Response Time: {response_time} seconds")
    return response, metadata, res.get('source_documents', [])

if __name__ == "__main__":
    main()