Spaces:
Runtime error
Runtime error
File size: 2,075 Bytes
4a0f825 bcdd0e1 3ad5867 844aee5 4a0f825 c12f640 4a0f825 161f64b 4a0f825 | 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 | 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("""
<style>
body {
background-color: #0e1117;
color: white;
}
.stChatMessage {
border-radius: 10px;
padding: 10px;
}
</style>
""", 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) |