File size: 24,969 Bytes
87c3140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93fd830
 
 
 
 
 
 
 
 
87c3140
93fd830
 
 
 
87c3140
93fd830
87c3140
93fd830
87c3140
93fd830
87c3140
93fd830
87c3140
93fd830
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87c3140
 
93fd830
87c3140
 
 
 
 
93fd830
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
import os, yaml, platform, traceback
from vouchervision.LeafMachine2_Config_Builder import get_default_download_folder, write_config_file
from vouchervision.general_utils import validate_dir, print_main_fail
from vouchervision.vouchervision_main import voucher_vision
from general_utils import get_cfg_from_full_path

def build_VV_config():
    #############################################
    ############ Set common defaults ############
    #############################################
    # Changing the values below will set new 
    # default values each time you open the 
    # VoucherVision user interface
    #############################################
    #############################################
    #############################################

    dir_home = os.path.dirname(os.path.dirname(__file__))
    run_name = 'test'
    # dir_images_local = 'D:/Dropbox/LM2_Env/Image_Datasets/GBIF_BroadSample_3SppPerFamily1'
    dir_images_local = os.path.join(dir_home,'demo','demo_images')
    
    # The default output location is the computer's "Downloads" folder
    #    You can set dir_output directly by typing the folder path,
    # OR you can uncomment the line "dir_output = default_output_folder" 
    #    to have VoucherVision save to the Downloads folder by default
    default_output_folder = get_default_download_folder()
    dir_output = default_output_folder
    # dir_output = 'D:/D_Desktop/LM2'

    prefix_removal = '' #'MICH-V-'
    suffix_removal = ''
    catalog_numerical_only = False

    LLM_version_user = 'Azure GPT 4'
    prompt_version = 'Version 2' # from ["Version 1", "Version 1 No Domain Knowledge", "Version 2"]
    use_LeafMachine2_collage_images = False # Use LeafMachine2 collage images

    batch_size = 500

    path_domain_knowledge = os.path.join(dir_home,'domain_knowledge','SLTP_UM_AllAsiaMinimalInRegion.xlsx')
    embeddings_database_name = os.path.splitext(os.path.basename(path_domain_knowledge))[0]

    #############################################
    #############################################
    ########## DO NOT EDIT BELOW HERE ###########
    #############################################
    #############################################
    return assemble_config(dir_home, run_name, dir_images_local,dir_output,
                    prefix_removal,suffix_removal,catalog_numerical_only,LLM_version_user,batch_size,
                    path_domain_knowledge,embeddings_database_name,use_LeafMachine2_collage_images,
                    prompt_version, use_domain_knowledge=False)

