an ai chatbot app for education and students
Drop files here
or click to upload
import streamlit as st
import random
import time
# Educational AI Chatbot for Students
st.set_page_config(
page_title="EduBot - AI Learning Assistant",
page_icon="🎓",
layout="wide"
)
# Initialize session state
if "messages" not in st.session_state:
st.session_state.messages = []
if "subject" not in st.session_state:
st.session_state.subject = "General"
if "difficulty" not in st.session_state:
st.session_state.difficulty = "Beginner"
# Educational knowledge base
educational_responses = {
"Math": {
"Beginner": [
"Great question! Let me break this down step by step. For basic math, remember to follow the order of operations: Parentheses, Exponents, Multiplication/Division (left to right), Addition/Subtraction (left to right).",
"Math can be fun! Try visualizing the problem. For example, if you're adding 5 + 3, imagine 5 objects and then 3 more objects. Count them all together!",
"Practice makes perfect in math! Start with simple problems and gradually work your way up. Would you like me to give you a practice problem?"
],
"Intermediate": [
"That's an excellent algebra question! Remember that what you do to one side of an equation, you must do to the other side to keep it balanced.",
"For geometry problems, try drawing a diagram. Visual representation often makes complex problems much clearer!",
"When working with fractions, remember to find a common denominator before adding or subtracting. For multiplication, multiply numerators together and denominators together."
],
"Advanced": [
"In calculus, remember that derivatives tell us about rates of change, while integrals help us find areas under curves.",
"For complex mathematical proofs, start with what you know and work step by step toward what you need to prove. Each step should follow logically from the previous one.",
"Advanced mathematics often involves pattern recognition. Look for similarities between new problems and ones you've solved before."
]
},
"Science": {
"Beginner": [
"Science is all about observation and asking questions! The scientific method starts with making observations, then forming hypotheses, and testing them.",
"Remember that in science, there are often patterns in nature. Look for what stays the same and what changes in different situations.",
"Don't be afraid to experiment! Some of the greatest scientific discoveries came from curiosity and trying new things."
],
"Intermediate": [
"When studying chemistry, remember that atoms are like building blocks. Different combinations create different substances with unique properties.",
"In physics, many concepts are connected. For example, force, mass, and acceleration are related by Newton's second law: F = ma.",
"Biology is the study of life. Remember that all living things share certain characteristics: they grow, reproduce, respond to their environment, and maintain homeostasis."
],
"Advanced": [
"Advanced science often involves understanding complex systems. Break them down into smaller, manageable parts and study how they interact.",
"In scientific research, always consider multiple variables and controls. Correlation doesn't necessarily imply causation.",
"Keep up with current scientific literature and research. Science is constantly evolving with new discoveries and technologies."
]
},
"History": {
"Beginner": [
"History helps us understand how we got to where we are today. Think of it as a story about real people who lived before us.",
"When studying history, try to understand the context - what was life like for people during that time period?",
"Remember that history is often about cause and effect. One event often leads to another, creating a chain of historical developments."
],
"Intermediate": [
"To understand historical events better, consider multiple perspectives. How did different groups of people experience the same event?",
"Look for patterns in history. While events don't repeat exactly, similar situations often have similar outcomes.",
"Primary sources (documents from the time period) give us direct insight into historical events and how people felt about them."
],
"Advanced": [
"Advanced historical analysis involves understanding historiography - how different historians have interpreted the same events over time.",
"Consider the economic, social, political, and cultural factors that influenced historical events. History is multidimensional.",
"Critical thinking is essential in history. Always ask: What evidence supports this claim? What might be missing from this narrative?"
]
},
"English": {
"Beginner": [
"Reading comprehension improves with practice! Start by identifying the main idea of what you're reading, then look for supporting details.",
"When writing, remember the basic structure: introduction, body paragraphs with evidence, and conclusion.",
"Grammar can be tricky, but don't worry! Focus on one rule at a time and practice it until it becomes natural."
],
"Intermediate": [
"Literary analysis involves looking beyond the surface. Ask yourself: What is the author really trying to say? What techniques do they use?",
"When writing essays, make sure each paragraph has a clear topic sentence and supporting evidence. Transitions help connect your ideas smoothly.",
"Vocabulary grows through reading and context. When you encounter new words, try to understand their meaning from the surrounding text."
],
"Advanced": [
"Advanced literature study involves understanding themes, symbolism, and literary devices. How does the author's style contribute to the meaning?",
"In analytical writing, present a clear thesis and support it with textual evidence. Consider counterarguments to strengthen your position.",
"Understanding literary movements and historical context enhances your interpretation of texts. How does the time period influence the work?"
]
},
"General": [
"Learning is a journey, not a destination! Every question you ask helps you grow. What would you like to explore today?",
"The best way to learn is through active engagement. Try to connect new information to what you already know.",
"Don't be discouraged by challenges - they're opportunities to grow! Every expert was once a beginner.",
"Study techniques that work well include: taking breaks, teaching others, and practicing regularly rather than cramming.",
"Critical thinking is a valuable skill across all subjects. Always ask 'why' and 'how' to deepen your understanding."
]
}
def get_educational_response(subject, difficulty, user_message):
"""Generate an educational response based on subject and difficulty"""
# Simulate processing time
time.sleep(1)
# Check for specific keywords in user message
keywords = {
"homework": "I'd be happy to help guide you through your homework! Remember, the goal is to help you understand, not just get the answer. What specific part are you struggling with?",
"test": "Test preparation is important! Try these strategies: review your notes, practice problems, get enough sleep, and stay calm during the test. What subject is your test on?",
"study": "Great study habits include: setting a regular schedule, finding a quiet space, taking breaks, and actively engaging with the material. What are you studying?",
"difficult": "It's normal to find some topics challenging! Break difficult concepts into smaller parts, ask for help when needed, and practice regularly. What's giving you trouble?",
"help": "I'm here to help you learn! What specific topic or question do you have? Remember, asking questions is a sign of good learning!",
}
user_lower = user_message.lower()
for keyword, response in keywords.items():
if keyword in user_lower:
return response
# Get response based on subject and difficulty
if subject in educational_responses and difficulty in educational_responses[subject]:
responses = educational_responses[subject][difficulty]
else:
responses = educational_responses["General"]
return random.choice(responses)
# Sidebar for settings
with st.sidebar:
st.header("🎓 EduBot Settings")
# Subject selection
st.session_state.subject = st.selectbox(
"Choose your subject:",
["General", "Math", "Science", "History", "English"],
index=["General", "Math", "Science", "History", "English"].index(st.session_state.subject)
)
# Difficulty level
st.session_state.difficulty = st.selectbox(
"Select difficulty level:",
["Beginner", "Intermediate", "Advanced"],
index=["Beginner", "Intermediate", "Advanced"].index(st.session_state.difficulty)
)
st.markdown("---")
# Study tips
st.subheader("💡 Quick Study Tips")
study_tips = [
"Take regular breaks (Pomodoro technique)",
"Teach someone else what you learned",
"Use active recall instead of re-reading",
"Create mind maps for complex topics",
"Practice spaced repetition",
"Ask questions to deepen understanding"
]
for tip in study_tips:
st.write(f"• {tip}")
st.markdown("---")
# Clear chat button
if st.button("🗑️ Clear Chat History"):
st.session_state.messages = []
st.rerun()
# Main chat interface
st.title("🎓 EduBot - Your AI Learning Assistant")
st.markdown(f"**Current Subject:** {st.session_state.subject} | **Level:** {st.session_state.difficulty}")
# Welcome message
if not st.session_state.messages:
welcome_msg = f"""
👋 Welcome to EduBot! I'm here to help you learn and understand {st.session_state.subject} at the {st.session_state.difficulty} level.
You can ask me about:
• Homework help and guidance
• Concept explanations
• Study strategies
• Test preparation tips
• General learning questions
Remember: I'm here to guide your learning, not just give you answers! Let's explore knowledge together. 📚
"""
st.session_state.messages.append({"role": "assistant", "content": welcome_msg})
# Display chat messages
chat_container = st.container()
with chat_container:
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Chat input
if prompt := st.chat_input("Ask me anything about your studies..."):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message
with st.chat_message("user"):
st.markdown(prompt)
# Generate and display assistant response
with st.chat_message("assistant"):
with st.spinner("Thinking about your question..."):
response = get_educational_response(
st.session_state.subject,
st.session_state.difficulty,
prompt
)
st.markdown(response)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
# Footer
st.markdown("---")
st.markdown(
"""
<div style='text-align: center; color: #666;'>
<small>EduBot is designed to guide your learning journey. Always verify important information with your teachers or textbooks!</small>
</div>
""",
unsafe_allow_html=True
)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?