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

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
.streamlit/config.toml CHANGED
@@ -1,4 +1,5 @@
1
  [theme]
2
  primaryColor = "#00ff00"
3
  backgroundColor="#1a1a1a"
4
- secondaryBackgroundColor="#5c5c5c"
 
 
1
  [theme]
2
  primaryColor = "#00ff00"
3
  backgroundColor="#1a1a1a"
4
+ secondaryBackgroundColor="#303030"
5
+ textColor = "cccccc"
vouchervision/API_validation.py CHANGED
@@ -7,10 +7,10 @@ from langchain_openai import AzureChatOpenAI
7
  from vertexai.language_models import TextGenerationModel
8
  from vertexai.preview.generative_models import GenerativeModel
9
  from google.cloud import vision
10
- from google.oauth2 import service_account
11
  from datetime import datetime
12
  import google.generativeai as genai
13
-
 
14
 
15
 
16
  class APIvalidation:
@@ -181,6 +181,20 @@ class APIvalidation:
181
 
182
  return temp_filename
183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
 
185
  def check_google_vertex_genai_api_key(self):
186
  results = {"palm2": False, "gemini": False}
@@ -213,6 +227,7 @@ class APIvalidation:
213
  except Exception as e:
214
  pass
215
 
 
216
  return results
217
  except Exception as e: # Replace with a more specific exception if possible
218
  return results
@@ -222,7 +237,9 @@ class APIvalidation:
222
  # Assuming genai and vertexai are clients for Google services
223
  # os.environ["GOOGLE_API_KEY"] = os.getenv('PALM_API_KEY')
224
  os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = self.get_google_credentials()
225
- vertexai.init(project= os.getenv('GOOGLE_PROJECT_ID'), location=os.getenv('GOOGLE_LOCATION'))
 
 
226
 
227
  try:
228
  model = TextGenerationModel.from_pretrained("text-bison@001")
 
7
  from vertexai.language_models import TextGenerationModel
8
  from vertexai.preview.generative_models import GenerativeModel
9
  from google.cloud import vision
 
10
  from datetime import datetime
11
  import google.generativeai as genai
12
+ from google.oauth2 import service_account
13
+ from googleapiclient.discovery import build
14
 
15
 
16
  class APIvalidation:
 
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}
 
227
  except Exception as e:
228
  pass
229
 
230
+
231
  return results
232
  except Exception as e: # Replace with a more specific exception if possible
233
  return results
 
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")
vouchervision/general_utils.py CHANGED
@@ -294,7 +294,7 @@ def test_GPU():
294
  success = True
295
  else:
296
  info.append("No GPU found!")
297
- info.append("LeafMachine2 image cropping and embedding search will be slow or not possible.")
298
 
299
  return success, info
300
 
 
294
  success = True
295
  else:
296
  info.append("No GPU found!")
297
+ info.append("LeafMachine2 collages will run slowly, trOCR may not be available.")
298
 
299
  return success, info
300
 
vouchervision/utils_VoucherVision.py CHANGED
@@ -7,6 +7,8 @@ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
7
  from langchain_openai import AzureChatOpenAI
8
  from OCR_google_cloud_vision import OCRGoogle
9
  import google.generativeai as genai
 
 
10
 
11
  from vouchervision.LLM_OpenAI import OpenAIHandler
12
  from vouchervision.LLM_GooglePalm2 import GooglePalm2Handler
@@ -418,11 +420,26 @@ class VoucherVision():
418
 
419
  return temp_filename
420
 
421
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
  def set_API_keys(self):
423
  if self.is_hf:
424
  openai_api_key = os.getenv('OPENAI_API_KEY')
425
- google_application_credentials = os.getenv('GOOGLE_APPLICATION_CREDENTIALS')
426
  palm_api_key = os.getenv('PALM_API_KEY')
427
  mistral_api_key = os.getenv('MISTRAL_API_KEY')
428
  here_api_key = os.getenv('here_api_key')
@@ -430,12 +447,12 @@ class VoucherVision():
430
  open_cage_api_key = os.getenv('open_cage_geocode')
431
  google_project_id = os.getenv('GOOGLE_PROJECT_ID')
432
  google_project_location = os.getenv('GOOGLE_LOCATION')
433
- os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = self.get_google_credentials()
434
 
435
 
436
 
437
  self.has_key_openai = openai_api_key is not None
438
- self.has_key_google_OCR = google_application_credentials is not None
439
  self.has_key_palm2 = palm_api_key is not None
440
  self.has_key_mistral = mistral_api_key is not None
441
  self.has_key_here = here_api_key is not None
@@ -444,12 +461,29 @@ class VoucherVision():
444
  self.has_key_google_project_id = google_project_id is not None
445
  self.has_key_google_project_location = google_project_location is not None
446
 
447
- if self.has_key_openai:
448
- openai.api_key = openai_api_key
 
449
 
450
- if self.has_key_google_project_id and self.has_key_google_project_location:
451
- genai.configure(api_key=os.getenv('PALM_API_KEY'))
452
- vertexai.init(project=os.getenv('GOOGLE_PROJECT_ID'), location=os.getenv('GOOGLE_LOCATION'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
453
 
454
  if os.getenv('AZURE_API_KEY') is not None:
455
  azure_api_version = os.getenv('AZURE_API_VERSION')
 
7
  from langchain_openai import AzureChatOpenAI
8
  from OCR_google_cloud_vision import OCRGoogle
9
  import google.generativeai as genai
10
+ from google.oauth2 import service_account
11
+ from googleapiclient.discovery import build
12
 
13
  from vouchervision.LLM_OpenAI import OpenAIHandler
14
  from vouchervision.LLM_GooglePalm2 import GooglePalm2Handler
 
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):
440
  if self.is_hf:
441
  openai_api_key = os.getenv('OPENAI_API_KEY')
442
+ # google_application_credentials = os.getenv('google_service_account_json')
443
  palm_api_key = os.getenv('PALM_API_KEY')
444
  mistral_api_key = os.getenv('MISTRAL_API_KEY')
445
  here_api_key = os.getenv('here_api_key')
 
447
  open_cage_api_key = os.getenv('open_cage_geocode')
448
  google_project_id = os.getenv('GOOGLE_PROJECT_ID')
449
  google_project_location = os.getenv('GOOGLE_LOCATION')
450
+ # os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = self.get_google_credentials()
451
 
452
 
453
 
454
  self.has_key_openai = openai_api_key is not None
455
+ # self.has_key_google_OCR = google_application_credentials is not None
456
  self.has_key_palm2 = palm_api_key is not None
457
  self.has_key_mistral = mistral_api_key is not None
458
  self.has_key_here = here_api_key is not None
 
461
  self.has_key_google_project_id = google_project_id is not None
462
  self.has_key_google_project_location = google_project_location is not None
463
 
464
+ genai_api_key = os.getenv('PALM_API_KEY')
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:
482
+ # openai.api_key = openai_api_key
483
+
484
+ # if self.has_key_google_project_id and self.has_key_google_project_location:
485
+ # genai.configure(api_key=os.getenv('PALM_API_KEY'))
486
+ # vertexai.init(project=os.getenv('GOOGLE_PROJECT_ID'), location=os.getenv('GOOGLE_LOCATION'))
487
 
488
  if os.getenv('AZURE_API_KEY') is not None:
489
  azure_api_version = os.getenv('AZURE_API_VERSION')