GROQ_Q_and_A / app.py
ShubhamMhaske's picture
Update app.py
89dd4fc verified
import streamlit as st
from groq import Groq
# Custom CSS for styling
def add_custom_css():
st.markdown(
"""
<style>
body {
background-color: #f7f9fc;
color: #333;
font-family: 'Arial', sans-serif;
}
.main > div {
background: white;
border-radius: 10px;
padding: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #45a049;
}
.stMarkdown {
font-size: 16px;
}
</style>
""",
unsafe_allow_html=True
)
# Add custom CSS
add_custom_css()
# 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 for API key
api_key = st.text_input("πŸ”‘ Enter your GROQ API Key:", type="password")
# Check if API key is provided
if not api_key:
st.warning("⚠️ Please enter your GROQ API Key to proceed.")
else:
# Initialize the Groq client
try:
client = Groq(api_key=api_key)
st.success("βœ… GROQ API Key successfully set!")
except Exception as e:
st.error(f"❌ Error initializing Groq client: {str(e)}")
# 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(
"<div style='text-align: center;'>✨ <strong>Powered by Groq AI</strong> ✨</div>",
unsafe_allow_html=True
)