Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +89 -21
src/streamlit_app.py
CHANGED
|
@@ -27,41 +27,51 @@ st.markdown("""
|
|
| 27 |
border-radius: 0.5rem;
|
| 28 |
margin-bottom: 1rem;
|
| 29 |
}
|
|
|
|
|
|
|
|
|
|
| 30 |
</style>
|
| 31 |
""", unsafe_allow_html=True)
|
| 32 |
|
| 33 |
# Title and description
|
| 34 |
st.title("π€ Groq AI Chat Assistant")
|
| 35 |
-
st.markdown("Powered by Groq's lightning-fast LLM API")
|
| 36 |
|
| 37 |
# Sidebar configuration
|
| 38 |
with st.sidebar:
|
| 39 |
st.header("βοΈ Configuration")
|
| 40 |
|
|
|
|
|
|
|
| 41 |
# API Key input
|
| 42 |
api_key = st.text_input(
|
| 43 |
"Enter your Groq API Key",
|
| 44 |
type="password",
|
| 45 |
-
help="Get your API key from https://console.groq.com"
|
| 46 |
)
|
| 47 |
|
| 48 |
if not api_key:
|
| 49 |
-
st.warning("Please enter your Groq API key to proceed.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# Model selection
|
| 52 |
model_option = st.selectbox(
|
| 53 |
"Select Model",
|
| 54 |
[
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
-
"
|
| 58 |
],
|
| 59 |
-
help="Choose the AI model to use"
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
# Temperature slider
|
| 63 |
temperature = st.slider(
|
| 64 |
-
"Temperature",
|
| 65 |
min_value=0.0,
|
| 66 |
max_value=2.0,
|
| 67 |
value=0.7,
|
|
@@ -71,7 +81,7 @@ with st.sidebar:
|
|
| 71 |
|
| 72 |
# Max tokens slider
|
| 73 |
max_tokens = st.slider(
|
| 74 |
-
"Max Tokens",
|
| 75 |
min_value=100,
|
| 76 |
max_value=2048,
|
| 77 |
value=1024,
|
|
@@ -79,17 +89,34 @@ with st.sidebar:
|
|
| 79 |
help="Maximum length of response"
|
| 80 |
)
|
| 81 |
|
|
|
|
|
|
|
| 82 |
# Clear chat button
|
| 83 |
if st.button("ποΈ Clear Chat History", use_container_width=True):
|
| 84 |
st.session_state.messages = []
|
| 85 |
st.rerun()
|
| 86 |
|
| 87 |
-
st.
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# Initialize session state for chat history
|
| 95 |
if "messages" not in st.session_state:
|
|
@@ -125,6 +152,8 @@ if api_key:
|
|
| 125 |
)
|
| 126 |
|
| 127 |
with st.chat_message("assistant"):
|
|
|
|
|
|
|
| 128 |
# Call Groq API
|
| 129 |
response = client.chat.completions.create(
|
| 130 |
model=model_option,
|
|
@@ -135,7 +164,7 @@ if api_key:
|
|
| 135 |
)
|
| 136 |
|
| 137 |
assistant_message = response.choices[0].message.content
|
| 138 |
-
|
| 139 |
|
| 140 |
# Add assistant response to history
|
| 141 |
st.session_state.messages.append({
|
|
@@ -148,19 +177,58 @@ if api_key:
|
|
| 148 |
|
| 149 |
except Exception as e:
|
| 150 |
error_str = str(e).lower()
|
|
|
|
| 151 |
|
| 152 |
if "proxies" in error_str or "proxy" in error_str:
|
| 153 |
st.error("β Proxy Error - Trying alternative connection method...")
|
| 154 |
st.info("Refresh the page and try again. If problem persists, restart the Space.")
|
| 155 |
-
elif "authentication" in error_str or "401" in error_str or "invalid" in error_str:
|
| 156 |
-
st.error("β Invalid API Key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
elif "rate_limit" in error_str or "429" in error_str:
|
| 158 |
st.error("β Rate limit reached - Please wait a moment and try again")
|
| 159 |
elif "connection" in error_str:
|
| 160 |
st.error("β Connection Error - Please check your internet connection")
|
|
|
|
|
|
|
|
|
|
| 161 |
else:
|
| 162 |
-
st.error(f"β Error: {
|
| 163 |
-
st.info("Make sure you have an active Groq account with credits")
|
| 164 |
|
| 165 |
else:
|
| 166 |
-
st.info("π Please enter your Groq API key in the sidebar to start chatting.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
border-radius: 0.5rem;
|
| 28 |
margin-bottom: 1rem;
|
| 29 |
}
|
| 30 |
+
.css-1dp5vir {
|
| 31 |
+
padding: 2rem;
|
| 32 |
+
}
|
| 33 |
</style>
|
| 34 |
""", unsafe_allow_html=True)
|
| 35 |
|
| 36 |
# Title and description
|
| 37 |
st.title("π€ Groq AI Chat Assistant")
|
| 38 |
+
st.markdown("β‘ Powered by Groq's lightning-fast LLM API - Get instant AI responses!")
|
| 39 |
|
| 40 |
# Sidebar configuration
|
| 41 |
with st.sidebar:
|
| 42 |
st.header("βοΈ Configuration")
|
| 43 |
|
| 44 |
+
st.markdown("---")
|
| 45 |
+
|
| 46 |
# API Key input
|
| 47 |
api_key = st.text_input(
|
| 48 |
"Enter your Groq API Key",
|
| 49 |
type="password",
|
| 50 |
+
help="Get your free API key from https://console.groq.com"
|
| 51 |
)
|
| 52 |
|
| 53 |
if not api_key:
|
| 54 |
+
st.warning("π Please enter your Groq API key to proceed.")
|
| 55 |
+
else:
|
| 56 |
+
st.success("β
API Key loaded")
|
| 57 |
+
|
| 58 |
+
st.markdown("---")
|
| 59 |
|
| 60 |
# Model selection
|
| 61 |
model_option = st.selectbox(
|
| 62 |
"Select Model",
|
| 63 |
[
|
| 64 |
+
"llama-3.3-70b-versatile",
|
| 65 |
+
"llama-3.1-8b-instant",
|
| 66 |
+
"gemma2-9b-it"
|
| 67 |
],
|
| 68 |
+
help="Choose the AI model to use",
|
| 69 |
+
index=0
|
| 70 |
)
|
| 71 |
|
| 72 |
# Temperature slider
|
| 73 |
temperature = st.slider(
|
| 74 |
+
"Temperature (Creativity)",
|
| 75 |
min_value=0.0,
|
| 76 |
max_value=2.0,
|
| 77 |
value=0.7,
|
|
|
|
| 81 |
|
| 82 |
# Max tokens slider
|
| 83 |
max_tokens = st.slider(
|
| 84 |
+
"Max Tokens (Response Length)",
|
| 85 |
min_value=100,
|
| 86 |
max_value=2048,
|
| 87 |
value=1024,
|
|
|
|
| 89 |
help="Maximum length of response"
|
| 90 |
)
|
| 91 |
|
| 92 |
+
st.markdown("---")
|
| 93 |
+
|
| 94 |
# Clear chat button
|
| 95 |
if st.button("ποΈ Clear Chat History", use_container_width=True):
|
| 96 |
st.session_state.messages = []
|
| 97 |
st.rerun()
|
| 98 |
|
| 99 |
+
st.markdown("---")
|
| 100 |
+
|
| 101 |
+
# Info section
|
| 102 |
+
st.markdown("**π Model Information**")
|
| 103 |
+
|
| 104 |
+
model_info = {
|
| 105 |
+
"llama-3.3-70b-versatile": "Most powerful model - Best for complex tasks",
|
| 106 |
+
"llama-3.1-8b-instant": "Fastest responses - Good for quick questions",
|
| 107 |
+
"gemma2-9b-it": "Balanced performance - Good all-rounder"
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
st.info(f"**{model_option}**\n\n{model_info[model_option]}")
|
| 111 |
+
|
| 112 |
+
st.markdown("---")
|
| 113 |
+
|
| 114 |
+
st.markdown("**π Resources**")
|
| 115 |
+
st.markdown("""
|
| 116 |
+
- [Get Groq API Key](https://console.groq.com)
|
| 117 |
+
- [Groq Documentation](https://console.groq.com/docs)
|
| 118 |
+
- [API Status](https://status.groq.com)
|
| 119 |
+
""")
|
| 120 |
|
| 121 |
# Initialize session state for chat history
|
| 122 |
if "messages" not in st.session_state:
|
|
|
|
| 152 |
)
|
| 153 |
|
| 154 |
with st.chat_message("assistant"):
|
| 155 |
+
message_placeholder = st.empty()
|
| 156 |
+
|
| 157 |
# Call Groq API
|
| 158 |
response = client.chat.completions.create(
|
| 159 |
model=model_option,
|
|
|
|
| 164 |
)
|
| 165 |
|
| 166 |
assistant_message = response.choices[0].message.content
|
| 167 |
+
message_placeholder.markdown(assistant_message)
|
| 168 |
|
| 169 |
# Add assistant response to history
|
| 170 |
st.session_state.messages.append({
|
|
|
|
| 177 |
|
| 178 |
except Exception as e:
|
| 179 |
error_str = str(e).lower()
|
| 180 |
+
full_error = str(e)
|
| 181 |
|
| 182 |
if "proxies" in error_str or "proxy" in error_str:
|
| 183 |
st.error("β Proxy Error - Trying alternative connection method...")
|
| 184 |
st.info("Refresh the page and try again. If problem persists, restart the Space.")
|
| 185 |
+
elif "authentication" in error_str or "401" in error_str or "invalid" in error_str or "api_error" in error_str:
|
| 186 |
+
st.error("β Invalid API Key")
|
| 187 |
+
st.warning("Please check your Groq API key:")
|
| 188 |
+
st.info("""
|
| 189 |
+
1. Go to https://console.groq.com
|
| 190 |
+
2. Click API Keys in sidebar
|
| 191 |
+
3. Copy your key (should start with 'gsk_')
|
| 192 |
+
4. Paste it above - NO extra spaces!
|
| 193 |
+
5. Make sure your account has credits
|
| 194 |
+
""")
|
| 195 |
+
with st.expander("Show full error"):
|
| 196 |
+
st.code(full_error)
|
| 197 |
elif "rate_limit" in error_str or "429" in error_str:
|
| 198 |
st.error("β Rate limit reached - Please wait a moment and try again")
|
| 199 |
elif "connection" in error_str:
|
| 200 |
st.error("β Connection Error - Please check your internet connection")
|
| 201 |
+
elif "decommissioned" in error_str or "model" in error_str:
|
| 202 |
+
st.error("β Model Error - This model is no longer available")
|
| 203 |
+
st.info("Try selecting a different model from the sidebar")
|
| 204 |
else:
|
| 205 |
+
st.error(f"β Error: {full_error}")
|
| 206 |
+
st.info("Make sure you have an active Groq account with available credits")
|
| 207 |
|
| 208 |
else:
|
| 209 |
+
st.info("π Please enter your Groq API key in the sidebar to start chatting.")
|
| 210 |
+
|
| 211 |
+
st.markdown("---")
|
| 212 |
+
|
| 213 |
+
st.markdown("## π Getting Started")
|
| 214 |
+
st.markdown("""
|
| 215 |
+
1. **Get API Key**: Visit [console.groq.com](https://console.groq.com) and sign up (free)
|
| 216 |
+
2. **Create Key**: Go to API Keys section and create a new API key
|
| 217 |
+
3. **Paste Key**: Paste your key in the sidebar on the left
|
| 218 |
+
4. **Start Chatting**: Type your message and press Enter!
|
| 219 |
+
|
| 220 |
+
### Features
|
| 221 |
+
- β‘ Lightning-fast responses
|
| 222 |
+
- π― Multiple AI models to choose from
|
| 223 |
+
- ποΈ Adjustable temperature and response length
|
| 224 |
+
- πΎ Chat history management
|
| 225 |
+
- π Works on desktop and mobile
|
| 226 |
+
""")
|
| 227 |
+
|
| 228 |
+
# Footer
|
| 229 |
+
st.markdown("---")
|
| 230 |
+
st.markdown("""
|
| 231 |
+
<div style="text-align: center; color: gray; font-size: 12px;">
|
| 232 |
+
Made with Streamlit & Groq API | Β© 2025
|
| 233 |
+
</div>
|
| 234 |
+
""", unsafe_allow_html=True)
|