nclbond commited on
Commit
0a49502
·
verified ·
1 Parent(s): 65fa600

Modified the code to include the points in extended description

Browse files

load/process of knowledge base, chunked knowledge base and converted to embeddings, converted user's query into embedding to calculate similarity scores

Files changed (1) hide show
  1. app.py +58 -1
app.py CHANGED
@@ -1,9 +1,65 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  def respond(message, history):
5
 
6
- messages = [{"role": "system", "content": "You are a friendly chatbot."}]
7
 
8
  if history:
9
  messages.extend(history)
@@ -17,6 +73,7 @@ def respond(message, history):
17
 
18
  return response.choices[0].message.content.strip()
19
 
 
20
  chatbot = gr.ChatInterface(respond)
21
 
22
  chatbot.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ with open("knowledgebase.txt", "r", encoding="utf-8") as file:
5
+ knowledgebase_text = file.read()
6
+
7
+ def preprocess_text(text):
8
+
9
+ cleaned_text = text.strip()
10
+
11
+ chunks = cleaned_text.split("\n")
12
+
13
+ # empty list where cleaned chunks will be stored
14
+ cleaned_chunks = []
15
+
16
+ # cleans chunks and adds to our list of cleaned chunks
17
+ for chunk in chunks:
18
+ strip_chunk = chunk.strip()
19
+ if strip_chunk:
20
+ cleaned_chunks.append(strip_chunk)
21
+
22
+ return cleaned_chunks
23
+
24
+ model = SentenceTransformer('all-MiniLM-L6-v2')
25
+
26
+ # converts text chunks into vector embedding and stores as tensor for calculations
27
+ def create_embeddings(cleaned_chunks):
28
+
29
+ chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
30
+
31
+ return chunk_embeddings
32
+
33
+ chunk_embeddings = create_embeddings(cleaned_chunks)
34
+
35
+ def get_top_chunks (query, chunk_embeddings, cleaned_chunks):
36
+
37
+ # converts query text into vector embedding
38
+ query_embedding = model.encode(query, convert_to_tensor=True)
39
+
40
+ query_embedding_normalized = query_embedding/query_embedding.norm()
41
+
42
+ chunk_embeddings_normalized = chunk_embeddings/chunk_embeddings.norm(dim=1, keepdim=True)
43
+
44
+ similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
45
+
46
+ top_indices = torch.topk(similarities, k=3).indices
47
+
48
+ top_chunks = []
49
+
50
+ for i in top_indices:
51
+ chunk = cleaned_chunks[i]
52
+ top_chunks.append(chunk)
53
+
54
+ return top_chunks
55
+
56
+ top_results = get_top_chunks("How does an Aquarius show up in their family?", chunk_embeddings, cleaned_chunks)
57
+
58
+
59
+
60
  def respond(message, history):
61
 
62
+ messages = [{"role": "system", "content": "You are a clairvoyant chatbot with vast knowledge on zodiac signs, but you also give the information very clearly so users can understand the message you're conveying."}]
63
 
64
  if history:
65
  messages.extend(history)
 
73
 
74
  return response.choices[0].message.content.strip()
75
 
76
+
77
  chatbot = gr.ChatInterface(respond)
78
 
79
  chatbot.launch()