NaimaAqeel commited on
Commit
6e6d28c
·
verified ·
1 Parent(s): ba470cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -16
app.py CHANGED
@@ -56,15 +56,14 @@ document_texts_path = "document_texts.pkl"
56
 
57
  document_texts = []
58
 
59
- if os.path.exists(index_path):
60
  try:
61
  with open(index_path, "rb") as f:
62
  index = pickle.load(f)
63
  print("Loaded FAISS index from faiss_index.pkl")
64
- if os.path.exists(document_texts_path):
65
- with open(document_texts_path, "rb") as f:
66
- document_texts = pickle.load(f)
67
- print("Loaded document texts from document_texts.pkl")
68
  except Exception as e:
69
  print(f"Error loading FAISS index or document texts: {e}")
70
  else:
@@ -76,8 +75,8 @@ else:
76
 
77
  def upload_files(files):
78
  global index, document_texts
79
- for file in files:
80
- try:
81
  content = file.read()
82
  if file.name.endswith('.pdf'):
83
  with open("temp.pdf", "wb") as f:
@@ -95,23 +94,19 @@ def upload_files(files):
95
  embeddings = embedding_model.encode(sentences)
96
  index.add(np.array(embeddings))
97
  document_texts.append(text)
98
- except Exception as e:
99
- print(f"Error processing file {file.name}: {e}")
100
- return f"Error processing file {file.name}: {e}"
101
 
102
- # Save the updated index and documents
103
- try:
104
  with open(index_path, "wb") as f:
105
  pickle.dump(index, f)
106
  print("Saved updated FAISS index to faiss_index.pkl")
107
  with open(document_texts_path, "wb") as f:
108
  pickle.dump(document_texts, f)
109
  print("Saved updated document texts to document_texts.pkl")
 
 
110
  except Exception as e:
111
- print(f"Error saving FAISS index or document texts: {e}")
112
- return f"Error saving FAISS index or document texts: {e}"
113
-
114
- return "Files processed successfully"
115
 
116
  def query_text(text):
117
  try:
@@ -152,6 +147,7 @@ demo.launch()
152
 
153
 
154
 
 
155
 
156
 
157
 
 
56
 
57
  document_texts = []
58
 
59
+ if os.path.exists(index_path) and os.path.exists(document_texts_path):
60
  try:
61
  with open(index_path, "rb") as f:
62
  index = pickle.load(f)
63
  print("Loaded FAISS index from faiss_index.pkl")
64
+ with open(document_texts_path, "rb") as f:
65
+ document_texts = pickle.load(f)
66
+ print("Loaded document texts from document_texts.pkl")
 
67
  except Exception as e:
68
  print(f"Error loading FAISS index or document texts: {e}")
69
  else:
 
75
 
76
  def upload_files(files):
77
  global index, document_texts
78
+ try:
79
+ for file in files:
80
  content = file.read()
81
  if file.name.endswith('.pdf'):
82
  with open("temp.pdf", "wb") as f:
 
94
  embeddings = embedding_model.encode(sentences)
95
  index.add(np.array(embeddings))
96
  document_texts.append(text)
 
 
 
97
 
98
+ # Save the updated index and documents
 
99
  with open(index_path, "wb") as f:
100
  pickle.dump(index, f)
101
  print("Saved updated FAISS index to faiss_index.pkl")
102
  with open(document_texts_path, "wb") as f:
103
  pickle.dump(document_texts, f)
104
  print("Saved updated document texts to document_texts.pkl")
105
+
106
+ return "Files processed successfully"
107
  except Exception as e:
108
+ print(f"Error processing files: {e}")
109
+ return f"Error processing files: {e}"
 
 
110
 
111
  def query_text(text):
112
  try:
 
147
 
148
 
149
 
150
+
151
 
152
 
153