Boardpac/theekshanas commited on
Commit
6ae91a0
1 Parent(s): 3715d20

fixed issue - rerun qa when update params

Browse files
Files changed (2) hide show
  1. app.py +90 -59
  2. qaPipeline.py +2 -2
app.py CHANGED
@@ -6,6 +6,7 @@ D.M. Theekshana Samaradiwakara
6
  """
7
 
8
  import os
 
9
  import streamlit as st
10
  from streamlit.logger import get_logger
11
 
@@ -33,70 +34,60 @@ def initialize_session_state():
33
  "dataset": DATASETS["DEFAULT"],
34
  "chat_history": None,
35
  "is_parameters_changed":False,
36
- "show_source_files": False
 
37
  }
38
 
39
  for k, v in SESSION_DEFAULTS.items():
40
  if k not in st.session_state:
41
  st.session_state[k] = v
42
 
43
-
44
- def main():
45
-
46
- st.set_page_config(page_title="Chat with data",
47
- page_icon=":books:")
48
- st.write(css, unsafe_allow_html=True)
49
-
50
- initialize_session_state()
51
-
52
-
53
-
54
- st.header("Chat with your own data:")
55
-
56
- user_question = st.text_input(
57
- "Ask a question about your documents:",
58
- placeholder="enter question",
59
- )
60
- # Interactive questions and answers
61
- if user_question:
62
- with st.spinner("Processing"):
63
- handle_userinput(user_question)
64
-
65
-
66
  with st.sidebar:
67
  st.subheader("Chat parameters")
68
 
69
- chat_model = st.selectbox(
70
- "Chat model",
71
- MODELS,
72
- key="chat_model",
73
- help="Select the LLM model for the chat",
74
- on_change=update_parameters_change,
75
- )
76
-
77
- # data_source = st.selectbox(
78
- # "dataset",
79
- # DATASETS,
80
- # key="data_source",
81
- # help="Select the private data_source for the chat",
82
- # on_change=update_parameters_change,
83
- # )
84
-
85
- st.session_state.dataset = "DEFAULT"
86
-
87
- show_source = st.checkbox(
88
- label="show source files",
89
- key="show_source",
90
- help="Select this to show relavant source files for the query",
91
- on_change=update_parameters_change,
92
- )
93
-
94
-
95
- if st.session_state.is_parameters_changed:
96
- st.button("Update", on_click=parameters_change_button, args=[chat_model, show_source])
97
-
98
-
99
-
 
 
 
 
 
 
 
 
 
 
 
100
  st.markdown("\n")
101
 
102
  # if st.button("Create FAISS db"):
@@ -108,8 +99,6 @@ def main():
108
  # st.error(f"Error : {e}")#, icon=":books:")
109
  # return
110
 
111
-
112
-
113
  st.markdown(
114
  "### How to use\n"
115
  "1. Select the chat model\n" # noqa: E501
@@ -118,31 +107,70 @@ def main():
118
  )
119
 
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
 
123
  def update_parameters_change():
124
  st.session_state.is_parameters_changed = True
125
 
 
126
  def parameters_change_button(chat_model, show_source):
127
  st.session_state.model = chat_model
128
  st.session_state.dataset = "DEFAULT"
129
  st.session_state.show_source_files = show_source
130
- st.success("chat parameters updated")
131
  st.session_state.is_parameters_changed = False
132
 
 
 
 
133
 
 
134
  def get_answer_from_backend(query, model, dataset):
135
  # response = qaPipeline.run(query=query, model=model, dataset=dataset)
136
  response = qaPipeline.run_agent(query=query, model=model, dataset=dataset)
137
  return response
138
 
 
139
  def show_query_response(query, response, show_source_files):
140
  docs = []
141
  if isinstance(response, dict):
142
  answer, docs = response['result'], response['source_documents']
143
  else:
144
  answer = response
145
-
146
  st.write(user_template.replace(
147
  "{{MSG}}", query), unsafe_allow_html=True)
148
  st.write(bot_template.replace(
@@ -158,6 +186,7 @@ def show_query_response(query, response, show_source_files):
158
  st.markdown(source.page_content)
159
 
160
  # st.write(response)
 
161
 
162
  def is_query_valid(query: str) -> bool:
163
  if (not query) or (query.strip() == ''):
@@ -165,6 +194,7 @@ def is_query_valid(query: str) -> bool:
165
  return False
166
  return True
167
 
 
168
  def handle_userinput(query):
169
  # Get the answer from the chain
170
  try:
@@ -188,5 +218,6 @@ def handle_userinput(query):
188
  st.error(f"Error : {e}")#, icon=":books:")
189
  return
190
 
 
191
  if __name__ == "__main__":
192
  main()
 
6
  """
7
 
8
  import os
9
+ import time
10
  import streamlit as st
11
  from streamlit.logger import get_logger
12
 
 
34
  "dataset": DATASETS["DEFAULT"],
35
  "chat_history": None,
36
  "is_parameters_changed":False,
37
+ "show_source_files": False,
38
+ "user_question":'',
39
  }
40
 
41
  for k, v in SESSION_DEFAULTS.items():
42
  if k not in st.session_state:
43
  st.session_state[k] = v
44
 
45
+ def side_bar():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  with st.sidebar:
47
  st.subheader("Chat parameters")
48
 
