Spaces:
Running
Running
from fastapi import HTTPException, UploadFile | |
from fastapi.responses import JSONResponse | |
from app.infrastructure.repository.document_handeler_repository import ( | |
DocumentHandelerRepository, | |
) | |
from app.modules.documentHandeler.features.createEmbeddings_feature import ( | |
CreateEmbeddingsFeature, | |
) | |
from app.modules.documentHandeler.features.deleteDocument_feature import ( | |
DeleteDocumentFeature, | |
) | |
from app.modules.documentHandeler.features.extractText_feature import ExtractTextFeature | |
from app.modules.documentHandeler.features.getAllChunkedText_feature import ( | |
GetAllChunkedTextFeature, | |
) | |
class DocumentHandelerController: | |
def __init__( | |
self, | |
delete_document_feature: DeleteDocumentFeature, | |
create_embeddings_feature: CreateEmbeddingsFeature, | |
get_all_chunked_text_feature: GetAllChunkedTextFeature, | |
): | |
self.create_embeddings_feature = create_embeddings_feature | |
self.delete_document_feature = delete_document_feature | |
self.get_all_chunked_text_feature = get_all_chunked_text_feature | |
async def handle_file_upload(self, file: UploadFile) -> JSONResponse: | |
try: | |
text_file = await ExtractTextFeature.extract_text_from_pdf(file) | |
result = await self.create_embeddings_feature.create_embeddings( | |
text_file, file.filename | |
) | |
return JSONResponse(status_code=200, content=result.model_dump()) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail="Probelm on controller") | |
async def delete_document(self, text: str) -> JSONResponse: | |
try: | |
result = await self.delete_document_feature.delete_document_by_filename( | |
text | |
) | |
if result: | |
return JSONResponse( | |
status_code=200, content={"message": "Document deleted"} | |
) | |
return JSONResponse( | |
status_code=404, content={"message": "Document not found"} | |
) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |
async def get_all_chunks(self) -> JSONResponse: | |
try: | |
result = await self.get_all_chunked_text_feature.get_all_chunked_text() | |
return JSONResponse(status_code=200, content=result.model_dump()) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |