Spaces:
Sleeping
Sleeping
File size: 3,407 Bytes
16b74a5 c96f2ef 16b74a5 c96f2ef 16b74a5 c96f2ef 16b74a5 c96f2ef 16b74a5 c96f2ef 16b74a5 c96f2ef 16b74a5 c96f2ef | 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 | 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!") |