Chananchida commited on
Commit
bf56fe9
·
verified ·
1 Parent(s): 2a9d006

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -76
app.py CHANGED
@@ -142,30 +142,8 @@ class Chatbot:
142
  similar_contexts = [self.df['Context'][indices[0][i]] for i in range(self.k)]
143
  return similar_questions, similar_contexts, distances, indices
144
 
145
- def predict_faiss(self, message):
146
- message = message.strip()
147
- question_vector = self.get_embeddings(message)
148
- question_vector = self.prepare_sentences_vector([question_vector])
149
- similar_questions, similar_contexts, distances, indices = self.faiss_search(question_vector)
150
- Answers = [self.df['Answer'][i] for i in indices[0]]
151
- Answer = Answers[0]
152
-
153
- return Answer
154
-
155
- def predict_faiss(self, message):
156
- message = message.strip()
157
- question_vector = self.get_embeddings([message])
158
- question_vector = self.prepare_sentences_vector([question_vector])
159
- similar_questions, similar_contexts, distances, indices = self.faiss_search(question_vector)
160
- Answers = [self.df['Answer'][i] for i in indices[0]]
161
- Answer = Answers[0]
162
-
163
- return Answer
164
-
165
-
166
 
167
- # Function to predict using BERT embedding
168
- def predict_bert_embedding(self,message):
169
  message = message.strip()
170
  question_vector = self.get_embeddings(message)
171
  question_vector=self.prepare_sentences_vector([question_vector])
@@ -173,62 +151,11 @@ class Chatbot:
173
  Answer = self.model_pipeline(similar_questions, similar_contexts)
174
  return Answer
175
 
176
- # def predict_semantic_search(self,message,corpus_embeddings):
177
- # message = message.strip()
178
- # query_embedding = self.embedding_model.encode(message, convert_to_tensor=True)
179
- # query_embedding = query_embedding.to('cpu')
180
- # hits = util.semantic_search(query_embedding, corpus_embeddings, top_k=1)
181
- # hit = hits[0][0]
182
- # context=self.df['Context'][hit['corpus_id']]
183
- # score="{:.4f})".format(hit['score'])
184
- # Answer = self.model_pipeline(message, context)
185
- # return Answer
186
- def predict_semantic_search(self, message):
187
- corpus_embeddings = bot._chatbot.prepare_sentences_vector(bot._chatbot.get_embeddings(bot._chatbot.df['Context']))
188
- message = message.strip()
189
- query_embedding = self.embedding_model.encode(message, convert_to_tensor=True)
190
- query_embedding = query_embedding.to('cuda')
191
- hits = util.semantic_search(query_embedding, corpus_embeddings, top_k=1)
192
- hit = hits[0][0]
193
- context = self.df['Context'][hit['corpus_id']]
194
- Answer = self.model_pipeline(message, context)
195
- return Answer
196
-
197
-
198
-
199
- def predict_without_faiss(self,message):
200
- MostSimilarContext = ""
201
- min_distance = 1000
202
- message = message.strip(' \t\n')
203
- question_vector = self.get_embeddings([message])
204
- question_vector=self.prepare_sentences_vector(question_vector)
205
- for j, _question_vector in enumerate(self.vectors):
206
- distance = euclidean_distances(question_vector, _question_vector.reshape(1, -1))[0][0]
207
- if distance < min_distance:
208
- min_distance = distance
209
- MostSimilarContext = self.df['Context'][j]
210
- similar_question = self.df['Question'][j]
211
- if distance <= 0.02469331026:
212
- break
213
- predict_answer = self.model_pipeline(message, MostSimilarContext)
214
- Answer = predict_answer.strip().replace("<unk>","@")
215
- return Answer
216
-
217
  bot = ChatbotModel()
218
 
219
  """#Gradio"""
220
 
221
 
222
- EXAMPLE_PATH = ["หลิน ไห่เฟิง มีชื่อเรียกอีกชื่อว่าอะไร" , "ใครเป็นผู้ตั้งสภาเศรษฐกิจโลกขึ้นในปี พ.ศ. 2514 โดยทุกปีจะมีการประชุมที่ประเทศสวิตเซอร์แลนด์", "โปรดิวเซอร์ของอัลบั้มตลอดกาล ของวงคีรีบูนคือใคร", "สกุลเดิมของหม่อมครูนุ่ม นวรัตน ณ อยุธยา คืออะไร"]
223
-
224
- # demoFaiss = gr.ChatInterface(fn=bot._chatbot.predict_faiss, examples=EXAMPLE_PATH)
225
- # demoBert = gr.ChatInterface(fn=bot._chatbot.predict_bert_embedding,examples=EXAMPLE_PATH)
226
- # demoSemantic = gr.ChatInterface(fn=bot._chatbot.predict_semantic_search,examples=EXAMPLE_PATH)
227
- # demoWithoutFiss = gr.ChatInterface(fn=bot._chatbot.predict_without_faiss,examples=EXAMPLE_PATH)
228
- demoFaiss = gr.Interface(fn=bot._chatbot.predict_faiss, inputs="text", outputs="text", title="TH wiki (just Faiss)")
229
- demoBert = gr.Interface(fn=bot._chatbot.predict_bert_embedding, inputs="text", outputs="text", title="TH wiki (Faiss & Model)")
230
- demoSemantic = gr.Interface(fn=bot._chatbot.predict_semantic_search, inputs="text", outputs="text", title="TH wiki (Semantic Search & Model)")
231
- demoWithoutFiss = gr.Interface(fn=bot._chatbot.predict_without_faiss, inputs="text", outputs="text", title="TH wiki (just Model)")
232
-
233
- demo = gr.TabbedInterface([demoFaiss, demoWithoutFiss, demoBert, demoSemantic], ["Faiss", "Model", "Faiss & Model", "Semantic Search & Model"])
234
  demo.launch()
 
142
  similar_contexts = [self.df['Context'][indices[0][i]] for i in range(self.k)]
143
  return similar_questions, similar_contexts, distances, indices
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
+ def predict(self,message):
 
147
  message = message.strip()
148
  question_vector = self.get_embeddings(message)
149
  question_vector=self.prepare_sentences_vector([question_vector])
 
151
  Answer = self.model_pipeline(similar_questions, similar_contexts)
152
  return Answer
153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  bot = ChatbotModel()
155
 
156
  """#Gradio"""
157
 
158
 
159
+ # EXAMPLE = ["หลิน ไห่เฟิง มีชื่อเรียกอีกชื่อว่าอะไร" , "ใครเป็นผู้ตั้งสภาเศรษฐกิจโลกขึ้นในปี พ.ศ. 2514 โดยทุกปีจะมีการประชุมที่ประเทศสวิตเซอร์แลนด์", "โปรดิวเซอร์ของอัลบั้มตลอดกาล ของวงคีรีบูนคือใคร", "สกุลเดิมของหม่อมครูนุ่ม นวรัตน ณ อยุธยา คืออะไร"]
160
+ demo = gr.Interface(fn=bot._chatbot.predict, inputs="text", outputs="text", title="CE66-04_Thai Question Answering System by using Deep Learning")
 
 
 
 
 
 
 
 
 
 
161
  demo.launch()