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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from deep_translator import GoogleTranslator
|
| 5 |
+
from gtts import gTTS
|
| 6 |
+
import base64
|
| 7 |
+
import tempfile
|
| 8 |
+
import google.generativeai as genai
|
| 9 |
+
import uuid
|
| 10 |
+
import speech_recognition as sr
|
| 11 |
+
|
| 12 |
+
# Load environment variables
|
| 13 |
+
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("π 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 |
+
|
| 57 |
+
def safe_translate(text, lang):
|
| 58 |
+
max_len = 500
|
| 59 |
+
chunks = [text[i:i+max_len] for i in range(0, len(text), max_len)]
|
| 60 |
+
return " ".join([GoogleTranslator(source='auto', target=lang).translate(chunk) for chunk in chunks])
|
| 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:
|
| 80 |
+
- Current knowledge: {knowledge}
|
| 81 |
+
- Goal: {goal}
|
| 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)
|
| 93 |
+
Do NOT return video titles without links.
|
| 94 |
+
"""
|
| 95 |
+
|
| 96 |
+
response = model.generate_content(prompt)
|
| 97 |
+
plan = response.text
|
| 98 |
+
|
| 99 |
+
# Translate if needed
|
| 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:
|
| 133 |
+
topic = st.session_state.study_topic
|
| 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}.
|
| 163 |
+
Each question must include:
|
| 164 |
+
- Question
|
| 165 |
+
- Four options (a, b, c, d)
|
| 166 |
+
- Correct answer line: Answer: x)
|
| 167 |
+
Format:
|
| 168 |
+
Q: [question]
|
| 169 |
+
a) ...
|
| 170 |
+
b) ...
|
| 171 |
+
c) ...
|
| 172 |
+
d) ...
|
| 173 |
+
Answer: x)
|
| 174 |
+
"""
|
| 175 |
+
quiz_text = model.generate_content(prompt).text
|
| 176 |
+
st.session_state.quiz_data = quiz_text.strip().split("\n\n")
|
| 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)
|
| 184 |
+
opts = [line for line in lines if line.strip()[:2] in ["a)", "b)", "c)", "d)"]]
|
| 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)
|
| 211 |
+
with open(fp.name, "rb") as f:
|
| 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}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
google-generativeai
|
| 3 |
+
gtts
|
| 4 |
+
SpeechRecognition
|
| 5 |
+
pydub
|
| 6 |
+
python-dotenv
|
| 7 |
+
deep-translator
|