phyloforfun commited on
Commit
f56fafe
1 Parent(s): 5590fea

Major update. Support for 15 LLMs, World Flora Online taxonomy validation, geolocation, 2 OCR methods, significant UI changes, stability improvements, consistent JSON parsing

Browse files
app.py CHANGED
@@ -2454,4 +2454,29 @@ if st.session_state.proceed_to_build_llm_prompt:
2454
  # elif st.session_state.proceed_to_private and not st.session_state['is_hf']:
2455
  # create_private_file()
2456
  elif st.session_state.proceed_to_main:
2457
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2454
  # elif st.session_state.proceed_to_private and not st.session_state['is_hf']:
2455
  # create_private_file()
2456
  elif st.session_state.proceed_to_main:
2457
+ main()
2458
+
2459
+
2460
+
2461
+
2462
+
2463
+
2464
+
2465
+ # print(f"proceed_to_main3 {st.session_state['proceed_to_main']}")
2466
+ # print(f"is_hf3 {st.session_state['is_hf']}")
2467
+ # print(f"private_file3 {st.session_state['private_file']}")
2468
+ # print(f"proceed_to_build_llm_prompt3 {st.session_state['proceed_to_build_llm_prompt']}")
2469
+ # print(f"proceed_to_private3 {st.session_state['proceed_to_private']}")
2470
+
2471
+ # # if not st.session_state.private_file and not st.session_state['is_hf']:
2472
+ # # create_private_file()
2473
+ # # elif st.session_state.proceed_to_build_llm_prompt:
2474
+ # if st.session_state.proceed_to_build_llm_prompt:
2475
+ # build_LLM_prompt_config()
2476
+ # # elif st.session_state.proceed_to_private and not st.session_state['is_hf']:
2477
+ # # create_private_file()
2478
+ # elif st.session_state.proceed_to_main:
2479
+ # main()
2480
+
2481
+
2482
+
vouchervision/API_validation.py CHANGED
@@ -48,51 +48,51 @@ class APIvalidation:
48
  except:
49
  return False
50
 
51
- def check_google_ocr_api_key(self):
52
- # if os.path.exists(self.cfg_private['google_cloud']['path_json_file']):
53
- # os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = self.cfg_private['google_cloud']['path_json_file']
54
- # elif os.path.exists(self.cfg_private['google_cloud']['path_json_file_service_account2']):
55
- # os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = self.cfg_private['google_cloud']['path_json_file_service_account2']
56
- # else:
57
- # return False
58
 
59
- try:
60
- if not self.cfg_private:
61
- # Convert JSON key from string to a dictionary
62
- service_account_json_str = os.getenv('google_service_account_json')
63
- if not service_account_json_str:
64
- print("Service account JSON not found in environment variables.")
65
- return False
66
-
67
- # Convert JSON string to a dictionary
68
- service_account_info = json.loads(service_account_json_str)
69
- # Create credentials from the service account info
70
- credentials = service_account.Credentials.from_service_account_info(service_account_info)
71
- # Initialize the client with the credentials
72
- client = vision.ImageAnnotatorClient(credentials=credentials)
73
- logo_path = os.path.join(self.dir_home, 'img','logo.png')
74
- with io.open(logo_path, 'rb') as image_file:
75
- content = image_file.read()
76
- image = vision.Image(content=content)
77
- response = client.document_text_detection(image=image)
78
- texts = response.text_annotations
79
- normal_cleaned_text = texts[0].description if texts else None
80
- print(f"OCR TEST: {normal_cleaned_text}")
81
- else:
82
- logo_path = os.path.join(self.dir_home, 'img','logo.png')
83
- client = vision.ImageAnnotatorClient()
84
- with io.open(logo_path, 'rb') as image_file:
85
- content = image_file.read()
86
- image = vision.Image(content=content)
87
- response = client.document_text_detection(image=image)
88
- texts = response.text_annotations
89
- normal_cleaned_text = texts[0].description if texts else None
90
- if normal_cleaned_text:
91
- return True
92
- else:
93
- return False
94
- except:
95
- return False
96
 
97
  def check_azure_openai_api_key(self):
98
  if self.cfg_private:
@@ -171,30 +171,30 @@ class APIvalidation:
171
  except Exception as e: # Replace with a more specific exception if possible
172
  return False
173
 
174
- def get_google_credentials(self):
175
- # Convert JSON key from string to a dictionary
176
- service_account_json_str = os.getenv('google_service_account_json')
177
 
178
- with tempfile.NamedTemporaryFile(mode="w+", delete=False,suffix=".json") as temp:
179
- temp.write(service_account_json_str)
180
- temp_filename = temp.name
181
 
182
- return temp_filename
183
 
184
- def init_google_client(opt, opt2):
185
- # Fetch the credentials JSON string from Hugging Face Secrets
186
- creds_json_str = os.getenv('google_service_account_json')
187
 
188
- if creds_json_str:
189
- creds_dict = json.loads(creds_json_str)
190
- credentials = service_account.Credentials.from_service_account_info(creds_dict)
191
 
