kaitongg commited on
Commit
0744752
·
verified ·
1 Parent(s): 6c76f74

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -11
app.py CHANGED
@@ -85,20 +85,40 @@ GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
85
 
86
  if GEMINI_API_KEY:
87
  genai.configure(api_key=GEMINI_API_KEY)
88
- # Use the correct model name for the stable API
 
89
  try:
90
- # Try gemini-pro first (most stable)
91
- gemini_model = genai.GenerativeModel('gemini-pro')
92
- print("✓ Gemini API initialized successfully with gemini-pro!")
 
93
  except Exception as e:
94
- print(f"Failed to load gemini-pro: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  try:
96
- # Fallback to gemini-1.5-flash-latest
97
- gemini_model = genai.GenerativeModel('gemini-1.5-flash-latest')
98
- print("✓ Gemini API initialized successfully with gemini-1.5-flash-latest!")
99
- except Exception as e2:
100
- print(f"Failed to load gemini-1.5-flash-latest: {e2}")
101
- gemini_model = None
 
 
 
 
 
102
  else:
103
  gemini_model = None
104
  print("⚠️ Warning: GEMINI_API_KEY not found in environment variables")
 
85
 
86
  if GEMINI_API_KEY:
87
  genai.configure(api_key=GEMINI_API_KEY)
88
+
89
+ # List available models to help debug
90
  try:
91
+ print("Available Gemini models:")
92
+ for m in genai.list_models():
93
+ if 'generateContent' in m.supported_generation_methods:
94
+ print(f" - {m.name}")
95
  except Exception as e:
96
+ print(f"Could not list models: {e}")
97
+
98
+ # Try different model names in order of preference
99
+ model_names_to_try = [
100
+ 'models/gemini-1.5-flash',
101
+ 'models/gemini-1.5-pro',
102
+ 'models/gemini-pro',
103
+ 'gemini-1.5-flash',
104
+ 'gemini-1.5-pro',
105
+ 'gemini-pro'
106
+ ]
107
+
108
+ gemini_model = None
109
+ for model_name in model_names_to_try:
110
  try:
111
+ gemini_model = genai.GenerativeModel(model_name)
112
+ # Test the model with a simple query
113
+ test_response = gemini_model.generate_content("Hi")
114
+ print(f"✓ Gemini API initialized successfully with {model_name}!")
115
+ break
116
+ except Exception as e:
117
+ print(f"Failed to load {model_name}: {e}")
118
+ continue
119
+
120
+ if gemini_model is None:
121
+ print("⚠️ Warning: Could not initialize any Gemini model")
122
  else:
123
  gemini_model = None
124
  print("⚠️ Warning: GEMINI_API_KEY not found in environment variables")