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

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: none; 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")
    # 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:
            st.markdown(f'<div style="padding: 10px; margin-bottom: 10px; background-color: #475063; border-radius: 10px;">Question - {user_input}</div>', unsafe_allow_html=True)
            # Add bot response here (you can replace this with your bot logic)
            response, metadata, source_documents = generate_bot_response(user_input)
            st.markdown(f'<div style="padding: 10px; margin-bottom: 10px; background-color: #475063; border-radius: 10px;">{response}</div>', 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()