nibas commited on
Commit
207b9f8
·
verified ·
1 Parent(s): 2b5dd62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -12
app.py CHANGED
@@ -6,6 +6,7 @@ from huggingface_hub import InferenceClient
6
  import os
7
 
8
 
 
9
  st.set_page_config(layout="wide")
10
 
11
 
@@ -16,7 +17,7 @@ my_initial_rag_text = f"""This is a RAG (Retrieval-Augmented Generation) chatbot
16
  - Uses Streamlit for the web interface
17
  - Employs SentenceTransformer for generating embeddings
18
  - Uses HuggingFace's InferenceClient for LLM interaction
19
- - Has a default text about a training module on LLMs
20
 
21
  2. State Management:
22
  - Maintains several session state variables for:
@@ -128,13 +129,36 @@ By using the Software, you agree to the terms and conditions of the disclaimer."
128
  # Add a text area for RAG text input
129
  st.text_area(label="Please enter your RAG text here:", value=my_initial_rag_text, height=500, key="my_rag_text", on_change=delete_chat_messages)
130
 
 
 
 
 
 
 
 
131
  # Check if the sentences are not already in the session state
132
  if "my_sentences" not in st.session_state:
133
- my_sentences_split = st.session_state["my_rag_text"].split("\n")
134
- st.session_state["my_sentences"] = []
135
- for my_sentence in my_sentences_split:
136
- if my_sentence.strip():
137
- st.session_state["my_sentences"].append(my_sentence.strip())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
 
139
  # Check if the embeddings are not already in the session state
140
  if "my_embeddings" not in st.session_state:
@@ -166,19 +190,21 @@ with column_2:
166
  similarity_to_question = cosine_similarity(my_question_embedding, st.session_state.my_embeddings).flatten()
167
 
168
  # Number of sentences to keep based on similarity
169
- nof_keep_sentences = 10
170
 
171
  # Get the indices of the top similar sentences
172
- sorted_indices = similarity_to_question.argsort()[::-1][:nof_keep_sentences]
173
 
174
  # Retrieve the top similar sentences
175
- sorted_sentences = [st.session_state.my_sentences[i] for i in sorted_indices]
176
 
177
  # Construct the augmented prompt with the similar sentences
178
- augmented_prompt = "Here is the context:"
179
  for sentence in sorted_sentences:
180
  augmented_prompt += "\n\n" + 20*"-" + f"\n\n{sentence}"
181
- augmented_prompt += "\n\n" + 20*"-" + "\n\n" + "The user said:" + f"\n\n{prompt}"
 
 
182
 
183
  # Display the user's prompt in the chat container with a specific avatar
184
  messages_container.chat_message("user", avatar=":material/psychology_alt:").markdown(prompt)
@@ -204,8 +230,12 @@ with column_2:
204
 
205
 
206
  # Display the chat messages history
207
- st.write("Messages History:")
208
  st.json(st.session_state["my_chat_messages"], expanded=False)
209
  # Display the augmented prompt used for generating the response
210
  st.write("Augmented prompt:")
211
  st.json({"augmented_prompt": augmented_prompt}, expanded=False)
 
 
 
 
 
6
  import os
7
 
8
 
9
+
10
  st.set_page_config(layout="wide")
11
 
12
 
 
17
  - Uses Streamlit for the web interface
18
  - Employs SentenceTransformer for generating embeddings
19
  - Uses HuggingFace's InferenceClient for LLM interaction
20
+ - Has a default text about a GRNET training module on LLMs
21
 
22
  2. State Management:
23
  - Maintains several session state variables for:
 
129
  # Add a text area for RAG text input
130
  st.text_area(label="Please enter your RAG text here:", value=my_initial_rag_text, height=500, key="my_rag_text", on_change=delete_chat_messages)
131
 
132
+
133
+
134
+
135
+ # Define parameters
136
+ min_window_size = 5 # number of sentences per chunk
137
+ max_window_size = 10 # number of sentences per chunk
138
+ print(100*"-")
139
  # Check if the sentences are not already in the session state
140
  if "my_sentences" not in st.session_state:
141
+ text = st.session_state["my_rag_text"]
142
+
143
+ # Split only on .?!;: followed by space OR on \n
144
+ pattern = r'(?<=[.?!;:])\s+|\n'
145
+ sentence_split = re.split(pattern, text)
146
+
147
+ sentences = [s.strip() for s in sentence_split if s.strip()]
148
+
149
+ # Rolling window: include partial windows at end
150
+ chunks = []
151
+ for rolling_window_size in range(min_window_size, max_window_size+1):
152
+ for i in range(0, len(sentences)-rolling_window_size+1):
153
+ chunk = " ".join(sentences[i:i+rolling_window_size]).strip()
154
+ if chunk:
155
+ chunks.append(chunk)
156
+ print(f"*****{chunk}*****\n")
157
+
158
+ st.session_state["my_sentences"] = chunks
159
+ print(len(st.session_state["my_sentences"]))
160
+
161
+
162
 
163
  # Check if the embeddings are not already in the session state
164
  if "my_embeddings" not in st.session_state:
 
190
  similarity_to_question = cosine_similarity(my_question_embedding, st.session_state.my_embeddings).flatten()
191
 
192
  # Number of sentences to keep based on similarity
193
+ nof_keep_sentences = 3
194
 
195
  # Get the indices of the top similar sentences
196
+ sorted_indices = similarity_to_question.argsort()[::-1][:nof_keep_sentences][::-1]
197
 
198
  # Retrieve the top similar sentences
199
+ sorted_sentences = ["Importance: " + str(round(100*similarity_to_question[i])) + f"% {5*'>'} " + st.session_state.my_sentences[i] for i in sorted_indices]
200
 
201
  # Construct the augmented prompt with the similar sentences
202
+ augmented_prompt = "Here is my context:"
203
  for sentence in sorted_sentences:
204
  augmented_prompt += "\n\n" + 20*"-" + f"\n\n{sentence}"
205
+ augmented_prompt += "\n\n" + 20*"-" + "\n\n" + "use the context I gave you to reply on the following:"
206
+ augmented_prompt += "\n\n" + f"\n\n{prompt}"
207
+
208
 
209
  # Display the user's prompt in the chat container with a specific avatar
210
  messages_container.chat_message("user", avatar=":material/psychology_alt:").markdown(prompt)
 
230
 
231
 
232
  # Display the chat messages history
233
+ st.write("Messages History All:")
234
  st.json(st.session_state["my_chat_messages"], expanded=False)
235
  # Display the augmented prompt used for generating the response
236
  st.write("Augmented prompt:")
237
  st.json({"augmented_prompt": augmented_prompt}, expanded=False)
238
+
239
+
240
+
241
+