def assemble_config(dir_home, run_name, dir_images_local,dir_output,
                    prefix_removal,suffix_removal,catalog_numerical_only,LLM_version_user,batch_size,
                    path_domain_knowledge,embeddings_database_name,use_LeafMachine2_collage_images,
                    prompt_version, use_domain_knowledge=False):
    

    # Initialize the base structure
    config_data = {
        'leafmachine': {}
    }

    # Modular sections to be added to 'leafmachine'
    do_section = {
        'check_for_illegal_filenames': False,
        'check_for_corrupt_images_make_vertical': True,
    }

    print_section = {
        'verbose': True,
        'optional_warnings': True
    }

    logging_section = {
        'log_level': None
    }


    project_section = {
        'dir_output': dir_output, 
        'run_name': run_name,
        'image_location': 'local',
        'batch_size': batch_size,
        'num_workers': 1,
        'dir_images_local': dir_images_local,
        'continue_run_from_partial_xlsx': '',
        'prefix_removal': prefix_removal,
        'suffix_removal': suffix_removal,
        'catalog_numerical_only': catalog_numerical_only,
        'use_domain_knowledge': use_domain_knowledge,
        'embeddings_database_name': embeddings_database_name,
        'build_new_embeddings_database': False,
        'path_to_domain_knowledge_xlsx': path_domain_knowledge,
        'prompt_version': prompt_version,
        'delete_all_temps': False,
        'delete_temps_keep_VVE': False,
    }

    modules_section = {
        'specimen_crop': True
    }

    LLM_version = LLM_version_user 
    use_RGB_label_images = use_LeafMachine2_collage_images # Use LeafMachine2 collage images
    
    cropped_components_section = {
        'do_save_cropped_annotations': True,
        'save_cropped_annotations': ['label','barcode'],
        'save_per_image': False,
        'save_per_annotation_class': True,
        'binarize_labels': False,
        'binarize_labels_skeletonize': False
    }

    data_section = {
        'save_json_rulers': False,
        'save_json_measurements': False,
        'save_individual_csv_files_rulers': False,
        'save_individual_csv_files_measurements': False,
        'save_individual_csv_files_landmarks': False,
        'save_individual_efd_files': False,
        'include_darwin_core_data_from_combined_file': False,
        'do_apply_conversion_factor': False
    }

    overlay_section = {
        'save_overlay_to_pdf': False,
        'save_overlay_to_jpgs': True,
        'overlay_dpi': 300, # Between 100 to 300
        'overlay_background_color': 'black', # Either 'white' or 'black'

        'show_archival_detections': True,
        'show_plant_detections': True,
        'show_segmentations': True,
        'show_landmarks': True,
        'ignore_archival_detections_classes': [],
        'ignore_plant_detections_classes': ['leaf_whole', 'specimen'], # Could also include 'leaf_partial' and others if needed
        'ignore_landmark_classes': [],

        'line_width_archival': 12, # Previous value given was 2
        'line_width_plant': 12, # Previous value given was 6
        'line_width_seg': 12, # 12 is specified as "thick"
        'line_width_efd': 12, # 3 is specified as "thick" but 12 is given here
        'alpha_transparency_archival': 0.3,
        'alpha_transparency_plant': 0,
        'alpha_transparency_seg_whole_leaf': 0.4,
        'alpha_transparency_seg_partial_leaf': 0.3
    }

    archival_component_detector_section = {
        'detector_type': 'Archival_Detector',
        'detector_version': 'PREP_final',
        'detector_iteration': 'PREP_final',
        'detector_weights': 'best.pt',
        'minimum_confidence_threshold': 0.5, # Default is 0.5
        'do_save_prediction_overlay_images': True,
        'ignore_objects_for_overlay': []
    }

    # Add the sections to the 'leafmachine' key
    config_data['leafmachine']['do'] = do_section
    config_data['leafmachine']['print'] = print_section
    config_data['leafmachine']['logging'] = logging_section
    config_data['leafmachine']['project'] = project_section
    config_data['leafmachine']['LLM_version'] = LLM_version
    config_data['leafmachine']['use_RGB_label_images'] = use_RGB_label_images
    config_data['leafmachine']['cropped_components'] = cropped_components_section
    config_data['leafmachine']['modules'] = modules_section
    config_data['leafmachine']['data'] = data_section
    config_data['leafmachine']['overlay'] = overlay_section
    config_data['leafmachine']['archival_component_detector'] = archival_component_detector_section

    return config_data, dir_home

def build_api_tests(api):
    dir_home = os.path.dirname(os.path.dirname(__file__))
    path_to_configs = os.path.join(dir_home,'demo','demo_configs')

    dir_home = os.path.dirname(os.path.dirname(__file__))
    dir_images_local = os.path.join(dir_home,'demo','demo_images')
    validate_dir(os.path.join(dir_home,'demo','demo_configs'))
    path_domain_knowledge = os.path.join(dir_home,'domain_knowledge','SLTP_UM_AllAsiaMinimalInRegion.xlsx')
    embeddings_database_name = os.path.splitext(os.path.basename(path_domain_knowledge))[0]
    prefix_removal = ''
    suffix_removal = ''
    catalog_numerical_only = False
    batch_size = 500


    # ### Option 1: "GPT 4" of ["GPT 4", "GPT 3.5", "Azure GPT 4", "Azure GPT 3.5", "PaLM 2"]
    # LLM_version_user = 'Azure GPT 4'
    
    # ### Option 2: False of [False, True]
    # use_LeafMachine2_collage_images = False
    
    # ### Option 3: False of [False, True]
    # use_domain_knowledge = True

    test_results = {}
    if api == 'openai':
        OPT1, OPT2, OPT3 = TestOptionsAPI_openai.get_options()
    elif api == 'palm':
        OPT1, OPT2, OPT3 = TestOptionsAPI_palm.get_options()
    elif api == 'azure_openai':
        OPT1, OPT2, OPT3 = TestOptionsAPI_azure_openai.get_options()
    else:
        raise

    ind = -1
    ind_opt1 = -1
    ind_opt2 = -1
    ind_opt3 = -1

    for opt1 in OPT1:
        ind_opt1+= 1
        for opt2 in OPT2:
            ind_opt2 += 1
            for opt3 in OPT3:
                ind += 1
                ind_opt3 += 1
                
                LLM_version_user = opt1
                use_LeafMachine2_collage_images = opt2
                prompt_version = opt3

                filename = f"{ind}__OPT1-{ind_opt1}__OPT2-{ind_opt2}__OPT3-{ind_opt3}.yaml"
                run_name = f"{ind}__OPT1-{ind_opt1}__OPT2-{ind_opt2}__OPT3-{ind_opt3}"

                dir_output = os.path.join(dir_home,'demo','demo_output','run_name')
                validate_dir(dir_output)
                
                config_data, dir_home = assemble_config(dir_home, run_name, dir_images_local,dir_output,
                    prefix_removal,suffix_removal,catalog_numerical_only,LLM_version_user,batch_size,
                    path_domain_knowledge,embeddings_database_name,use_LeafMachine2_collage_images,
                    prompt_version)
                
                write_config_file(config_data, os.path.join(dir_home,'demo','demo_configs'),filename=filename)

                test_results[run_name] = False
            ind_opt3 = -1
        ind_opt2 = -1
    ind_opt1 = -1
        
    return dir_home, path_to_configs, test_results

