larrysim commited on
Commit
308b460
Β·
verified Β·
1 Parent(s): 63b23af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -28
app.py CHANGED
@@ -24,7 +24,7 @@ for k, v in defaults.items():
24
  with st.sidebar:
25
  st.header("πŸ” API Configuration")
26
 
27
- # -------- BEFORE VALIDATION --------
28
  if not st.session_state.api_validated:
29
  st.text_input(
30
  "Enter Google API Key",
@@ -37,43 +37,39 @@ with st.sidebar:
37
  st.warning("Please enter an API key.")
38
  else:
39
  try:
40
- # βœ… CORRECT: Configure SDK globally
41
- genai.configure(
42
- api_key=st.session_state.api_key_input
43
- )
44
-
45
- # Create client AFTER configure
46
- client = genai.Client()
47
-
48
- # βœ… Minimal real inference validation
49
- client.models.generate_content(
50
- model="gemini-2.5-flash",
51
- contents="OK"
52
- )
53
-
54
- # Fetch available models
55
- models = [
56
- m.name for m in client.models.list()
57
- if "gemini" in m.name.lower()
58
- ]
59
 
60
  # Persist validated state
61
  st.session_state.api_key = st.session_state.api_key_input
62
  st.session_state.client = client
63
- st.session_state.models = models
64
- st.session_state.selected_model = models[0]
65
  st.session_state.api_validated = True
66
-
67
- # πŸ”’ Clear visible input
68
  st.session_state.api_key_input = ""
69
 
70
  st.success("βœ… API key validated successfully")
71
 
72
  except Exception as e:
73
- st.error("❌ Invalid or restricted API key")
74
- st.caption(str(e))
75
-
76
- # -------- AFTER VALIDATION --------
 
 
 
 
77
  else:
78
  st.success("πŸ”’ API Key validated (hidden)")
79
 
 
24
  with st.sidebar:
25
  st.header("πŸ” API Configuration")
26
 
27
+ # ---------- BEFORE VALIDATION ----------
28
  if not st.session_state.api_validated:
29
  st.text_input(
30
  "Enter Google API Key",
 
37
  st.warning("Please enter an API key.")
38
  else:
39
  try:
40
+ # Initialize client
41
+ client = genai.Client(api_key=st.session_state.api_key_input)
42
+
43
+ # Attempt to list models (authentication test)
44
+ models = []
45
+ try:
46
+ models = [
47
+ m.name for m in client.models.list()
48
+ if "gemini" in m.name.lower()
49
+ ]
50
+ except Exception:
51
+ # Listing models may fail even if key is valid
52
+ models = []
 
 
 
 
 
 
53
 
54
  # Persist validated state
55
  st.session_state.api_key = st.session_state.api_key_input
56
  st.session_state.client = client
57
+ st.session_state.models = models or ["gemini-2.5-flash"]
58
+ st.session_state.selected_model = st.session_state.models[0]
59
  st.session_state.api_validated = True
 
 
60
  st.session_state.api_key_input = ""
61
 
62
  st.success("βœ… API key validated successfully")
63
 
64
  except Exception as e:
65
+ error_msg = str(e).lower()
66
+ if "401" in error_msg or "403" in error_msg or "unauthorized" in error_msg:
67
+ st.error("❌ Invalid API key")
68
+ else:
69
+ st.warning("⚠️ API key accepted, but model access may be restricted")
70
+ st.caption(str(e))
71
+
72
+ # ---------- AFTER VALIDATION ----------
73
  else:
74
  st.success("πŸ”’ API Key validated (hidden)")
75