meesamraza commited on
Commit
93267e0
·
verified ·
1 Parent(s): a8b8696

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from groq import Groq
3
+ from dotenv import load_dotenv
4
+ import streamlit as st
5
+
6
+ # Load environment variables from .env file
7
+ load_dotenv()
8
+
9
+ # Get the API key from environment variable
10
+ api_key = os.getenv("GROQ_API_KEY")
11
+
12
+ # Initialize Groq client with the API key
13
+ client = Groq(api_key=api_key)
14
+
15
+ # Define your chatbot logic for student exam preparation assistant
16
+ def chatbot():
17
+ st.title("Student Exam Preparation Assistant 🎓")
18
+ 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?")
19
+
20
+ # Add an attractive header with an emoji
21
+ st.markdown("**Ask me anything about exam preparation!**")
22
+ st.markdown("I can help you with study tips, time management strategies, practice questions, and more. Let’s get started! 😄")
23
+
24
+ # Input field for the user to type a message
25
+ user_input = st.text_input("Type your exam preparation question here:")
26
+
27
+ # Add a submit button
28
+ if st.button("Submit"):
29
+ if user_input:
30
+ # Display user's input
31
+ st.write(f"You: {user_input}")
32
+
33
+ # Sending user's input to Groq API for completion
34
+ try:
35
+ completion = client.chat.completions.create(
36
+ model="deepseek-r1-distill-llama-70b", # You can change this model based on your preference
37
+ messages=[{"role": "user", "content": user_input}],
38
+ temperature=0.6,
39
+ max_completion_tokens=4096,
40
+ top_p=0.95,
41
+ stream=True,
42
+ stop=None,
43
+ )
44
+
45
+ # Collect the response chunk by chunk
46
+ response = ""
47
+ for chunk in completion:
48
+ # Get the assistant's response from each chunk
49
+ response += chunk.choices[0].delta.content or ""
50
+
51
+ # Display assistant's response
52
+ st.write(f"Assistant: {response}")
53
+
54
+ except Exception as e:
55
+ st.write(f"Error occurred: {e}")
56
+ else:
57
+ st.write("Please type a question before submitting. 😊")
58
+
59
+ # Run the chatbot with dynamic user input
60
+ if __name__ == "__main__":
61
+ chatbot()