mingbaer commited on
Commit
dcae35d
·
verified ·
1 Parent(s): ca7b7b5

Attempting to debug

Browse files
Files changed (1) hide show
  1. app.py +7 -9
app.py CHANGED
@@ -12,20 +12,18 @@ import torch
12
  import numpy as np
13
 
14
  # upload knowledge base - from sentiment analysis lab
15
- with open("essay_writing.txt", "r", encoding="utf-8") as file:
16
- essay_writing = file.read()
17
 
18
  # split the text into chunks
19
- cleaned_text = essay_writing.strip()
20
- chunks = cleaned_text.split("\n")
21
- cleaned_chunks = [chunk.strip() for chunk in chunks if chunk.strip()]
22
 
23
  # load an embedding model
24
  model = SentenceTransformer('all-MiniLM-L6-v2')
25
 
26
  chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
27
 
28
- def pull_relevant_info(query):
29
  query_embedding = model.encode(query, convert_to_tensor=True)
30
  query_embedding_normalized = query_embedding / query_embedding.norm()
31
 
@@ -33,9 +31,9 @@ def pull_relevant_info(query):
33
 
34
  similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
35
 
36
- top_indices = torch.topk(similarities, k=3).indices.cpu().numpy()
37
 
38
- relevant_info = "\n".join([chunks[i] for i in top_indices])
39
 
40
  return relevant_info
41
 
@@ -43,7 +41,7 @@ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
43
 
44
  def respond(message, history):
45
 
46
- info = pull_relevant_info(message)
47
  system_message = (f"You are a friendly chatbot. Use the following information to help answer the user's question:\n{info}\n")
48
  messages = [{"role": "system", "content": system_message}]
49
 
 
12
  import numpy as np
13
 
14
  # upload knowledge base - from sentiment analysis lab
15
+ with open("essay_writing.txt", "r", encoding="utf-8") as f:
16
+ essay_writing = f.read()
17
 
18
  # split the text into chunks
19
+ cleaned_chunks = [chunk.strip() for chunk in essay_writing.split("\n\n") if chunk.strip()]
 
 
20
 
21
  # load an embedding model
22
  model = SentenceTransformer('all-MiniLM-L6-v2')
23
 
24
  chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
25
 
26
+ def pull_relevant_info(query, top_k=3):
27
  query_embedding = model.encode(query, convert_to_tensor=True)
28
  query_embedding_normalized = query_embedding / query_embedding.norm()
29
 
 
31
 
32
  similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
33
 
34
+ top_indices = torch.topk(similarities, k=top_k).indices.cpu().numpy()
35
 
36
+ relevant_info = "\n\n".join([chunks[i] for i in top_indices])
37
 
38
  return relevant_info
39
 
 
41
 
42
  def respond(message, history):
43
 
44
+ info = pull_relevant_info(message, top_k=3)
45
  system_message = (f"You are a friendly chatbot. Use the following information to help answer the user's question:\n{info}\n")
46
  messages = [{"role": "system", "content": system_message}]
47