192
- # Initialize Google API client (if needed for your use case)
193
- client = build(opt, opt2, credentials=credentials) # Adjust with actual service details
194
 
195
- return client, credentials
196
- else:
197
- print("Google API credentials not found.")
198
 
199
  def check_google_vertex_genai_api_key(self):
200
  results = {"palm2": False, "gemini": False}
@@ -236,10 +236,14 @@ class APIvalidation:
236
  try:
237
  # Assuming genai and vertexai are clients for Google services
238
  # os.environ["GOOGLE_API_KEY"] = os.getenv('PALM_API_KEY')
239
- os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = self.get_google_credentials()
240
- client, credentials = self.init_google_client('gemini-pro', 'v1')
241
- print(credentials)
242
- vertexai.init(project= os.getenv('GOOGLE_PROJECT_ID'), location=os.getenv('GOOGLE_LOCATION'), credentials=credentials)
 
 
 
 
243
 
244
  try:
245
  model = TextGenerationModel.from_pretrained("text-bison@001")
@@ -305,7 +309,7 @@ class APIvalidation:
305
  # Check each key and add to the respective list
306
  # Google OCR key check
307
  if self.has_API_key(k_google_palm_api) and self.has_API_key(k_project_id) and self.has_API_key(k_location):
308
- is_valid = self.check_google_ocr_api_key()
309
  if is_valid:
310
  present_keys.append('Google OCR (Valid)')
311
  else:
 
48
  except:
49
  return False
50
 
51
+ # def check_google_ocr_api_key(self): ##################################################################################### maybe check without initi, post the vertexai
52
+ # # if os.path.exists(self.cfg_private['google_cloud']['path_json_file']):
53
+ # # os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = self.cfg_private['google_cloud']['path_json_file']
54
+ # # elif os.path.exists(self.cfg_private['google_cloud']['path_json_file_service_account2']):
55
+ # # os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = self.cfg_private['google_cloud']['path_json_file_service_account2']
56
+ # # else:
57
+ # # return False
58
 
59
+ # try:
60
+ # if not self.cfg_private:
61
+ # # Convert JSON key from string to a dictionary
62
+ # service_account_json_str = os.getenv('google_service_account_json')
63
+ # if not service_account_json_str:
64
+ # print("Service account JSON not found in environment variables.")
65
+ # return False
66
+
67
+ # # Convert JSON string to a dictionary
68
+ # service_account_info = json.loads(service_account_json_str)
69
+ # # Create credentials from the service account info
70
+ # credentials = service_account.Credentials.from_service_account_info(service_account_info)
71
+ # # Initialize the client with the credentials
72
+ # client = vision.ImageAnnotatorClient(credentials=credentials)
73
+ # logo_path = os.path.join(self.dir_home, 'img','logo.png')
74
+ # with io.open(logo_path, 'rb') as image_file:
75
+ # content = image_file.read()
76
+ # image = vision.Image(content=content)
77
+ # response = client.document_text_detection(image=image)
78
+ # texts = response.text_annotations
79
+ # normal_cleaned_text = texts[0].description if texts else None
80
+ # print(f"OCR TEST: {normal_cleaned_text}")
81
+ # else:
82
+ # logo_path = os.path.join(self.dir_home, 'img','logo.png')
83
+ # client = vision.ImageAnnotatorClient()
84
+ # with io.open(logo_path, 'rb') as image_file:
85
+ # content = image_file.read()
86
+ # image = vision.Image(content=content)
87
+ # response = client.document_text_detection(image=image)
88
+ # texts = response.text_annotations
89
+ # normal_cleaned_text = texts[0].description if texts else None
90
+ # if normal_cleaned_text:
91
+ # return True
92
+ # else:
93
+ # return False
94
+ # except:
95
+ # return False
96
 
97
  def check_azure_openai_api_key(self):
98
  if self.cfg_private:
 
171
  except Exception as e: # Replace with a more specific exception if possible
172
  return False
173
 
174
+ # def get_google_credentials(self):
175
+ # # Convert JSON key from string to a dictionary
176
+ # service_account_json_str = os.getenv('google_service_account_json')
177
 
178
+ # with tempfile.NamedTemporaryFile(mode="w+", delete=False,suffix=".json") as temp:
179
+ # temp.write(service_account_json_str)
180
+ # temp_filename = temp.name
181
 
182
+ # return temp_filename
183
 
184
+ # def init_google_client(opt, opt2):
185
+ # # Fetch the credentials JSON string from Hugging Face Secrets
186
+ # creds_json_str = os.getenv('google_service_account_json')
187
 
188
+ # if creds_json_str:
189
+ # creds_dict = json.loads(creds_json_str)
190
+ # credentials = service_account.Credentials.from_service_account_info(creds_dict)
191
 
192
+ # # Initialize Google API client (if needed for your use case)
193
+ # client = build(opt, opt2, credentials=credentials) # Adjust with actual service details
194
 
195
+ # return client, credentials
196
+ # else:
197
+ # print("Google API credentials not found.")
198
 
