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

# Load environment variables from .env file
load_dotenv()

# Get API key from .env
GROQ_API_KEY = os.getenv("GROQ_API_KEY")

# Streamlit UI
st.title("Hugging Face & GROQ API Chatbot")
st.write("Enter a message below to interact with the AI.")

# User input
user_input = st.text_area("Your Message", "Explain the importance of fast language models")

if st.button("Generate Response"):
    if not GROQ_API_KEY:
        st.error("API Key is missing. Please check your .env file.")
    else:
        try:
            # Initialize GROQ client
            client = Groq(api_key=GROQ_API_KEY)

            # Generate response
            chat_completion = client.chat.completions.create(
                messages=[{"role": "user", "content": user_input}],
                model="llama-3.3-70b-versatile",
            )

            # Display response
            response = chat_completion.choices[0].message.content
            st.write("### AI Response:")
            st.write(response)

        except Exception as e:
            st.error(f"Error: {e}")