Daniel Marques commited on
Commit
9ef164b
1 Parent(s): 6a04a81

feat: add dele folder

Browse files
Files changed (1) hide show
  1. main.py +24 -28
main.py CHANGED
@@ -4,6 +4,7 @@ from fastapi.staticfiles import StaticFiles
4
 
5
  from pydantic import BaseModel
6
  import os
 
7
  import shutil
8
  import subprocess
9
 
@@ -74,14 +75,16 @@ QA = RetrievalQA.from_chain_type(
74
  class Predict(BaseModel):
75
  prompt: str
76
 
 
 
 
77
  app = FastAPI(title="homepage-app")
78
  api_app = FastAPI(title="api app")
79
 
80
  app.mount("/api", api_app, name="api")
81
  app.mount("/", StaticFiles(directory="static",html = True), name="static")
82
 
83
-
84
- @api_app.get("/run_ingest")
85
  def run_ingest_route():
86
  global DB
87
  global RETRIEVER
@@ -133,16 +136,29 @@ def run_ingest_route():
133
  except Exception as e:
134
  raise HTTPException(status_code=500, detail=f"Error occurred: {str(e)}")
135
 
136
- @api_app.get("/api/delete_source")
137
  def delete_source_route():
138
- folder_name = "SOURCE_DOCUMENTS"
 
139
 
140
- if os.path.exists(folder_name):
141
- shutil.rmtree(folder_name)
142
 
143
- os.makedirs(folder_name)
 
 
 
 
144
 
145
- return {"message": f"Folder '{folder_name}' successfully deleted and recreated."}
 
 
 
 
 
 
 
 
 
146
 
147
  @api_app.post('/predict')
148
  async def predict(data: Predict):
@@ -208,26 +224,6 @@ async def create_upload_file(file: UploadFile):
208
  shutil.copyfileobj(file.file, buffer)
209
 
210
  return {"filename": file.filename}
211
- # async def create_upload_file(file: Union[UploadFile, None] = None):
212
- # try:
213
- # if not file:
214
- # raise HTTPException(status_code=400, detail="No upload file sent")
215
- # else:
216
- # if file.filename == "":
217
- # raise HTTPException(status_code=400, detail="No selected file")
218
- # if file:
219
- # filename = file.filename
220
- # folder_path = "SOURCE_DOCUMENTS"
221
-
222
- # if not os.path.exists(folder_path):
223
- # os.makedirs(folder_path)
224
-
225
- # file_path = os.path.join(folder_path, filename)
226
- # file.save(file_path)
227
-
228
- # return {"response": "File saved successfully"}
229
- # except Exception as e:
230
- # raise HTTPException(status_code=400, detail=e)
231
 
232
  @api_app.websocket("/ws")
233
  async def websocket_endpoint(websocket: WebSocket):
 
4
 
5
  from pydantic import BaseModel
6
  import os
7
+ import glob
8
  import shutil
9
  import subprocess
10
 
 
75
  class Predict(BaseModel):
76
  prompt: str
77
 
78
+ class Delete(BaseModel):
79
+ filename: str
80
+
81
  app = FastAPI(title="homepage-app")
82
  api_app = FastAPI(title="api app")
83
 
84
  app.mount("/api", api_app, name="api")
85
  app.mount("/", StaticFiles(directory="static",html = True), name="static")
86
 
87
+ @api_app.get("/training")
 
88
  def run_ingest_route():
89
  global DB
90
  global RETRIEVER
 
136
  except Exception as e:
137
  raise HTTPException(status_code=500, detail=f"Error occurred: {str(e)}")
138
 
139
+ @api_app.get("/api/delete")
140
  def delete_source_route():
141
+ upload_dir = os.path.join(os.getcwd(), PATH_NAME_SOURCE_DIRECTORY)
142
+ files = glob.glob(os.path.join(upload_dir, '*'))
143
 
144
+ return {"directory": upload_dir, "files": files}
 
145
 
146
+ @api_app.delete("/api/delete")
147
+ def delete_source_route(data: Delete):
148
+ filename = data.filename
149
+ path_source_documents = os.path.join(os.getcwd(), PATH_NAME_SOURCE_DIRECTORY)
150
+ file_to_delete = f"{path_source_documents}/${filename}"
151
 
152
+ if os.path.exists(file_to_delete):
153
+ try:
154
+ os.remove(file_to_delete)
155
+ print(f"{file_to_delete} has been deleted.")
156
+
157
+ return {"message": f"{file_to_delete} has been deleted."}
158
+ except OSError as e:
159
+ raise HTTPException(status_code=400, detail=print(f"error: {e}."))
160
+ else:
161
+ raise HTTPException(status_code=400, detail=print(f"The file {file_to_delete} does not exist."))
162
 
163
  @api_app.post('/predict')
164
  async def predict(data: Predict):
 
224
  shutil.copyfileobj(file.file, buffer)
225
 
226
  return {"filename": file.filename}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
 
228
  @api_app.websocket("/ws")
229
  async def websocket_endpoint(websocket: WebSocket):