phyloforfun commited on
Commit
af032b9
1 Parent(s): 1efd92a

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 (2) hide show
  1. app.py +5 -2
  2. vouchervision/API_validation.py +144 -57
app.py CHANGED
@@ -1427,8 +1427,11 @@ def display_api_key_status():
1427
 
1428
 
1429
  def check_api_key_status():
1430
- path_cfg_private = os.path.join(st.session_state.dir_home, 'PRIVATE_DATA.yaml')
1431
- cfg_private = get_cfg_from_full_path(path_cfg_private)
 
 
 
1432
 
1433
  API_Validator = APIvalidation(cfg_private, st.session_state.dir_home)
1434
  present_keys, missing_keys, date_of_check = API_Validator.report_api_key_status() # Assuming this function returns two lists
 
1427
 
1428
 
1429
  def check_api_key_status():
1430
+ try:
1431
+ path_cfg_private = os.path.join(st.session_state.dir_home, 'PRIVATE_DATA.yaml')
1432
+ cfg_private = get_cfg_from_full_path(path_cfg_private)
1433
+ except:
1434
+ cfg_private = None
1435
 
1436
  API_Validator = APIvalidation(cfg_private, st.session_state.dir_home)
1437
  present_keys, missing_keys, date_of_check = API_Validator.report_api_key_status() # Assuming this function returns two lists
vouchervision/API_validation.py CHANGED
@@ -34,7 +34,11 @@ class APIvalidation:
34
  return False
35
 
36
  def check_openai_api_key(self):
37
- openai.api_key = self.cfg_private['openai']['OPENAI_API_KEY']
 
 
 
 
38
  try:
39
  openai.models.list()
40
  return True
@@ -66,32 +70,63 @@ class APIvalidation:
66
  return False
67
 
68
  def check_azure_openai_api_key(self):
69
- try:
70
- # Initialize the Azure OpenAI client
71
- model = AzureChatOpenAI(
72
- deployment_name = 'gpt-35-turbo',#'gpt-35-turbo',
73
- openai_api_version = self.cfg_private['openai_azure']['api_version'],
74
- openai_api_key = self.cfg_private['openai_azure']['openai_api_key'],
75
- azure_endpoint = self.cfg_private['openai_azure']['openai_api_base'],
76
- openai_organization = self.cfg_private['openai_azure']['openai_organization'],
77
- )
78
- msg = HumanMessage(content="hello")
79
- # self.llm_object.temperature = self.config.get('temperature')
80
- response = model([msg])
 
81
 
82
- # Check the response content (you might need to adjust this depending on how your AzureChatOpenAI class handles responses)
83
- if response:
84
- return True
85
- else:
 
 
 
86
  return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
- except Exception as e: # Use a more specific exception if possible
89
- return False
 
 
 
 
 
 
90
 
91
  def check_mistral_api_key(self):
 
 
 
 
 
92
  try:
93
  # Initialize the Mistral Client with the API key
94
- client = MistralClient(api_key=self.cfg_private['mistral']['mistral_key'])
95
 
96
  # Create a simple message
97
  messages = [ChatMessage(role="user", content="hello")]
@@ -112,46 +147,98 @@ class APIvalidation:
112
 
113
  def check_google_vertex_genai_api_key(self):
114
  results = {"palm2": False, "gemini": False}
115
- try:
116
- # Assuming genai and vertexai are clients for Google services
117
- os.environ["GOOGLE_API_KEY"] = self.cfg_private['google_palm']['google_palm_api']
118
- # genai.configure(api_key=self.cfg_private['google_palm']['google_palm_api'])
119
- vertexai.init(project= self.cfg_private['google_palm']['project_id'], location=self.cfg_private['google_palm']['location'])
120
-
121
  try:
122
- model = TextGenerationModel.from_pretrained("text-bison@001")
123
- response = model.predict("Hello")
124
- test_response_palm = response.text
125
- # llm_palm = ChatGoogleGenerativeAI(model="text-bison@001")
126
- # test_response_palm = llm_palm.invoke("Hello")
127
- if test_response_palm:
128
- results["palm2"] = True
129
- except Exception as e:
130
- pass
131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  try:
133
- model = GenerativeModel("gemini-pro")
134
- response = model.generate_content("Hello")
135
- test_response_gemini = response.text
136
- # llm_gemini = ChatGoogleGenerativeAI(model="gemini-pro")
137
- # test_response_gemini = llm_gemini.invoke("Hello")
138
- if test_response_gemini:
139
- results["gemini"] = True
140
- except Exception as e:
141
- pass
142
-
143
- return results
144
- except Exception as e: # Replace with a more specific exception if possible
145
- return results
146
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
  def report_api_key_status(self):
