pendrag commited on
Commit
5fc2cfd
·
1 Parent(s): cd0cbb5

updated version of import

Browse files
Files changed (2) hide show
  1. app.py +25 -5
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,5 +1,13 @@
1
  from openai import OpenAI
2
- import google.generativeai as genai
 
 
 
 
 
 
 
 
3
 
4
  import os
5
  import requests
@@ -281,11 +289,23 @@ def search(query, progress=gr.Progress()):
281
 
282
  if GENAI_API == "openai":
283
  client = OpenAI()
284
- elif GENAI_API.lower() in ("gemini", "google", "genai"):
285
- genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
286
- client = genai
 
 
 
 
 
 
 
 
 
 
 
 
287
  else:
288
- # Default to OpenAI client if GENAI_API is unrecognized.
289
  client = OpenAI()
290
 
291
  with gr.Blocks() as demo:
 
1
  from openai import OpenAI
2
+ # Prefer the new Google GenAI SDK if available (import style: `from google import genai`).
3
+ # Fall back to the legacy `google.generativeai` package if that's what's installed.
4
+ try:
5
+ from google import genai # new `google-genai` package
6
+ except Exception:
7
+ try:
8
+ import google.generativeai as genai # older, deprecated package
9
+ except Exception:
10
+ genai = None
11
 
12
  import os
13
  import requests
 
289
 
290
  if GENAI_API == "openai":
291
  client = OpenAI()
292
+ elif GENAI_API and GENAI_API.lower() in ("gemini", "google", "genai"):
293
+ # If the genai package couldn't be imported earlier, tell the user.
294
+ if genai is None:
295
+ raise RuntimeError(
296
+ "GENAI_API is set to Gemini but no Google GenAI SDK is installed. "
297
+ "Install `google-genai` (preferred) or `google-generativeai`, or set GENAI_API=openai."
298
+ )
299
+
300
+ # Prefer the new `genai.Client()` style when available (google-genai SDK).
301
+ if hasattr(genai, "Client"):
302
+ client = genai.Client(api_key=os.getenv('GEMINI_API_KEY'))
303
+ else:
304
+ # Legacy SDK: configure module-level API key and use the module as client.
305
+ genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
306
+ client = genai
307
  else:
308
+ # Default to OpenAI client if GENAI_API is unrecognized or unset.
309
  client = OpenAI()
310
 
311
  with gr.Blocks() as demo:
requirements.txt CHANGED
@@ -2,4 +2,4 @@ gradio
2
  openai
3
  requests
4
  httpx<0.28
5
- google-generativeai
 
2
  openai
3
  requests
4
  httpx<0.28
5
+ google-genai