PRC142004 commited on
Commit
5550f92
·
verified ·
1 Parent(s): d7071e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -8
app.py CHANGED
@@ -24,18 +24,50 @@ def markdown_to_html(text):
24
 
25
  # IMPORTANT: Replace with your actual Gemini API key
26
  # Load API Keys (Gemini)
27
- # Load API Keys (Gemini)
28
- formatted_gemini_key = os.getenv('GEMINI_API_KEY') or os.getenv('GEMINI_API_KEY_1') or os.getenv('GEMINI_API_KEY_2')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  gemini_client = None
31
- if formatted_gemini_key:
32
- print(f"Initializing Gemini Client with API key: {formatted_gemini_key[:10]}...")
33
  try:
34
- gemini_client = genai.Client(api_key=formatted_gemini_key)
35
  except Exception as e:
36
  print(f"Error initializing Gemini Client: {e}")
37
  else:
38
- print("WARNING: GEMINI_API_KEY not found. Gemini models will be skipped.")
 
 
 
39
  def validate_coordinates(lat, lon):
40
  """Validate and convert latitude and longitude to float."""
41
  try:
@@ -425,12 +457,13 @@ def call_gemini_api(input_data, language):
425
  print("DEBUG: Gemini client not initialized. Skipping Gemini models.")
426
 
427
  # 4. NVIDIA Fallback (if Gemini fails)
428
- if not report_data and os.getenv("NVIDIA_API_KEY"):
429
  try:
430
  from openai import OpenAI
 
431
  nv_client = OpenAI(
432
  base_url="https://integrate.api.nvidia.com/v1",
433
- api_key=os.getenv("NVIDIA_API_KEY")
434
  )
435
 
436
  # Council of models for text generation
@@ -467,6 +500,8 @@ def call_gemini_api(input_data, language):
467
  continue
468
  except Exception as e:
469
  print(f"CRITICAL: NVIDIA client setup failed: {e}")
 
 
470
 
471
  if not report_data:
472
  print("DEBUG: All models failed to generate valid report.")
 
24
 
25
  # IMPORTANT: Replace with your actual Gemini API key
26
  # Load API Keys (Gemini)
27
+ # --- ROBUST API KEY DETECTION ---
28
+ def get_api_key(service_type):
29
+ """
30
+ Intelligently fetches API keys by scanning multiple environment variables.
31
+ - service_type="gemini": Looks for keys starting with "AIza"
32
+ - service_type="nvidia": Looks for keys starting with "nvapi-"
33
+ """
34
+ potential_vars = [
35
+ "GEMINI_API_KEY", "GEMINI_API_KEY_1", "GEMINI_API_KEY_2", "GEMINI_API",
36
+ "NVIDIA_API_KEY", "NVIDIA_API"
37
+ ]
38
+
39
+ found_key = None
40
+
41
+ for var_name in potential_vars:
42
+ val = os.getenv(var_name)
43
+ if not val:
44
+ continue
45
+
46
+ if service_type == "gemini" and val.startswith("AIza"):
47
+ print(f"DEBUG: Found Gemini Key in variable '{var_name}'")
48
+ return val
49
+ elif service_type == "nvidia" and val.startswith("nvapi-"):
50
+ print(f"DEBUG: Found NVIDIA Key in variable '{var_name}'")
51
+ return val
52
+
53
+ return None
54
+
55
+ # Initialize Clients
56
+ gemini_key = get_api_key("gemini")
57
+ nvidia_key = get_api_key("nvidia")
58
 
59
  gemini_client = None
60
+ if gemini_key:
61
+ print(f"Initializing Gemini Client with key: {gemini_key[:10]}...")
62
  try:
63
+ gemini_client = genai.Client(api_key=gemini_key)
64
  except Exception as e:
65
  print(f"Error initializing Gemini Client: {e}")
66
  else:
67
+ print("WARNING: No valid Gemini API Key (starting with 'AIza') found in environment variables.")
68
+
69
+ # NVIDIA fallback logic will use `nvidia_key` later
70
+
71
  def validate_coordinates(lat, lon):
72
  """Validate and convert latitude and longitude to float."""
73
  try:
 
457
  print("DEBUG: Gemini client not initialized. Skipping Gemini models.")
458
 
459
  # 4. NVIDIA Fallback (if Gemini fails)
460
+ if not report_data and nvidia_key:
461
  try:
462
  from openai import OpenAI
463
+ # Use the detected nvidia_key
464
  nv_client = OpenAI(
465
  base_url="https://integrate.api.nvidia.com/v1",
466
+ api_key=nvidia_key
467
  )
468
 
469
  # Council of models for text generation
 
500
  continue
501
  except Exception as e:
502
  print(f"CRITICAL: NVIDIA client setup failed: {e}")
503
+ elif not report_data:
504
+ print("DEBUG: No valid report and NVIDIA key not found/valid.")
505
 
506
  if not report_data:
507
  print("DEBUG: All models failed to generate valid report.")