Spaces:
Sleeping
Sleeping
Added resource 2
Browse files
app.py
CHANGED
|
@@ -24,17 +24,17 @@ model = SentenceTransformer('all-MiniLM-L6-v2')
|
|
| 24 |
|
| 25 |
def create_embeddings(text_chunks):
|
| 26 |
# Convert each text chunk into a vector embedding and store as a tensor
|
| 27 |
-
chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True)
|
| 28 |
# Return the chunk_embeddings
|
| 29 |
return chunk_embeddings
|
| 30 |
|
| 31 |
# Call the create_embeddings function and store the result in a new chunk_embeddings variable
|
| 32 |
-
chunk_embeddings = create_embeddings(cleaned_chunks)
|
| 33 |
|
| 34 |
|
| 35 |
def get_top_chunks(query, chunk_embeddings, text_chunks):
|
| 36 |
# Convert the query text into a vector embedding
|
| 37 |
-
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 38 |
|
| 39 |
# Normalize the query embedding to unit length for accurate similarity comparison
|
| 40 |
query_embedding_normalized = query_embedding / query_embedding.norm()
|
|
@@ -43,7 +43,7 @@ def get_top_chunks(query, chunk_embeddings, text_chunks):
|
|
| 43 |
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
| 44 |
|
| 45 |
# Calculate cosine similarity between query and all chunks using matrix multiplication
|
| 46 |
-
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
|
| 47 |
|
| 48 |
# Find the indices of the 3 chunks with highest similarity scores
|
| 49 |
top_indices = torch.topk(similarities, k=3).indices
|
|
@@ -82,6 +82,7 @@ def respond(message, history):
|
|
| 82 |
|
| 83 |
#EMMA'S PRACTICE EDITS#
|
| 84 |
url = "https://mentalhealthfirstaid.org/mental-health-resources/"
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
about_text = f"""
|
|
@@ -95,6 +96,8 @@ support tool.</p>
|
|
| 95 |
<p>All credits to the owner of the videos. We do not own any of the resources provided.
|
| 96 |
|
| 97 |
<p>Click <a href="{url}" target="_blank">Free Resources</a> to access Free Mental Health Resources.</p>
|
|
|
|
|
|
|
| 98 |
"""
|
| 99 |
|
| 100 |
with gr.Blocks() as demo:
|
|
|
|
| 24 |
|
| 25 |
def create_embeddings(text_chunks):
|
| 26 |
# Convert each text chunk into a vector embedding and store as a tensor
|
| 27 |
+
chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True)
|
| 28 |
# Return the chunk_embeddings
|
| 29 |
return chunk_embeddings
|
| 30 |
|
| 31 |
# Call the create_embeddings function and store the result in a new chunk_embeddings variable
|
| 32 |
+
chunk_embeddings = create_embeddings(cleaned_chunks)
|
| 33 |
|
| 34 |
|
| 35 |
def get_top_chunks(query, chunk_embeddings, text_chunks):
|
| 36 |
# Convert the query text into a vector embedding
|
| 37 |
+
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 38 |
|
| 39 |
# Normalize the query embedding to unit length for accurate similarity comparison
|
| 40 |
query_embedding_normalized = query_embedding / query_embedding.norm()
|
|
|
|
| 43 |
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
| 44 |
|
| 45 |
# Calculate cosine similarity between query and all chunks using matrix multiplication
|
| 46 |
+
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
|
| 47 |
|
| 48 |
# Find the indices of the 3 chunks with highest similarity scores
|
| 49 |
top_indices = torch.topk(similarities, k=3).indices
|
|
|
|
| 82 |
|
| 83 |
#EMMA'S PRACTICE EDITS#
|
| 84 |
url = "https://mentalhealthfirstaid.org/mental-health-resources/"
|
| 85 |
+
yt = "https://youtu.be/7CCTOvZH0KU?si=6G80QeaA4cKssFeX"
|
| 86 |
|
| 87 |
|
| 88 |
about_text = f"""
|
|
|
|
| 96 |
<p>All credits to the owner of the videos. We do not own any of the resources provided.
|
| 97 |
|
| 98 |
<p>Click <a href="{url}" target="_blank">Free Resources</a> to access Free Mental Health Resources.</p>
|
| 99 |
+
<p>Click <a href="{url}" target="_blank">here</a> to learn more about mental health.</p>
|
| 100 |
+
<p>You've got this! :) .</p>
|
| 101 |
"""
|
| 102 |
|
| 103 |
with gr.Blocks() as demo:
|