noorulamean444 commited on
Commit
5b84f6f
1 Parent(s): 45cbcf8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -6
app.py CHANGED
@@ -2,6 +2,7 @@ import requests
2
  import os
3
  import gradio as gr
4
  import time
 
5
  from utils import package_installer
6
 
7
  package_installer('sentence_transformers')
@@ -23,13 +24,29 @@ def query(payload):
23
 
24
 
25
  def chat(message,history):
26
- chat_history = ''
27
- for chats in history:
28
- # formatted_assistant_msg = chats[1].replace(chats[0],'').strip().removesuffix('<|end|>')
29
- chat_history += chats[1]
30
-
31
  prompt = f"<|user|>\n{message}<|end|>\n<|assistant|>"
32
- user_input = chat_history + prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  inp_dict = {"inputs":user_input,
34
  "parameters": {"max_new_tokens":750,"temperature":0.01}}
35
  output = query(inp_dict)
 
2
  import os
3
  import gradio as gr
4
  import time
5
+ import heapq
6
  from utils import package_installer
7
 
8
  package_installer('sentence_transformers')
 
24
 
25
 
26
  def chat(message,history):
27
+
 
 
 
 
28
  prompt = f"<|user|>\n{message}<|end|>\n<|assistant|>"
29
+ chat_history = []
30
+
31
+ if history:
32
+
33
+ for chats in history:
34
+ chat_history.append(chats[1])
35
+
36
+ emb_prompt = embedding_model.encode(prompt,convert_to_tensor=True)
37
+ emb_chat_history = embedding_model.encode(chat_history,convert_to_tensor=True)
38
+ cosine_similarity_scores = util.cos_sim(emb_prompt,emb_chat_history)
39
+ top_2_scores = heapq.nlargest(2,cosine_similarity_scores[0])
40
+ top_2_chats = [chat_history[i] for i in sorted(list(cosine_similarity_scores[0]).index(val) for val in top_2_scores)]
41
+
42
+ similar_chat_history = ''
43
+ if chat_history:
44
+ for chats in chat_history:
45
+ # formatted_assistant_msg = chats[1].replace(chats[0],'').strip().removesuffix('<|end|>')
46
+ similar_chat_history += chats
47
+
48
+ #prompt = f"<|user|>\n{message}<|end|>\n<|assistant|>"
49
+ user_input = similar_chat_history + prompt
50
  inp_dict = {"inputs":user_input,
51
  "parameters": {"max_new_tokens":750,"temperature":0.01}}
52
  output = query(inp_dict)