Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# --- 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"])
|
| 60 |
+
|
| 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 |
+
|
| 73 |
+
# Assistant response
|
| 74 |
+
with st.chat_message("assistant"):
|
| 75 |
+
placeholder = st.empty()
|
| 76 |
+
placeholder.markdown("Thinking...")
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
response = client.models.generate_content(
|
| 80 |
+
model=selected_model,
|
| 81 |
+
contents=prompt
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
reply = response.text
|
| 85 |
+
placeholder.markdown(reply)
|
| 86 |
+
|
| 87 |
+
st.session_state.messages.append(
|
| 88 |
+
{"role": "assistant", "content": reply}
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
except Exception as e:
|
| 92 |
+
placeholder.error(f"❌ Error: {e}")
|