import streamlit as st from groq import Groq import os # ------------------------- # 🔐 API KEY (Hugging Face Secret) # ------------------------- api_key = os.getenv("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") # ✅ DEFINE CLIENT HERE client = Groq(api_key=api_key) # ------------------------- # PAGE CONFIG # ------------------------- st.set_page_config( page_title="Groq Chatbot", page_icon="🤖", layout="centered" ) # ------------------------- # UI STYLE (MODERN DARK) # ------------------------- st.markdown(""" """, unsafe_allow_html=True) # ------------------------- # SESSION STATE # ------------------------- if "messages" not in st.session_state: st.session_state.messages = [] # ------------------------- # SIDEBAR # ------------------------- with st.sidebar: st.title("⚙️ Settings") model = st.selectbox( "Model", ["llama-3.1-70b-versatile" ,"llama-3.1-8b-instant" , "mixtral-8x7b-32768" ] ) if st.button("🗑 Clear Chat"): st.session_state.messages = [] st.rerun() # ------------------------- # MAIN UI # ------------------------- st.title("🤖 Simple AI Assistant") # Show messages for msg in st.session_state.messages: st.chat_message(msg["role"]).write(msg["content"]) # Input user_input = st.chat_input("Type your message...") if user_input: st.session_state.messages.append({"role": "user", "content": user_input}) st.chat_message("user").write(user_input) with st.spinner("Thinking..."): try: response = client.chat.completions.create( model=model, messages=st.session_state.messages ) reply = response.choices[0].message.content except Exception as e: reply = f"Error: {str(e)}" st.session_state.messages.append({"role": "assistant", "content": reply}) st.chat_message("assistant").write(reply)