Spaces:
Sleeping
Sleeping
| 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**") | |