test / app.py
prasanthr0416's picture
Update app.py
c96f2ef verified
import streamlit as st
from groq import Groq
# Page config
st.set_page_config(
page_title="Groq API Tester",
page_icon="πŸš€",
layout="centered"
)
# Title
st.title("πŸš€ Groq API Key Tester")
st.markdown("Test if your Groq API key is working with free tier models")
# Get API key from secrets or input
api_key = st.secrets.get("GROQ_API_KEY") if hasattr(st.secrets, "GROQ_API_KEY") else None
if not api_key:
api_key = st.text_input("Enter your Groq API Key:", type="password")
# Model selection - INCLUDING THE OPENAI MODELS
model = st.selectbox(
"Select Model (all free tier):",
[
"openai/gpt-oss-20b", # OpenAI's 20B model
"openai/gpt-oss-safeguard-20b", # OpenAI with safety features
"mixtral-8x7b-32768",
"llama-3.3-70b-versatile",
"gemma2-9b-it"
]
)
# Show info about selected model
if "openai" in model:
st.info(f"βœ… Using OpenAI model: {model} - Free tier active!")
# Test input
user_input = st.text_area("Enter your test message:", "What is artificial intelligence in 2 sentences?")
# Test button
if st.button("Test API Key", type="primary"):
if not api_key:
st.error("Please enter your Groq API key!")
else:
try:
with st.spinner("Testing API connection..."):
# Initialize Groq client
client = Groq(api_key=api_key)
# Make API call
completion = client.chat.completions.create(
model=model,
messages=[
{"role": "user", "content": user_input}
],
temperature=0.7,
max_tokens=500
)
# Get response
response = completion.choices[0].message.content
# Show success
st.success("βœ… API Key is working! Free tier is active.")
# Show response
st.markdown("### πŸ€– Model Response:")
st.info(response)
# Show token usage
st.caption(f"Tokens used: {completion.usage.total_tokens} | "
f"Prompt: {completion.usage.prompt_tokens} | "
f"Completion: {completion.usage.completion_tokens}")
except Exception as e:
st.error(f"❌ Error: {str(e)}")
# Updated requirements.txt
st.sidebar.markdown("### πŸ“‹ Requirements")
st.sidebar.code("""
streamlit==1.43.2
groq==0.8.0
httpx==0.27.2
""")
# Instructions
with st.expander("πŸ“‹ How to use"):
st.markdown("""
1. Get your free API key from [console.groq.com](https://console.groq.com)
2. Enter the key above or add to Hugging Face secrets as `GROQ_API_KEY`
3. Select a model - **including OpenAI's free models!**
4. Type any test message
5. Click "Test API Key"
**OpenAI Models Available for Free on Groq:**
- `openai/gpt-oss-20b` - OpenAI's 20B parameter model
- `openai/gpt-oss-safeguard-20b` - Same with safety guardrails
All models have same limits: 30 req/min, 1K req/day
""")
# Footer
st.markdown("---")
st.markdown("Made with ❀️ to test Groq API | OpenAI models included!")