Spaces:
Runtime error
Runtime error
fix indentation
Browse files
app.py
CHANGED
|
@@ -19,9 +19,9 @@ cleaned_chunks = []
|
|
| 19 |
|
| 20 |
#putting text in chunks
|
| 21 |
for chunk in chunks:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
#import model for embeddings
|
| 27 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
|
@@ -30,38 +30,40 @@ chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
|
|
| 30 |
|
| 31 |
def get_top_chunks(query):
|
| 32 |
# creating a function taking query as my parameter
|
| 33 |
-
|
| 34 |
# encode query to vector embedding for comparison
|
| 35 |
-
|
| 36 |
# normalize query to 1: allows for comparison of meaning
|
| 37 |
-
|
| 38 |
# normalizing chunks for comparison of meaning
|
| 39 |
-
|
| 40 |
-
print(similarities)
|
| 41 |
# using matmul (matrix multiplication) method to compare query to chunks
|
| 42 |
-
|
| 43 |
-
top_indices = torch.topk(similarities, k=3).indices
|
| 44 |
-
print(top_indices)
|
| 45 |
# get the indices of the chunks thart are most similar to my query
|
| 46 |
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
# for each index number in top_indices, get back the text
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
return top_chunks
|
| 55 |
|
| 56 |
def respond(message, history):
|
| 57 |
messages = [{"role": "system", "content": "You are a big sister chatbot named, Nessie. You help people feel better about their bodies and self-image."}]
|
| 58 |
# change the personality
|
| 59 |
context = get_top_chunks(message)
|
|
|
|
| 60 |
if history:
|
| 61 |
messages.extend(history)
|
| 62 |
messages.append({"role": "user", "content": message})
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
for messages in client.chat_completion(
|
| 66 |
messages,
|
| 67 |
max_tokens = 500,
|
|
|
|
| 19 |
|
| 20 |
#putting text in chunks
|
| 21 |
for chunk in chunks:
|
| 22 |
+
stripped_chunk = chunk.strip()
|
| 23 |
+
if stripped_chunk:
|
| 24 |
+
cleaned_chunks.append(stripped_chunk)
|
| 25 |
|
| 26 |
#import model for embeddings
|
| 27 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
|
|
|
| 30 |
|
| 31 |
def get_top_chunks(query):
|
| 32 |
# creating a function taking query as my parameter
|
| 33 |
+
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 34 |
# encode query to vector embedding for comparison
|
| 35 |
+
query_embedding_normalized = query_embedding / query_embedding.norm()
|
| 36 |
# normalize query to 1: allows for comparison of meaning
|
| 37 |
+
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
| 38 |
# normalizing chunks for comparison of meaning
|
| 39 |
+
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
|
|
|
|
| 40 |
# using matmul (matrix multiplication) method to compare query to chunks
|
| 41 |
+
top_indices = torch.topk(similarities, k=3).indices
|
|
|
|
|
|
|
| 42 |
# get the indices of the chunks thart are most similar to my query
|
| 43 |
|
| 44 |
+
top_chunks = []
|
| 45 |
|
| 46 |
+
for i in top_indices:
|
| 47 |
+
chunk = chunks[i]
|
| 48 |
# for each index number in top_indices, get back the text
|
| 49 |
+
top_chunks.append(chunk)
|
| 50 |
+
|
| 51 |
+
return top_chunks
|
| 52 |
|
|
|
|
| 53 |
|
| 54 |
def respond(message, history):
|
| 55 |
messages = [{"role": "system", "content": "You are a big sister chatbot named, Nessie. You help people feel better about their bodies and self-image."}]
|
| 56 |
# change the personality
|
| 57 |
context = get_top_chunks(message)
|
| 58 |
+
|
| 59 |
if history:
|
| 60 |
messages.extend(history)
|
| 61 |
messages.append({"role": "user", "content": message})
|
| 62 |
|
| 63 |
+
user_context = f"{message}\nInformation: {context}"
|
| 64 |
+
messages.append({"role": "user", "content": user_context})
|
| 65 |
+
|
| 66 |
+
response = ""
|
| 67 |
for messages in client.chat_completion(
|
| 68 |
messages,
|
| 69 |
max_tokens = 500,
|