Spaces:
Sleeping
Sleeping
File size: 6,667 Bytes
c5188bc 6aa286a 60ac57b 13443fa 60ac57b 13443fa 60ac57b b1931eb 60ac57b b1931eb 60ac57b b1931eb 60ac57b b1931eb 60ac57b 6aa286a 60ac57b 13443fa 60ac57b 6aa286a 60ac57b 6aa286a 60ac57b 6aa286a 60ac57b 13443fa 60ac57b 6aa286a 60ac57b 6aa286a 60ac57b 07a9a02 60ac57b | 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | import os
import streamlit as st
from groq import Groq
# Page configuration
st.set_page_config(
page_title="Groq Chatbot",
page_icon="β‘",
layout="centered",
initial_sidebar_state="expanded"
)
# Custom CSS for modern dark theme
st.markdown("""
<style>
/* Main container styling */
.main {
background: linear-gradient(135deg, #0f0c29 0%, #302b63 50%, #24243e 100%);
color: #ffffff;
}
/* Chat container */
.stChatMessage {
background: rgba(255, 255, 255, 0.05);
border-radius: 15px;
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.1);
margin: 10px 0;
padding: 15px;
}
/* User message styling */
.stChatMessage[data-testid="stChatMessageUser"] {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
/* Assistant message styling */
.stChatMessage[data-testid="stChatMessageAssistant"] {
background: rgba(255, 255, 255, 0.08);
}
/* Input box styling */
.stTextInput > div > div > input {
background: rgba(255, 255, 255, 0.1);
border: 2px solid rgba(102, 126, 234, 0.5);
border-radius: 25px;
color: white;
padding: 15px 20px;
font-size: 16px;
}
.stTextInput > div > div > input:focus {
border-color: #667eea;
box-shadow: 0 0 15px rgba(102, 126, 234, 0.5);
}
/* Button styling */
.stButton > button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 25px;
padding: 10px 30px;
font-weight: bold;
transition: all 0.3s ease;
}
.stButton > button:hover {
transform: translateY(-2px);
box-shadow: 0 10px 20px rgba(102, 126, 234, 0.4);
}
/* Sidebar styling */
.css-1d391kg {
background: rgba(0, 0, 0, 0.3);
}
/* Headers */
h1 {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
font-weight: 800;
}
/* Scrollbar styling */
::-webkit-scrollbar {
width: 10px;
}
::-webkit-scrollbar-track {
background: rgba(255, 255, 255, 0.05);
}
::-webkit-scrollbar-thumb {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 5px;
}
/* Typing indicator animation */
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.typing-indicator {
animation: pulse 1.5s infinite;
color: #667eea;
}
</style>
""", unsafe_allow_html=True)
# Initialize Groq client
@st.cache_resource
def get_groq_client():
api_key = os.environ.get("GROQ_API_KEY")
if not api_key:
st.error("β οΈ GROQ_API_KEY not found in environment variables!")
st.stop()
return Groq(api_key=api_key)
# Initialize session state for chat history
if "messages" not in st.session_state:
st.session_state.messages = [
{"role": "assistant", "content": "π Hello! I'm your AI assistant powered by Groq. How can I help you today?"}
]
if "model" not in st.session_state:
st.session_state.model = "llama-3.3-70b-versatile"
# Sidebar configuration
with st.sidebar:
st.markdown("### βοΈ Settings")
# Model selection
model_options = {
"Llama 3.3 70B": "llama-3.3-70b-versatile",
"Llama 3.1 70B": "llama-3.1-70b-versatile",
"Llama 3.1 8B": "llama-3.1-8b-instant",
"Mixtral 8x7B": "mixtral-8x7b-32768",
"Gemma 2 9B": "gemma2-9b-it"
}
selected_model = st.selectbox(
"Choose Model",
options=list(model_options.keys()),
index=0
)
st.session_state.model = model_options[selected_model]
st.markdown("---")
# Temperature slider
temperature = st.slider("Temperature", 0.0, 1.0, 0.7, 0.1)
# Max tokens slider
max_tokens = st.slider("Max Tokens", 256, 4096, 1024, 128)
st.markdown("---")
# Clear chat button
if st.button("ποΈ Clear Chat", use_container_width=True):
st.session_state.messages = [
{"role": "assistant", "content": "π Chat cleared! How can I help you?"}
]
st.rerun()
st.markdown("---")
st.markdown("### π Stats")
st.write(f"Messages: {len(st.session_state.messages)}")
# API Key status
if os.environ.get("GROQ_API_KEY"):
st.success("π API Key: Connected")
else:
st.error("π API Key: Missing")
# Main chat interface
st.title("β‘ Groq Chatbot")
st.markdown("*Lightning-fast AI conversations*")
# Display chat messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Chat input
if prompt := st.chat_input("Type your message here..."):
# Add user message to history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message
with st.chat_message("user"):
st.markdown(prompt)
# Get AI response
with st.chat_message("assistant"):
message_placeholder = st.empty()
try:
client = get_groq_client()
# Show typing indicator
message_placeholder.markdown('<span class="typing-indicator">π€ Thinking...</span>', unsafe_allow_html=True)
# Call Groq API
chat_completion = client.chat.completions.create(
messages=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
model=st.session_state.model,
temperature=temperature,
max_tokens=max_tokens,
stream=False
)
response = chat_completion.choices[0].message.content
# Display response with typewriter effect simulation
message_placeholder.markdown(response)
# Add to history
st.session_state.messages.append({"role": "assistant", "content": response})
except Exception as e:
message_placeholder.error(f"β Error: {str(e)}")
# Footer
st.markdown("---")
st.markdown(
"<div style='text-align: center; color: rgba(255,255,255,0.5);'>"
"Powered by Groq β‘ | Built with Streamlit β€οΈ"
"</div>",
unsafe_allow_html=True
) |