phyloforfun commited on
Commit
6965e7c
1 Parent(s): 582e440

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
@@ -1468,7 +1468,7 @@ def check_api_key_status():
1468
  except:
1469
  cfg_private = None
1470
 
1471
- API_Validator = APIvalidation(cfg_private, st.session_state.dir_home)
1472
  present_keys, missing_keys, date_of_check = API_Validator.report_api_key_status() # Assuming this function returns two lists
1473
 
1474
  # Prepare annotations for present keys
 
1468
  except:
1469
  cfg_private = None
1470
 
1471
+ API_Validator = APIvalidation(cfg_private, st.session_state.dir_home, st.session_state['is_hf'])
1472
  present_keys, missing_keys, date_of_check = API_Validator.report_api_key_status() # Assuming this function returns two lists
1473
 
1474
  # Prepare annotations for present keys
vouchervision/API_validation.py CHANGED
@@ -14,9 +14,10 @@ from googleapiclient.discovery import build
14
 
15
  class APIvalidation:
16
 
17
- def __init__(self, cfg_private, dir_home) -> None:
18
  self.cfg_private = cfg_private
19
  self.dir_home = dir_home
 
20
  self.formatted_date = self.get_formatted_date()
21
 
22
  def get_formatted_date(self):
@@ -94,7 +95,7 @@ class APIvalidation:
94
  # return False
95
 
96
  def check_azure_openai_api_key(self):
97
- if self.cfg_private:
98
  try:
99
  # Initialize the Azure OpenAI client
