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

# Load environment variables from .env file
load_dotenv()

# Get the GROQ API key from the .env file
API_KEY = os.getenv("GROQ_API_KEY")
if not API_KEY:
    st.error("GROQ_API_KEY is not set. Please check your .env file.")

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

# Streamlit App
st.title("Q&A Application with Groq API")
st.write("Ask a question and get a response using Groq's AI model!")

# Input prompt from the user
user_input = st.text_input("Enter your question:", "")

# Button to trigger the API call
if st.button("Get Answer"):
    if user_input.strip():
        try:
            # API call to Groq
            chat_completion = client.chat.completions.create(
                messages=[{"role": "user", "content": user_input}],
                model="llama3-8b-8192"
            )
            # Extracting the response
            response = chat_completion.choices[0].message.content
            # Display the response
            st.subheader("Response:")
            st.write(response)
        except Exception as e:
            st.error(f"Error: {str(e)}")
    else:
        st.warning("Please enter a valid question.")

# Footer
st.markdown("---")
st.markdown("**Powered by Groq AI**")