Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,45 @@
|
|
1 |
import streamlit as st
|
2 |
-
import os
|
3 |
-
from dotenv import load_dotenv
|
4 |
from groq import Groq
|
5 |
|
6 |
-
# Load environment variables from .env file
|
7 |
-
load_dotenv()
|
8 |
-
|
9 |
-
# Get the GROQ API key from the .env file
|
10 |
-
API_KEY = os.getenv("GROQ_API_KEY")
|
11 |
-
if not API_KEY:
|
12 |
-
st.error("GROQ_API_KEY is not set. Please check your .env file.")
|
13 |
-
|
14 |
-
# Initialize the Groq client
|
15 |
-
client = Groq(api_key=API_KEY)
|
16 |
-
|
17 |
# Streamlit App
|
18 |
st.title("Q&A Application with Groq API")
|
19 |
st.write("Ask a question and get a response using Groq's AI model!")
|
20 |
|
21 |
-
# Input
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
# Button to trigger the API call
|
25 |
-
if st.button("Get Answer"):
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
|
43 |
# Footer
|
44 |
st.markdown("---")
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
from groq import Groq
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
# Streamlit App
|
5 |
st.title("Q&A Application with Groq API")
|
6 |
st.write("Ask a question and get a response using Groq's AI model!")
|
7 |
|
8 |
+
# Input for API key
|
9 |
+
api_key = st.text_input("Enter your GROQ API Key:", type="password")
|
10 |
+
|
11 |
+
# Check if API key is provided
|
12 |
+
if not api_key:
|
13 |
+
st.warning("Please enter your GROQ API Key to proceed.")
|
14 |
+
else:
|
15 |
+
# Initialize the Groq client
|
16 |
+
try:
|
17 |
+
client = Groq(api_key=api_key)
|
18 |
+
st.success("GROQ API Key successfully set!")
|
19 |
+
except Exception as e:
|
20 |
+
st.error(f"Error initializing Groq client: {str(e)}")
|
21 |
+
|
22 |
+
# Input prompt from the user
|
23 |
+
user_input = st.text_input("Enter your question:", "")
|
24 |
|
25 |
+
# Button to trigger the API call
|
26 |
+
if st.button("Get Answer"):
|
27 |
+
if user_input.strip():
|
28 |
+
try:
|
29 |
+
# API call to Groq
|
30 |
+
chat_completion = client.chat.completions.create(
|
31 |
+
messages=[{"role": "user", "content": user_input}],
|
32 |
+
model="llama3-8b-8192"
|
33 |
+
)
|
34 |
+
# Extracting the response
|
35 |
+
response = chat_completion.choices[0].message.content
|
36 |
+
# Display the response
|
37 |
+
st.subheader("Response:")
|
38 |
+
st.write(response)
|
39 |
+
except Exception as e:
|
40 |
+
st.error(f"Error: {str(e)}")
|
41 |
+
else:
|
42 |
+
st.warning("Please enter a valid question.")
|
43 |
|
44 |
# Footer
|
45 |
st.markdown("---")
|