Chananchida commited on
Commit
adbb82c
·
verified ·
1 Parent(s): a9a7ef8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -32
app.py CHANGED
@@ -27,10 +27,10 @@ DEFAULT_MODEL = 'wangchanberta-hyp'
27
  DEFAULT_SENTENCE_EMBEDDING_MODEL = 'intfloat/multilingual-e5-base'
28
 
29
  MODEL_DICT = {
30
- 'wangchanberta': 'Chananchida/wangchanberta-th-wiki-qa_ref-params',
31
- 'wangchanberta-hyp': 'Chananchida/wangchanberta-th-wiki-qa_hyp-params',
32
- 'mdeberta': 'Chananchida/mdeberta-v3-th-wiki-qa_ref-params',
33
- 'mdeberta-hyp': 'Chananchida/mdeberta-v3-th-wiki-qa_hyp-params',
34
  }
35
 
36
  DATA_PATH = 'models/dataset.xlsx'
@@ -138,40 +138,26 @@ class Chatbot:
138
  similar_questions, similar_contexts, distances, indices = self.faiss_search(question_vector)
139
  context = similar_contexts[0]
140
  Answer = self.model_pipeline(str(message), context)
141
- start_index = context.find(Answer)
142
- end_index = start_index + len(Answer)
143
- output = {
144
- "user_question": message,
145
- "answer": self.df['Answer'][indices[0][0]],
146
- "distance": round(distances[0][0], 4),
147
- "highlight_start": start_index,
148
- "highlight_end": end_index
149
- }
150
- return output
151
-
152
-
153
- def highlight_text(text, start_index, end_index):
154
- if start_index < 0:
155
- start_index = 0
156
- if end_index > len(text):
157
- end_index = len(text)
158
- highlighted_text = ""
159
- for i, char in enumerate(text):
160
- if i == start_index:
161
- highlighted_text += "<mark>"
162
- highlighted_text += char
163
- if i == end_index - 1:
164
- highlighted_text += "</mark>"
165
- return highlighted_text
166
-
167
 
168
  if __name__ == "__main__":
169
  bot = ChatbotModel()
170
 
171
  def chat_interface(question, history):
172
  response = bot._chatbot.predict(question)
173
- highlighted_answer = highlight_text(response["answer"], response["highlight_start"], response["highlight_end"])
174
- return highlighted_answer
 
175
 
176
  EXAMPLE = ["หลิน ไห่เฟิง มีชื่อเรียกอีกชื่อว่าอะไร" , "ใครเป็นผู้ตั้งสภาเศรษฐกิจโลกขึ้นในปี พ.ศ. 2514 โดยทุกปีจะมีการประชุมที่ประเทศสวิตเซอร์แลนด์", "โปรดิวเซอร์ของอัลบั้มตลอดกาล ของวงคีรีบูนคือใคร", "สกุลเดิมของหม่อมครูนุ่ม นวรัตน ณ อยุธยา คืออะไร"]
177
 
 
27
  DEFAULT_SENTENCE_EMBEDDING_MODEL = 'intfloat/multilingual-e5-base'
28
 
29
  MODEL_DICT = {
30
+ 'wangchanberta': 'powerpuf-bot/wangchanberta-th-wiki-qa_ref-params',
31
+ 'wangchanberta-hyp': 'powerpuf-bot/wangchanberta-th-wiki-qa_hyp-params',
32
+ 'mdeberta': 'powerpuf-bot/mdeberta-v3-th-wiki-qa_ref-params',
33
+ 'mdeberta-hyp': 'powerpuf-bot/mdeberta-v3-th-wiki-qa_hyp-params',
34
  }
35
 
36
  DATA_PATH = 'models/dataset.xlsx'
 
138
  similar_questions, similar_contexts, distances, indices = self.faiss_search(question_vector)
139
  context = similar_contexts[0]
140
  Answer = self.model_pipeline(str(message), context)
141
+ # start_index = context.find(Answer)
142
+ # end_index = start_index + len(Answer)
143
+ # output = {
144
+ # "user_question": message,
145
+ # "answer": self.df['Answer'][indices[0][0]],
146
+ # "distance": round(distances[0][0], 4),
147
+ # "highlight_start": start_index,
148
+ # "highlight_end": end_index
149
+ # }
150
+ # return output
151
+ return Answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
  if __name__ == "__main__":
154
  bot = ChatbotModel()
155
 
156
  def chat_interface(question, history):
157
  response = bot._chatbot.predict(question)
158
+ # highlighted_answer = highlight_text(response["answer"], response["highlight_start"], response["highlight_end"])
159
+ # return highlighted_answer
160
+ return response
161
 
162
  EXAMPLE = ["หลิน ไห่เฟิง มีชื่อเรียกอีกชื่อว่าอะไร" , "ใครเป็นผู้ตั้งสภาเศรษฐกิจโลกขึ้นในปี พ.ศ. 2514 โดยทุกปีจะมีการประชุมที่ประเทศสวิตเซอร์แลนด์", "โปรดิวเซอร์ของอัลบั้มตลอดกาล ของวงคีรีบูนคือใคร", "สกุลเดิมของหม่อมครูนุ่ม นวรัตน ณ อยุธยา คืออะไร"]
163