Spaces:
No application file
No application file
| import os | |
| import streamlit as st | |
| from groq import Groq | |
| # ---------------------------- | |
| # Groq API key | |
| # ---------------------------- | |
| # On Hugging Face, you'll set this as a secret: GROQ_API_KEY | |
| GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "") | |
| client = Groq(api_key=GROQ_API_KEY) | |
| # ---------------------------- | |
| # Streamlit UI | |
| # ---------------------------- | |
| st.set_page_config(page_title="Groq AI Chatbot", page_icon="🤖") | |
| st.title("🤖 Groq AI Chatbot (Streamlit)") | |
| if "messages" not in st.session_state: | |
| st.session_state["messages"] = [] | |
| # Display chat history | |
| for msg in st.session_state["messages"]: | |
| if msg["role"] == "user": | |
| st.markdown(f"**You:** {msg['content']}") | |
| else: | |
| st.markdown(f"**AI:** {msg['content']}") | |
| # User input | |
| prompt = st.text_input("Type your question here:") | |
| if prompt: | |
| st.session_state["messages"].append({"role": "user", "content": prompt}) | |
| try: | |
| completion = client.chat.completions.create( | |
| model="llama-3.1-8b-instant", | |
| messages=st.session_state["messages"] | |
| ) | |
| reply = completion.choices[0].message.content | |
| except Exception as e: | |
| reply = f"(Error: {e})" | |
| st.session_state["messages"].append({"role": "assistant", "content": reply}) | |
| st.experimental_rerun() | |