forked from Gene-Weaver/VoucherVisionGO
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.py
More file actions
865 lines (726 loc) · 33 KB
/
Copy pathclient.py
File metadata and controls
865 lines (726 loc) · 33 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
import os
import sys
import json
import time
import argparse
import requests
import glob
import csv
import tempfile
import pandas as pd
import concurrent.futures
from collections import OrderedDict
from termcolor import colored
from tabulate import tabulate
from tqdm import tqdm
N_SIZE=100
N_INDENT=2
"""
NOTE:
You can use any of the Gemini models, not just those that I specify: https://ai.google.dev/gemini-api/docs/models
Just pick the one you want (e.g. gemini-2.5-flash-preview-04-17) as long as it supports: Audio, images, videos, and text
"""
class OrderedDictJSONEncoder(json.JSONEncoder):
def encode(self, obj):
if isinstance(obj, OrderedDict):
# Convert OrderedDict to a list of tuples for ordered serialization
return '{' + ','.join(f'"{k}":{self.encode(v)}' for k, v in obj.items()) + '}'
return super().encode(obj)
def ordereddict_to_json(ordereddict_data, output_type="json"):
"""
Convert an OrderedDict to JSON
Args:
ordereddict_data: The OrderedDict to convert
output_type: "json" (string) or "dict" (Python dictionary)
Returns:
Either a JSON string or Python dictionary based on output_type
"""
def convert_to_dict(obj):
if isinstance(obj, OrderedDict):
return {k: convert_to_dict(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [convert_to_dict(v) for v in obj]
else:
return obj
regular_dict = convert_to_dict(ordereddict_data)
if output_type.lower() == "dict":
return regular_dict
else: # Default to JSON string
return json.dumps(regular_dict, indent=4)
def process_image(fname, server_url, image_path, output_dir, verbose=False,
engines=None, llm_model=None, prompt=None, auth_token=None, ocr_only=False):
"""
Process an image using the VoucherVision API server
Args:
fname (str): Name for the output file
server_url (str): URL of the VoucherVision API server
image_path (str): Path to the image file or URL of the image
output_dir (str): Directory to save output files
verbose (bool): Whether to print verbose output
engines (list): List of OCR engine options to use
llm_model (str): LLM model to use for creating JSON
prompt (str): Custom prompt file to use
auth_token (str): Authentication token for the API (Firebase token or API key)
ocr_only (bool): Whether to only perform OCR and skip VoucherVision processing
Returns:
dict: The processed results from the server
"""
# Always verify authentication using the cached verification
if not verify_authentication(server_url, auth_token):
print("Aborting. Authentication failed.")
return
# Check if the image path is a URL or a local file
if image_path.startswith(('http://', 'https://')):
# For URL-based images, download them first
if verbose:
print(f"Processing image from URL: {image_path}")
response = requests.get(image_path)
if response.status_code != 200:
raise Exception(f"Failed to download image from URL: {response.status_code}")
# Save to a temp file
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
temp_file_path = temp_file.name
temp_file.write(response.content)
try:
# Pass the auth token to the recursive call
return process_image(fname, server_url, temp_file_path, output_dir, verbose, engines, llm_model, prompt, auth_token, ocr_only)
finally:
# Clean up the temporary file
os.remove(temp_file_path)
# Prepare the request data
url = f"{server_url}/process"
# Prepare the multipart form data
files = {'file': open(image_path, 'rb')}
# Add engine options and prompt if provided
data = {}
if engines:
data['engines'] = engines
if llm_model:
data['llm_model'] = llm_model
if prompt:
data['prompt'] = prompt
if ocr_only:
data['ocr_only'] = 'true'
# Determine auth header type based on auth_token format
# API keys are typically alphanumeric strings without periods
# Firebase tokens are JWT tokens with periods separating sections
headers = {}
if auth_token:
if '.' in auth_token and len(auth_token) > 100:
# Likely a Firebase token
headers["Authorization"] = f"Bearer {auth_token}"
else:
# Likely an API key
headers["X-API-Key"] = auth_token
try:
# Send the request
if verbose:
print(f"Sending request to {url}")
if ocr_only:
print("OCR-only mode: Skipping VoucherVision JSON parsing")
response = requests.post(url, files=files, data=data, headers=headers)
# Check if the request was successful
if response.status_code == 200:
results = json.loads(response.text, object_pairs_hook=OrderedDict)
# If formatted_json is a string that contains JSON, parse it with OrderedDict
if 'formatted_json' in results and isinstance(results['formatted_json'], str):
try:
# Try to parse it as JSON with order preserved
results['formatted_json'] = json.loads(results['formatted_json'], object_pairs_hook=OrderedDict)
except json.JSONDecodeError:
# Not valid JSON, leave as string
pass
results['filename'] = fname # Add in the filename
return results
else:
error_msg = f"Error: {response.status_code}"
try:
error_details = response.json()
error_msg += f" - {error_details.get('error', 'Unknown error')}"
except:
error_msg += f" - {response.text}"
raise Exception(error_msg)
finally:
# Close the file
files['file'].close()
def process_image_file(server_url, image_path, engines, llm_model, prompt, output_dir, verbose, auth_token=None, ocr_only=False):
"""
Process a single image file and save the results
Args:
server_url (str): URL of the VoucherVision API server
image_path (str): Path to the image file or URL
engines (list): List of OCR engine options to use
llm_model
prompt (str): Custom prompt file to use
output_dir (str): Directory to save output files
verbose (bool): Whether to print verbose output
auth_token (str): Authentication token for the API
ocr_only (bool): Whether to only perform OCR and skip VoucherVision processing
Returns:
dict: The processing results
"""
output_file = get_output_filename(image_path, output_dir)
fname = os.path.basename(output_file).split(".")[0]
try:
# Process the image
results = process_image(fname, server_url, image_path, output_dir, verbose, engines, llm_model, prompt, auth_token, ocr_only)
# Print summary of results if verbose is enabled
if verbose:
print_results_summary(results, fname)
print(f"Processed: {image_path}")
# Save the results - ensure we preserve order
with open(output_file, 'w') as f:
# Use json.dump with an OrderedDict to preserve key order
json.dump(results, f, indent=2, sort_keys=False,
cls=OrderedDictJSONEncoder) # Use custom encoder
if verbose:
print(f"Individual results saved to: {output_file}")
return results
except Exception as e:
print(f"Error processing {image_path}: {e}")
return None
def process_images_parallel(server_url, image_paths, engines, llm_model, prompt, output_dir, verbose, max_workers=4, auth_token=None, ocr_only=False):
"""
Process multiple images in parallel
Args:
server_url (str): URL of the VoucherVision API server
image_paths (list): List of paths to image files or URLs
engines (list): List of OCR engine options to use
llm_model
prompt (str): Custom prompt file to use
output_dir (str): Directory to save output files
verbose (bool): Whether to print verbose output
max_workers (int): Maximum number of parallel workers
auth_token (str): Authentication token for the API
ocr_only (bool): Whether to only perform OCR and skip VoucherVision processing
Returns:
list: List of processing results
"""
results = []
print(f"Processing {len(image_paths)} images with up to {max_workers} parallel workers")
if ocr_only:
print("OCR-only mode: Skipping VoucherVision processing")
# Create a progress bar
progress_bar = tqdm(total=len(image_paths), desc="Processing", unit="image")
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
# Create a dictionary mapping futures to their corresponding file paths
# Verbose is forced off when using parallel, it's too messy in console printout
future_to_path = {
executor.submit(
process_image_file,
server_url,
path,
engines,
llm_model,
prompt,
output_dir,
False,
auth_token,
ocr_only,
): path for path in image_paths
}
# Process as they complete
for future in concurrent.futures.as_completed(future_to_path):
path = future_to_path[future]
try:
result = future.result()
if result:
results.append(result)
except Exception as e:
print(f"\nError processing {path}: {e}")
finally:
# Update the progress bar
progress_bar.update(1)
# Close the progress bar
progress_bar.close()
return results
def print_results_summary(results, fname):
"""
Print a summary of the VoucherVision processing results with enhanced formatting.
Dynamically determines fields from the JSON structure.
Args:
results (dict): The processing results from the server
"""
from termcolor import colored
from tabulate import tabulate
import json
print("\n" + "="*N_SIZE)
print("VOUCHERVISION RESULTS SUMMARY", colored(f"{fname}", 'green', attrs=['bold']))
print("="*N_SIZE)
# Print top-level sections one by one
for section_name, section_data in results.items():
if section_name != "ocr":
print(colored(f"{section_name.upper()}:", 'cyan', attrs=['bold']))
if section_name == 'ocr_info':
# Handle OCR results specially
# Print engine summary table
ocr_table = []
total_cost = 0
for engine, engine_data in section_data.items():
tokens_in = engine_data.get('tokens_in', 0)
tokens_out = engine_data.get('tokens_out', 0)
cost = engine_data.get('total_cost', 0)
total_cost += cost
ocr_table.append([
engine,
f"{tokens_in:,}",
f"{tokens_out:,}",
f"${cost:.6f}"
])
# Add total row if we have engine data
if ocr_table:
ocr_table.append([
colored("TOTAL", attrs=['bold']),
"",
"",
colored(f"${total_cost:.6f}", 'yellow', attrs=['bold'])
])
print(tabulate(ocr_table,
headers=['Engine', 'Tokens In', 'Tokens Out', 'Cost'],
tablefmt='grid'))
# Print the OCR text
elif section_name == 'ocr':
print(colored("OCR Text:", 'magenta', attrs=['bold']))
print(str(section_data))
elif section_name == 'parsing_info':
# Enhanced handling for tokens_LLM that now includes model, cost_in, and cost_out
llm_table = []
# Check if we have the enhanced structure or the basic one
if isinstance(section_data, dict) and 'model' in section_data:
# Enhanced structure with all fields
model = section_data.get('model', '')
tokens_in = section_data.get('input', 0)
tokens_out = section_data.get('output', 0)
cost_in = section_data.get('cost_in', 0)
cost_out = section_data.get('cost_out', 0)
total_cost = cost_in + cost_out
# Add a row with all the data
llm_table.append([
model,
f"{tokens_in:,}",
f"{tokens_out:,}",
f"${cost_in:.6f}",
f"${cost_out:.6f}",
colored(f"${total_cost:.6f}", 'yellow', attrs=['bold'])
])
# Print the enhanced table with all fields
print(tabulate(llm_table,
headers=['Model', 'Tokens In', 'Tokens Out', 'Cost In', 'Cost Out', 'Total Cost'],
tablefmt='grid'))
else:
# Basic structure with just input and output tokens
llm_table.append([
f"{section_data.get('input', 0):,}",
f"{section_data.get('output', 0):,}"
])
# Print the basic table
print(tabulate(llm_table,
headers=['Tokens In', 'Tokens Out'],
tablefmt='simple'))
elif section_name == 'formatted_json':
# Format the extracted JSON data
if isinstance(section_data, dict):
# Create a table for all top-level fields
json_table = []
for field, value in section_data.items():
# Format the value
if value == "":
formatted_value = colored("(empty)", 'dark_grey')
elif isinstance(value, (dict, list)):
# For nested structures, show a placeholder
formatted_value = colored(f"({type(value).__name__})", 'blue')
else:
formatted_value = str(value)
json_table.append([field, formatted_value])
# Print the table
if json_table:
print(tabulate(json_table, tablefmt='simple'))
else:
# If not a dict, just print the data
print(json.dumps(section_data, indent=N_INDENT, sort_keys=False, cls=OrderedDictJSONEncoder))
else:
# Generic handler for any other sections
try:
print(json.dumps(section_data, indent=N_INDENT, sort_keys=False, cls=OrderedDictJSONEncoder))
except:
print(str(section_data))
def get_output_filename(input_path, output_dir=None):
"""
Generate an output filename based on the input file path
Args:
input_path (str): Path to the input file
output_dir (str): Directory to save the output file (optional)
Returns:
str: Path to the output file
"""
# Extract the base filename without extension
if input_path.startswith(('http://', 'https://')):
# For URLs, use the last part of the URL as the filename
base_name = os.path.basename(input_path).split('?')[0] # Remove query params if any
else:
base_name = os.path.basename(input_path)
# Replace the extension with .json
name_without_ext = os.path.splitext(base_name)[0]
output_filename = f"{name_without_ext}.json"
# If output directory is specified, join it with the filename
if output_dir:
os.makedirs(output_dir, exist_ok=True)
return os.path.join(output_dir, output_filename)
return output_filename
def read_file_list(list_file):
"""
Read a list of file paths or URLs from a file
Args:
list_file (str): Path to the file containing the list
Returns:
list: List of file paths or URLs
"""
file_paths = []
# Check file extension
ext = os.path.splitext(list_file)[1].lower()
if ext == '.csv':
# Handle CSV file
with open(list_file, 'r', newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
if row and row[0].strip(): # Skip empty rows
file_paths.append(row[0].strip())
else:
# Handle text file (one path per line)
with open(list_file, 'r') as f:
for line in f:
line = line.strip()
if line: # Skip empty lines
file_paths.append(line)
return file_paths
def save_results_to_csv(results_list, output_dir):
"""
Save a list of VoucherVision results to a CSV file using the filename
that's already included in the results
Args:
results_list (list): List of dictionaries containing the results
output_dir (str): Directory to save the CSV file
"""
if not results_list:
print("No results to save to CSV")
return
# Extract formatted_json from each result and add filename
vvgo_data = []
for i, result in enumerate(results_list):
# Debug info for the first few results
# if i < 2:
# print(f"\nDebug - Result keys: {list(result.keys() if result else [])}")
# Skip if result is empty
if not result:
continue
# Get the filename directly from the result
# filename = "" #result.get('filename', '')
# if not filename:
# Fallback methods if filename is not directly available
# print(result)
if 'filename' in result:
filename = os.path.splitext(os.path.basename(result['filename']))[0]
else:
# Use index as last resort
filename = f"file_{i+1}"
# Get the JSON data (try formatted_json first, then vvgo_json)
json_data = None
json_key = None
if 'formatted_json' in result:
json_key = 'formatted_json'
elif 'vvgo_json' in result:
json_key = 'vvgo_json'
if json_key:
# Get the JSON data in the appropriate format
if isinstance(result[json_key], OrderedDict):
json_data = result[json_key]
elif isinstance(result[json_key], str):
# Parse string JSON with OrderedDict
try:
json_data = json.loads(result[json_key], object_pairs_hook=OrderedDict)
except json.JSONDecodeError:
continue
elif isinstance(result[json_key], dict):
# Convert regular dict to OrderedDict
json_data = OrderedDict(result[json_key])
else:
json_data = result[json_key]
if json_data:
# Create a new OrderedDict with filename as the first key
data_with_filename = OrderedDict([('filename', filename)])
# Add all other keys in their original order
for key, value in json_data.items():
data_with_filename[key] = value
vvgo_data.append(data_with_filename)
if not vvgo_data:
print("No VoucherVision JSON data found in results")
print("Available keys in results:", [list(r.keys()) for r in results_list[:3] if r])
return
# Get the order of columns from the first result
if vvgo_data and isinstance(vvgo_data[0], OrderedDict):
column_order = list(vvgo_data[0].keys())
else:
column_order = None # Let pandas decide
# Convert to DataFrame
df = pd.DataFrame(vvgo_data)
# Ensure column order with filename first if we have a specific order
if column_order:
# Make sure all columns exist in the DataFrame
available_columns = [col for col in column_order if col in df.columns]
df = df[available_columns]
# Save to CSV
csv_path = os.path.join(output_dir, 'results.csv')
df.to_csv(csv_path, index=False)
print(f"Combined results saved to CSV: {csv_path}")
print(f"Total records processed: {len(df)}")
# Print column names for verification
if not df.empty:
print(f"CSV columns: {', '.join(df.columns.tolist())}")
def verify_authentication(server_url, auth_token=None):
"""Verify the authentication token before starting any processing"""
if not auth_token:
print("ERROR: No authentication token provided.")
print("Visit the login page to get your token: " + server_url + "/login")
print("Or visit the API key management page: " + server_url + "/api-key-management")
return False
try:
# We'll check both authentication methods - API key or Firebase token
# First, try as API key
headers = {"X-API-Key": auth_token}
response = requests.get(f"{server_url}/auth-check", headers=headers)
if response.status_code == 200:
print("Authentication successful using API key.")
return True
# If that fails, try as Firebase token
headers = {"Authorization": f"Bearer {auth_token}"}
response = requests.get(f"{server_url}/auth-check", headers=headers)
if response.status_code == 200:
print("Authentication successful using Firebase token.")
return True
elif response.status_code == 401:
print("ERROR: Authentication failed. Please provide a valid authentication token or API key.")
print("Visit the login page to get your token: " + server_url + "/login")
print("Or visit the API key management page: " + server_url + "/api-key-management")
return False
else:
print(f"ERROR: Server returned unexpected status code: {response.status_code}")
return False
except Exception as e:
print(f"ERROR: Could not connect to server: {str(e)}")
return False
def process_vouchers(server, output_dir, engines=["gemini-1.5-pro", "gemini-2.0-flash"], llm_model="gemini-2.0-flash",
prompt="SLTPvM_default.yaml", image=None, directory=None,
file_list=None, verbose=False, save_to_csv=False, max_workers=4, auth_token=None, ocr_only=False):
"""
Process voucher images through the VoucherVision API.
Args:
server (str): URL of the VoucherVision API server
output_dir (str): Directory to save the output JSON results
engines (list): OCR engine options to use
llm_model
prompt (str): Custom prompt file to use
image (str): Path to a single image file or URL to process
directory (str): Path to a directory containing images to process
file_list (str): Path to a file containing a list of image paths or URLs
verbose (bool): Print all output to console
save_to_csv (bool): Save all formatted_json results to a CSV file
max_workers (int): Maximum number of parallel workers
auth_token (str): Authentication token for the API
ocr_only (bool): Whether to only perform OCR and skip VoucherVision processing
Returns:
list: List of processed results if save_to_csv is True, otherwise None
"""
# First verify authentication before doing anything
if not verify_authentication(server, auth_token):
print("Aborting. Authentication failed.")
return
import os
import time
import sys
import glob
# Ensure max_workers is no more than 32
max_workers = min(max_workers, 32)
# Create output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)
# Start timing
start_time = time.time()
# If in OCR-only mode, inform user
if ocr_only:
print("Running in OCR-only mode: Skipping VoucherVision JSON parsing")
try:
# To store all results if save-to-csv is enabled
all_results = []
# Process based on the input type
if image:
# Single image (no need for parallelization)
result = process_image_file(server, image, engines, llm_model, prompt, output_dir, verbose, auth_token, ocr_only)
if result and save_to_csv:
all_results.append(result)
elif directory:
# Directory of images - use parallel processing
if not os.path.isdir(directory):
raise ValueError(f"Directory not found: {directory}")
# Get all image files in the directory
image_extensions = ['.jpg', '.jpeg', '.png', '.tif', '.tiff', '.bmp', '.gif']
image_files = []
for ext in image_extensions:
# Just use the lowercase extension - Windows is case-insensitive anyway
image_files.extend(glob.glob(os.path.join(directory, f"*{ext}")))
# Remove duplicates using lowercase comparison
seen = set()
unique_files = []
for file in image_files:
lowercase_path = file.lower()
if lowercase_path not in seen:
seen.add(lowercase_path)
unique_files.append(file)
image_files = unique_files
print(f"Found {len(image_files)} unique image files to process")
if not image_files:
print(f"No image files found in {directory}")
return None
# Process images in parallel
results = process_images_parallel(
server,
image_files,
engines,
llm_model,
prompt,
output_dir,
verbose,
max_workers,
auth_token,
ocr_only,
)
if save_to_csv:
all_results.extend(results)
elif file_list:
# List of image paths or URLs from a file - use parallel processing
file_paths = read_file_list(file_list)
if not file_paths:
print(f"No file paths found in {file_list}")
return None
print(f"Found {len(file_paths)} paths to process")
# Process files in parallel
results = process_images_parallel(
server,
file_paths,
engines,
llm_model,
prompt,
output_dir,
verbose,
max_workers,
auth_token,
ocr_only,
)
if save_to_csv:
all_results.extend(results)
# Save to CSV if requested
if save_to_csv and all_results:
save_results_to_csv(all_results, output_dir)
if save_to_csv:
return all_results
return None
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
sys.exit(1)
else:
raise
finally:
# End timing and report
end_time = time.time()
elapsed_seconds = end_time - start_time
minutes, seconds = divmod(elapsed_seconds, 60)
print(f"\n{'-' * N_SIZE}")
print(f"Total operation time: {int(minutes)} minutes and {int(seconds)} seconds")
print(f"{'-' * N_SIZE}")
def main():
# Parse command line arguments
parser = argparse.ArgumentParser(description='VoucherVisionGO Client')
parser.add_argument('--server', required=True,
help='URL of the VoucherVision API server (e.g., http://localhost:8080)')
parser.add_argument('--auth-token', required=True,
help='Authentication token (https://vouchervision-go-738307415303.us-central1.run.app/login)')
# Create a mutually exclusive group for input sources
input_group = parser.add_mutually_exclusive_group(required=True)
input_group.add_argument('--image',
help='Path to a single image file or URL to process')
input_group.add_argument('--directory',
help='Path to a directory containing images to process')
input_group.add_argument('--file-list',
help='Path to a file containing a list of image paths or URLs (one per line or CSV)')
parser.add_argument('--engines', nargs='+', default=["gemini-1.5-pro", "gemini-2.0-flash"],
help='OCR engine options to use (default: gemini-1.5-pro gemini-2.0-flash)')
parser.add_argument('--llm-model', default="gemini-2.0-flash",
help='OCR engine options to use (default: gemini-2.0-flash)')
parser.add_argument('--prompt', default="SLTPvM_default.yaml",
help='Custom prompt file to use (default: SLTPvM_default.yaml)')
parser.add_argument('--output-dir', required=True,
help='Directory to save the output JSON results')
parser.add_argument('--verbose', action='store_true',
help='Print all output to console')
parser.add_argument('--save-to-csv', action='store_true',
help='Save all formatted_json results to a CSV file in the output directory')
parser.add_argument('--max-workers', type=int, default=4,
help='Maximum number of parallel workers (default: 4)')
parser.add_argument('--ocr-only', action='store_true',
help='Only perform OCR and skip VoucherVision processing')
args = parser.parse_args()
# Call the processing function with CLI arguments
process_vouchers(
server=args.server,
output_dir=args.output_dir,
engines=args.engines,
llm_model=args.llm_model,
prompt=args.prompt,
image=args.image,
directory=args.directory,
file_list=args.file_list,
verbose=args.verbose,
save_to_csv=args.save_to_csv,
max_workers=args.max_workers,
auth_token=args.auth_token,
ocr_only=args.ocr_only,
)
if __name__ == "__main__":
main()
### Usage examples:
# Single image:
# python client.py --server https://vouchervision-go-XXXXXX.app --image "./demo/images/MICH_16205594_Poaceae_Jouvea_pilosa.jpg" --output-dir "./demo/results_single_image" --verbose
# URL image:
# python client.py --server https://vouchervision-go-XXXXXX.app --image "https://swbiodiversity.org/imglib/h_seinet/seinet/KHD/KHD00041/KHD00041592_lg.jpg" --output-dir "./demo/results_single_url" --verbose
# Directory of images:
# python client.py --server https://vouchervision-go-XXXXXX.app --directory "./demo/images" --output-dir "./demo/results_dir_images" --verbose --max-workers 4
# python client.py --server https://vouchervision-go-XXXXXX.app --directory "./demo/images" --output-dir "./demo/results_dir_images_custom_prompt_save_to_csv" --verbose --prompt "SLTPvM_default_chromosome.yaml" --max-workers 4
# List of files:
# python client.py --server https://vouchervision-go-XXXXXX.app --file-list "./demo/csv/file_list.csv" --output-dir "./demo/results_file_list_csv" --verbose --max-workers 2
# python client.py --server https://vouchervision-go-XXXXXX.app --file-list "./demo/txt/file_list.txt" --output-dir "./demo/results_file_list_txt" --verbose --max-workers 4
# Custom prompt:
# python client.py --server https://vouchervision-go-XXXXXX.app --image "https://swbiodiversity.org/imglib/h_seinet/seinet/KHD/KHD00041/KHD00041592_lg.jpg" --output-dir "./demo/results_single_image_custom_prompt" --verbose --prompt "SLTPvM_default_chromosome.yaml"
# Save results to CSV:
# python client.py --server https://vouchervision-go-XXXXXX.app --directory ./demo/images --output-dir "./demo/results_dir_images_save_to_csv" --save-to-csv
### Programmatic Example
'''
from client import process_vouchers
if __name__ == '__main__':
process_vouchers(server="https://vouchervision-go-XXXXXX.app",
output_dir="./output",
prompt="SLTPvM_default_chromosome.yaml",
image="https://swbiodiversity.org/imglib/seinet/sernec/EKY/31234100396/31234100396116.jpg",
directory=None,
file_list=None,
verbose=True,
save_to_csv=True,
max_workers=4)
process_vouchers(server="https://vouchervision-go-XXXXXX.app",
output_dir="./output2",
prompt="SLTPvM_default_chromosome.yaml",
image=None,
directory="D:/Dropbox/VoucherVisionGO/demo/images",
file_list=None,
verbose=True,
save_to_csv=True,
max_workers=4)
'''