import os from groq import Groq from dotenv import load_dotenv import streamlit as st # Load environment variables from .env file load_dotenv() # Get the API key from environment variable api_key = os.getenv("GROQ_API_KEY") # Initialize Groq client with the API key client = Groq(api_key=api_key) # Define your chatbot logic for student exam preparation assistant def chatbot(): st.title("Student Exam Preparation Assistant πŸŽ“") st.write("Welcome to your personal exam preparation assistant! Whether you're preparing for a high school exam, college exams, or any professional tests, I'm here to help. What would you like assistance with today?") # Add an attractive header with an emoji st.markdown("**Ask me anything about exam preparation!**") st.markdown("I can help you with study tips, time management strategies, practice questions, and more. Let’s get started! πŸ˜„") # Input field for the user to type a message user_input = st.text_input("Type your exam preparation question here:") # Add a submit button if st.button("Submit"): if user_input: # Display user's input st.write(f"You: {user_input}") # Sending user's input to Groq API for completion try: completion = client.chat.completions.create( model="deepseek-r1-distill-llama-70b", # You can change this model based on your preference messages=[{"role": "user", "content": user_input}], temperature=0.6, max_completion_tokens=4096, top_p=0.95, stream=True, stop=None, ) # Collect the response chunk by chunk response = "" for chunk in completion: # Get the assistant's response from each chunk response += chunk.choices[0].delta.content or "" # Display assistant's response st.write(f"Assistant: {response}") except Exception as e: st.write(f"Error occurred: {e}") else: st.write("Please type a question before submitting. 😊") # Run the chatbot with dynamic user input if __name__ == "__main__": chatbot()