""" Beautiful WhatsApp-Style Chatbot with Gradient Theme """ import streamlit as st from groq import Groq # --- Page Configuration --- st.set_page_config( page_title="Chat Assistant", page_icon="💬", layout="centered", initial_sidebar_state="collapsed" ) # --- Beautiful Custom CSS --- st.markdown(""" """, unsafe_allow_html=True) # --- App Interface --- st.markdown("

💬 Chat Assistant

", unsafe_allow_html=True) st.markdown("""

A modern, WhatsApp-style chat interface powered by Groq AI

""", unsafe_allow_html=True) # --- API Key Section (Beautiful Container) --- with st.container(): st.markdown("""

🔑 Enter Your API Key

""", unsafe_allow_html=True) col1, col2 = st.columns([3, 1]) with col1: api_key = st.text_input( "API Key", type="password", placeholder="Paste your Groq API key here (gsk_...)", label_visibility="collapsed" ) with col2: st.link_button("⚡ Get Free Key", "https://console.groq.com/keys", use_container_width=True) if not api_key: st.warning("⚠️ Please enter your API key above to start chatting!") st.info(""" **How to get your FREE key in 30 seconds:** 1. Click **"Get Free Key"** button above ☝️ 2. Sign in with Google/GitHub (instant) 3. Click **"Create API Key"** 4. Copy and paste it in the box above """) st.stop() # --- Model Selection --- col1, col2 = st.columns([2, 1]) with col1: model = st.selectbox( "🤖 Choose AI Model:", ["llama-3.1-8b-instant", "mixtral-8x7b-32768", "gemma2-9b-it"], help="Free tier: 20 requests/min, 200/day" ) with col2: if st.button("🗑️ Clear Chat", use_container_width=True): st.session_state.messages = [] st.rerun() st.markdown("---") # --- Chat History --- if "messages" not in st.session_state: st.session_state.messages = [ {"role": "assistant", "content": "Hey there! 👋 I'm your AI assistant. How can I help you today?"} ] # --- Display Chat Messages --- for message in st.session_state.messages: with st.chat_message(message["role"], avatar="🧑" if message["role"] == "user" else "🤖"): st.markdown(message["content"]) # --- Chat Input --- if prompt := st.chat_input("💭 Type your message here...", key="chat_input"): # Add user message st.session_state.messages.append({"role": "user", "content": prompt}) with st.chat_message("user", avatar="🧑"): st.markdown(prompt) # Generate response with st.chat_message("assistant", avatar="🤖"): with st.spinner("✨ Thinking..."): try: client = Groq(api_key=api_key) response = client.chat.completions.create( model=model, messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages], temperature=0.7, max_tokens=1024 ) assistant_response = response.choices[0].message.content st.markdown(assistant_response) # Add to history st.session_state.messages.append( {"role": "assistant", "content": assistant_response} ) except Exception as e: error_msg = str(e) if "rate limit" in error_msg.lower(): st.error("⏱️ Whoa! Too fast! Free tier allows 20 requests per minute. Please wait a moment.") elif "invalid" in error_msg.lower(): st.error("🔑 Invalid API key. Please check your key and try again.") else: st.error(f"❌ Error: {error_msg}") st.markdown("---") st.caption("🔒 Your API key is session-only and never stored | Powered by Groq (Free Tier)")