AnukulChandra/Mini-AI_Assistant
0
1import logging2 3from fastapi import APIRouter, UploadFile, File, HTTPException4from services.chunking import split_text5from services.ingestion import validate_file, read_document6from services.vector_store import create_vector_store, save_vector_store7 8logger = logging.getLogger(__name__)9 10router = APIRouter(prefix="/upload")11 12 13@router.post("/document")14async def upload_document(file: UploadFile = File(...)):15 try:16 validate_file(file)17 except ValueError as e:18 raise HTTPException(status_code=400, detail=str(e))19 20 try:21 text = read_document(file)22 except ValueError as e:23 raise HTTPException(status_code=400, detail=str(e))24 25 try:26 chunks = split_text(text)27 vector_store = create_vector_store(chunks)28 save_vector_store(vector_store)29 except Exception as e:30 logger.exception("Document processing failed")31 raise HTTPException(status_code=500, detail=f"Document processing failed: {e}")32 33 return {34 "filename": file.filename,35 "chunks": len(chunks),36 "message": "Document processed and indexed successfully.",37 }38 