larrysim commited on
Commit
1fa6e70
Β·
verified Β·
1 Parent(s): fa6de7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -50
app.py CHANGED
@@ -1,112 +1,106 @@
1
  import streamlit as st
2
  from google import genai
3
 
4
- # Page Config
5
  st.set_page_config(page_title="Gemini API Tester", page_icon="⚑")
6
-
7
  st.title("⚑ Gemini API Tester")
8
- st.markdown("Enter your Google API key to load available Gemini models.")
9
-
10
- # --- Initialize session state ---
11
- if "api_key" not in st.session_state:
12
- st.session_state.api_key = ""
13
-
14
- if "api_validated" not in st.session_state:
15
- st.session_state.api_validated = False
16
-
17
- if "models" not in st.session_state:
18
- st.session_state.models = []
19
 
20
- if "client" not in st.session_state:
21
- st.session_state.client = None
22
-
23
- # --- Sidebar ---
 
 
 
 
 
 
 
 
 
 
 
24
  with st.sidebar:
25
  st.header("πŸ” API Configuration")
26
 
27
- # πŸ”Ή API Key Input (only show if not validated)
28
  if not st.session_state.api_validated:
29
  api_key_input = st.text_input(
30
- "Enter your Google API Key",
31
  type="password"
32
  )
33
 
34
- if st.button("Validate API Key"):
 
 
35
  if not api_key_input:
36
- st.warning("⚠️ Please enter an API key")
37
  else:
38
  try:
39
  client = genai.Client(api_key=api_key_input)
40
 
41
- # Fetch Gemini models
42
  models = [
43
- m.name
44
- for m in client.models.list()
45
  if "gemini" in m.name.lower()
46
  ]
47
 
48
  if not models:
49
- st.error("❌ API key valid, but no Gemini models found")
50
  else:
51
- # Save to session state
52
  st.session_state.api_key = api_key_input
53
  st.session_state.client = client
54
  st.session_state.models = models
 
55
  st.session_state.api_validated = True
56
 
57
- st.success("βœ… API Key validated successfully")
58
 
59
  except Exception as e:
60
- st.error("❌ Invalid API Key")
61
  st.caption(str(e))
62
 
63
- # πŸ”Ή After validation (API key hidden)
64
  else:
65
- st.success("πŸ” API Key validated")
66
 
67
- selected_model = st.selectbox(
68
  "Available Models",
69
- st.session_state.models
 
 
 
70
  )
71
 
72
  if st.button("πŸ”„ Change API Key"):
73
- st.session_state.api_key = ""
74
- st.session_state.api_validated = False
75
- st.session_state.models = []
76
- st.session_state.client = None
77
- st.experimental_rerun()
78
-
79
- # --- Chat State ---
80
- if "messages" not in st.session_state:
81
- st.session_state.messages = []
82
 
83
- # Show chat history
84
  for msg in st.session_state.messages:
85
  with st.chat_message(msg["role"]):
86
  st.markdown(msg["content"])
87
 
88
- # --- Chat Input ---
89
  if prompt := st.chat_input("Ask something..."):
90
 
91
  if not st.session_state.api_validated:
92
- st.error("❌ Please validate your API key first.")
93
  st.stop()
94
 
95
  # User message
96
- st.session_state.messages.append(
97
- {"role": "user", "content": prompt}
98
- )
99
  with st.chat_message("user"):
100
  st.markdown(prompt)
101
 
102
- # Assistant response
103
  with st.chat_message("assistant"):
104
  placeholder = st.empty()
105
  placeholder.markdown("Thinking...")
106
 
107
  try:
108
  response = st.session_state.client.models.generate_content(
109
- model=selected_model,
110
  contents=prompt
111
  )
112
 
@@ -118,4 +112,4 @@ if prompt := st.chat_input("Ask something..."):
118
  )
119
 
120
  except Exception as e:
121
- placeholder.error(f"❌ Error: {e}")
 
1
  import streamlit as st
2
  from google import genai
3
 
4
+ # ---------------- Page Config ----------------
5
  st.set_page_config(page_title="Gemini API Tester", page_icon="⚑")
 
6
  st.title("⚑ Gemini API Tester")
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ # ---------------- Session State ----------------
9
+ state_defaults = {
10
+ "api_key": None,
11
+ "api_validated": False,
12
+ "client": None,
13
+ "models": [],
14
+ "selected_model": None,
15
+ "messages": []
16
+ }
17
+
18
+ for k, v in state_defaults.items():
19
+ if k not in st.session_state:
20
+ st.session_state[k] = v
21
+
22
+ # ---------------- Sidebar ----------------
23
  with st.sidebar:
24
  st.header("πŸ” API Configuration")
25
 
26
+ # ---------- NOT VALIDATED ----------
27
  if not st.session_state.api_validated:
28
  api_key_input = st.text_input(
29
+ "Enter Google API Key",
30
  type="password"
31
  )
32
 
33
+ validate = st.button("Validate API Key")
34
+
35
+ if validate:
36
  if not api_key_input:
37
+ st.warning("Please enter an API key.")
38
  else:
39
  try:
40
  client = genai.Client(api_key=api_key_input)
41
 
 
42
  models = [
43
+ m.name for m in client.models.list()
 
44
  if "gemini" in m.name.lower()
45
  ]
46
 
47
  if not models:
48
+ st.error("API key valid, but no Gemini models available.")
49
  else:
50
+ # Persist validated state
51
  st.session_state.api_key = api_key_input
52
  st.session_state.client = client
53
  st.session_state.models = models
54
+ st.session_state.selected_model = models[0]
55
  st.session_state.api_validated = True
56
 
57
+ st.success("API key validated successfully")
58
 
59
  except Exception as e:
60
+ st.error("Invalid API key")
61
  st.caption(str(e))
62
 
63
+ # ---------- VALIDATED ----------
64
  else:
65
+ st.success("πŸ”’ API Key validated (hidden)")
66
 
67
+ st.session_state.selected_model = st.selectbox(
68
  "Available Models",
69
+ st.session_state.models,
70
+ index=st.session_state.models.index(
71
+ st.session_state.selected_model
72
+ ) if st.session_state.selected_model in st.session_state.models else 0
73
  )
74
 
75
  if st.button("πŸ”„ Change API Key"):
76
+ for k in ["api_key", "api_validated", "client", "models", "selected_model"]:
77
+ st.session_state[k] = state_defaults[k]
 
 
 
 
 
 
 
78
 
79
+ # ---------------- Chat History ----------------
80
  for msg in st.session_state.messages:
81
  with st.chat_message(msg["role"]):
82
  st.markdown(msg["content"])
83
 
84
+ # ---------------- Chat Input ----------------
85
  if prompt := st.chat_input("Ask something..."):
86
 
87
  if not st.session_state.api_validated:
88
+ st.error("Please validate an API key first.")
89
  st.stop()
90
 
91
  # User message
92
+ st.session_state.messages.append({"role": "user", "content": prompt})
 
 
93
  with st.chat_message("user"):
94
  st.markdown(prompt)
95
 
96
+ # Assistant
97
  with st.chat_message("assistant"):
98
  placeholder = st.empty()
99
  placeholder.markdown("Thinking...")
100
 
101
  try:
102
  response = st.session_state.client.models.generate_content(
103
+ model=st.session_state.selected_model,
104
  contents=prompt
105
  )
106
 
 
112
  )
113
 
114
  except Exception as e:
115
+ placeholder.error(f"Error: {e}")