NaimaAqeel commited on
Commit
9afffa7
·
verified ·
1 Parent(s): 9599ad9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -0
app.py CHANGED
@@ -73,6 +73,10 @@ else:
73
  pickle.dump(index, f)
74
  print("Created new FAISS index and saved to faiss_index.pkl")
75
 
 
 
 
 
76
  def upload_files(files):
77
  global index, document_texts
78
  try:
@@ -89,6 +93,7 @@ def upload_files(files):
89
 
90
  # Process the text and update FAISS index
91
  sentences = text.split("\n")
 
92
  embeddings = embedding_model.encode(sentences)
93
  print(f"Embeddings shape: {embeddings.shape}") # Debug: Show the shape of the embeddings
94
  index.add(np.array(embeddings))
@@ -125,6 +130,10 @@ def query_text(text):
125
  top_documents.append(document_texts[idx]) # Append the actual sentences for the response
126
  else:
127
  print(f"Invalid index found: {idx}")
 
 
 
 
128
  return top_documents
129
  except Exception as e:
130
  print(f"Error querying text: {e}")
@@ -161,6 +170,7 @@ demo.launch()
161
 
162
 
163
 
 
164
 
165
 
166
 
 
73
  pickle.dump(index, f)
74
  print("Created new FAISS index and saved to faiss_index.pkl")
75
 
76
+ def preprocess_text(text):
77
+ # Add more preprocessing steps if necessary
78
+ return text.strip()
79
+
80
  def upload_files(files):
81
  global index, document_texts
82
  try:
 
93
 
94
  # Process the text and update FAISS index
95
  sentences = text.split("\n")
96
+ sentences = [preprocess_text(sentence) for sentence in sentences if sentence.strip()]
97
  embeddings = embedding_model.encode(sentences)
98
  print(f"Embeddings shape: {embeddings.shape}") # Debug: Show the shape of the embeddings
99
  index.add(np.array(embeddings))
 
130
  top_documents.append(document_texts[idx]) # Append the actual sentences for the response
131
  else:
132
  print(f"Invalid index found: {idx}")
133
+
134
+ # Remove duplicates
135
+ top_documents = list(dict.fromkeys(top_documents))
136
+
137
  return top_documents
138
  except Exception as e:
139
  print(f"Error querying text: {e}")
 
170
 
171
 
172
 
173
+
174
 
175
 
176