File size: 1,429 Bytes
6a58bcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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}")