-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookReaderUI.py
More file actions
104 lines (89 loc) · 4.05 KB
/
bookReaderUI.py
File metadata and controls
104 lines (89 loc) · 4.05 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from logging import disable
import streamlit as st
import time
from bookerReaderModule import BookerReaderModule
bookReader = BookerReaderModule()
if "url_exist" not in st.session_state:
st.session_state.url_exist = None
if "use_url" not in st.session_state:
st.session_state.use_url = False
st.title("RAGy Chatbot")
# # After getting URL from user it process URL and will show progress here. NOTE: will be manage from document loading class
def show_progress_bar():
progress_text = "Processing URL, Please wait!"
# print(f"{progress_text} {st.session_state.load_file_progress_disabled}")
progress_container = st.empty()
my_bar = progress_container.progress(0, text=progress_text)
for percent_complete in range(100):
time.sleep(0.01)
my_bar.progress(percent_complete + 1, text=progress_text)
time.sleep(1)
my_bar.empty()
st.info("File has been processed")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
st.session_state.messages.append(
{"role": "assistant", "content": "Hi, how can I assist you today?"})
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("Message QA Chatbot..."):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# Display assistant response in chat message container
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
query = [msg["content"]
for msg in st.session_state.messages if msg["role"] == "user"][-1].lower()
# Process Query here and get response
if st.session_state.url_exist == None:
url = bookReader.check_if_url_contains(query)
if url:
st.session_state.url_exist = url
# print("this is the line", st.session_state.use_url,st.session_state.url_exist)
if st.session_state.url_exist and "yes" in query.lower().split(" "):
print("st.session_state.url_exist -> ", st.session_state.url_exist)
bookReader.preProcessDataIndexing(fileType="WEBURL", fileURL=st.session_state.url_exist)
show_progress_bar()
st.session_state.use_url = False
st.session_state.url_exist = None
assistant_response = "Processing URL"
elif st.session_state.url_exist and "no" in query.lower().split(" "):
st.session_state.use_url = False
st.session_state.url_exist = None
assistant_response = "Anything else I can do for you?"
elif st.session_state.url_exist:
assistant_response = f"Do you want to talk about \"{st.session_state.url_exist}\""
st.session_state.use_url = True
# print("this is the line", st.session_state.use_url,st.session_state.url_exist)
else:
assistant_response = "Process Query"
assistant_response = bookReader.process_user_query(query)
print(assistant_response)
if st.session_state.url_exist:
st.session_state.url_exist = None
# # Simulate stream of response with milliseconds delay
if "\n" in assistant_response:
for chunk in assistant_response.split("\n"):
for item in chunk.split():
full_response += item + " "
time.sleep(0.05)
# Add a blinking cursor to simulate typing
message_placeholder.markdown(full_response + "▌")
full_response += "\n"
else:
for item in assistant_response.split():
full_response += item + " "
time.sleep(0.05)
# Add a blinking cursor to simulate typing
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(assistant_response)
# Add assistant response to chat history
st.session_state.messages.append(
{"role": "assistant", "content": full_response})