Almaatla commited on
Commit
08d2180
·
verified ·
1 Parent(s): f621dca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -9,6 +9,7 @@ from sentence_transformers import SentenceTransformer
9
  import faiss
10
  import numpy as np
11
  import json
 
12
 
13
  app = FastAPI()
14
  model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
@@ -64,14 +65,14 @@ def get_database_length():
64
 
65
  @app.post("/admin/database/reset")
66
  def reset_database():
67
- index = faiss.IndexFlatL2(384)
68
  documents = []
69
  return {"message": "Database reset"}
70
 
71
  @app.get("/admin/documents/download")
72
  def download_documents():
73
  # Convert the documents list to a JSON string
74
- documents_json = json.dumps([{text: doc} for doc in documents])
75
 
76
  # Create a response with the JSON string as the content
77
  response = Response(content=documents_json, media_type="application/json")
@@ -83,27 +84,32 @@ def download_documents():
83
 
84
  @app.get("/admin/database/download")
85
  def download_database():
86
- # Save the FAISS index to a file
87
- faiss.write_index(index, "database.index")
 
 
 
 
 
88
 
89
- # Create a response with the index file as the content
90
- response = FileResponse("database.index")
91
 
92
  # Set the content disposition header to trigger a download
93
- response.headers["Content-Disposition"] = "attachment; filename=database.index"
94
 
95
  return response
96
 
97
  @app.post("/admin/database/upload")
98
  def upload_database(file: UploadFile = File(...)):
99
  # Read the contents of the uploaded file
100
- contents = file.file.read()
101
 
102
  # Load the FAISS index from the file contents
103
- index = faiss.read_index_binary(contents.encode())
104
 
105
- # Clear the existing documents and add the new ones
106
  documents.clear()
107
- documents.extend(index.reconstruct_n(0, index.ntotal))
108
 
109
- return {"message": "Database uploaded"}
 
9
  import faiss
10
  import numpy as np
11
  import json
12
+ import io
13
 
14
  app = FastAPI()
15
  model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
 
65
 
66
  @app.post("/admin/database/reset")
67
  def reset_database():
68
+ index = faiss.reset()
69
  documents = []
70
  return {"message": "Database reset"}
71
 
72
  @app.get("/admin/documents/download")
73
  def download_documents():
74
  # Convert the documents list to a JSON string
75
+ documents_json = json.dumps([{"text": doc} for doc in documents])
76
 
77
  # Create a response with the JSON string as the content
78
  response = Response(content=documents_json, media_type="application/json")
 
84
 
85
  @app.get("/admin/database/download")
86
  def download_database():
87
+ # Save the FAISS index and documents list to a single file
88
+ data = {
89
+ "index": faiss.write_index_binary(index),
90
+ "documents": documents
91
+ }
92
+ with open("database.json", "w") as f:
93
+ json.dump(data, f)
94
 
95
+ # Create a response with the database file as the content
96
+ response = FileResponse("database.json")
97
 
98
  # Set the content disposition header to trigger a download
99
+ response.headers["Content-Disposition"] = "attachment; filename=database.json"
100
 
101
  return response
102
 
103
  @app.post("/admin/database/upload")
104
  def upload_database(file: UploadFile = File(...)):
105
  # Read the contents of the uploaded file
106
+ contents = json.load(file.file)
107
 
108
  # Load the FAISS index from the file contents
109
+ index = faiss.read_index_binary(contents["index"])
110
 
111
+ # Load the documents list from the file contents
112
  documents.clear()
113
+ documents.extend(contents["documents"])
114
 
115
+ return {"message": "Database uploaded", "documents": documents}