chatbot5 / app.py
amasood's picture
Create app.py
6a58bcf verified
import streamlit as st
import os
from groq import Groq
# Set up Streamlit page config
st.set_page_config(page_title="Groq Chatbot", page_icon="πŸ€–")
# Title
st.title("πŸ€– Chat with Groq LLM")
# Input API Key (optional for local testing, hidden in HF Spaces)
if "GROQ_API_KEY" not in os.environ:
groq_api_key = st.text_input("Enter your GROQ API Key", type="password")
else:
groq_api_key = os.environ.get("GROQ_API_KEY")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Show chat history
for msg in st.session_state.messages:
st.chat_message(msg["role"]).markdown(msg["content"])
# Input field
user_input = st.chat_input("Type your message here...")
if user_input and groq_api_key:
# Display user message
st.chat_message("user").markdown(user_input)
st.session_state.messages.append({"role": "user", "content": user_input})
# Send request to Groq API
client = Groq(api_key=groq_api_key)
try:
response = client.chat.completions.create(
messages=st.session_state.messages,
model="llama3-70b-8192" # Or "llama-3.3-70b-versatile" if that's valid
)
reply = response.choices[0].message.content
st.chat_message("assistant").markdown(reply)
st.session_state.messages.append({"role": "assistant", "content": reply})
except Exception as e:
st.error(f"Error: {e}")