49
+ with st.form('param_form'):
50
+
51
+ chat_model = st.selectbox(
52
+ "Chat model",
53
+ MODELS,
54
+ key="chat_model",
55
+ help="Select the LLM model for the chat",
56
+ # on_change=update_parameters_change,
57
+ )
58
+
59
+ # data_source = st.selectbox(
60
+ # "dataset",
61
+ # DATASETS,
62
+ # key="data_source",
63
+ # help="Select the private data_source for the chat",
64
+ # on_change=update_parameters_change,
65
+ # )
66
+
67
+ st.session_state.dataset = "DEFAULT"
68
+
69
+ show_source = st.checkbox(
70
+ label="show source files",
71
+ key="show_source",
72
+ help="Select this to show relavant source files for the query",
73
+ # on_change=update_parameters_change,
74
+ )
75
+
76
+ submitted = st.form_submit_button(
77
+ "Submit",
78
+ # on_click=update_parameters_change
79
+ )
80
+
81
+ if submitted:
82
+ parameters_change_button(chat_model, show_source)
83
+
84
+
85
+ # if st.session_state.is_parameters_changed:
86
+ # st.button("Update",
87
+ # on_click=parameters_change_button,
88
+ # args=[chat_model, show_source]
89
+ # )
90
+
91
  st.markdown("\n")
92
 
93
  # if st.button("Create FAISS db"):
 
99
  # st.error(f"Error : {e}")#, icon=":books:")
100
  # return
101
 
 
 
102
  st.markdown(
103
  "### How to use\n"
104
  "1. Select the chat model\n" # noqa: E501
 
107
  )
108
 
109
 
110
+ def chat_body():
111
+ st.header("Chat with your own data:")
112
+
113
+ user_question = st.text_input(
114
+ "Ask a question about your documents:",
115
+ placeholder="enter question",
116
+ key='user_question',
117
+ on_change=submit_user_question,
118
+ )
119
+
120
+ # if user_question:
121
+ # submit_user_question()
122
+ # # user_question = False
123
+
124
+
125
+
126
+ def submit_user_question():
127
+ with st.spinner("Processing"):
128
+ user_question = st.session_state.user_question
129
+ # st.success(user_question)
130
+ handle_userinput(user_question)
131
+ st.session_state.user_question=''
132
+
133
+
134
+ def main():
135
+
136
+ st.set_page_config(page_title="Chat with data",
137
+ page_icon=":books:")
138
+ st.write(css, unsafe_allow_html=True)
139
+
140
+ initialize_session_state()
141
+
142
+ side_bar()
143
+ chat_body()
144
 
145
 
146
  def update_parameters_change():
147
  st.session_state.is_parameters_changed = True
148
 
149
+
150
  def parameters_change_button(chat_model, show_source):
151
  st.session_state.model = chat_model
152
  st.session_state.dataset = "DEFAULT"
153
  st.session_state.show_source_files = show_source
 
154
  st.session_state.is_parameters_changed = False
155
 
156
+ alert = st.success("chat parameters updated")
157
+ time.sleep(1) # Wait for 3 seconds
158
+ alert.empty() # Clear the alert
159
 
160
+
161
  def get_answer_from_backend(query, model, dataset):
162
  # response = qaPipeline.run(query=query, model=model, dataset=dataset)
163
  response = qaPipeline.run_agent(query=query, model=model, dataset=dataset)
164
  return response
165
 
166
+
167
  def show_query_response(query, response, show_source_files):
168
  docs = []
169
  if isinstance(response, dict):
170
  answer, docs = response['result'], response['source_documents']
171
  else:
172
  answer = response
173
+
174
  st.write(user_template.replace(
175
  "{{MSG}}", query), unsafe_allow_html=True)
176
  st.write(bot_template.replace(
 
186
  st.markdown(source.page_content)
187
 
188
  # st.write(response)
189
+
190
 
191
  def is_query_valid(query: str) -> bool:
192
  if (not query) or (query.strip() == ''):
 
194
  return False
195
  return True
196
 
197
+
198
  def handle_userinput(query):
199
  # Get the answer from the chain
200
  try:
 
218
  st.error(f"Error : {e}")#, icon=":books:")
219
  return
220
 
221
+
222
  if __name__ == "__main__":
223
  main()
qaPipeline.py CHANGED
@@ -145,7 +145,7 @@ class QAPipeline:
145
  You can have a general conversation with the users like greetings.
146
  But only answer questions related to banking sector like financial and legal.
147
  If you dont know the answer say you dont know, dont try to makeup answers.
148
- each answer should start with code word BoardPac Conversation AI:
149
  Question: {question}
150
  """
151
  )
@@ -160,7 +160,7 @@ class QAPipeline:
160
  {context}
161
  Given this information, please answer the question with the latest information.
162
  If you dont know the answer say you dont know, dont try to makeup answers.
163
- each answer should start with code word BoardPac Retrieval AI:
164
  Question: {question}
165
  """
166
  )
 
145
  You can have a general conversation with the users like greetings.
146
  But only answer questions related to banking sector like financial and legal.
147
  If you dont know the answer say you dont know, dont try to makeup answers.
148
+ each answer should start with code word BoardPac AI (Conversation):
149
  Question: {question}
150
  """
151
  )
 
160
  {context}
161
  Given this information, please answer the question with the latest information.
162
  If you dont know the answer say you dont know, dont try to makeup answers.
163
+ each answer should start with code word BoardPac AI (Retrieval):
164
  Question: {question}
165
  """
166
  )