def build_demo_tests(llm_version):
    dir_home = os.path.dirname(os.path.dirname(__file__))
    path_to_configs = os.path.join(dir_home,'demo','demo_configs')

    dir_home = os.path.dirname(os.path.dirname(__file__))
    dir_images_local = os.path.join(dir_home,'demo','demo_images')
    validate_dir(os.path.join(dir_home,'demo','demo_configs'))
    path_domain_knowledge = os.path.join(dir_home,'domain_knowledge','SLTP_UM_AllAsiaMinimalInRegion.xlsx')
    embeddings_database_name = os.path.splitext(os.path.basename(path_domain_knowledge))[0]
    prefix_removal = ''
    suffix_removal = ''
    catalog_numerical_only = False
    batch_size = 500


    # ### Option 1: "GPT 4" of ["GPT 4", "GPT 3.5", "Azure GPT 4", "Azure GPT 3.5", "PaLM 2"]
    # LLM_version_user = 'Azure GPT 4'
    
    # ### Option 2: False of [False, True]
    # use_LeafMachine2_collage_images = False
    
    # ### Option 3: False of [False, True]
    # use_domain_knowledge = True

    test_results = {}
    if llm_version == 'gpt':
        OPT1, OPT2, OPT3 = TestOptionsGPT.get_options()
    elif llm_version == 'palm':
        OPT1, OPT2, OPT3 = TestOptionsPalm.get_options()
    else:
        raise

    ind = -1
    ind_opt1 = -1
    ind_opt2 = -1
    ind_opt3 = -1

    for opt1 in OPT1:
        ind_opt1+= 1
        for opt2 in OPT2:
            ind_opt2 += 1
            for opt3 in OPT3:
                ind += 1
                ind_opt3 += 1
                
                LLM_version_user = opt1
                use_LeafMachine2_collage_images = opt2
                prompt_version = opt3

                filename = f"{ind}__OPT1-{ind_opt1}__OPT2-{ind_opt2}__OPT3-{ind_opt3}.yaml"
                run_name = f"{ind}__OPT1-{ind_opt1}__OPT2-{ind_opt2}__OPT3-{ind_opt3}"

                dir_output = os.path.join(dir_home,'demo','demo_output','run_name')
                validate_dir(dir_output)
                

                if llm_version == 'gpt':
                    if prompt_version in ['Version 1']:
                        config_data, dir_home = assemble_config(dir_home, run_name, dir_images_local,dir_output,
                            prefix_removal,suffix_removal,catalog_numerical_only,LLM_version_user,batch_size,
                            path_domain_knowledge,embeddings_database_name,use_LeafMachine2_collage_images,
                            prompt_version, use_domain_knowledge=True)
                    else:
                        config_data, dir_home = assemble_config(dir_home, run_name, dir_images_local,dir_output,
                            prefix_removal,suffix_removal,catalog_numerical_only,LLM_version_user,batch_size,
                            path_domain_knowledge,embeddings_database_name,use_LeafMachine2_collage_images,
                            prompt_version)
                elif llm_version == 'palm':
                    if prompt_version in ['Version 1 PaLM 2']:
                        config_data, dir_home = assemble_config(dir_home, run_name, dir_images_local,dir_output,
                            prefix_removal,suffix_removal,catalog_numerical_only,LLM_version_user,batch_size,
                            path_domain_knowledge,embeddings_database_name,use_LeafMachine2_collage_images,
                            prompt_version, use_domain_knowledge=True)
                    else:
                        config_data, dir_home = assemble_config(dir_home, run_name, dir_images_local,dir_output,
                            prefix_removal,suffix_removal,catalog_numerical_only,LLM_version_user,batch_size,
                            path_domain_knowledge,embeddings_database_name,use_LeafMachine2_collage_images,
                            prompt_version)
                
                
                write_config_file(config_data, os.path.join(dir_home,'demo','demo_configs'),filename=filename)

                test_results[run_name] = False
            ind_opt3 = -1
        ind_opt2 = -1
    ind_opt1 = -1
        
    return dir_home, path_to_configs, test_results