149
  missing_keys = []
150
  present_keys = []
151
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  # Check each key and add to the respective list
153
  # OpenAI key check
154
- if self.has_API_key(self.cfg_private['openai']['OPENAI_API_KEY']):
155
  is_valid = self.check_openai_api_key()
156
  if is_valid:
157
  present_keys.append('OpenAI (Valid)')
@@ -161,7 +248,7 @@ class APIvalidation:
161
  missing_keys.append('OpenAI')
162
 
163
  # Azure OpenAI key check
164
- if self.has_API_key(self.cfg_private['openai_azure']['api_version']):
165
  is_valid = self.check_azure_openai_api_key()
166
  if is_valid:
167
  present_keys.append('Azure OpenAI (Valid)')
@@ -171,7 +258,7 @@ class APIvalidation:
171
  missing_keys.append('Azure OpenAI')
172
 
173
  # Google PALM2/Gemini key check
174
- if self.has_API_key(self.cfg_private['google_palm']['google_palm_api']) and self.has_API_key(self.cfg_private['google_palm']['project_id']) and self.has_API_key(self.cfg_private['google_palm']['location']):
175
  google_results = self.check_google_vertex_genai_api_key()
176
  if google_results['palm2']:
177
  present_keys.append('Palm2 (Valid)')
@@ -185,7 +272,7 @@ class APIvalidation:
185
  missing_keys.append('Google VertexAI/GenAI')
186
 
187
  # Google OCR key check
188
- if self.has_API_key(self.cfg_private['google_palm']['google_palm_api']) and self.has_API_key(self.cfg_private['google_palm']['project_id']) and self.has_API_key(self.cfg_private['google_palm']['location']):
189
  is_valid = self.check_google_ocr_api_key()
190
  if is_valid:
191
  present_keys.append('Google OCR (Valid)')
@@ -195,8 +282,8 @@ class APIvalidation:
195
  missing_keys.append('Google OCR')
196
 
197
  # Mistral key check
198
- if self.has_API_key(self.cfg_private['mistral']['mistral_key']):
199
- is_valid = self.check_mistral_api_key()
200
  if is_valid:
201
  present_keys.append('Mistral (Valid)')
202
  else:
@@ -205,12 +292,12 @@ class APIvalidation:
205
  missing_keys.append('Mistral')
206
 
207
 
208
- if self.has_API_key(self.cfg_private['here']['api_key']):
209
  present_keys.append('HERE Geocode (Valid)')
210
  else:
211
  missing_keys.append('HERE Geocode (Invalid)')
212
 
213
- if self.has_API_key(self.cfg_private['open_cage_geocode']['api_key']):
214
  present_keys.append('OpenCage Geocode (Valid)')
215
  else:
216
  missing_keys.append('OpenCage Geocode (Invalid)')
 
34
  return False
35
 
36
  def check_openai_api_key(self):
37
+ if self.cfg_private:
38
+ openai.api_key = self.cfg_private['openai']['OPENAI_API_KEY']
39
+ else:
40
+ openai.api_key = os.getenv('OPENAI_API_KEY')
41
+
42
  try:
43
  openai.models.list()
44
  return True
 
70
  return False
71
 
72
  def check_azure_openai_api_key(self):
73
+ if self.cfg_private:
74
+ try:
75
+ # Initialize the Azure OpenAI client
76
+ model = AzureChatOpenAI(
77
+ deployment_name = 'gpt-35-turbo',#'gpt-35-turbo',
78
+ openai_api_version = self.cfg_private['openai_azure']['api_version'],
79
+ openai_api_key = self.cfg_private['openai_azure']['openai_api_key'],
80
+ azure_endpoint = self.cfg_private['openai_azure']['openai_api_base'],
81
+ openai_organization = self.cfg_private['openai_azure']['openai_organization'],
82
+ )
83
+ msg = HumanMessage(content="hello")
84
+ # self.llm_object.temperature = self.config.get('temperature')
85
+ response = model([msg])
86
 
