Files changed (1) hide show
  1. app.py +71 -51
app.py CHANGED
@@ -1,11 +1,14 @@
1
  import gradio as gr
2
-
3
  from huggingface_hub import InferenceClient
4
  import os
5
- client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct", token=os.environ.get("HF"))
6
  from sentence_transformers import SentenceTransformer
7
  import torch
8
 
 
 
 
 
 
9
  with open("knowledge.txt", "r", encoding="utf-8") as file:
10
  knowledge_text = file.read()
11
 
@@ -13,90 +16,107 @@ def preprocess_text(text):
13
  cleaned_text = text.strip()
14
  chunks = cleaned_text.split("\n")
15
  cleaned_chunks = []
 
16
  for chunk in chunks:
17
  stripped_chunk = chunk.strip()
18
  if len(stripped_chunk) > 0:
19
  cleaned_chunks.append(stripped_chunk)
 
20
  return cleaned_chunks
 
21
  cleaned_chunks = preprocess_text(knowledge_text)
22
 
23
- 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) # Replace ... with the cleaned_chunks list
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) #complete this line
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) # Complete this line
38
-
39
- # Normalize the query embedding to unit length for accurate similarity comparison
40
- query_embedding_normalized = query_embedding / query_embedding.norm()
41
 
42
- # Normalize all chunk embeddings to unit length for consistent comparison
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) # Complete this line
47
 
48
- # Find the indices of the 3 chunks with highest similarity scores
49
- top_indices = torch.topk(similarities, k=3).indices
50
 
 
51
 
52
- # Create an empty list to store the most relevant chunks
53
- top_chunks = []
 
54
 
55
- # Loop through the top indices and retrieve the corresponding text chunks
56
- # This is only one way scholars may write this, but there are other ways!
57
- for i in top_indices:
58
- chunk = text_chunks[i]
59
- top_chunks.append(chunk)
60
 
 
 
 
 
 
 
 
61
 
62
- # Return the list of most relevant chunks
63
- return top_chunks
64
 
 
65
 
 
66
 
67
- def respond(message, history):
68
- messages = [{"role": "system",
69
- "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."
70
- }]
71
- if history:
72
- messages.extend(history)
73
- messages.append({"role":"user",
74
- "content":message
75
- })
76
- response = " "
77
- for msg in client.chat_completion(messages, max_tokens = 1000, temperature = 1, top_p = 0.5, stream = True):
78
  token = msg.choices[0].delta.content
79
- response += token
80
- yield response
81
-
82
 
83
- #EMMA'S PRACTICE EDITS#
84
  about_text = """
85
  ## About this bot
86
- Welcome to Mind Matters, an online resource that reminds *your that your mind matters*
87
- Disclaimer; Mind Matters should not be used as an alternative to seeking professional help. I am simply a support tool.
 
 
88
  """
89
 
90
- with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
91
  with gr.Row():
92
  with gr.Column(scale=1):
93
  gr.Markdown(about_text)
94
 
95
  with gr.Column(scale=2):
96
- gr.ChatInterface(fn=respond, title = "Mind Matters", description = "Always here to help", editable = True)
 
 
 
 
 
97
 
98
  demo.launch()
99
 
100
 
101
- chatbot = gr.ChatInterface(fn=respond, title = "Mind Matters", description = "Always here to help", editable = True)
102
- chatbot.launch()
 
 
 
 
 
 
 
1
  import gradio as gr
 
2
  from huggingface_hub import InferenceClient
3
  import os
 
4
  from sentence_transformers import SentenceTransformer
5
  import torch
6
 
7
+ client = InferenceClient(
8
+ model="Qwen/Qwen2.5-7B-Instruct",
9
+ token=os.environ.get("HF")
10
+ )
11
+
12
  with open("knowledge.txt", "r", encoding="utf-8") as file:
13
  knowledge_text = file.read()
14
 
 
16
  cleaned_text = text.strip()
17
  chunks = cleaned_text.split("\n")
18
  cleaned_chunks = []
19
+
20
  for chunk in chunks:
21
  stripped_chunk = chunk.strip()
22
  if len(stripped_chunk) > 0:
23
  cleaned_chunks.append(stripped_chunk)
24
+
25
  return cleaned_chunks
26
+
27
  cleaned_chunks = preprocess_text(knowledge_text)
28
 
29
+ model = SentenceTransformer("all-MiniLM-L6-v2")
30
 
31
  def create_embeddings(text_chunks):
32
+ chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True)
33
+ return chunk_embeddings
 
 
 
 
 
34
 
35
+ chunk_embeddings = create_embeddings(cleaned_chunks)
36
 
37
  def get_top_chunks(query, chunk_embeddings, text_chunks):
38
+ query_embedding = model.encode(query, convert_to_tensor=True)
 
 
 
 
39
 
40
+ query_embedding_normalized = query_embedding / query_embedding.norm()
41
+ chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
42
 
43
+ similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
 
44
 
45
+ top_indices = torch.topk(similarities, k=3).indices
 
46
 
47
+ top_chunks = []
48
 
49
+ for i in top_indices:
50
+ chunk = text_chunks[i.item()]
51
+ top_chunks.append(chunk)
52
 
53
+ return top_chunks
 
 
 
 
54
 
55
+ def respond(message, history):
56
+ messages = [
57
+ {
58
+ "role": "system",
59
+ "content": "You are an emotional support chatbot. Do not talk about anything else other than mental health and helping the user. Make sure the user feels comfortable."
60
+ }
61
+ ]
62
 
63
+ if history:
64
+ messages.extend(history)
65
 
66
+ messages.append({"role": "user", "content": message})
67
 
68
+ response = ""
69
 
70
+ for msg in client.chat_completion(
71
+ messages,
72
+ max_tokens=1000,
73
+ temperature=1,
74
+ top_p=0.5,
75
+ stream=True
76
+ ):
 
 
 
 
77
  token = msg.choices[0].delta.content
78
+ if token:
79
+ response += token
80
+ yield response
81
 
 
82
  about_text = """
83
  ## About this bot
84
+
85
+ Welcome to Mind Matters, an online resource that reminds you that your mind matters.
86
+
87
+ Disclaimer: Mind Matters should not be used as an alternative to seeking professional help. It is simply a support tool.
88
  """
89
 
90
+ custom_theme = gr.themes.Soft(
91
+ primary_hue="pink",
92
+ secondary_hue="fuchsia",
93
+ neutral_hue="gray",
94
+ spacing_size="lg",
95
+ radius_size="lg",
96
+ text_size="lg"
97
+ )
98
+
99
+ with gr.Blocks(theme=custom_theme) as demo:
100
  with gr.Row():
101
  with gr.Column(scale=1):
102
  gr.Markdown(about_text)
103
 
104
  with gr.Column(scale=2):
105
+ gr.ChatInterface(
106
+ fn=respond,
107
+ title="Mind Matters",
108
+ description="Always here to help",
109
+ editable=True
110
+ )
111
 
112
  demo.launch()
113
 
114
 
115
+ custom_theme = gr.themes.Soft(
116
+ primary_hue="pink",
117
+ secondary_hue="fuchsia",
118
+ neutral_hue="gray"
119
+ )
120
+
121
+ with gr.Blocks(theme=custom_theme) as demo:
122
+