Chananchida commited on
Commit
0f46926
·
verified ·
1 Parent(s): 8b04727

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -25
app.py CHANGED
@@ -94,42 +94,30 @@ def predict_test(model, tokenizer, embedding_model, context, question, index):
94
  question_vector = prepare_sentences_vector([question_vector])
95
  distances, indices = faiss_search(index, question_vector, 3) # Retrieve top 3 indices
96
 
97
- # most_similar_contexts = []
98
  most_similar_contexts = ''
99
  for i in range(3): # Loop through top 3 indices
100
  most_sim_context = context[indices[0][i]].strip()
101
  # most_similar_contexts.append(most_sim_context)
102
  most_similar_contexts += str(i)+': '+most_sim_context + "\n\n"
103
 
104
- _time = time.time() - t
105
- output = {
106
- "user_question": question,
107
- "answer": most_similar_contexts,
108
- # "answer": Answer,
109
- "totaltime": round(_time, 3),
110
- "distance": round(distances[0][0], 4)
111
- }
112
- # print('\nAnswer:',Answer)
113
-
114
  return most_similar_contexts
115
 
116
- def chat_interface(question, history):
117
- response = predict_test(model, tokenizer, embedding_model, context, question, index)
118
- return response
119
-
120
- examples=['ภูมิทัศน์สื่อไทยในปี 2567 มีแนวโน้มว่า ',
121
- 'Fragmentation คือ',
122
- 'ติ๊กต๊อก คือ',
123
- 'รายงานจาก Reuters Institute'
124
- ]
125
- interface = gr.ChatInterface(fn=chat_interface,
126
- examples=examples)
127
 
128
 
129
  if __name__ == "__main__":
130
- # Load your model, tokenizer, data, and index here...
131
- # model, tokenizer = load_model('wangchanberta-hyp')
132
  embedding_model = load_embedding_model()
133
- # df = load_data()
134
  index = set_index(prepare_sentences_vector(get_embeddings(embedding_model, context)))
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  interface.launch()
 
94
  question_vector = prepare_sentences_vector([question_vector])
95
  distances, indices = faiss_search(index, question_vector, 3) # Retrieve top 3 indices
96
 
 
97
  most_similar_contexts = ''
98
  for i in range(3): # Loop through top 3 indices
99
  most_sim_context = context[indices[0][i]].strip()
100
  # most_similar_contexts.append(most_sim_context)
101
  most_similar_contexts += str(i)+': '+most_sim_context + "\n\n"
102
 
 
 
 
 
 
 
 
 
 
 
103
  return most_similar_contexts
104
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
 
107
  if __name__ == "__main__":
 
 
108
  embedding_model = load_embedding_model()
 
109
  index = set_index(prepare_sentences_vector(get_embeddings(embedding_model, context)))
110
+
111
+ def chat_interface(question, history):
112
+ response = predict_test(model, tokenizer, embedding_model, context, question, index)
113
+ return response
114
+
115
+ examples=['ภูมิทัศน์สื่อไทยในปี 2567 มีแนวโน้มว่า ',
116
+ 'Fragmentation คือ',
117
+ 'ติ๊กต๊อก คือ',
118
+ 'รายงานจาก Reuters Institute'
119
+ ]
120
+ interface = gr.ChatInterface(fn=chat_interface,
121
+ examples=examples)
122
+
123
  interface.launch()