87
+ # Check the response content (you might need to adjust this depending on how your AzureChatOpenAI class handles responses)
88
+ if response:
89
+ return True
90
+ else:
91
+ return False
92
+
93
+ except Exception as e: # Use a more specific exception if possible
94
  return False
95
+ else:
96
+ try:
97
+ azure_api_version = os.getenv('AZURE_API_VERSION')
98
+ azure_api_key = os.getenv('AZURE_API_KEY')
99
+ azure_api_base = os.getenv('AZURE_API_BASE')
100
+ azure_organization = os.getenv('AZURE_ORGANIZATION')
101
+ # Initialize the Azure OpenAI client
102
+ model = AzureChatOpenAI(
103
+ deployment_name = 'gpt-35-turbo',#'gpt-35-turbo',
104
+ openai_api_version = azure_api_version,
105
+ openai_api_key = azure_api_key,
106
+ azure_endpoint = azure_api_base,
107
+ openai_organization = azure_organization,
108
+ )
109
+ msg = HumanMessage(content="hello")
110
+ # self.llm_object.temperature = self.config.get('temperature')
111
+ response = model([msg])
112
 
113
+ # Check the response content (you might need to adjust this depending on how your AzureChatOpenAI class handles responses)
114
+ if response:
115
+ return True
116
+ else:
117
+ return False
118
+
119
+ except Exception as e: # Use a more specific exception if possible
120
+ return False
121
 
122
  def check_mistral_api_key(self):
123
+ if self.cfg_private:
124
+ client = MistralClient(api_key=self.cfg_private['mistral']['mistral_key'])
125
+ else:
126
+ client = MistralClient(api_key=os.getenv('MISTRAL_API_KEY'))
127
+
128
  try:
129
  # Initialize the Mistral Client with the API key
 
130
 
131
  # Create a simple message
132
  messages = [ChatMessage(role="user", content="hello")]
 
147
 
148
  def check_google_vertex_genai_api_key(self):
149
  results = {"palm2": False, "gemini": False}
150
+ if self.cfg_private:
 
 
 
 
 
151
  try:
152
+ # Assuming genai and vertexai are clients for Google services
153
+ os.environ["GOOGLE_API_KEY"] = self.cfg_private['google_palm']['google_palm_api']
154
+ # genai.configure(api_key=self.cfg_private['google_palm']['google_palm_api'])
155
+ vertexai.init(project= self.cfg_private['google_palm']['project_id'], location=self.cfg_private['google_palm']['location'])
 
 
 
 
 
156
 
157
+ try:
158
+ model = TextGenerationModel.from_pretrained("text-bison@001")
159
+ response = model.predict("Hello")
160
+ test_response_palm = response.text
161
+ # llm_palm = ChatGoogleGenerativeAI(model="text-bison@001")
162
+ # test_response_palm = llm_palm.invoke("Hello")
163
+ if test_response_palm:
164
+ results["palm2"] = True
165
+ except Exception as e:
166
+ pass
167
+
168
+ try:
169
+ model = GenerativeModel("gemini-pro")
170
+ response = model.generate_content("Hello")
171
+ test_response_gemini = response.text
172
+ # llm_gemini = ChatGoogleGenerativeAI(model="gemini-pro")
173
+ # test_response_gemini = llm_gemini.invoke("Hello")
174
+ if test_response_gemini:
175
+ results["gemini"] = True
176
+ except Exception as e:
177
+ pass
178
+
179
+ return results
180
+ except Exception as e: # Replace with a more specific exception if possible
181
+ return results
182
+ else:
183
  try:
