Sample_ChatBot / old_2_app.py
DanishShahzad's picture
Rename 2_old_app.py to old_2_app.py
7b5fc0d verified
Raw History Blame Contribute Delete
1.3 kB
# app.py
import streamlit as st
from groq_chat import chat_with_groq
st.set_page_config(page_title="Groq Chatbot", page_icon="🤖", layout="centered")
st.title("🤖 Groq Chatbot (LLaMA3-powered)")
st.markdown("Chat with a blazing-fast LLaMA model via the Groq API.")
# Initialize session state
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
# Clear chat button
if st.button("🧹 Clear Chat"):
st.session_state.chat_history = []
st.rerun()
# Display previous chat
for msg in st.session_state.chat_history:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
# Chat input
prompt = st.chat_input("Type your message...")
if prompt:
# Add user message
st.session_state.chat_history.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Get and display AI response
with st.spinner("Groq is thinking..."):
try:
response = chat_with_groq(prompt, history=st.session_state.chat_history)
st.session_state.chat_history.append({"role": "assistant", "content": response})
with st.chat_message("assistant"):
st.markdown(response)
except Exception as e:
st.error(f"💥 Error: {e}")