|
|
import axios from 'axios'; |
|
|
|
|
|
|
|
|
const API_BASE_URL = import.meta.env.VITE_API_URL || window.location.origin; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function processSingleInvoice(imageBlob, filename, enhanceImage = false, reasoningMode = "simple") { |
|
|
const formData = new FormData(); |
|
|
formData.append('file', imageBlob, filename); |
|
|
formData.append('enhance_image', enhanceImage); |
|
|
formData.append('reasoning_mode', reasoningMode); |
|
|
|
|
|
const response = await axios.post(`${API_BASE_URL}/process-invoice`, formData, { |
|
|
headers: { |
|
|
'Content-Type': 'multipart/form-data', |
|
|
}, |
|
|
}); |
|
|
|
|
|
return response.data; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function processBatchInvoices(images, onProgress) { |
|
|
const results = []; |
|
|
|
|
|
for (let i = 0; i < images.length; i++) { |
|
|
try { |
|
|
const result = await processSingleInvoice(images[i].blob, images[i].filename); |
|
|
const resultWithMetadata = { |
|
|
...result, |
|
|
filename: images[i].filename, |
|
|
originalFile: images[i].originalFile, |
|
|
pageNumber: images[i].pageNumber, |
|
|
index: i, |
|
|
success: true |
|
|
}; |
|
|
results.push(resultWithMetadata); |
|
|
|
|
|
if (onProgress) { |
|
|
onProgress(i, resultWithMetadata); |
|
|
} |
|
|
} catch (error) { |
|
|
const errorResult = { |
|
|
filename: images[i].filename, |
|
|
originalFile: images[i].originalFile, |
|
|
pageNumber: images[i].pageNumber, |
|
|
index: i, |
|
|
success: false, |
|
|
error: error.response?.data?.detail || error.message |
|
|
}; |
|
|
results.push(errorResult); |
|
|
|
|
|
if (onProgress) { |
|
|
onProgress(i, errorResult); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return results; |
|
|
} |
|
|
|