File size: 2,338 Bytes
7b1cd48
 
 
6399ceb
 
7b1cd48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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()