Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -42,6 +42,38 @@ When answering questions:
|
|
| 42 |
5. Be encouraging and supportive to prospective students
|
| 43 |
"""
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
def get_chatbot_response(message, history):
|
| 46 |
"""Generate response using Gemini API"""
|
| 47 |
|
|
@@ -49,41 +81,14 @@ def get_chatbot_response(message, history):
|
|
| 49 |
return "β οΈ Please set the GEMINI_API_KEY environment variable in Hugging Face Spaces settings."
|
| 50 |
|
| 51 |
try:
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
'gemini-1.5-flash-latest',
|
| 55 |
-
'gemini-1.5-flash',
|
| 56 |
-
'gemini-1.5-pro-latest',
|
| 57 |
-
'gemini-1.5-pro',
|
| 58 |
-
'gemini-pro'
|
| 59 |
-
]
|
| 60 |
-
|
| 61 |
-
model = None
|
| 62 |
-
last_error = None
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
try:
|
| 67 |
-
model = genai.GenerativeModel(model_name)
|
| 68 |
-
# Test if model works with a simple generation
|
| 69 |
-
test_response = model.generate_content("Hi")
|
| 70 |
-
# If successful, break the loop
|
| 71 |
-
break
|
| 72 |
-
except Exception as e:
|
| 73 |
-
last_error = e
|
| 74 |
-
continue
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
try:
|
| 79 |
-
available_models = []
|
| 80 |
-
for m in genai.list_models():
|
| 81 |
-
if 'generateContent' in m.supported_generation_methods:
|
| 82 |
-
available_models.append(m.name)
|
| 83 |
-
|
| 84 |
-
return f"β Could not initialize any Gemini model.\n\nAvailable models: {', '.join(available_models)}\n\nLast error: {str(last_error)}\n\nPlease check your API key at https://makersuite.google.com/app/apikey"
|
| 85 |
-
except:
|
| 86 |
-
return f"β Error: {str(last_error)}\n\nPlease ensure your GEMINI_API_KEY is valid. Get a new key at: https://aistudio.google.com/app/apikey"
|
| 87 |
|
| 88 |
# Build conversation history
|
| 89 |
chat_history = []
|
|
@@ -106,7 +111,7 @@ def get_chatbot_response(message, history):
|
|
| 106 |
error_msg = str(e)
|
| 107 |
if "API_KEY_INVALID" in error_msg or "invalid" in error_msg.lower():
|
| 108 |
return f"β Invalid API Key Error: {error_msg}\n\nπ Please get a valid API key from: https://aistudio.google.com/app/apikey\n\nThen set it in Hugging Face Spaces: Settings β Repository secrets β GEMINI_API_KEY"
|
| 109 |
-
return f"β Error: {error_msg}\n\nIf the error persists, please verify your API key is correct."
|
| 110 |
|
| 111 |
# Custom CSS for NIELIT branding
|
| 112 |
custom_css = """
|
|
|
|
| 42 |
5. Be encouraging and supportive to prospective students
|
| 43 |
"""
|
| 44 |
|
| 45 |
+
def get_available_model_name():
|
| 46 |
+
"""
|
| 47 |
+
Dynamically finds a working Gemini model from the user's account.
|
| 48 |
+
"""
|
| 49 |
+
try:
|
| 50 |
+
available_models = []
|
| 51 |
+
for m in genai.list_models():
|
| 52 |
+
if 'generateContent' in m.supported_generation_methods:
|
| 53 |
+
available_models.append(m.name)
|
| 54 |
+
|
| 55 |
+
if not available_models:
|
| 56 |
+
return None
|
| 57 |
+
|
| 58 |
+
# Priority list: Try to find these specific models first
|
| 59 |
+
preferred_order = [
|
| 60 |
+
"models/gemini-1.5-flash",
|
| 61 |
+
"models/gemini-1.5-pro",
|
| 62 |
+
"models/gemini-pro",
|
| 63 |
+
"models/gemini-1.0-pro"
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
# Check if any preferred model is available
|
| 67 |
+
for preferred in preferred_order:
|
| 68 |
+
if preferred in available_models:
|
| 69 |
+
return preferred
|
| 70 |
+
|
| 71 |
+
# If none of the preferred ones exist, use the first available
|
| 72 |
+
return available_models[0]
|
| 73 |
+
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return None
|
| 76 |
+
|
| 77 |
def get_chatbot_response(message, history):
|
| 78 |
"""Generate response using Gemini API"""
|
| 79 |
|
|
|
|
| 81 |
return "β οΈ Please set the GEMINI_API_KEY environment variable in Hugging Face Spaces settings."
|
| 82 |
|
| 83 |
try:
|
| 84 |
+
# Get the best available model
|
| 85 |
+
model_name = get_available_model_name()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
if not model_name:
|
| 88 |
+
return "β No available Gemini models found for your API key.\n\nπ Please verify your API key at: https://aistudio.google.com/app/apikey\n\nMake sure you have access to Gemini models."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
# Initialize the model
|
| 91 |
+
model = genai.GenerativeModel(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
# Build conversation history
|
| 94 |
chat_history = []
|
|
|
|
| 111 |
error_msg = str(e)
|
| 112 |
if "API_KEY_INVALID" in error_msg or "invalid" in error_msg.lower():
|
| 113 |
return f"β Invalid API Key Error: {error_msg}\n\nπ Please get a valid API key from: https://aistudio.google.com/app/apikey\n\nThen set it in Hugging Face Spaces: Settings β Repository secrets β GEMINI_API_KEY"
|
| 114 |
+
return f"β Error: {error_msg}\n\nModel used: {model_name if 'model_name' in locals() else 'unknown'}\n\nIf the error persists, please verify your API key is correct."
|
| 115 |
|
| 116 |
# Custom CSS for NIELIT branding
|
| 117 |
custom_css = """
|