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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -37
app.py CHANGED
@@ -4,56 +4,83 @@ from google import genai
4
  # Page Config
5
  st.set_page_config(page_title="Gemini API Tester", page_icon="⚑")
6
 
7
- # --- Title ---
8
  st.title("⚑ Gemini API Tester")
9
- st.markdown("Enter your Google API key to see which Gemini models are available.")
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  # --- Sidebar ---
12
  with st.sidebar:
13
  st.header("πŸ” API Configuration")
14
 
15
- api_key = st.text_input(
16
- "Enter your Google API Key",
17
- type="password"
18
- )
19
-
20
- models = []
21
- client = None
22
 
23
- if api_key:
24
- try:
25
- # Initialize client
26
- client = genai.Client(api_key=api_key)
27
-
28
- # Fetch available models
29
- models = [
30
- model.name
31
- for model in client.models.list()
32
- if "gemini" in model.name.lower()
33
- ]
34
-
35
- if models:
36
- st.success("βœ… API Key is valid")
37
  else:
38
- st.warning("⚠️ API key is valid, but no Gemini models found")
39
-
40
- except Exception as e:
41
- st.error("❌ Invalid API Key")
42
- st.caption(str(e))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- # Model selection (only show if models exist)
45
- selected_model = None
46
- if models:
47
  selected_model = st.selectbox(
48
  "Available Models",
49
- models
50
  )
51
 
 
 
 
 
 
 
 
52
  # --- Chat State ---
53
  if "messages" not in st.session_state:
54
  st.session_state.messages = []
55
 
56
- # Display chat history
57
  for msg in st.session_state.messages:
58
  with st.chat_message(msg["role"]):
59
  st.markdown(msg["content"])
@@ -61,12 +88,14 @@ for msg in st.session_state.messages:
61
  # --- Chat Input ---
62
  if prompt := st.chat_input("Ask something..."):
63
 
64
- if not client or not selected_model:
65
- st.error("❌ Please enter a valid API key and select a model first.")
66
  st.stop()
67
 
68
  # User message
69
- st.session_state.messages.append({"role": "user", "content": prompt})
 
 
70
  with st.chat_message("user"):
71
  st.markdown(prompt)
72
 
@@ -76,7 +105,7 @@ if prompt := st.chat_input("Ask something..."):
76
  placeholder.markdown("Thinking...")
77
 
78
  try:
79
- response = client.models.generate_content(
80
  model=selected_model,
81
  contents=prompt
82
  )
 
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"])
 
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
 
 
105
  placeholder.markdown("Thinking...")
106
 
107
  try:
108
+ response = st.session_state.client.models.generate_content(
109
  model=selected_model,
110
  contents=prompt
111
  )