Spaces:
Sleeping
Sleeping
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://docs.google.com/spreadsheets/d/181FRjjbBnAn0aBaNOmOgMPKSGRtE8vgKex0vmgTmgFs/edit#gid=0" 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;">Connect to Google</a>', | |
unsafe_allow_html=True) | |
st.title("ATrad Chat App") | |
# Chat area ----------------------------------------------------------------------- | |
user_input = st.text_input("You:", 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() | |