Spaces:
Runtime error
Runtime error
| from fastapi import APIRouter, Depends, HTTPException, status, UploadFile, File | |
| from sqlalchemy.orm import Session | |
| from typing import List | |
| from app.database.connection import get_db | |
| from app.database.models import User | |
| from app.schemas.document import DocumentResponse, DocumentListResponse | |
| from app.services.document_service import DocumentService | |
| from app.middleware.auth import get_current_user | |
| router = APIRouter(prefix="/api/documents", tags=["documents"]) | |
| async def upload_document( | |
| file: UploadFile = File(...), | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user) | |
| ): | |
| """ | |
| Upload a document for processing. | |
| Args: | |
| file: Uploaded file | |
| db: Database session | |
| current_user: Current authenticated user | |
| Returns: | |
| Created document metadata | |
| Raises: | |
| HTTPException: If file validation fails | |
| """ | |
| try: | |
| # Upload document | |
| document = DocumentService.upload_document( | |
| db=db, | |
| file=file.file, | |
| user_id=current_user.id, | |
| filename=file.filename | |
| ) | |
| # Extract and process text content | |
| from app.utils.document_extractor import document_extractor | |
| try: | |
| # Extract text from the uploaded file | |
| content = document_extractor.extract_text( | |
| file_path=document.file_path, | |
| file_type=document.file_type | |
| ) | |
| # Process with RAG service | |
| num_chunks = DocumentService.process_document_content( | |
| db, | |
| document.id, | |
| content | |
| ) | |
| print(f"Document {document.filename} processed: {num_chunks} chunks created") | |
| except Exception as e: | |
| print(f"Error processing document content: {e}") | |
| # Document is uploaded but not processed for RAG | |
| # You might want to mark this in the database | |
| return DocumentResponse.from_orm(document) | |
| except ValueError as e: | |
| raise HTTPException( | |
| status_code=status.HTTP_400_BAD_REQUEST, | |
| detail=str(e) | |
| ) | |
| except Exception as e: | |
| print(f"Error uploading document: {e}") | |
| raise HTTPException( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| detail="Error uploading document" | |
| ) | |
| async def get_user_documents( | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user) | |
| ): | |
| """ | |
| Get all documents for the current user. | |
| Args: | |
| db: Database session | |
| current_user: Current authenticated user | |
| Returns: | |
| List of user documents | |
| """ | |
| documents = DocumentService.get_user_documents(db, current_user.id) | |
| return DocumentListResponse( | |
| documents=[DocumentResponse.from_orm(doc) for doc in documents], | |
| total=len(documents) | |
| ) | |
| async def delete_document( | |
| document_id: str, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user) | |
| ): | |
| """ | |
| Delete a document and its vector embeddings. | |
| Args: | |
| document_id: Document ID | |
| db: Database session | |
| current_user: Current authenticated user | |
| Raises: | |
| HTTPException: If document not found | |
| """ | |
| success = DocumentService.delete_document(db, document_id) | |
| if not success: | |
| raise HTTPException( | |
| status_code=status.HTTP_404_NOT_FOUND, | |
| detail="Document not found" | |
| ) | |