Update app.py
Browse files
app.py
CHANGED
|
@@ -3,39 +3,71 @@ from dotenv import load_dotenv
|
|
| 3 |
from groq import Groq
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
-
# Load environment variables
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
st.
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
st.
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from groq import Groq
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
+
# Load environment variables from .env file
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
+
# Ensure that the API key is loaded correctly
|
| 10 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 11 |
|
| 12 |
+
if not api_key:
|
| 13 |
+
st.error("API key is missing! Please check your .env file or set the environment variable.")
|
| 14 |
+
else:
|
| 15 |
+
# Initialize Groq client with the API key
|
| 16 |
+
client = Groq(api_key=api_key)
|
| 17 |
|
| 18 |
+
# Custom CSS to style headings and overall appearance
|
| 19 |
+
st.markdown("""
|
| 20 |
+
<style>
|
| 21 |
+
.big-heading {
|
| 22 |
+
font-size: 36px;
|
| 23 |
+
font-weight: bold;
|
| 24 |
+
color: #333;
|
| 25 |
+
}
|
| 26 |
+
.sub-header {
|
| 27 |
+
font-size: 24px;
|
| 28 |
+
font-weight: bold;
|
| 29 |
+
color: #555;
|
| 30 |
+
}
|
| 31 |
+
.header {
|
| 32 |
+
font-size: 20px;
|
| 33 |
+
font-weight: normal;
|
| 34 |
+
color: #444;
|
| 35 |
+
}
|
| 36 |
+
.output-text {
|
| 37 |
+
font-size: 18px;
|
| 38 |
+
font-weight: normal;
|
| 39 |
+
color: #000;
|
| 40 |
+
}
|
| 41 |
+
</style>
|
| 42 |
+
""", unsafe_allow_html=True)
|
| 43 |
|
| 44 |
+
# Streamlit UI
|
| 45 |
+
st.markdown('<p class="big-heading">Python Bot with Groq\'s API</p>', unsafe_allow_html=True)
|
| 46 |
+
st.markdown('<p class="sub-header">Interact with an AI Model Powered by Groq</p>', unsafe_allow_html=True)
|
| 47 |
+
|
| 48 |
+
# Input Section
|
| 49 |
+
st.markdown('<p class="header">Input</p>', unsafe_allow_html=True)
|
| 50 |
+
user_input = st.text_input("Enter your question or message below:")
|
| 51 |
+
|
| 52 |
+
# Submit button
|
| 53 |
+
if st.button("Submit"):
|
| 54 |
+
if user_input.strip():
|
| 55 |
+
# Output Section
|
| 56 |
+
st.markdown('<p class="header">Output</p>', unsafe_allow_html=True)
|
| 57 |
+
with st.spinner("Fetching response from the LLM..."):
|
| 58 |
+
try:
|
| 59 |
+
# Interact with Groq's LLM
|
| 60 |
+
chat_completion = client.chat.completions.create(
|
| 61 |
+
messages=[
|
| 62 |
+
{"role": "user", "content": user_input},
|
| 63 |
+
],
|
| 64 |
+
model="llama3-8b-8192",
|
| 65 |
+
stream=False,
|
| 66 |
+
)
|
| 67 |
+
response = chat_completion.choices[0].message.content
|
| 68 |
+
st.success("Here is the AI's response:")
|
| 69 |
+
st.markdown(f'<p class="output-text">{response}</p>', unsafe_allow_html=True)
|
| 70 |
+
except Exception as e:
|
| 71 |
+
st.error(f"Error: {e}")
|
| 72 |
+
else:
|
| 73 |
+
st.warning("Please enter a valid message!")
|