Tanmay211998 commited on
Commit
7896b1b
·
verified ·
1 Parent(s): f070650

Create QA_Bot.py

Browse files
Files changed (1) hide show
  1. QA_Bot.py +37 -0
QA_Bot.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from QnA import Q_A
3
+ import re,time
4
+
5
+
6
+ def QA_Bot(vectorstore):
7
+ st.title("Q&A Bot")
8
+ # Initialize chat history
9
+ if "messages" not in st.session_state:
10
+ st.session_state.messages = []
11
+
12
+ # Display chat messages from history on app rerun
13
+ for message in st.session_state.messages:
14
+ with st.chat_message(message["role"]):
15
+ st.markdown(message["content"])
16
+
17
+ # React to user input
18
+ if prompt := st.chat_input("What is up?"):
19
+ # Display user message in chat message container
20
+ st.chat_message("user").markdown(prompt)
21
+ # Add user message to chat history
22
+ st.session_state.messages.append({"role": "user", "content": prompt})
23
+
24
+ ai_response = Q_A(vectorstore,prompt)
25
+ response = f"Echo: {ai_response}"
26
+ # Display assistant response in chat message container
27
+ with st.chat_message("assistant"):
28
+ message_placeholder = st.empty()
29
+ full_response = ""
30
+ for chunk in re.split(r'(\s+)', response):
31
+ full_response += chunk + " "
32
+ time.sleep(0.01)
33
+
34
+ # Add a blinking cursor to simulate typing
35
+ message_placeholder.markdown(full_response + "▌")
36
+ # Add assistant response to chat history
37
+ st.session_state.messages.append({"role": "assistant", "content": full_response})