199
  def check_google_vertex_genai_api_key(self):
200
  results = {"palm2": False, "gemini": False}
 
236
  try:
237
  # Assuming genai and vertexai are clients for Google services
238
  # os.environ["GOOGLE_API_KEY"] = os.getenv('PALM_API_KEY')
239
+ # os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = self.get_google_credentials()
240
+ # client, credentials = self.init_google_client('gemini-pro', 'v1')
241
+ # print(credentials)
242
+ palm_api_key = os.getenv('PALM_API_KEY')
243
+ google_project_id = os.getenv('GOOGLE_PROJECT_ID')
244
+ google_location = os.getenv('GOOGLE_LOCATION')
245
+
246
+ vertexai.init(project=google_project_id, location=google_location)#, credentials=credentials)
247
 
248
  try:
249
  model = TextGenerationModel.from_pretrained("text-bison@001")
 
309
  # Check each key and add to the respective list
310
  # Google OCR key check
311
  if self.has_API_key(k_google_palm_api) and self.has_API_key(k_project_id) and self.has_API_key(k_location):
312
+ is_valid = True #self.check_google_ocr_api_key() ###############################################################################################################################
313
  if is_valid:
314
  present_keys.append('Google OCR (Valid)')
315
  else:
vouchervision/utils_VoucherVision.py CHANGED
@@ -410,30 +410,30 @@ class VoucherVision():
410
  else:
411
  return False
412
 
413
- def get_google_credentials(self):
414
- # Convert JSON key from string to a dictionary
415
- service_account_json_str = os.getenv('google_service_account_json')
416
 
417
- with tempfile.NamedTemporaryFile(mode="w+", delete=False,suffix=".json") as temp:
418
- temp.write(service_account_json_str)
419
- temp_filename = temp.name
420
 
421
- return temp_filename
422
 
423
- def init_google_client(opt, opt2):
424
- # Fetch the credentials JSON string from Hugging Face Secrets
425
- creds_json_str = os.getenv('google_service_account_json')
426
 
427
- if creds_json_str:
428
- creds_dict = json.loads(creds_json_str)
429
- credentials = service_account.Credentials.from_service_account_info(creds_dict)
430
 
431
- # Initialize Google API client (if needed for your use case)
432
- client = build(opt, opt2, credentials=credentials) # Adjust with actual service details
433
 
434
- return client, credentials
435
- else:
436
- print("Google API credentials not found.")
437
 
438
 
439
  def set_API_keys(self):
@@ -465,17 +465,17 @@ class VoucherVision():
465
  google_project_id = os.getenv('GOOGLE_PROJECT_ID')
466
  google_location = os.getenv('GOOGLE_LOCATION')
467
 
468
- try:
469
- if genai_api_key:
470
- genai.configure(api_key=genai_api_key)
471
- except:
472
- print(f"failed genai.configure")
473
 
474
  try:
475
  if google_project_id and google_location:
476
- clien, credentials = self.init_google_client('gemini-pro', 'v1')
477
- print(credentials)
478
- vertexai.init(project=google_project_id, location=google_location, credentials=credentials)
479
  except:
480
  print(f"failed vertexai.init")
481
  # if self.has_key_openai:
 
410
  else:
411
  return False
412
 
413
+ # def get_google_credentials(self):
414
+ # # Convert JSON key from string to a dictionary
415
+ # service_account_json_str = os.getenv('google_service_account_json')
416
 
417
+ # with tempfile.NamedTemporaryFile(mode="w+", delete=False,suffix=".json") as temp:
418
+ # temp.write(service_account_json_str)
419
+ # temp_filename = temp.name
420
 
421
+ # return temp_filename
422
 
423
+ # def init_google_client(opt, opt2):
424
+ # # Fetch the credentials JSON string from Hugging Face Secrets
425
+ # creds_json_str = os.getenv('google_service_account_json')
426
 
427
+ # if creds_json_str:
428
+ # creds_dict = json.loads(creds_json_str)
429
+ # credentials = service_account.Credentials.from_service_account_info(creds_dict)
430
 
431
+ # # Initialize Google API client (if needed for your use case)
432
+ # client = build(opt, opt2, credentials=credentials) # Adjust with actual service details
433
 
434
+ # return client, credentials
435
+ # else:
436
+ # print("Google API credentials not found.")
437
 
438
 
439
  def set_API_keys(self):
 
465
  google_project_id = os.getenv('GOOGLE_PROJECT_ID')
466
  google_location = os.getenv('GOOGLE_LOCATION')
467
 
468
+ # try:
469
+ # if genai_api_key:
470
+ # genai.configure(api_key=genai_api_key)
471
+ # except:
472
+ # print(f"failed genai.configure")
473
 
474
  try:
475
  if google_project_id and google_location:
476
+ # clien, credentials = self.init_google_client('gemini-pro', 'v1')
477
+ # print(credentials)
478
+ vertexai.init(project=google_project_id, location=google_location)#, credentials=credentials)
479
  except:
480
  print(f"failed vertexai.init")
481
  # if self.has_key_openai: