File size: 2,215 Bytes
060dc2a
 
 
 
 
 
 
 
 
50fd07f
d062149
060dc2a
 
d062149
060dc2a
 
50fd07f
d062149
060dc2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import axios from 'axios';

// Use same origin in production (when deployed), localhost in development
const API_BASE_URL = import.meta.env.VITE_API_URL || window.location.origin;

/**
 * Process a single invoice image
 * @param {Blob} imageBlob - Image blob
 * @param {string} filename - Original filename
 * @param {boolean} enhanceImage - Whether to apply OpenCV enhancement
 * @param {string} reasoningMode - VLM reasoning mode: "simple" or "reason"
 * @returns {Promise<Object>} Processed result
 */
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;
}

/**
 * Process multiple invoices in batch
 * @param {Array} images - Array of {blob, filename} objects
 * @param {Function} onProgress - Progress callback (index, result)
 * @returns {Promise<Array>} Array of results
 */
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;
}