DocuRAG / Api /app /modules /uploadDocument /routes /uploadDocument_route.py
abadesalex's picture
src directory
fe7c659
raw
history blame
1.61 kB
from fastapi import APIRouter, File, HTTPException, UploadFile
from fastapi.responses import JSONResponse
from app.db_local_storage.vector_files_db import vector_files_db
from app.modules.uploadDocument.controllers.file_upload_controller import (
FileUploadController,
)
from app.db_local_storage.documents_db import documents_db
router = APIRouter()
fileUploadController = FileUploadController()
@router.get("/get_documents")
async def get_all_documents():
return JSONResponse(status_code=200, content={"data": documents_db})
@router.get("/get_embeddings")
async def get_all_embeddings():
return JSONResponse(status_code=200, content={"data": vector_files_db})
@router.delete("/delete_document/{id}")
async def delete_document(id: int):
document = next((doc for doc in documents_db if doc["id"] == id), None)
if document:
documents_db.remove(document)
else:
raise HTTPException(status_code=404, detail="Document not found")
vector_file_key = next(
(
key
for key, value in vector_files_db.items()
if value["metadata"]["id"] == id
),
None,
)
if vector_file_key:
del vector_files_db[vector_file_key]
return JSONResponse(status_code=200, content={"message": "Document deleted"})
# {
# "data": {
# "document_1": {
# "metadata": {
# "id": 1,
# "filename": "doc_test.pdf",
# "chunks": 4
# },
@router.post("/document")
async def upload_file(file: UploadFile = File(...)):
return await fileUploadController.handle_file_upload(file)