phyloforfun commited on
Commit
a03d740
1 Parent(s): 9945fe7

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
Files changed (1) hide show
  1. vouchervision/API_validation.py +43 -18
vouchervision/API_validation.py CHANGED
@@ -1,4 +1,4 @@
1
- import os, io, openai, vertexai
2
  import google.generativeai as genai
3
  from mistralai.client import MistralClient
4
  from mistralai.models.chat_completion import ChatMessage
@@ -7,6 +7,7 @@ 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 datetime import datetime
11
  import google.generativeai as genai
12
 
@@ -56,14 +57,35 @@ class APIvalidation:
56
  # return False
57
 
58
  try:
59
- logo_path = os.path.join(self.dir_home, 'img','logo.png')
60
- client = vision.ImageAnnotatorClient()
61
- with io.open(logo_path, 'rb') as image_file:
62
- content = image_file.read()
63
- image = vision.Image(content=content)
64
- response = client.document_text_detection(image=image)
65
- texts = response.text_annotations
66
- normal_cleaned_text = texts[0].description if texts else None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  if normal_cleaned_text:
68
  return True
69
  else:
@@ -240,6 +262,17 @@ class APIvalidation:
240
 
241
 
242
  # Check each key and add to the respective list
 
 
 
 
 
 
 
 
 
 
 
243
  # OpenAI key check
244
  if self.has_API_key(k_OPENAI_API_KEY):
245
  is_valid = self.check_openai_api_key()
@@ -274,15 +307,7 @@ class APIvalidation:
274
  else:
275
  missing_keys.append('Google VertexAI/GenAI')
276
 
277
- # Google OCR key check
278
- if self.has_API_key(k_google_palm_api) and self.has_API_key(k_project_id) and self.has_API_key(k_location):
279
- is_valid = self.check_google_ocr_api_key()
280
- if is_valid:
281
- present_keys.append('Google OCR (Valid)')
282
- else:
283
- present_keys.append('Google OCR (Invalid)')
284
- else:
285
- missing_keys.append('Google OCR')
286
 
287
  # Mistral key check
288
  if self.has_API_key(k_mistral):
 
1
+ import os, io, openai, vertexai, json
2
  import google.generativeai as genai
3
  from mistralai.client import MistralClient
4
  from mistralai.models.chat_completion import ChatMessage
 
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
 
 
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
+ else:
81
+ logo_path = os.path.join(self.dir_home, 'img','logo.png')
82
+ client = vision.ImageAnnotatorClient()
83
+ with io.open(logo_path, 'rb') as image_file:
84
+ content = image_file.read()
85
+ image = vision.Image(content=content)
86
+ response = client.document_text_detection(image=image)
87
+ texts = response.text_annotations
88
+ normal_cleaned_text = texts[0].description if texts else None
89
  if normal_cleaned_text:
90
  return True
91
  else:
 
262
 
263
 
264
  # Check each key and add to the respective list
265
+ # Google OCR key check
266
+ if self.has_API_key(k_google_palm_api) and self.has_API_key(k_project_id) and self.has_API_key(k_location):
267
+ is_valid = self.check_google_ocr_api_key()
268
+ if is_valid:
269
+ present_keys.append('Google OCR (Valid)')
270
+ else:
271
+ present_keys.append('Google OCR (Invalid)')
272
+ else:
273
+ missing_keys.append('Google OCR')
274
+
275
+
276
  # OpenAI key check
277
  if self.has_API_key(k_OPENAI_API_KEY):
278
  is_valid = self.check_openai_api_key()
 
307
  else:
308
  missing_keys.append('Google VertexAI/GenAI')
309
 
310
+
 
 
 
 
 
 
 
 
311
 
312
  # Mistral key check
313
  if self.has_API_key(k_mistral):