PyaeSoneK commited on
Commit
caef49d
·
1 Parent(s): 70b7c5b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +273 -0
app.py ADDED
@@ -0,0 +1,273 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #This version includes the memory and custom prompt, representing the final version
2
+
3
+ import streamlit as st
4
+ from streamlit_chat import message as st_message
5
+ import pandas as pd
6
+ import numpy as np
7
+ import datetime
8
+ import gspread
9
+ import pickle
10
+ import os
11
+ import csv
12
+ import json
13
+ import torch
14
+ from tqdm.auto import tqdm
15
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
16
+
17
+
18
+
19
+
20
+
21
+ from langchain import HuggingFacePipeline
22
+ from langchain.chains import RetrievalQA
23
+ from langchain.prompts import PromptTemplate
24
+ from langchain.memory import ConversationBufferWindowMemory
25
+
26
+
27
+ from langchain.chains import LLMChain
28
+ from langchain.chains import ConversationalRetrievalChain
29
+ from langchain.chains.question_answering import load_qa_chain
30
+ from langchain.chains.conversational_retrieval.prompts import CONDENSE_QUESTION_PROMPT
31
+
32
+
33
+
34
+ prompt_template = """
35
+ You are the chatbot and the advanced legal assitant that can give answers to all the legal questions a common citizen would have . Your job is to give answers when questions about General legal information, Family law, Employment law, Consumer rights, Housing and tenancy, Personal injury, Wills and estates, Criminal law are asked.
36
+ Your job is to answer questions only and only related to Legal aspect. Anything unrelated should be responded with the fact that your main job is solely to provide assistance regarding Legality.
37
+ MUST only use the following pieces of context to answer the question at the end. If the answers are not in the context or you are not sure of the answer, just say that you don't know, don't try to make up an answer.
38
+ {context}
39
+ Question: {question}
40
+ When encountering abusive, offensive, or harmful language, such as fuck, bitch,etc, just politely ask the users to maintain appropriate behaviours.
41
+ Always make sure to elaborate your response and use vibrant, positive tone to represent good branding of the school.
42
+ Never answer with any unfinished response
43
+ Answer:
44
+ """
45
+ PROMPT = PromptTemplate(
46
+ template=prompt_template, input_variables=["context", "question"]
47
+ )
48
+ chain_type_kwargs = {"prompt": PROMPT}
49
+
50
+
51
+ st.set_page_config(
52
+ page_title = '👨‍⚖️Seon\'s Legal QA For Dummies ⚖️',
53
+ page_icon = '🕵')
54
+
55
+
56
+
57
+
58
+
59
+
60
+ @st.cache_resource
61
+ def load_llm_model():
62
+ # llm = HuggingFacePipeline.from_model_id(model_id= 'PyaeSoneK/LlamaV2LegalFineTuned',
63
+ # task= 'text2text-generation',
64
+ # model_kwargs={ "device_map": "auto",
65
+ # "load_in_8bit": True,"max_length": 256, "temperature": 0,
66
+ # "repetition_penalty": 1.5})
67
+
68
+
69
+ llm = HuggingFacePipeline.from_model_id(model_id= 'PyaeSoneK/LlamaV2LegalFineTuned',
70
+ task= 'text2text-generation',
71
+
72
+ model_kwargs={ "max_length": 256, "temperature": 0,
73
+ "torch_dtype":torch.float32,
74
+ "repetition_penalty": 1.3})
75
+ return llm
76
+
77
+
78
+ @st.cache_resource
79
+ def load_conversational_qa_memory_retriever():
80
+
81
+ question_generator = LLMChain(llm=llm_model, prompt=CONDENSE_QUESTION_PROMPT)
82
+ doc_chain = load_qa_chain(llm_model, chain_type="stuff", prompt = PROMPT)
83
+ memory = ConversationBufferWindowMemory(k = 3, memory_key="chat_history", return_messages=True, output_key='answer')
84
+
85
+
86
+
87
+ conversational_qa_memory_retriever = ConversationalRetrievalChain(
88
+ retriever=vector_database.as_retriever(),
89
+ question_generator=question_generator,
90
+ combine_docs_chain=doc_chain,
91
+ return_source_documents=True,
92
+ memory = memory,
93
+ get_chat_history=lambda h :h)
94
+ return conversational_qa_memory_retriever, question_generator
95
+
96
+ def load_retriever(llm, db):
97
+ qa_retriever = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff",
98
+ retriever=db.as_retriever(),
99
+ chain_type_kwargs= chain_type_kwargs)
100
+
101
+ return qa_retriever
102
+
103
+ def retrieve_document(query_input):
104
+ related_doc = vector_database.similarity_search(query_input)
105
+ return related_doc
106
+
107
+
108
+
109
+ def retrieve_answer():
110
+ prompt_answer= st.session_state.my_text_input
111
+ answer = qa_retriever.run(prompt_answer)
112
+ log = {"timestamp": datetime.datetime.now(),
113
+ "question":st.session_state.my_text_input,
114
+ "generated_answer": answer[6:],
115
+ "rating":0 }
116
+
117
+ st.session_state.history.append(log)
118
+ update_worksheet_qa()
119
+ st.session_state.chat_history.append({"message": st.session_state.my_text_input, "is_user": True})
120
+ st.session_state.chat_history.append({"message": answer[6:] , "is_user": False})
121
+
122
+ st.session_state.my_text_input = ""
123
+
124
+ return answer[6:] #this positional slicing helps remove "<pad> " at the beginning
125
+
126
+
127
+ def new_retrieve_answer():
128
+ prompt_answer= st.session_state.my_text_input + ". Try to be elaborate and informative in your answer."
129
+ answer = conversational_qa_memory_retriever({"question": prompt_answer, })
130
+ log = {"timestamp": datetime.datetime.now(),
131
+ "question":st.session_state.my_text_input,
132
+ "generated_answer": answer['answer'][6:],
133
+ "rating":0 }
134
+
135
+ print(f"condensed quesion : {question_generator.run({'chat_history': answer['chat_history'], 'question' : prompt_answer})}")
136
+
137
+ print(answer["chat_history"])
138
+ st.session_state.history.append(log)
139
+ update_worksheet_qa()
140
+ st.session_state.chat_history.append({"message": st.session_state.my_text_input, "is_user": True})
141
+ st.session_state.chat_history.append({"message": answer['answer'][6:] , "is_user": False})
142
+
143
+ st.session_state.my_text_input = ""
144
+
145
+ return answer['answer'][6:] #this positional slicing helps remove "<pad> " at the beginning
146
+
147
+ # def update_score():
148
+ # st.session_state.session_rating = st.session_state.rating
149
+
150
+
151
+ def update_worksheet_qa():
152
+ # st.session_state.session_rating = st.session_state.rating
153
+ #This if helps validate the initiated rating, if 0, then the google sheet would not be updated
154
+ #(edited) now even with the score of 0, we still want to store the log because some users do not give the score to complete the logging
155
+ # if st.session_state.session_rating == 0:
156
+ worksheet_qa.append_row([st.session_state.history[-1]['timestamp'].strftime(datetime_format),
157
+ st.session_state.history[-1]['question'],
158
+ st.session_state.history[-1]['generated_answer'],
159
+ 0])
160
+ # else:
161
+ # worksheet_qa.append_row([st.session_state.history[-1]['timestamp'].strftime(datetime_format),
162
+ # st.session_state.history[-1]['question'],
163
+ # st.session_state.history[-1]['generated_answer'],
164
+ # st.session_state.session_rating
165
+ # ])
166
+
167
+ def update_worksheet_comment():
168
+ worksheet_comment.append_row([datetime.datetime.now().strftime(datetime_format),
169
+ feedback_input])
170
+ success_message = st.success('Feedback successfully submitted, thank you', icon="✅",
171
+ )
172
+ time.sleep(3)
173
+ success_message.empty()
174
+
175
+
176
+ def clean_chat_history():
177
+ st.session_state.chat_history = []
178
+ conversational_qa_memory_retriever.memory.chat_memory.clear() #add this to remove
179
+
180
+ #--------------
181
+
182
+
183
+ if "history" not in st.session_state: #this one is for the google sheet logging
184
+ st.session_state.history = []
185
+
186
+
187
+ if "chat_history" not in st.session_state: #this one is to pass previous messages into chat flow
188
+ st.session_state.chat_history = []
189
+ # if "session_rating" not in st.session_state:
190
+ # st.session_state.session_rating = 0
191
+
192
+
193
+ credentials= json.loads(st.secrets['google_sheet_credential'])
194
+
195
+ service_account = gspread.service_account_from_dict(credentials)
196
+ workbook= service_account.open("aitGPT-qa-log")
197
+ worksheet_qa = workbook.worksheet("Sheet1")
198
+ worksheet_comment = workbook.worksheet("Sheet2")
199
+ datetime_format= "%Y-%m-%d %H:%M:%S"
200
+
201
+
202
+
203
+ load_scraped_web_info()
204
+ embedding_model = load_embedding_model()
205
+ vector_database = load_faiss_index()
206
+ llm_model = load_llm_model()
207
+ qa_retriever = load_retriever(llm= llm_model, db= vector_database)
208
+ conversational_qa_memory_retriever, question_generator = load_conversational_qa_memory_retriever()
209
+ print("all load done")
210
+
211
+
212
+ # Try adding this to set to clear the memory in each session
213
+ if st.session_state.chat_history == []:
214
+ conversational_qa_memory_retriever.memory.chat_memory.clear()
215
+ #Addional things for Conversation flows
216
+
217
+
218
+
219
+
220
+
221
+
222
+ st.write("# 🦜Legal QA For Dummies 🔗 ")
223
+ st.markdown("""
224
+ ####This Legal QA is designed for normal people trying to get the legal answers orbiting around in their life.
225
+ The goal of this chatbot is to provide answers and advice quick access information about Legality : Law and Regulations and What Ca
226
+ """)
227
+ st.write(' ⚠️ Please expect to wait **~ 10 - 20 seconds per question** as thi app is running on CPU against 3-billion-parameter LLM')
228
+
229
+ st.markdown("---")
230
+ st.write(" ")
231
+ st.write("""
232
+ ### ❔ Ask a question
233
+ """)
234
+
235
+
236
+ for chat in st.session_state.chat_history:
237
+ st_message(**chat)
238
+
239
+ query_input = st.text_input(label= 'Boraden Your General Legal Knowledge Here!' , key = 'my_text_input', on_change= new_retrieve_answer )
240
+ # generate_button = st.button(label = 'Ask question!')
241
+
242
+ # if generate_button:
243
+ # answer = retrieve_answer(query_input)
244
+ # log = {"timestamp": datetime.datetime.now(),
245
+ # "question":query_input,
246
+ # "generated_answer": answer,
247
+ # "rating":0 }
248
+
249
+ # st.session_state.history.append(log)
250
+ # update_worksheet_qa()
251
+ # st.session_state.chat_history.append({"message": query_input, "is_user": True})
252
+ # st.session_state.chat_history.append({"message": answer, "is_user": False})
253
+
254
+ # print(st.session_state.chat_history)
255
+
256
+
257
+ clear_button = st.button("Start new convo",
258
+ on_click=clean_chat_history)
259
+
260
+
261
+ st.write(" ")
262
+ st.write(" ")
263
+
264
+ st.markdown("---")
265
+ st.write("""
266
+ ### 💌 Your voice matters
267
+ """)
268
+
269
+ feedback_input = st.text_area(label= 'please leave your feedback or any ideas to make this bot more knowledgeable and fun')
270
+ feedback_button = st.button(label = 'Submit feedback!')
271
+
272
+ if feedback_button:
273
+ update_worksheet_comment()