Update app.py
Browse files
app.py
CHANGED
@@ -85,7 +85,6 @@ def download_documents():
|
|
85 |
|
86 |
return response
|
87 |
|
88 |
-
##### TESTING ######
|
89 |
@app.post("/admin/documents/upload")
|
90 |
def upload_documents(file: UploadFile = File(...)):
|
91 |
# Read the contents of the uploaded file
|
@@ -137,38 +136,27 @@ def download_database():
|
|
137 |
return response
|
138 |
|
139 |
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
response = FileResponse("database.index")
|
146 |
-
|
147 |
-
# Set the content disposition header to trigger a download
|
148 |
-
response.headers["Content-Disposition"] = "attachment; filename=database.index"
|
149 |
-
|
150 |
-
return response
|
151 |
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
"index": faiss.write_index_binary(index),
|
157 |
-
"documents": documents
|
158 |
-
}
|
159 |
-
with open("database.json", "w") as f:
|
160 |
-
json.dump(data, f)
|
161 |
-
|
162 |
-
# Create a response with the database file as the content
|
163 |
-
response = FileResponse("database.json")
|
164 |
|
165 |
-
|
166 |
-
response.headers["Content-Disposition"] = "attachment; filename=database.json"
|
167 |
|
168 |
-
return response
|
169 |
|
170 |
@app.post("/admin/database/upload")
|
171 |
-
def
|
172 |
# Read the contents of the uploaded file
|
173 |
contents = file.file.read()
|
174 |
|
|
|
85 |
|
86 |
return response
|
87 |
|
|
|
88 |
@app.post("/admin/documents/upload")
|
89 |
def upload_documents(file: UploadFile = File(...)):
|
90 |
# Read the contents of the uploaded file
|
|
|
136 |
return response
|
137 |
|
138 |
|
139 |
+
@app.post("/admin/database/upload")
|
140 |
+
def upload_database(file: UploadFile = File(...)):
|
141 |
+
# Open the uploaded file as a binary file object
|
142 |
+
with open(file.filename, "wb") as f:
|
143 |
+
f.write(file.file.read())
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
+
# Open the file as a binary file object
|
146 |
+
with open(file.filename, "rb") as f:
|
147 |
+
# Load the FAISS index from the file object
|
148 |
+
global index
|
149 |
+
index = faiss.read_index_binary(f)
|
150 |
|
151 |
+
# Clear the existing documents list and add the new documents
|
152 |
+
global documents
|
153 |
+
documents = index.reconstruct_n(0, index.ntotal).tolist()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
+
return {"message": f"Database uploaded with {len(documents)} documents"}
|
|
|
156 |
|
|
|
157 |
|
158 |
@app.post("/admin/database/upload")
|
159 |
+
def upload_database_0(file: UploadFile = File(...)):
|
160 |
# Read the contents of the uploaded file
|
161 |
contents = file.file.read()
|
162 |
|