YALCINKAYA commited on
Commit
147b714
·
verified ·
1 Parent(s): e013ea6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -25
app.py CHANGED
@@ -68,34 +68,49 @@ else:
68
 
69
  # Function to upload document
70
  def upload_document(file_path, embed_model):
71
- # Generate unique document ID
72
- doc_id = uuid.uuid4().int % (2**63 - 1)
73
-
74
- # Ensure the file is saved to the correct directory with secure handling
75
- file_location = os.path.join(UPLOAD_DIR, os.path.basename(file_path))
76
-
77
- # Safely copy the file to the upload directory
78
- shutil.copy(file_path, file_location)
79
-
80
- # Read the content of the uploaded file
81
  try:
82
- with open(file_location, "r", encoding="utf-8") as f:
83
- text = f.read()
84
- except Exception as e:
85
- print(f"Error reading file {file_location}: {e}")
86
- return
87
-
88
- # Embed the text and add it to the FAISS index
89
- try:
90
- vector = embed_model.encode(text).astype("float32")
91
- index.add_with_ids(np.array([vector]), np.array([doc_id], dtype=np.int64))
92
- document_store[doc_id] = {"path": file_location, "text": text}
93
 
94
- # Save the FAISS index after adding the document
95
- faiss.write_index(index, faiss_index_file)
96
- print(f"Document uploaded with doc_id: {doc_id}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  except Exception as e:
98
- print(f"Error during document upload: {e}")
 
99
 
100
  @app.route("/list_uploads", methods=["GET"])
101
  def list_uploaded_files():
 
68
 
69
  # Function to upload document
70
  def upload_document(file_path, embed_model):
 
 
 
 
 
 
 
 
 
 
71
  try:
72
+ # Generate unique document ID
73
+ doc_id = uuid.uuid4().int % (2**63 - 1)
74
+
75
+ # Ensure the file is saved to the correct directory with secure handling
76
+ file_location = os.path.join(UPLOAD_DIR, os.path.basename(file_path))
 
 
 
 
 
 
77
 
78
+ # Safely copy the file to the upload directory
79
+ shutil.copy(file_path, file_location)
80
+
81
+ # Read the content of the uploaded file
82
+ try:
83
+ with open(file_location, "r", encoding="utf-8") as f:
84
+ text = f.read()
85
+ except Exception as e:
86
+ print(f"Error reading file {file_location}: {e}")
87
+ return {"error": f"Error reading file: {e}"}, 502 # Error while reading file
88
+
89
+ # Embed the text and add it to the FAISS index
90
+ try:
91
+ # Ensure the embedding model is valid
92
+ if embed_model is None:
93
+ raise ValueError("Embedding model is not initialized properly.")
94
+
95
+ vector = embed_model.encode(text).astype("float32")
96
+ index.add_with_ids(np.array([vector]), np.array([doc_id], dtype=np.int64))
97
+ document_store[doc_id] = {"path": file_location, "text": text}
98
+
99
+ # Save the FAISS index after adding the document
100
+ try:
101
+ faiss.write_index(index, faiss_index_file)
102
+ print(f"Document uploaded with doc_id: {doc_id}")
103
+ except Exception as e:
104
+ print(f"Error saving FAISS index: {e}")
105
+ return {"error": f"Error saving FAISS index: {e}"}, 503 # Error while saving FAISS index
106
+
107
+ except Exception as e:
108
+ print(f"Error during document upload: {e}")
109
+ return {"error": f"Error during document upload: {e}"}, 504 # Error during embedding or FAISS processing
110
+
111
  except Exception as e:
112
+ print(f"Unexpected error: {e}")
113
+ return {"error": f"Unexpected error: {e}"}, 500 # General error
114
 
115
  @app.route("/list_uploads", methods=["GET"])
116
  def list_uploaded_files():