class TestOptionsGPT:
    OPT1 = ["GPT 4", "GPT 3.5", "Azure GPT 4", "Azure GPT 3.5"]
    OPT2 = [False, True]
    OPT3 = ["Version 1", "Version 1 No Domain Knowledge", "Version 2"]

    @classmethod
    def get_options(cls):
        return cls.OPT1, cls.OPT2, cls.OPT3
    @classmethod
    def get_length(cls):
        return 24
    
class TestOptionsPalm:
    OPT1 = ["PaLM 2"]
    OPT2 = [False, True]
    OPT3 = ["Version 1 PaLM 2", "Version 1 PaLM 2 No Domain Knowledge", "Version 2 PaLM 2"]

    @classmethod
    def get_options(cls):
        return cls.OPT1, cls.OPT2, cls.OPT3
    @classmethod
    def get_length(cls):
        return 6
    
class TestOptionsAPI_openai:
    OPT1 = ["GPT 3.5"]
    OPT2 = [False]
    OPT3 = ["Version 2"]

    @classmethod
    def get_options(cls):
        return cls.OPT1, cls.OPT2, cls.OPT3
    @classmethod
    def get_length(cls):
        return 24
    
class TestOptionsAPI_azure_openai:
    OPT1 = ["Azure GPT 3.5"]
    OPT2 = [False]
    OPT3 = ["Version 2"]

    @classmethod
    def get_options(cls):
        return cls.OPT1, cls.OPT2, cls.OPT3
    @classmethod
    def get_length(cls):
        return 24
    
class TestOptionsAPI_palm:
    OPT1 = ["PaLM 2"]
    OPT2 = [False]
    OPT3 = ["Version 2 PaLM 2"]

    @classmethod
    def get_options(cls):
        return cls.OPT1, cls.OPT2, cls.OPT3
    @classmethod
    def get_length(cls):
        return 6
    
