larrysim commited on
Commit
086f49f
Β·
verified Β·
1 Parent(s): 3fa0141

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -39
app.py CHANGED
@@ -26,52 +26,52 @@ with st.sidebar:
26
 
27
  # ---------- BEFORE VALIDATION ----------
28
  if not st.session_state.api_validated:
29
- api_key_input = st.text_input(
30
- "Enter Google API Key",
31
- type="password",
32
- key="api_key_input"
33
- )
34
-
35
- # Validate API Key immediately when button is clicked
36
- if st.button("Validate API Key"):
37
- if not api_key_input:
38
- st.warning("Please enter an API key.")
39
- else:
40
- try:
41
- # Initialize client
42
- client = genai.Client(api_key=api_key_input)
43
 
44
- # Attempt to list models (authentication test)
 
 
 
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
- models = []
52
-
53
- # Persist validated state
54
- st.session_state.api_key = api_key_input
55
- st.session_state.client = client
56
- st.session_state.models = models or ["gemini-2.5-flash"]
57
- st.session_state.selected_model = st.session_state.models[0]
58
- st.session_state.api_validated = True
59
-
60
- st.success("βœ… API key validated successfully")
61
-
62
- except Exception as e:
63
- error_msg = str(e).lower()
64
- if "401" in error_msg or "403" in error_msg or "unauthorized" in error_msg:
65
- st.error("❌ Invalid API key")
66
- else:
67
- st.warning("⚠️ API key accepted, but model access may be restricted")
68
- st.caption(str(e))
 
 
 
 
 
69
 
70
  # ---------- AFTER VALIDATION ----------
71
  else:
72
  st.success("πŸ”’ API Key validated (hidden)")
73
 
74
- # Model selector is now immediately visible
75
  st.selectbox(
76
  "Available Models",
77
  st.session_state.models,
 
26
 
27
  # ---------- BEFORE VALIDATION ----------
28
  if not st.session_state.api_validated:
29
+ with st.form("api_form", clear_on_submit=False):
30
+ api_key_input = st.text_input(
31
+ "Enter Google API Key",
32
+ type="password",
33
+ key="api_key_input"
34
+ )
35
+ submitted = st.form_submit_button("Validate API Key")
 
 
 
 
 
 
 
36
 
37
+ if submitted:
38
+ if not api_key_input:
39
+ st.warning("Please enter an API key.")
40
+ else:
41
  try:
42
+ # Initialize client
43
+ client = genai.Client(api_key=api_key_input)
44
+
45
+ # Attempt to list models (authentication test)
46
+ try:
47
+ models = [
48
+ m.name for m in client.models.list()
49
+ if "gemini" in m.name.lower()
50
+ ]
51
+ except Exception:
52
+ models = []
53
+
54
+ # Persist validated state
55
+ st.session_state.api_key = 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
+
61
+ st.success("βœ… API key validated successfully")
62
+
63
+ except Exception as e:
64
+ error_msg = str(e).lower()
65
+ if "401" in error_msg or "403" in error_msg or "unauthorized" in error_msg:
66
+ st.error("❌ Invalid API key")
67
+ else:
68
+ st.warning("⚠️ API key accepted, but model access may be restricted")
69
+ st.caption(str(e))
70
 
71
  # ---------- AFTER VALIDATION ----------
72
  else:
73
  st.success("πŸ”’ API Key validated (hidden)")
74
 
 
75
  st.selectbox(
76
  "Available Models",
77
  st.session_state.models,