Text Extraction Done, UI Fixes Done, Extracted Template Layout Saved in DB

This commit is contained in:
2026-07-12 14:24:17 +05:30
parent 3163bb213e
commit 3d6614f408
79 changed files with 3139 additions and 81 deletions

Binary file not shown.

Binary file not shown.

54
backend/api/documents.py Normal file
View File

@@ -0,0 +1,54 @@
import json
from fastapi import APIRouter, Depends, HTTPException, status, UploadFile, File
from sqlalchemy.orm import Session
from typing import List
from database import get_db
from models.template_models import TemplateDocument, DocumentLayout
from schemas.template_schemas import DocumentSchema, DocumentLayoutSchema, TemplateRecognitionResult
from engine.ocr.document_processor import DocumentProcessor
from engine.recognition.template_engine import TemplateRecognitionEngine
router = APIRouter(prefix="/api/documents", tags=["Documents"])
@router.post("/upload", response_model=DocumentSchema, status_code=status.HTTP_201_CREATED)
async def upload_document(file: UploadFile = File(...), db: Session = Depends(get_db)):
# 1. Create document record
content = await file.read()
file_hash = hash(content) # Simple hash for demo
db_doc = TemplateDocument(
document_name=file.filename,
document_type=file.content_type,
file_name=file.filename,
file_hash=str(file_hash),
status="processing"
)
db.add(db_doc)
db.commit()
db.refresh(db_doc)
# 2. Process File (OCR & Layout)
processor = DocumentProcessor(db)
processor.process_file(db_doc.pk_document_id, file.filename, content)
db.refresh(db_doc)
return db_doc
@router.get("/{document_id}", response_model=DocumentSchema)
def get_document(document_id: int, db: Session = Depends(get_db)):
doc = db.query(TemplateDocument).filter(TemplateDocument.pk_document_id == document_id).first()
if not doc:
raise HTTPException(status_code=404, detail="Document not found")
return doc
@router.get("/{document_id}/layout", response_model=List[DocumentLayoutSchema])
def get_document_layout(document_id: int, db: Session = Depends(get_db)):
layouts = db.query(DocumentLayout).filter(DocumentLayout.fk_document_id == document_id).order_by(DocumentLayout.sequence_no).all()
return layouts
@router.post("/{document_id}/recognize", response_model=TemplateRecognitionResult)
def recognize_template(document_id: int, db: Session = Depends(get_db)):
engine = TemplateRecognitionEngine(db)
return engine.recognize_template(document_id)

53
backend/api/templates.py Normal file
View File

@@ -0,0 +1,53 @@
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import List
from database import get_db
from schemas.template_schemas import (
TemplateCreate, TemplateSchema,
TemplateFieldCreate, TemplateFieldSchema,
TemplateFieldMappingCreate, TemplateFieldMappingSchema
)
from services.template_service import TemplateService
router = APIRouter(prefix="/api/templates", tags=["Templates"])
@router.post("", response_model=TemplateSchema, status_code=status.HTTP_201_CREATED)
def create_template(template_data: TemplateCreate, db: Session = Depends(get_db)):
service = TemplateService(db)
return service.create_template(template_data)
@router.get("", response_model=List[TemplateSchema])
def get_templates(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
service = TemplateService(db)
return service.get_all_templates(skip, limit)
@router.get("/{template_id}", response_model=TemplateSchema)
def get_template(template_id: int, db: Session = Depends(get_db)):
service = TemplateService(db)
template = service.get_template(template_id)
if not template:
raise HTTPException(status_code=404, detail="Template not found")
return template
@router.delete("/{template_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_template(template_id: int, db: Session = Depends(get_db)):
service = TemplateService(db)
if not service.delete_template(template_id):
raise HTTPException(status_code=404, detail="Template not found")
return None
@router.post("/{template_id}/fields", response_model=TemplateFieldSchema, status_code=status.HTTP_201_CREATED)
def add_template_field(template_id: int, field_data: TemplateFieldCreate, db: Session = Depends(get_db)):
service = TemplateService(db)
# Check if template exists
if not service.get_template(template_id):
raise HTTPException(status_code=404, detail="Template not found")
return service.add_template_field(template_id, field_data)
@router.post("/{template_id}/mappings/save", response_model=List[TemplateFieldMappingSchema])
def save_mappings(template_id: int, mappings: List[TemplateFieldMappingCreate], db: Session = Depends(get_db)):
service = TemplateService(db)
if not service.get_template(template_id):
raise HTTPException(status_code=404, detail="Template not found")
return service.save_mapping(template_id, mappings)