def run_demo_tests_GPT(progress_report):
    dir_home, path_to_configs, test_results = build_demo_tests('gpt')
    progress_report.set_n_overall(len(test_results.items()))

    JSON_results = {}

    for ind, (cfg, result) in enumerate(test_results.items()):
        OPT1, OPT2, OPT3 = TestOptionsGPT.get_options()
        
        test_ind, ind_opt1, ind_opt2, ind_opt3 = cfg.split('__')
        opt1_readable = OPT1[int(ind_opt1.split('-')[1])]

        if opt1_readable in ["Azure GPT 4", "Azure GPT 3.5"]:
            api_version = 'gpt-azure'
        elif opt1_readable in ["GPT 4", "GPT 3.5"]:
            api_version = 'gpt'
        else:
            raise

        opt2_readable = "Use LeafMachine2 for Collage Images" if OPT2[int(ind_opt2.split('-')[1])] else "Don't use LeafMachine2 for Collage Images"
        opt3_readable = f"Prompt {OPT3[int(ind_opt3.split('-')[1])]}"
        # Construct the human-readable test name
        human_readable_name = f"{opt1_readable}, {opt2_readable}, {opt3_readable}"
        get_n_overall = progress_report.get_n_overall()
        progress_report.update_overall(f"Test {int(test_ind)+1} of {get_n_overall} --- Validating {human_readable_name}")
        print_main_fail(f"Starting validation test: {human_readable_name}")
        cfg_file_path = os.path.join(path_to_configs,'.'.join([cfg,'yaml']))
        
        if check_API_key(dir_home, api_version) and check_API_key(dir_home, 'google-vision-ocr'):
            try:
                last_JSON_response, total_cost = voucher_vision(cfg_file_path, dir_home, cfg_test=None, progress_report=progress_report, test_ind=int(test_ind))
                test_results[cfg] = True
                JSON_results[ind] = last_JSON_response
            except Exception as e:
                JSON_results[ind] = None
                test_results[cfg] = False
                print(f"An exception occurred: {e}")
                traceback.print_exc()  # This will print the full traceback
        else:
            fail_response = ''
            if not check_API_key(dir_home, 'google-vision-ocr'):
                fail_response += "No API key found for Google Vision OCR"
            if not check_API_key(dir_home, api_version):
                fail_response += f"  +  No API key found for {api_version}"
            test_results[cfg] = False
            JSON_results[ind] = fail_response
            print(f"No API key found for {fail_response}")
            
    return test_results, JSON_results

def run_demo_tests_Palm(progress_report):
    api_version = 'palm'

    dir_home, path_to_configs, test_results = build_demo_tests('palm')
    progress_report.set_n_overall(len(test_results.items()))

    JSON_results = {}

    for ind, (cfg, result) in enumerate(test_results.items()):
        OPT1, OPT2, OPT3 = TestOptionsPalm.get_options()
        test_ind, ind_opt1, ind_opt2, ind_opt3 = cfg.split('__')
        opt1_readable = OPT1[int(ind_opt1.split('-')[1])]
        opt2_readable = "Use LeafMachine2 for Collage Images" if OPT2[int(ind_opt2.split('-')[1])] else "Don't use LeafMachine2 for Collage Images"
        opt3_readable = f"Prompt {OPT3[int(ind_opt3.split('-')[1])]}"
        # opt3_readable = "Use Domain Knowledge" if OPT3[int(ind_opt3.split('-')[1])] else "Don't use Domain Knowledge"
        # Construct the human-readable test name
        human_readable_name = f"{opt1_readable}, {opt2_readable}, {opt3_readable}"
        get_n_overall = progress_report.get_n_overall()
        progress_report.update_overall(f"Test {int(test_ind)+1} of {get_n_overall} --- Validating {human_readable_name}")
        print_main_fail(f"Starting validation test: {human_readable_name}")
        cfg_file_path = os.path.join(path_to_configs,'.'.join([cfg,'yaml']))
        
        if check_API_key(dir_home, api_version) and check_API_key(dir_home, 'google-vision-ocr') :
            try:
                last_JSON_response, total_cost = voucher_vision(cfg_file_path, dir_home, cfg_test=None, progress_report=progress_report, test_ind=int(test_ind))
                test_results[cfg] = True
                JSON_results[ind] = last_JSON_response
            except Exception as e:
                test_results[cfg] = False
                JSON_results[ind] = None
                print(f"An exception occurred: {e}")
                traceback.print_exc()  # This will print the full traceback
        else:
            fail_response = ''
            if not check_API_key(dir_home, 'google-vision-ocr'):
                fail_response += "No API key found for Google Vision OCR"
            if not check_API_key(dir_home, api_version):
                fail_response += f"  +  No API key found for {api_version}"
            test_results[cfg] = False
            JSON_results[ind] = fail_response
            print(f"No API key found for {fail_response}")

    return test_results, JSON_results

