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