chatbot2 / app.py
amasood's picture
Update app.py
d9f1c93 verified
import os
import streamlit as st
from groq import Groq
# βœ… Set your Groq API key here (replace with your actual key)
#GROQ_API_KEY = MY_KEY # πŸ” Replace this with your real key
GROQ_API_KEY = os.environ.get("MY_KEY")
# Initialize Groq client
client = Groq(api_key=GROQ_API_KEY)
# Set page configuration
st.set_page_config(page_title="Groq Chatbot", page_icon="πŸ€–")
# Title
st.title("πŸ€– Groq Chatbot using LLaMA 3")
# Text input from user
user_input = st.text_input("You:", placeholder="Type your question here...")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat history
for msg in st.session_state.messages:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
# Handle user input
if user_input:
# Add user message to history
st.session_state.messages.append({"role": "user", "content": user_input})
with st.chat_message("user"):
st.markdown(user_input)
# Call Groq API
try:
response = client.chat.completions.create(
messages=st.session_state.messages,
model="llama3-70b-8192", # model ID from Groq
)
reply = response.choices[0].message.content
# Add assistant reply to history
st.session_state.messages.append({"role": "assistant", "content": reply})
with st.chat_message("assistant"):
st.markdown(reply)
except Exception as e:
st.error(f"Error: {e}")