Implement code changes to enhance functionality and improve performance
This commit is contained in:
+77
-27
@@ -310,7 +310,7 @@ async def import_transactions_from_image(
|
|||||||
tags=["Document Processing"],
|
tags=["Document Processing"],
|
||||||
)
|
)
|
||||||
async def upload_multiple_documents(
|
async def upload_multiple_documents(
|
||||||
files: List[UploadFile] = File(...), db: db_dependency = None
|
files: List[UploadFile] = File(...), db: db_dependency = None
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Upload multiple receipt images for processing.
|
Upload multiple receipt images for processing.
|
||||||
@@ -318,51 +318,73 @@ async def upload_multiple_documents(
|
|||||||
This endpoint accepts multiple image files and returns file IDs
|
This endpoint accepts multiple image files and returns file IDs
|
||||||
that can be used with the /process/{file_id} endpoint.
|
that can be used with the /process/{file_id} endpoint.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
responses = []
|
responses = []
|
||||||
|
|
||||||
for file in files:
|
for file in files:
|
||||||
# Validate file type
|
# Validate file type
|
||||||
allowed_types = ["jpg", "jpeg", "png", "gif", "bmp", "pdf"]
|
|
||||||
file_extension = file.filename.split(".")[-1].lower()
|
|
||||||
|
|
||||||
|
allowed_types = ["jpg", "jpeg", "png", "gif", "bmp", "pdf"]
|
||||||
|
|
||||||
|
file_extension = file.filename.split(".")[-1].lower()
|
||||||
|
google_file_id, file_name = file.filename.split("|")
|
||||||
if file_extension not in allowed_types:
|
if file_extension not in allowed_types:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
detail=f"Unsupported file type for {file.filename}. Allowed: {allowed_types}",
|
detail=f"Unsupported file type for {file.filename}. Allowed: {allowed_types}",
|
||||||
)
|
)
|
||||||
|
logger.info(f"filename: {file_name}, extension: {file_extension}, google_file_id: {google_file_id}")
|
||||||
|
|
||||||
# Generate unique file ID
|
# Generate unique file ID
|
||||||
file_id = str(uuid.uuid4())
|
file_id = google_file_id # Using Google Drive file ID as file_id
|
||||||
|
|
||||||
# Read file content and save to disk
|
# Check if file already exists in database
|
||||||
content = await file.read()
|
existing_file = get_uploaded_file_from_db(db, file_id)
|
||||||
file_path = await document_processor.save_uploaded_file(
|
|
||||||
content, file.filename
|
if existing_file:
|
||||||
)
|
# File already exists, return existing record
|
||||||
|
logger.info(f"File {file_name} with ID {file_id} already exists, returning existing record")
|
||||||
|
responses.append(
|
||||||
|
DocumentUploadResponse(
|
||||||
|
file_id=existing_file.file_id,
|
||||||
|
filename=existing_file.filename,
|
||||||
|
file_type=existing_file.file_type,
|
||||||
|
upload_date=existing_file.upload_date,
|
||||||
|
status=existing_file.status,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# File doesn't exist, create new record
|
||||||
|
# Read file content and save to disk
|
||||||
|
content = await file.read()
|
||||||
|
file_path = await document_processor.save_uploaded_file(
|
||||||
|
content, file_name
|
||||||
|
)
|
||||||
|
|
||||||
# Create database record for uploaded file
|
# Create database record for uploaded file
|
||||||
db_uploaded_file = DBUploadedFile(
|
db_uploaded_file = DBUploadedFile(
|
||||||
file_id=file_id,
|
|
||||||
filename=file.filename,
|
|
||||||
file_path=file_path,
|
|
||||||
file_type=file_extension,
|
|
||||||
upload_date=datetime.now(),
|
|
||||||
status="uploaded",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Add to database
|
|
||||||
db.add(db_uploaded_file)
|
|
||||||
|
|
||||||
responses.append(
|
|
||||||
DocumentUploadResponse(
|
|
||||||
file_id=file_id,
|
file_id=file_id,
|
||||||
filename=file.filename,
|
filename=file_name,
|
||||||
|
file_path=file_path,
|
||||||
file_type=file_extension,
|
file_type=file_extension,
|
||||||
upload_date=datetime.now(),
|
upload_date=datetime.now(),
|
||||||
status="uploaded",
|
status="uploaded",
|
||||||
)
|
)
|
||||||
)
|
logger.info(f"Uploaded new file {file_name} with ID {file_id}")
|
||||||
|
|
||||||
|
# Add to database
|
||||||
|
db.add(db_uploaded_file)
|
||||||
|
|
||||||
|
responses.append(
|
||||||
|
DocumentUploadResponse(
|
||||||
|
file_id=file_id,
|
||||||
|
filename=file_name,
|
||||||
|
file_type=file_extension,
|
||||||
|
upload_date=datetime.now(),
|
||||||
|
status="uploaded",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
# Commit all uploaded files to database
|
# Commit all uploaded files to database
|
||||||
db.commit()
|
db.commit()
|
||||||
@@ -409,6 +431,34 @@ async def process_document(
|
|||||||
for rule in request.ai_rules
|
for rule in request.ai_rules
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# Check if receipt already exists for this file_id
|
||||||
|
existing_receipt = get_receipt_from_db(db, file_id)
|
||||||
|
|
||||||
|
if existing_receipt:
|
||||||
|
# Receipt already processed, return existing data
|
||||||
|
logger.info(f"Receipt for file {file_id} already exists, returning existing record")
|
||||||
|
return DocumentProcessResponse(
|
||||||
|
file_id=file_id,
|
||||||
|
receipt_id=existing_receipt.receipt_id,
|
||||||
|
extraction_success=existing_receipt.extraction_success == "True",
|
||||||
|
vendor=existing_receipt.vendor,
|
||||||
|
description=existing_receipt.description,
|
||||||
|
total_amount=existing_receipt.amount,
|
||||||
|
tax_amount=existing_receipt.tax_amount,
|
||||||
|
date=existing_receipt.date.strftime("%Y-%m-%d"),
|
||||||
|
category=existing_receipt.category,
|
||||||
|
confidence=existing_receipt.confidence,
|
||||||
|
error=existing_receipt.error_message,
|
||||||
|
receipt_currency=existing_receipt.receipt_currency,
|
||||||
|
receipt_location=existing_receipt.receipt_location,
|
||||||
|
calculated_tax=existing_receipt.calculated_tax,
|
||||||
|
is_depreciable=existing_receipt.is_depreciable == "True" if existing_receipt.is_depreciable else None,
|
||||||
|
name_of_asset=existing_receipt.name_of_asset,
|
||||||
|
cca_rate=existing_receipt.cca_rate,
|
||||||
|
useful_life=existing_receipt.useful_life,
|
||||||
|
residual_value=existing_receipt.residual_value,
|
||||||
|
)
|
||||||
|
|
||||||
# Process the file using the stored file path
|
# Process the file using the stored file path
|
||||||
receipt_data = await document_processor.process_file(
|
receipt_data = await document_processor.process_file(
|
||||||
db_uploaded_file.file_path,
|
db_uploaded_file.file_path,
|
||||||
|
|||||||
Reference in New Issue
Block a user