Viper51 commited on
Commit
d9269cc
Β·
verified Β·
1 Parent(s): 9b1cc6f

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- enable_llm_checkbox = st.checkbox("Enable LLM features (requires GOOGLE_API_KEY)", value=st.session_state.enable_llm)
 
 
 
 
 
 
 
 
 
 
 
 
 
323
  st.session_state.enable_llm = enable_llm_checkbox
324
 
325
- if st.session_state.enable_llm:
 
326
  try:
327
- GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
328
- if not GOOGLE_API_KEY:
329
- st.error("THIS IS THE REAL ERROR: GOOGLE_API_KEY not set. Check your Hugging Face secrets.")
330
- # st.session_state.enable_llm = False
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.warning(f"Unexpected error while enabling LLM: {e}")
 
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