184
+ # Assuming genai and vertexai are clients for Google services
185
+ os.environ["GOOGLE_API_KEY"] = os.getenv('PALM_API_KEY')
186
+ # genai.configure(api_key=self.cfg_private['google_palm']['google_palm_api'])
187
+ vertexai.init(project= os.getenv('GOOGLE_PROJECT_ID'), location=os.getenv('GOOGLE_LOCATION'))
188
+
189
+ try:
190
+ model = TextGenerationModel.from_pretrained("text-bison@001")
191
+ response = model.predict("Hello")
192
+ test_response_palm = response.text
193
+ # llm_palm = ChatGoogleGenerativeAI(model="text-bison@001")
194
+ # test_response_palm = llm_palm.invoke("Hello")
195
+ if test_response_palm:
196
+ results["palm2"] = True
197
+ except Exception as e:
198
+ pass
199
+
200
+ try:
201
+ model = GenerativeModel("gemini-pro")
202
+ response = model.generate_content("Hello")
203
+ test_response_gemini = response.text
204
+ # llm_gemini = ChatGoogleGenerativeAI(model="gemini-pro")
205
+ # test_response_gemini = llm_gemini.invoke("Hello")
206
+ if test_response_gemini:
207
+ results["gemini"] = True
208
+ except Exception as e:
209
+ pass
210
+
211
+ return results
212
+ except Exception as e: # Replace with a more specific exception if possible
213
+ return results
214
 
215
  def report_api_key_status(self):
216
  missing_keys = []
217
  present_keys = []
218
 
219
+ if self.cfg_private:
220
+ k_OPENAI_API_KEY = self.cfg_private['openai']['OPENAI_API_KEY']
221
+ k_openai_azure = self.cfg_private['openai_azure']['api_version']
222
+ k_google_palm_api = self.cfg_private['google_palm']['google_palm_api']
223
+ k_project_id = self.cfg_private['google_palm']['project_id']
224
+ k_location = self.cfg_private['google_palm']['location']
225
+ k_mistral = self.cfg_private['mistral']['mistral_key']
226
+ k_here = self.cfg_private['here']['api_key']
227
+ k_opencage = self.cfg_private['open_cage_geocode']['api_key']
228
+ else:
229
+ k_OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
230
+ k_openai_azure = os.getenv('AZURE_API_VERSION')
231
+ k_google_palm_api = os.getenv('PALM_API_KEY')
232
+ k_project_id = os.getenv('GOOGLE_PROJECT_ID')
233
+ k_location = os.getenv('GOOGLE_LOCATION')
234
+ k_mistral = os.getenv('MISTRAL_API_KEY')
235
+ k_here = os.getenv('here_api_key')
236
+ k_opencage = os.getenv('open_cage_geocode')
237
+
238
+
239
  # Check each key and add to the respective list
240
  # OpenAI key check
241
+ if self.has_API_key(k_OPENAI_API_KEY):
242
  is_valid = self.check_openai_api_key()
243
  if is_valid:
244
  present_keys.append('OpenAI (Valid)')
 
248
  missing_keys.append('OpenAI')
249
 
250
  # Azure OpenAI key check
251
+ if self.has_API_key(k_openai_azure):
252
  is_valid = self.check_azure_openai_api_key()
253
  if is_valid:
254
  present_keys.append('Azure OpenAI (Valid)')
 
258
  missing_keys.append('Azure OpenAI')
259
 
260
  # Google PALM2/Gemini key check
261
+ if self.has_API_key(k_google_palm_api) and self.has_API_key(k_project_id) and self.has_API_key(k_location):
262
  google_results = self.check_google_vertex_genai_api_key()
263
  if google_results['palm2']:
264
  present_keys.append('Palm2 (Valid)')
 
272
  missing_keys.append('Google VertexAI/GenAI')
273
 
274
  # Google OCR key check
275
+ if self.has_API_key(k_google_palm_api) and self.has_API_key(k_project_id) and self.has_API_key(k_location):
276
  is_valid = self.check_google_ocr_api_key()
277
  if is_valid:
278
  present_keys.append('Google OCR (Valid)')
 
282
  missing_keys.append('Google OCR')
283
 
284
  # Mistral key check
285
+ if self.has_API_key():
286
+ is_valid = self.check_mistral_api_key(k_mistral)
287
  if is_valid:
288
  present_keys.append('Mistral (Valid)')
289
  else:
 
292
  missing_keys.append('Mistral')
293
 
294
 
295
+ if self.has_API_key(k_here):
296
  present_keys.append('HERE Geocode (Valid)')
297
  else:
298
  missing_keys.append('HERE Geocode (Invalid)')
299
 
300
+ if self.has_API_key(k_opencage):
301
  present_keys.append('OpenCage Geocode (Valid)')
302
  else:
303
  missing_keys.append('OpenCage Geocode (Invalid)')