Spaces:
Sleeping
Sleeping
Sandaruth
commited on
Commit
•
4761e32
1
Parent(s):
fa2cbc9
app update
Browse files
app.py
CHANGED
@@ -1,57 +1,63 @@
|
|
1 |
import streamlit as st
|
2 |
from model import Web_qa
|
3 |
-
from color import print_colorful_msg
|
4 |
import time
|
5 |
|
6 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
start_time = time.time()
|
8 |
-
|
9 |
res = Web_qa(user_input)
|
10 |
-
print_colorful_msg(f"Response: {res}", color='green')
|
11 |
response = res['result']
|
12 |
metadata = [i.metadata for i in res.get("source_documents", [])]
|
13 |
end_time = time.time()
|
14 |
response_time = end_time - start_time
|
15 |
-
|
16 |
return response, metadata, res.get('source_documents', [])
|
17 |
|
18 |
-
# def chatbot_response(user_input): # Hugging Face Misral model
|
19 |
-
# start_time = time.time()
|
20 |
-
# print_colorful_msg(f"User Input: {user_input}", color='green')
|
21 |
-
# res = Web_qa(user_input)
|
22 |
-
# print_colorful_msg(f"Response: {res}", color='blue')
|
23 |
-
# response = res["result"].split(": Let me think about it...")[-1]
|
24 |
-
# metadata = [i.metadata for i in res.get("source_documents", [])]
|
25 |
-
# end_time = time.time()
|
26 |
-
# response_time = end_time - start_time
|
27 |
-
# print_colorful_msg(f"Response Time: {response_time} seconds", color='yellow')
|
28 |
-
# return response, metadata, res.get('source_documents', [])
|
29 |
-
|
30 |
-
def main():
|
31 |
-
print_colorful_msg("Starting chatbot main function ...", color='blue')
|
32 |
-
st.title("ATrad Chatbot")
|
33 |
-
|
34 |
-
user_input = st.text_input("Enter your message:")
|
35 |
-
if st.button("Send"):
|
36 |
-
response, metadata, source_documents = chatbot_response(user_input)
|
37 |
-
st.text_area("Chatbot Response:", value=response, height=200)
|
38 |
-
|
39 |
-
st.markdown("### Source Documents:")
|
40 |
-
if metadata:
|
41 |
-
for value in metadata:
|
42 |
-
st.write(f"{value}")
|
43 |
-
else:
|
44 |
-
st.write("No metadata available.")
|
45 |
-
|
46 |
-
# st.markdown("### Source Documents:")
|
47 |
-
if source_documents:
|
48 |
-
for doc in source_documents:
|
49 |
-
pass
|
50 |
-
# st.write(doc)
|
51 |
-
else:
|
52 |
-
st.write("No source documents found.")
|
53 |
-
|
54 |
-
print_colorful_msg("End Chatbot Response...", color='blue')
|
55 |
-
|
56 |
if __name__ == "__main__":
|
57 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
from model import Web_qa
|
|
|
3 |
import time
|
4 |
|
5 |
+
def main():
|
6 |
+
# Set up the layout --------------------------------------------------------------
|
7 |
+
st.sidebar.title("Guideline")
|
8 |
+
st.sidebar.markdown("""
|
9 |
+
1. Type your message in the chat box on the right.
|
10 |
+
2. Hit Enter or click the send button to send your message.
|
11 |
+
3. Chat bot responses will appear below.
|
12 |
+
4. Source documents will be displayed in the sidebar.
|
13 |
+
""")
|
14 |
+
|
15 |
+
# Button to connect to Google link ------------------------------------------------
|
16 |
+
|
17 |
+
st.sidebar.markdown('<a href="https://docs.google.com/spreadsheets/d/181FRjjbBnAn0aBaNOmOgMPKSGRtE8vgKex0vmgTmgFs/edit#gid=0" target="_blank" style="display: inline-block;'
|
18 |
+
'background-color: none; color: white; padding: 10px 20px; text-align: center;border: 1px solid white;'
|
19 |
+
'text-decoration: none; cursor: pointer; border-radius: 5px;">Connect to Google</a>',
|
20 |
+
unsafe_allow_html=True)
|
21 |
+
|
22 |
+
st.title("ATrad Chat App")
|
23 |
+
|
24 |
+
# Chat area -----------------------------------------------------------------------
|
25 |
+
|
26 |
+
user_input = st.text_input("You:", key="user_input")
|
27 |
+
# JavaScript code to submit the form on Enter key press
|
28 |
+
js_submit = f"""
|
29 |
+
document.addEventListener("keydown", function(event) {{
|
30 |
+
if (event.code === "Enter" && !event.shiftKey) {{
|
31 |
+
document.querySelector(".stTextInput").dispatchEvent(new Event("submit"));
|
32 |
+
}}
|
33 |
+
}});
|
34 |
+
"""
|
35 |
+
st.markdown(f'<script>{js_submit}</script>', unsafe_allow_html=True)
|
36 |
+
if st.button("Send"):
|
37 |
+
if user_input:
|
38 |
+
st.markdown(f'<div style="padding: 10px; margin-bottom: 10px; background-color: #475063; border-radius: 10px;">Question - {user_input}</div>', unsafe_allow_html=True)
|
39 |
+
# Add bot response here (you can replace this with your bot logic)
|
40 |
+
response, metadata, source_documents = generate_bot_response(user_input)
|
41 |
+
st.markdown(f'<div style="padding: 10px; margin-bottom: 10px; background-color: #475063; border-radius: 10px;">{response}</div>', unsafe_allow_html=True)
|
42 |
+
# Source documents
|
43 |
+
print("metadata", metadata)
|
44 |
+
st.sidebar.title("Source Documents")
|
45 |
+
for i, doc in enumerate(source_documents, 1):
|
46 |
+
tit=metadata[i-1]["source"].split("\\")[-1]
|
47 |
+
with st.sidebar.expander(f"{tit}"):
|
48 |
+
st.write(doc) # Assuming the Document object can be directly written to display its content
|
49 |
+
|
50 |
+
def generate_bot_response(user_input):
|
51 |
+
# Simple bot logic (replace with your actual bot logic)
|
52 |
start_time = time.time()
|
53 |
+
print(f"User Input: {user_input}")
|
54 |
res = Web_qa(user_input)
|
|
|
55 |
response = res['result']
|
56 |
metadata = [i.metadata for i in res.get("source_documents", [])]
|
57 |
end_time = time.time()
|
58 |
response_time = end_time - start_time
|
59 |
+
print(f"Response Time: {response_time} seconds")
|
60 |
return response, metadata, res.get('source_documents', [])
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
if __name__ == "__main__":
|
63 |
main()
|