Update app.py
Browse files
app.py
CHANGED
|
@@ -14,43 +14,43 @@ load_dotenv()
|
|
| 14 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 15 |
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 16 |
|
| 17 |
-
st.set_page_config(page_title="LearnMate - AI Buddy", page_icon="
|
| 18 |
-
st.title("
|
| 19 |
|
| 20 |
# Sidebar: Enhanced Sidebar with Goals and Tasks
|
| 21 |
-
st.sidebar.title("
|
| 22 |
|
| 23 |
# Learning Goals Section
|
| 24 |
-
st.sidebar.subheader("
|
| 25 |
learning_goal = st.sidebar.text_input("Add a Goal")
|
| 26 |
-
if st.sidebar.button("
|
| 27 |
if "goals" not in st.session_state:
|
| 28 |
st.session_state.goals = []
|
| 29 |
st.session_state.goals.append(learning_goal)
|
| 30 |
|
| 31 |
if "goals" in st.session_state:
|
| 32 |
for goal in st.session_state.goals:
|
| 33 |
-
st.sidebar.markdown(f"
|
| 34 |
|
| 35 |
# Project Tracker
|
| 36 |
-
st.sidebar.subheader("
|
| 37 |
if "todo" not in st.session_state:
|
| 38 |
st.session_state.todo = []
|
| 39 |
if "done" not in st.session_state:
|
| 40 |
st.session_state.done = []
|
| 41 |
|
| 42 |
-
new_task = st.sidebar.text_input("
|
| 43 |
-
if st.sidebar.button("
|
| 44 |
st.session_state.todo.append(new_task)
|
| 45 |
|
| 46 |
for i, task in enumerate(st.session_state.todo):
|
| 47 |
-
if st.sidebar.checkbox(f"
|
| 48 |
st.session_state.todo.remove(task)
|
| 49 |
st.session_state.done.append(task)
|
| 50 |
|
| 51 |
-
st.sidebar.subheader("
|
| 52 |
for i, task in enumerate(st.session_state.done):
|
| 53 |
-
st.sidebar.checkbox(f"
|
| 54 |
|
| 55 |
# Translation helper
|
| 56 |
|
|
@@ -61,19 +61,19 @@ def safe_translate(text, lang):
|
|
| 61 |
|
| 62 |
# Tabs
|
| 63 |
|
| 64 |
-
TABS = st.tabs(["
|
| 65 |
|
| 66 |
-
# ------------------------
|
| 67 |
with TABS[0]:
|
| 68 |
-
st.header("
|
| 69 |
|
| 70 |
-
lang = st.selectbox("
|
| 71 |
-
knowledge = st.text_area("
|
| 72 |
-
goal = st.text_area("
|
| 73 |
-
style = st.selectbox("
|
| 74 |
|
| 75 |
-
if st.button("
|
| 76 |
-
with st.spinner("
|
| 77 |
prompt = f"""
|
| 78 |
You are LearnMate, an expert AI tutor.
|
| 79 |
The user has the following:
|
|
@@ -82,11 +82,11 @@ with TABS[0]:
|
|
| 82 |
- Preferred learning style: {style}
|
| 83 |
|
| 84 |
Please generate a full markdown learning roadmap that includes:
|
| 85 |
-
1.
|
| 86 |
-
2.
|
| 87 |
-
3.
|
| 88 |
-
4.
|
| 89 |
-
5.
|
| 90 |
|
| 91 |
Format all sections clearly with markdown headers (##) and bullet points.
|
| 92 |
Example for video: [How Neural Networks Learn](https://www.youtube.com/watch?v=aircAruvnKk)
|
|
@@ -100,33 +100,33 @@ with TABS[0]:
|
|
| 100 |
if lang != "english":
|
| 101 |
plan = safe_translate(plan, lang)
|
| 102 |
|
| 103 |
-
st.markdown("###
|
| 104 |
st.markdown(plan)
|
| 105 |
|
| 106 |
# Enable download
|
| 107 |
st.download_button(
|
| 108 |
-
label="
|
| 109 |
data=plan,
|
| 110 |
file_name="learning_plan.txt",
|
| 111 |
mime="text/plain"
|
| 112 |
)
|
| 113 |
|
| 114 |
st.markdown("---")
|
| 115 |
-
st.success("
|
| 116 |
-
# ------------------------
|
| 117 |
-
# ------------------------
|
| 118 |
with TABS[1]:
|
| 119 |
-
st.header("
|
| 120 |
if "study_step" not in st.session_state:
|
| 121 |
st.session_state.study_step = 1
|
| 122 |
if "chat_history" not in st.session_state:
|
| 123 |
st.session_state.chat_history = []
|
| 124 |
|
| 125 |
if st.session_state.study_step == 1:
|
| 126 |
-
st.write("Let's get started
|
| 127 |
-
st.session_state.study_topic = st.text_input("
|
| 128 |
st.session_state.confidence_level = st.slider("Confidence (0-10)", 0, 10)
|
| 129 |
-
if st.button("
|
| 130 |
st.session_state.study_step = 2
|
| 131 |
|
| 132 |
elif st.session_state.study_step == 2:
|
|
@@ -134,29 +134,29 @@ with TABS[1]:
|
|
| 134 |
score = st.session_state.confidence_level
|
| 135 |
prompt = f"User is studying: {topic}, confidence: {score}/10. Suggest action plan, style-based activities & encouragement."
|
| 136 |
reply = model.generate_content(prompt).text
|
| 137 |
-
st.markdown("###
|
| 138 |
st.markdown(reply)
|
| 139 |
-
if st.button("
|
| 140 |
st.session_state.study_step = 3
|
| 141 |
|
| 142 |
elif st.session_state.study_step == 3:
|
| 143 |
-
st.subheader("
|
| 144 |
user_msg = st.text_input("You:", key="twin_input")
|
| 145 |
-
if st.button("
|
| 146 |
chat = model.start_chat(history=st.session_state.chat_history)
|
| 147 |
reply = chat.send_message(user_msg)
|
| 148 |
st.session_state.chat_history.append({"role": "user", "parts": [user_msg]})
|
| 149 |
st.session_state.chat_history.append({"role": "model", "parts": [reply.text]})
|
| 150 |
|
| 151 |
for msg in st.session_state.chat_history:
|
| 152 |
-
role = "
|
| 153 |
st.markdown(f"**{role}:** {msg['parts'][0]}")
|
| 154 |
-
# ------------------------
|
| 155 |
with TABS[2]:
|
| 156 |
-
st.header("
|
| 157 |
|
| 158 |
-
topic = st.text_input("
|
| 159 |
-
if st.button("
|
| 160 |
prompt = f"""
|
| 161 |
You are a quiz master.
|
| 162 |
Generate 5 multiple choice questions (MCQs) for the topic: {topic}.
|
|
@@ -177,7 +177,7 @@ with TABS[2]:
|
|
| 177 |
st.session_state.full_quiz_text = quiz_text
|
| 178 |
|
| 179 |
if "quiz_data" in st.session_state:
|
| 180 |
-
st.markdown("###
|
| 181 |
for i, q_block in enumerate(st.session_state.quiz_data):
|
| 182 |
lines = q_block.strip().split("\n")
|
| 183 |
q_line = next((l for l in lines if l.strip().lower().startswith("q:")), None)
|
|
@@ -185,26 +185,26 @@ with TABS[2]:
|
|
| 185 |
ans_line = next((l for l in lines if "Answer:" in l), None)
|
| 186 |
|
| 187 |
if not (q_line and opts and ans_line):
|
| 188 |
-
st.warning(f"
|
| 189 |
continue
|
| 190 |
|
| 191 |
correct = ans_line.split(":")[-1].strip().lower()
|
| 192 |
selected = st.radio(f"Q{i+1}: {q_line[2:].strip()}", opts, key=f"quiz_{i}")
|
| 193 |
|
| 194 |
-
if st.button(f"
|
| 195 |
if selected.lower().startswith(correct):
|
| 196 |
-
st.success("
|
| 197 |
else:
|
| 198 |
-
st.error(f"
|
| 199 |
|
| 200 |
# Download full quiz
|
| 201 |
st.markdown("---")
|
| 202 |
-
st.download_button("
|
| 203 |
-
# ------------------------
|
| 204 |
with TABS[3]:
|
| 205 |
-
st.header("
|
| 206 |
text = st.text_area("Enter content:")
|
| 207 |
-
if st.button("
|
| 208 |
tts = gTTS(text)
|
| 209 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
|
| 210 |
tts.save(fp.name)
|
|
@@ -212,16 +212,16 @@ with TABS[3]:
|
|
| 212 |
audio_data = f.read()
|
| 213 |
b64 = base64.b64encode(audio_data).decode()
|
| 214 |
st.audio(f"data:audio/mp3;base64,{b64}", format='audio/mp3')
|
| 215 |
-
st.download_button("
|
| 216 |
|
| 217 |
-
# ------------------------
|
| 218 |
with TABS[4]:
|
| 219 |
-
st.header("
|
| 220 |
lang = st.selectbox("Choose Language", ["hindi", "tamil", "telugu"])
|
| 221 |
msg = st.text_area("Type your message:")
|
| 222 |
-
if st.button("
|
| 223 |
try:
|
| 224 |
translated = GoogleTranslator(source="en", target=lang).translate(msg)
|
| 225 |
st.success(f"Translated ({lang.upper()}): {translated}")
|
| 226 |
except Exception as e:
|
| 227 |
-
st.error(f"Error: {e}")
|
|
|
|
| 14 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 15 |
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 16 |
|
| 17 |
+
st.set_page_config(page_title="LearnMate - AI Buddy", page_icon="📚")
|
| 18 |
+
st.title("🎓 LearnMate - AI Learning Companion")
|
| 19 |
|
| 20 |
# Sidebar: Enhanced Sidebar with Goals and Tasks
|
| 21 |
+
st.sidebar.title("📌 LearnMate Dashboard")
|
| 22 |
|
| 23 |
# Learning Goals Section
|
| 24 |
+
st.sidebar.subheader("🎯 Your Learning Goals")
|
| 25 |
learning_goal = st.sidebar.text_input("Add a Goal")
|
| 26 |
+
if st.sidebar.button("➕ Add Goal") and learning_goal:
|
| 27 |
if "goals" not in st.session_state:
|
| 28 |
st.session_state.goals = []
|
| 29 |
st.session_state.goals.append(learning_goal)
|
| 30 |
|
| 31 |
if "goals" in st.session_state:
|
| 32 |
for goal in st.session_state.goals:
|
| 33 |
+
st.sidebar.markdown(f"✅ {goal}")
|
| 34 |
|
| 35 |
# Project Tracker
|
| 36 |
+
st.sidebar.subheader("📋😎Task Tracker")
|
| 37 |
if "todo" not in st.session_state:
|
| 38 |
st.session_state.todo = []
|
| 39 |
if "done" not in st.session_state:
|
| 40 |
st.session_state.done = []
|
| 41 |
|
| 42 |
+
new_task = st.sidebar.text_input("🆕👉 New Task")
|
| 43 |
+
if st.sidebar.button("📌🎯 Add Task") and new_task:
|
| 44 |
st.session_state.todo.append(new_task)
|
| 45 |
|
| 46 |
for i, task in enumerate(st.session_state.todo):
|
| 47 |
+
if st.sidebar.checkbox(f"⬜ {task}", key=f"todo_{i}_{task}"):
|
| 48 |
st.session_state.todo.remove(task)
|
| 49 |
st.session_state.done.append(task)
|
| 50 |
|
| 51 |
+
st.sidebar.subheader("✅🙌Task Completed")
|
| 52 |
for i, task in enumerate(st.session_state.done):
|
| 53 |
+
st.sidebar.checkbox(f"✔️ {task}", value=True, disabled=True, key=f"done_{i}_{task}")
|
| 54 |
|
| 55 |
# Translation helper
|
| 56 |
|
|
|
|
| 61 |
|
| 62 |
# Tabs
|
| 63 |
|
| 64 |
+
TABS = st.tabs(["📘 Learning Path", "💬 Study Twin", "🧪 Quiz Generator", "🎧 Audio Summary", "🌐 Regional Buddy"])
|
| 65 |
|
| 66 |
+
# ------------------------ 📘 Learning Path ------------------------#
|
| 67 |
with TABS[0]:
|
| 68 |
+
st.header("📘 Build Your Learning Roadmap")
|
| 69 |
|
| 70 |
+
lang = st.selectbox("🌐 Language", ["english", "hindi", "tamil", "telugu"])
|
| 71 |
+
knowledge = st.text_area("🧠 Your Current Knowledge")
|
| 72 |
+
goal = st.text_area("🎯 Learning Goal")
|
| 73 |
+
style = st.selectbox("🧩 Learning Style", ["Visual", "Reading", "Hands-on", "Mixed"])
|
| 74 |
|
| 75 |
+
if st.button("🚀 Generate Plan"):
|
| 76 |
+
with st.spinner("🧠 Crafting your custom roadmap..."):
|
| 77 |
prompt = f"""
|
| 78 |
You are LearnMate, an expert AI tutor.
|
| 79 |
The user has the following:
|
|
|
|
| 82 |
- Preferred learning style: {style}
|
| 83 |
|
| 84 |
Please generate a full markdown learning roadmap that includes:
|
| 85 |
+
1. 📘 Stage-by-stage steps with estimated timelines.
|
| 86 |
+
2. 🎨 Visual-style flow or layout described in text if user chose 'Visual'.
|
| 87 |
+
3. 📺 Three **specific YouTube videos** including titles and real video **hyperlinks**.
|
| 88 |
+
4. 📚 Recommended resources, tools or tutorials related to the goal.
|
| 89 |
+
5. 🧠 Personalized study tips matching the selected learning style.
|
| 90 |
|
| 91 |
Format all sections clearly with markdown headers (##) and bullet points.
|
| 92 |
Example for video: [How Neural Networks Learn](https://www.youtube.com/watch?v=aircAruvnKk)
|
|
|
|
| 100 |
if lang != "english":
|
| 101 |
plan = safe_translate(plan, lang)
|
| 102 |
|
| 103 |
+
st.markdown("### 📜 Your Learning Plan")
|
| 104 |
st.markdown(plan)
|
| 105 |
|
| 106 |
# Enable download
|
| 107 |
st.download_button(
|
| 108 |
+
label="⬇️ Download Plan as .txt",
|
| 109 |
data=plan,
|
| 110 |
file_name="learning_plan.txt",
|
| 111 |
mime="text/plain"
|
| 112 |
)
|
| 113 |
|
| 114 |
st.markdown("---")
|
| 115 |
+
st.success("✅ Video links are now clickable. Save this roadmap and start learning!")
|
| 116 |
+
# ------------------------ 💬 Study Twin ------------------------
|
| 117 |
+
# ------------------------ 💬 Study Twin ------------------------
|
| 118 |
with TABS[1]:
|
| 119 |
+
st.header("💬 AI Study Twin👯")
|
| 120 |
if "study_step" not in st.session_state:
|
| 121 |
st.session_state.study_step = 1
|
| 122 |
if "chat_history" not in st.session_state:
|
| 123 |
st.session_state.chat_history = []
|
| 124 |
|
| 125 |
if st.session_state.study_step == 1:
|
| 126 |
+
st.write("Let's get started ✨")
|
| 127 |
+
st.session_state.study_topic = st.text_input("📘 What topic are you studying?")
|
| 128 |
st.session_state.confidence_level = st.slider("Confidence (0-10)", 0, 10)
|
| 129 |
+
if st.button("➡️ Continue"):
|
| 130 |
st.session_state.study_step = 2
|
| 131 |
|
| 132 |
elif st.session_state.study_step == 2:
|
|
|
|
| 134 |
score = st.session_state.confidence_level
|
| 135 |
prompt = f"User is studying: {topic}, confidence: {score}/10. Suggest action plan, style-based activities & encouragement."
|
| 136 |
reply = model.generate_content(prompt).text
|
| 137 |
+
st.markdown("### 🎯 Suggestion")
|
| 138 |
st.markdown(reply)
|
| 139 |
+
if st.button("💬 Ask a Question🌟"):
|
| 140 |
st.session_state.study_step = 3
|
| 141 |
|
| 142 |
elif st.session_state.study_step == 3:
|
| 143 |
+
st.subheader("🤖 Chat with Your Twin")
|
| 144 |
user_msg = st.text_input("You:", key="twin_input")
|
| 145 |
+
if st.button("📨 Send"):
|
| 146 |
chat = model.start_chat(history=st.session_state.chat_history)
|
| 147 |
reply = chat.send_message(user_msg)
|
| 148 |
st.session_state.chat_history.append({"role": "user", "parts": [user_msg]})
|
| 149 |
st.session_state.chat_history.append({"role": "model", "parts": [reply.text]})
|
| 150 |
|
| 151 |
for msg in st.session_state.chat_history:
|
| 152 |
+
role = "🧑 You" if msg["role"] == "user" else "🤖 Twin"
|
| 153 |
st.markdown(f"**{role}:** {msg['parts'][0]}")
|
| 154 |
+
# ------------------------ 🧪 Quiz Generator ------------------------
|
| 155 |
with TABS[2]:
|
| 156 |
+
st.header("🧪 Test Yourself!")
|
| 157 |
|
| 158 |
+
topic = st.text_input("📘 Enter a topic to quiz yourself:")
|
| 159 |
+
if st.button("🎯 Generate Quiz"):
|
| 160 |
prompt = f"""
|
| 161 |
You are a quiz master.
|
| 162 |
Generate 5 multiple choice questions (MCQs) for the topic: {topic}.
|
|
|
|
| 177 |
st.session_state.full_quiz_text = quiz_text
|
| 178 |
|
| 179 |
if "quiz_data" in st.session_state:
|
| 180 |
+
st.markdown("### 📝 Your Quiz")
|
| 181 |
for i, q_block in enumerate(st.session_state.quiz_data):
|
| 182 |
lines = q_block.strip().split("\n")
|
| 183 |
q_line = next((l for l in lines if l.strip().lower().startswith("q:")), None)
|
|
|
|
| 185 |
ans_line = next((l for l in lines if "Answer:" in l), None)
|
| 186 |
|
| 187 |
if not (q_line and opts and ans_line):
|
| 188 |
+
st.warning(f"❌ Skipping malformed Q{i+1}")
|
| 189 |
continue
|
| 190 |
|
| 191 |
correct = ans_line.split(":")[-1].strip().lower()
|
| 192 |
selected = st.radio(f"Q{i+1}: {q_line[2:].strip()}", opts, key=f"quiz_{i}")
|
| 193 |
|
| 194 |
+
if st.button(f"✔️ Check Q{i+1}", key=f"btn_{i}"):
|
| 195 |
if selected.lower().startswith(correct):
|
| 196 |
+
st.success("✅ Correct!")
|
| 197 |
else:
|
| 198 |
+
st.error(f"❌ Wrong. Correct answer is: {correct}")
|
| 199 |
|
| 200 |
# Download full quiz
|
| 201 |
st.markdown("---")
|
| 202 |
+
st.download_button("⬇️ Download Full Quiz (.txt)", st.session_state.full_quiz_text, file_name="quiz.txt")
|
| 203 |
+
# ------------------------ 🎧 Audio Summary ------------------------
|
| 204 |
with TABS[3]:
|
| 205 |
+
st.header("🎧 Audio Summary")
|
| 206 |
text = st.text_area("Enter content:")
|
| 207 |
+
if st.button("🔊 Generate Audio"):
|
| 208 |
tts = gTTS(text)
|
| 209 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
|
| 210 |
tts.save(fp.name)
|
|
|
|
| 212 |
audio_data = f.read()
|
| 213 |
b64 = base64.b64encode(audio_data).decode()
|
| 214 |
st.audio(f"data:audio/mp3;base64,{b64}", format='audio/mp3')
|
| 215 |
+
st.download_button("⬇️ Download Audio", audio_data, file_name="audio_summary.mp3")
|
| 216 |
|
| 217 |
+
# ------------------------ 🌐 Regional Buddy ------------------------
|
| 218 |
with TABS[4]:
|
| 219 |
+
st.header("🌐 Speak in Your Language")
|
| 220 |
lang = st.selectbox("Choose Language", ["hindi", "tamil", "telugu"])
|
| 221 |
msg = st.text_area("Type your message:")
|
| 222 |
+
if st.button("🔁 Translate"):
|
| 223 |
try:
|
| 224 |
translated = GoogleTranslator(source="en", target=lang).translate(msg)
|
| 225 |
st.success(f"Translated ({lang.upper()}): {translated}")
|
| 226 |
except Exception as e:
|
| 227 |
+
st.error(f"Error: {e}")
|