Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +46 -33
src/streamlit_app.py
CHANGED
|
@@ -307,45 +307,58 @@ if genai is None or ChatGoogleGenerativeAI is None:
|
|
| 307 |
if 'enable_llm' not in st.session_state:
|
| 308 |
st.session_state.enable_llm = False
|
| 309 |
|
| 310 |
-
if st.button("Test Google API Connection"):
|
| 311 |
-
if not st.session_state.enable_llm or llm is None:
|
| 312 |
-
st.error("LLM is not enabled or initialized. Check the checkbox and API key.")
|
| 313 |
-
else:
|
| 314 |
-
try:
|
| 315 |
-
with st.spinner("Pinging Google API..."):
|
| 316 |
-
# Use the simplest possible LLM call
|
| 317 |
-
llm.invoke("Hello, are you working?")
|
| 318 |
-
st.success("β
SUCCESS: API call worked!")
|
| 319 |
-
except Exception as e:
|
| 320 |
-
st.error(f"β API call FAILED. This is the error: {e}")
|
| 321 |
|
| 322 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
st.session_state.enable_llm = enable_llm_checkbox
|
| 324 |
|
| 325 |
-
if
|
|
|
|
| 326 |
try:
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
else:
|
| 332 |
-
# Try to configure genai and perform a lightweight sanity check.
|
| 333 |
-
try:
|
| 334 |
-
genai.configure(api_key=GOOGLE_API_KEY)
|
| 335 |
-
# Initialize LLM and model wrappers
|
| 336 |
-
llm = get_llm(GOOGLE_API_KEY)
|
| 337 |
-
gen_q_model, intro_model, eval_model = get_models(llm)
|
| 338 |
-
# optional quick test: attempt to build a simple prompt but don't invoke network here
|
| 339 |
-
except Exception as e:
|
| 340 |
-
st.error(f"THIS IS THE REAL ERROR: {e}")
|
| 341 |
-
# st.session_state.enable_llm = False
|
| 342 |
-
llm = None
|
| 343 |
-
gen_q_model = None
|
| 344 |
-
intro_model = None
|
| 345 |
-
eval_model = None
|
| 346 |
except Exception as e:
|
| 347 |
-
st.
|
|
|
|
| 348 |
st.session_state.enable_llm = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
|
| 350 |
# --- Session State Initialization ---
|
| 351 |
# This is crucial for making the app work step-by-step
|
|
|
|
| 307 |
if 'enable_llm' not in st.session_state:
|
| 308 |
st.session_state.enable_llm = False
|
| 309 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
|
| 311 |
+
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
| 312 |
+
api_key_exists = bool(GOOGLE_API_KEY)
|
| 313 |
+
|
| 314 |
+
if not api_key_exists:
|
| 315 |
+
st.warning("β οΈ GOOGLE_API_KEY not found in environment variables.")
|
| 316 |
+
st.info("Add GOOGLE_API_KEY to your Hugging Face Space secrets to enable AI features.")
|
| 317 |
+
|
| 318 |
+
# LLM Enable Checkbox
|
| 319 |
+
enable_llm_checkbox = st.checkbox(
|
| 320 |
+
"Enable LLM features (requires GOOGLE_API_KEY)",
|
| 321 |
+
value=st.session_state.enable_llm,
|
| 322 |
+
disabled=not api_key_exists,
|
| 323 |
+
help="AI-powered question generation and evaluation"
|
| 324 |
+
)
|
| 325 |
st.session_state.enable_llm = enable_llm_checkbox
|
| 326 |
|
| 327 |
+
# Initialize LLM if enabled
|
| 328 |
+
if st.session_state.enable_llm and api_key_exists:
|
| 329 |
try:
|
| 330 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 331 |
+
llm = get_llm(GOOGLE_API_KEY)
|
| 332 |
+
gen_q_model, intro_model, eval_model = get_models(llm)
|
| 333 |
+
st.success("β
LLM features enabled successfully")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
except Exception as e:
|
| 335 |
+
st.error(f"β Could not initialize LLM: {e}")
|
| 336 |
+
st.info("Check your API key and try again.")
|
| 337 |
st.session_state.enable_llm = False
|
| 338 |
+
llm = None
|
| 339 |
+
gen_q_model = None
|
| 340 |
+
intro_model = None
|
| 341 |
+
eval_model = None
|
| 342 |
+
|
| 343 |
+
# Test API Button (AFTER initialization)
|
| 344 |
+
if st.button("Test Google API Connection"):
|
| 345 |
+
if not st.session_state.enable_llm:
|
| 346 |
+
st.error("β LLM features are not enabled. Check the checkbox above first.")
|
| 347 |
+
elif llm is None:
|
| 348 |
+
st.error("β LLM is not initialized. Check API key configuration.")
|
| 349 |
+
else:
|
| 350 |
+
try:
|
| 351 |
+
with st.spinner("Testing API connection..."):
|
| 352 |
+
# Simple test call
|
| 353 |
+
test_response = llm.invoke("Say 'Hello' if you can hear me.")
|
| 354 |
+
st.success("β
SUCCESS! API is working correctly.")
|
| 355 |
+
st.info(f"Response: {test_response.content if hasattr(test_response, 'content') else str(test_response)}")
|
| 356 |
+
except Exception as e:
|
| 357 |
+
st.error(f"β API call FAILED with error: {e}")
|
| 358 |
+
st.info("This usually means: invalid API key, quota exceeded, or network issues.")
|
| 359 |
+
|
| 360 |
+
st.divider()
|
| 361 |
+
|
| 362 |
|
| 363 |
# --- Session State Initialization ---
|
| 364 |
# This is crucial for making the app work step-by-step
|