trinity-tandon commited on
Commit
f3db198
·
verified ·
1 Parent(s): 28310b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -1
app.py CHANGED
@@ -1,11 +1,65 @@
1
  import gradio as gr
2
  import random
3
  from huggingface_hub import InferenceClient
 
 
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  client = InferenceClient('Qwen/Qwen2.5-72B-Instruct')
6
  #client is where you can change the LLM model!
7
  def respond(message,history):
8
- messages = [{'role': 'system', 'content': 'You give really good gift ideas and are super helpful! You also tell me the price of each item. Give me 5 gift ideas if I ask'}]
9
  if history:
10
  messages.extend(history)
11
 
 
1
  import gradio as gr
2
  import random
3
  from huggingface_hub import InferenceClient
4
+ from sentence_transformers import SentenceTransformer
5
+ import torch
6
 
7
+ with open("knowledge.txt", "r", encoding="utf-8") as file:
8
+ recent = file.read()
9
+ # opens the text, saves as "file"
10
+ # reads the text and saves as water_cycle_text variable
11
+
12
+ print(recent)
13
+
14
+ cleaned_text = recent.strip()
15
+ # cleaning up the text
16
+ chunks = cleaned_text.split("\n")
17
+ # seperating the text into one sentence pieces
18
+ cleaned_chunks = []
19
+ # creating an empty list to put the cleaned chunks in
20
+
21
+ for chunk in chunks:
22
+ stripped_chunk = chunk.strip()
23
+ if stripped_chunk:
24
+ cleaned_chunks.append(stripped_chunk)
25
+ # loop through chunks and add not empty chunks to cleaned_chunks list
26
+ print(cleaned_chunks)
27
+
28
+ model = SentenceTransformer('all-MiniLM-L6-v2')
29
+
30
+ chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
31
+ # encode the model, pass through my cleaned chunks and convert to vector embeddings (not arrays)
32
+ print(chunk_embeddings)
33
+
34
+ def get_top_chunks(query):
35
+ # create my function taking query as parameter
36
+ query_embedding = model.encode(query, convert_to_tensor=True)
37
+ # encode query to vector embedding for comparison
38
+ query_embedding_normalized = query_embedding / query_embedding.norm()
39
+ # normalize my query to 1; allows for comparison of meaning
40
+ chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
41
+ # normailizing chunks for comparison of meaning
42
+
43
+ similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
44
+ print(similarities)
45
+ # using matmul (matrix multiplication method) to compare query to chunks
46
+ top_indices = torch.topk(similarities, k=3).indices
47
+ print(top_indices)
48
+ # get the indices of the chunks that are most similar to query
49
+
50
+ top_chunks = []
51
+
52
+ for i in top_indices:
53
+ chunk = chunks[i]
54
+ # for each index number in top_indices, get back the text
55
+ top_chunks.append(chunk)
56
+ # values of each index number is added to top_chunks
57
+ return top_chunks
58
+
59
  client = InferenceClient('Qwen/Qwen2.5-72B-Instruct')
60
  #client is where you can change the LLM model!
61
  def respond(message,history):
62
+ messages = [{'role': 'system', 'content': 'You give really good gift ideas and are super helpful! You also tell me the price of each item. Give me 5 gift ideas if I ask}]
63
  if history:
64
  messages.extend(history)
65