100
  model = AzureChatOpenAI(
@@ -145,7 +146,7 @@ class APIvalidation:
145
 
146
  def check_mistral_api_key(self):
147
  try:
148
- if self.cfg_private:
149
  client = MistralClient(api_key=self.cfg_private['mistral']['mistral_key'])
150
  else:
151
  client = MistralClient(api_key=os.getenv('MISTRAL_API_KEY'))
@@ -179,6 +180,10 @@ class APIvalidation:
179
  # temp_filename = temp.name
180
 
181
  # return temp_filename
 
 
 
 
182
 
183
  # def init_google_client(opt, opt2):
184
  # # Fetch the credentials JSON string from Hugging Face Secrets
@@ -197,7 +202,7 @@ class APIvalidation:
197
 
198
  def check_google_vertex_genai_api_key(self):
199
  results = {"palm2": False, "gemini": False}
200
- if self.cfg_private:
201
  try: # Local
202
  # Assuming genai and vertexai are clients for Google services
203
  # os.environ["GOOGLE_API_KEY"] = self.cfg_private['google_palm']['google_palm_api']
@@ -238,13 +243,15 @@ class APIvalidation:
238
  # os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = self.get_google_credentials()
239
  # client, credentials = self.init_google_client('gemini-pro', 'v1')
240
  # print(credentials)
 
241
  palm_api_key = os.getenv('PALM_API_KEY')
242
  google_project_id = os.getenv('GOOGLE_PROJECT_ID')
243
  google_location = os.getenv('GOOGLE_LOCATION')
244
  os.environ['GOOGLE_API_KEY'] = os.getenv('PALM_API_KEY')
245
- vertexai.init(project=os.getenv('GOOGLE_PROJECT_ID'), location=os.getenv('GOOGLE_LOCATION'))
246
  # genai.configure(api_key=palm_api_key)
247
  # vertexai.init(project=google_project_id, location=google_location)#, credentials=credentials)
 
248
 
249
  try:
250
  model = TextGenerationModel.from_pretrained("text-bison@001")
@@ -283,11 +290,11 @@ class APIvalidation:
283
  print(f"Immediate [{e}]")
284
  return results
285
 
286
- def report_api_key_status(self):
287
  missing_keys = []
288
  present_keys = []
289
 
290
- if self.cfg_private:
291
  k_OPENAI_API_KEY = self.cfg_private['openai']['OPENAI_API_KEY']
292
  k_openai_azure = self.cfg_private['openai_azure']['api_version']
293
  k_google_palm_api = self.cfg_private['google_palm']['google_palm_api']
 
14
 
15
  class APIvalidation:
16
 
17
+ def __init__(self, cfg_private, dir_home, is_hf) -> None:
18
  self.cfg_private = cfg_private
19
  self.dir_home = dir_home
20
+ self.is_hf = is_hf
21
  self.formatted_date = self.get_formatted_date()
22
 
23
  def get_formatted_date(self):
 
95
  # return False
96
 
97
  def check_azure_openai_api_key(self):
98
+ if not self.is_hf:
99
  try:
100
  # Initialize the Azure OpenAI client
101
  model = AzureChatOpenAI(
 
146
 
147
  def check_mistral_api_key(self):
148
  try:
149
+ if not self.is_hf:
150
  client = MistralClient(api_key=self.cfg_private['mistral']['mistral_key'])
151
  else:
152
  client = MistralClient(api_key=os.getenv('MISTRAL_API_KEY'))
 
180
  # temp_filename = temp.name
181
 
182
  # return temp_filename
183
+ def get_google_credentials(self):
184
+ creds_json_str = os.getenv('GOOGLE_SERVICE_ACCOUNT_JSON')
185
+ credentials = service_account.Credentials.from_service_account_info(json.loads(creds_json_str))
186
+ return credentials
187
 
188
  # def init_google_client(opt, opt2):
189
  # # Fetch the credentials JSON string from Hugging Face Secrets
 
202
 
203
  def check_google_vertex_genai_api_key(self):
204
  results = {"palm2": False, "gemini": False}
205
+ if not self.is_hf:
206
  try: # Local
207
  # Assuming genai and vertexai are clients for Google services
208
  # os.environ["GOOGLE_API_KEY"] = self.cfg_private['google_palm']['google_palm_api']
 
243
  # os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = self.get_google_credentials()
244
  # client, credentials = self.init_google_client('gemini-pro', 'v1')
245
  # print(credentials)
246
+ print("service account")
247
  palm_api_key = os.getenv('PALM_API_KEY')
248
  google_project_id = os.getenv('GOOGLE_PROJECT_ID')
249
  google_location = os.getenv('GOOGLE_LOCATION')
250
  os.environ['GOOGLE_API_KEY'] = os.getenv('PALM_API_KEY')
251
+ vertexai.init(project=os.getenv('GOOGLE_PROJECT_ID'), location=os.getenv('GOOGLE_LOCATION'),credentials=self.get_google_credentials())
252
  # genai.configure(api_key=palm_api_key)
253
  # vertexai.init(project=google_project_id, location=google_location)#, credentials=credentials)
254
+ print("service account pass")
255
 
256
  try:
257
  model = TextGenerationModel.from_pretrained("text-bison@001")
 
290
  print(f"Immediate [{e}]")
291
  return results
292
 
293
+ def report_api_key_status(self, is_hf):
294
  missing_keys = []
295
  present_keys = []
296
 
297
+ if not self.is_hf:
298
  k_OPENAI_API_KEY = self.cfg_private['openai']['OPENAI_API_KEY']
299
  k_openai_azure = self.cfg_private['openai_azure']['api_version']
300
  k_google_palm_api = self.cfg_private['google_palm']['google_palm_api']
vouchervision/utils_VoucherVision.py CHANGED
@@ -434,7 +434,10 @@ class VoucherVision():
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:
@@ -461,9 +464,13 @@ class VoucherVision():
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
  os.environ['GOOGLE_API_KEY'] = os.getenv('PALM_API_KEY')
466
  vertexai.init(project=os.getenv('GOOGLE_PROJECT_ID'), location=os.getenv('GOOGLE_LOCATION'))
 
 
 
 
467
  # try:
468
  # if genai_api_key:
469
  # genai.configure(api_key=genai_api_key)
@@ -551,7 +558,7 @@ class VoucherVision():
551
  vertexai.init(project=os.environ['GOOGLE_PROJECT_ID'], location=os.environ['GOOGLE_LOCATION'])
552
 
553
 
554
-
555
  # os.environ.pop("GOOGLE_APPLICATION_CREDENTIALS", None)
556
  # os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = self.cfg_private['google_cloud']['path_json_file'] ####
557
  # os.environ['GOOGLE_API_KEY'] = self.cfg_private['google_palm']['google_palm_api']
 
434
  # return client, credentials
435
  # else:
436
  # print("Google API credentials not found.")
437
+ def get_google_credentials(self):
438
+ creds_json_str = os.getenv('GOOGLE_SERVICE_ACCOUNT_JSON')
439
+ credentials = service_account.Credentials.from_service_account_info(json.loads(creds_json_str))
440
+ return credentials
441
 
442
  def set_API_keys(self):
443
  if self.is_hf:
 
464
  self.has_key_google_project_id = google_project_id is not None
465
  self.has_key_google_project_location = google_project_location is not None
466
 
467
+ # genai_api_key = os.getenv('PALM_API_KEY')
468
  os.environ['GOOGLE_API_KEY'] = os.getenv('PALM_API_KEY')
469
  vertexai.init(project=os.getenv('GOOGLE_PROJECT_ID'), location=os.getenv('GOOGLE_LOCATION'))
470
+
471
+
472
+ os.environ['GOOGLE_API_KEY'] = os.getenv('PALM_API_KEY')
473
+ vertexai.init(project=os.getenv('GOOGLE_PROJECT_ID'), location=os.getenv('GOOGLE_LOCATION'),credentials=self.get_google_credentials())
474
  # try:
475
  # if genai_api_key:
476
  # genai.configure(api_key=genai_api_key)
 
558
  vertexai.init(project=os.environ['GOOGLE_PROJECT_ID'], location=os.environ['GOOGLE_LOCATION'])
559
 
560
 
561
+
562
  # os.environ.pop("GOOGLE_APPLICATION_CREDENTIALS", None)
563
  # os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = self.cfg_private['google_cloud']['path_json_file'] ####
564
  # os.environ['GOOGLE_API_KEY'] = self.cfg_private['google_palm']['google_palm_api']