Spaces:
Runtime error

SimpleChatbot / app.py
muhammadfaheem34's picture
Update app.py
161f64b verified
Raw History Blame Contribute Delete
2.08 kB
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)