def run_api_tests(api):
    try:
        dir_home, path_to_configs, test_results = build_api_tests(api)

        JSON_results = {}

        for ind, (cfg, result) in enumerate(test_results.items()):
            if api == 'openai':
                OPT1, OPT2, OPT3 = TestOptionsAPI_openai.get_options()
            elif 'azure_openai':
                OPT1, OPT2, OPT3 = TestOptionsAPI_azure_openai.get_options()
            elif 'palm':
                OPT1, OPT2, OPT3 = TestOptionsAPI_palm.get_options()
            test_ind, ind_opt1, ind_opt2, ind_opt3 = cfg.split('__')
            opt1_readable = OPT1[int(ind_opt1.split('-')[1])]
            opt2_readable = "Use LeafMachine2 for Collage Images" if OPT2[int(ind_opt2.split('-')[1])] else "Don't use LeafMachine2 for Collage Images"
            opt3_readable = f"Prompt {OPT3[int(ind_opt3.split('-')[1])]}"
            # opt3_readable = "Use Domain Knowledge" if OPT3[int(ind_opt3.split('-')[1])] else "Don't use Domain Knowledge"
            # Construct the human-readable test name
            human_readable_name = f"{opt1_readable}, {opt2_readable}, {opt3_readable}"
            print_main_fail(f"Starting validation test: {human_readable_name}")
            cfg_file_path = os.path.join(path_to_configs,'.'.join([cfg,'yaml']))
            
            if check_API_key(dir_home, api) and check_API_key(dir_home, 'google-vision-ocr') :
                try:
                    last_JSON_response, total_cost = voucher_vision(cfg_file_path, dir_home, None, cfg_test=None, progress_report=None, test_ind=int(test_ind))
                    test_results[cfg] = True
                    JSON_results[ind] = last_JSON_response
                    return True

                except Exception as e:
                    print(e)
                    return False
            else:
                return False
    except Exception as e:
        print(e)
        return False

# def has_API_key(val):
#         if val != '':
#             return True
#         else:
#             return False
def has_API_key(key_name):
    # Check if the environment variable by key_name is not None
    return os.getenv(key_name) is not None

        
# def check_if_usable():
#     dir_home = os.path.dirname(os.path.dirname(__file__))
#     path_cfg_private = os.path.join(dir_home, 'PRIVATE_DATA.yaml')
#     cfg_private = get_cfg_from_full_path(path_cfg_private)

#     has_key_openai = has_API_key(cfg_private['openai']['OPENAI_API_KEY'])

#     has_key_azure_openai = has_API_key(cfg_private['openai_azure']['api_version']) 

#     has_key_palm2 = has_API_key(cfg_private['google_palm']['google_palm_api'])
    
#     has_key_google_OCR = has_API_key(cfg_private['google_cloud']['path_json_file'])

#     if has_key_google_OCR and (has_key_azure_openai or has_key_openai or has_key_palm2):
#         return True
#     else:
#         return False
def check_if_usable():
    has_key_openai = os.getenv('OPENAI_API_KEY') is not None
    has_key_palm2 = os.getenv('PALM') is not None
    has_key_google_OCR = os.getenv('GOOGLE_APPLICATION_CREDENTIALS') is not None

    return has_key_google_OCR and (has_key_openai or has_key_palm2)

# def check_API_key(dir_home, api_version):
#     dir_home = os.path.dirname(os.path.dirname(__file__))
#     path_cfg_private = os.path.join(dir_home, 'PRIVATE_DATA.yaml')
#     cfg_private = get_cfg_from_full_path(path_cfg_private)

#     has_key_openai = has_API_key(cfg_private['openai']['OPENAI_API_KEY'])

#     has_key_azure_openai = has_API_key(cfg_private['openai_azure']['api_version']) 

#     has_key_palm2 = has_API_key(cfg_private['google_palm']['google_palm_api'])

#     has_key_google_OCR = has_API_key(cfg_private['google_cloud']['path_json_file'])

#     if api_version == 'palm' and has_key_palm2:
#         return True
#     elif api_version in ['gpt','openai'] and has_key_openai:
#         return True
#     elif api_version in ['gpt-azure', 'azure_openai'] and has_key_azure_openai:
#         return True
#     elif api_version == 'google-vision-ocr' and has_key_google_OCR:
#         return True
#     else:
#         return False
def check_API_key(api_version):
    # The API keys are assumed to be set in the environment variables
    has_key_openai = os.getenv('OPENAI_API_KEY') is not None
    has_key_palm2 = os.getenv('PALM') is not None
    has_key_google_OCR = os.getenv('GOOGLE_APPLICATION_CREDENTIALS') is not None

    # Depending on the api_version, check if the corresponding key is present
    if api_version == 'palm' and has_key_palm2:
        return True
    elif api_version in ['gpt', 'openai'] and has_key_openai:
        return True
    elif api_version == 'google-vision-ocr' and has_key_google_OCR:
        return True
    else:
        return False