File size: 1,074 Bytes
aae46d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import streamlit as st
from groq import Groq
from dotenv import load_dotenv

# Load environment variables
load_dotenv()
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
if not GROQ_API_KEY:
    st.error("GROQ_API_KEY not found. Please set it before running the app.")
    st.stop()

# Initialize the Groq client
client = Groq(api_key=GROQ_API_KEY)

def get_groq_response(user_input):
    try:
        chat_completion = client.chat.completions.create(
            messages=[
                {"role": "user", "content": user_input}
            ],
            model="llama-3.3-70b-versatile",
        )
        return chat_completion.choices[0].message.content
    except Exception as e:
        return f"Error: {str(e)}"

# Streamlit UI
st.title("Hugging Face + GROQ API Chatbot")
user_input = st.text_area("Enter your message:")

if st.button("Generate Response"):
    if user_input:
        response = get_groq_response(user_input)
        st.write("**GROQ Response:**")
        st.write(response)
    else:
        st.warning("Please enter a message before submitting.")