Spaces:
Sleeping
Sleeping
Download app/api/documents.py from vara201/simple-rag: direct link, hf CLI and curl.
- Browser
- Download file 4.46 kB
-
https://huggingface.co/spaces/vara201/simple-rag/resolve/main/app/api/documents.py
- Command line
-
hf download hf://spaces/vara201/simple-rag/app/api/documents.py
-
curl -L -o documents.py https://huggingface.co/spaces/vara201/simple-rag/resolve/main/app/api/documents.py
4.46 kB
| from fastapi import ( | |
| APIRouter, | |
| UploadFile, | |
| File, | |
| Depends, | |
| HTTPException | |
| ) | |
| from sqlalchemy.ext.asyncio import ( | |
| AsyncSession | |
| ) | |
| from app.core.database import ( | |
| get_db | |
| ) | |
| from app.core.dependencies import ( | |
| get_current_user | |
| ) | |
| from app.schemas.document import ( | |
| DocumentResponse | |
| ) | |
| from app.services.document_service import ( | |
| DocumentService | |
| ) | |
| from app.repositories.chunk_repository import ChunkRepository | |
| from app.repositories.document_repository import DocumentRepository | |
| from app.services.cache_service import CacheService | |
| from app.rag.vector_store import VectorStore | |
| router = APIRouter( | |
| prefix="/documents", | |
| tags=["Documents"] | |
| ) | |
| async def list_documents( | |
| db: AsyncSession = Depends( | |
| get_db | |
| ), | |
| current_user=Depends( | |
| get_current_user | |
| ) | |
| ): | |
| """ | |
| Get all documents for the current user | |
| """ | |
| documents = await ( | |
| DocumentRepository | |
| .get_user_documents( | |
| db, | |
| current_user.id | |
| ) | |
| ) | |
| return [ | |
| { | |
| "id": doc.id, | |
| "filename": doc.filename, | |
| "file_path": doc.file_path, | |
| "status": doc.status, | |
| "chunking_strategy": doc.chunking_strategy, | |
| } | |
| for doc in documents | |
| ] | |
| async def upload_document( | |
| file: UploadFile = File(...), | |
| db: AsyncSession = Depends( | |
| get_db | |
| ), | |
| current_user=Depends( | |
| get_current_user | |
| ) | |
| ): | |
| return await ( | |
| DocumentService.save_file( | |
| file=file, | |
| user_id=current_user.id, | |
| db=db | |
| ) | |
| ) | |
| async def get_chunks( | |
| document_id: str, | |
| db: AsyncSession = Depends( | |
| get_db | |
| ) | |
| ): | |
| chunks = await ( | |
| ChunkRepository | |
| .get_by_document( | |
| db, | |
| document_id | |
| ) | |
| ) | |
| return [ | |
| { | |
| "chunk_index": | |
| chunk.chunk_index, | |
| "filename": | |
| chunk.filename, | |
| "start_page": | |
| chunk.start_page, | |
| "end_page": | |
| chunk.end_page, | |
| "token_count": | |
| chunk.token_count, | |
| "indexed": | |
| chunk.indexed, | |
| "content": | |
| chunk.content | |
| } | |
| for chunk in chunks | |
| ] | |
| async def get_document( | |
| document_id: str, | |
| db: AsyncSession = Depends( | |
| get_db | |
| ), | |
| current_user=Depends( | |
| get_current_user | |
| ) | |
| ): | |
| """ | |
| Get single document details with metadata | |
| """ | |
| document = await ( | |
| DocumentRepository | |
| .get_by_id( | |
| db, | |
| document_id | |
| ) | |
| ) | |
| if not document: | |
| raise HTTPException( | |
| status_code=404, | |
| detail="Document not found" | |
| ) | |
| if document.user_id != current_user.id: | |
| raise HTTPException( | |
| status_code=403, | |
| detail="Forbidden" | |
| ) | |
| return { | |
| "id": document.id, | |
| "filename": document.filename, | |
| "file_path": document.file_path, | |
| "status": document.status, | |
| } | |
| async def delete_document( | |
| document_id: str, | |
| db: AsyncSession = Depends( | |
| get_db | |
| ), | |
| current_user=Depends( | |
| get_current_user | |
| ) | |
| ): | |
| """ | |
| Delete a document and all its associated chunks | |
| """ | |
| document = await ( | |
| DocumentRepository | |
| .get_by_id( | |
| db, | |
| document_id | |
| ) | |
| ) | |
| if not document: | |
| raise HTTPException( | |
| status_code=404, | |
| detail="Document not found" | |
| ) | |
| if document.user_id != current_user.id: | |
| raise HTTPException( | |
| status_code=403, | |
| detail="Forbidden" | |
| ) | |
| await DocumentRepository.delete(db, document_id) | |
| await VectorStore.delete_document_vectors(document_id) | |
| CacheService.invalidate_user(current_user.id) | |
| return { | |
| "message": f"Document {document_id} deleted successfully", | |
| "status": "success" | |
| } |