Spaces:
Sleeping
Sleeping
File size: 3,750 Bytes
5526ddd 31e97db 5526ddd be74022 c77caa2 be74022 5526ddd 2ce2cd5 02148b2 5b9f800 31e97db d4985aa 646af15 2663344 31e97db ede7b94 7dc6219 1f60008 cb6ad1f 140cf14 cb6ad1f c1c3407 cb6ad1f 1f60008 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | import gradio as gr
from huggingface_hub import InferenceClient
import os
client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct", token=os.environ.get("HF"))
from sentence_transformers import SentenceTransformer
import torch
with open("knowledge.txt", "r", encoding="utf-8") as file:
knowledge_text = file.read()
def preprocess_text(text):
cleaned_text = text.strip()
chunks = cleaned_text.split("\n")
cleaned_chunks = []
for chunk in chunks:
stripped_chunk = chunk.strip()
if len(stripped_chunk) > 0:
cleaned_chunks.append(stripped_chunk)
return cleaned_chunks
cleaned_chunks = preprocess_text(knowledge_text)
model = SentenceTransformer('all-MiniLM-L6-v2')
def create_embeddings(text_chunks):
# Convert each text chunk into a vector embedding and store as a tensor
chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True) # Replace ... with the cleaned_chunks list
# Return the chunk_embeddings
return chunk_embeddings
# Call the create_embeddings function and store the result in a new chunk_embeddings variable
chunk_embeddings = create_embeddings(cleaned_chunks) #complete this line
def get_top_chunks(query, chunk_embeddings, text_chunks):
# Convert the query text into a vector embedding
query_embedding = model.encode(query, convert_to_tensor=True) # Complete this line
# Normalize the query embedding to unit length for accurate similarity comparison
query_embedding_normalized = query_embedding / query_embedding.norm()
# Normalize all chunk embeddings to unit length for consistent comparison
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
# Calculate cosine similarity between query and all chunks using matrix multiplication
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) # Complete this line
# Find the indices of the 3 chunks with highest similarity scores
top_indices = torch.topk(similarities, k=3).indices
# Create an empty list to store the most relevant chunks
top_chunks = []
# Loop through the top indices and retrieve the corresponding text chunks
# This is only one way scholars may write this, but there are other ways!
for i in top_indices:
chunk = text_chunks[i]
top_chunks.append(chunk)
# Return the list of most relevant chunks
return top_chunks
def respond(message, history):
messages = [{"role": "system",
"content":"You are an emotional support chatbot. You would not take about anything else other than mental health and helping the users. You need to make sure the user is comfortable."
}]
if history:
messages.extend(history)
messages.append({"role":"user",
"content":message
})
response = " "
for msg in client.chat_completion(messages, max_tokens = 1000, temperature = 1, top_p = 0.5, stream = True):
token = msg.choices[0].delta.content
response += token
yield response
#EMMA'S PRACTICE EDITS#
about_text = """
## About this bot
Welcome to Mind Matters, an online resource that reminds *your that your mind matters*
Disclaimer; Mind Matters should not be used as an alternative to seeking professional help. I am simply a support tool.
"""
with gr.Blocks() as demo:
with gr.Row():
with gr.Column(scale=1):
gr.Markdown(about_text)
with gr.Column(scale=2):
gr.ChatInterface(fn=respond, title = "Mind Matters", description = "Always here to help", editable = True)
demo.launch()
chatbot = gr.ChatInterface(fn=respond, title = "Mind Matters", description = "Always here to help", editable = True)
chatbot.launch()
|