File size: 15,553 Bytes
8fb9fd2
 
c370011
 
 
 
8fb9fd2
e4f7991
c370011
 
8fb9fd2
c370011
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fb9fd2
de7f305
 
 
 
 
 
8fb9fd2
 
 
 
 
c370011
e4f7991
c370011
 
 
 
 
 
8fb9fd2
c370011
 
 
 
 
 
 
8fb9fd2
c370011
 
 
 
e4f7991
c370011
 
 
 
 
 
 
 
 
 
 
 
 
 
e4f7991
c370011
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4f7991
8fb9fd2
 
 
 
 
c370011
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fb9fd2
 
e4f7991
 
 
 
 
 
 
 
 
 
c370011
e4f7991
 
 
 
 
8fb9fd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c370011
e4f7991
8fb9fd2
 
 
 
e4f7991
 
 
 
c370011
 
 
 
8fb9fd2
 
 
 
c370011
 
 
 
8fb9fd2
c370011
 
 
8fb9fd2
de7f305
8fb9fd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c370011
8fb9fd2
 
 
c370011
8fb9fd2
 
 
 
 
 
c370011
 
8fb9fd2
 
 
 
c370011
8fb9fd2
c370011
 
 
 
 
 
 
8fb9fd2
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import backend
import gradio as gr
import re, os, random, json, requests, math
import pandas as pd
from collections import defaultdict
from utilit import skills as all_skills
from groq import Groq

OLLAMA_URL = "http://localhost:11434/api/chat"
MODEL = "qwen2.5:1.5b"

HIERARCHY = {
    "Arithmetic Operations": {1: ["whole numbers", "ordering", "number line"], 2: ["integers"], 3: ["decimals"], 4: ["order of operations"]},
    "Fractions Decimals Percents": {1: ["equivalent fractions", "conversion"], 2: ["fraction", "decimal"], 3: ["mixed fractions"], 4: ["percent", "ratio", "rate"]},
    "Algebra Basics": {1: ["expression", "terms"], 2: ["like terms", "distributive"], 3: ["simplifying"], 4: ["polynomial"]},
    "Equations and Inequalities": {1: ["one step", "two step"], 2: ["solving"], 3: ["multi", "more than two"], 4: ["inequality"]},
    "Linear Functions": {1: ["pattern", "table"], 2: ["slope"], 3: ["graph"], 4: ["write linear equation", "from situation"]},
    "Systems of Equations": {3: ["graphing"], 4: ["substitution", "system"]},
    "Geometry": {1: ["angles", "triangle", "polygon"], 2: ["perimeter", "area"], 3: ["volume"], 4: ["surface area", "3d"]},
    "Statistics": {1: ["mean", "median", "mode"], 2: ["range"], 3: ["plot", "histogram"], 4: ["scatter", "sampling"]},
    "Probability": {1: ["single"], 2: ["venn"], 3: ["two"], 4: ["combinatorics", "counting"]},
    "Advanced Algebra": {2: ["factoring"], 3: ["quadratic"], 4: ["quadratic formula"]}
}

CLUSTERS = {
    "Arithmetic Operations": ["addition", "subtraction", "multiplication", "division", "order of operations", "computation", "estimation"],
    "Fractions Decimals Percents": ["fraction", "decimal", "percent", "ratio", "rate"],
    "Number Theory": ["prime", "factor", "multiple", "divisibility", "gcf", "lcm", "odd", "even"],
    "Algebra Basics": ["expression", "like terms", "distributive", "polynomial", "simplifying"],
    "Equations and Inequalities": ["equation", "inequality", "solving", "variable"],
    "Linear Functions": ["linear", "slope", "y-intercept", "graph", "table"],
    "Systems of Equations": ["system"],
    "Geometry": ["angle", "triangle", "polygon", "circle", "perimeter", "area", "volume", "surface area", "prism", "cone", "sphere"],
    "Statistics": ["mean", "median", "mode", "range", "plot", "histogram", "scatter"],
    "Probability": ["probability", "venn", "combinatorics", "counting"],
    "Transformations": ["rotation", "reflection", "translation", "symmetry"],
    "Coordinate Geometry": ["coordinate", "distance", "midpoint"],
    "Advanced Algebra": ["quadratic", "factoring", "trinomials"]
}

SKILL_VIDEOS = {
    "whole numbers": "https://www.youtube.com/watch?v=m94WTzp14SA",
    "ordering": "https://www.youtube.com/watch?v=0_fC9Lsn7-8",
    "number line": "https://www.youtube.com/watch?v=6u3pU9V7NoQ",
    "integers": "https://www.youtube.com/watch?v=vNguZreVv_g",
    "decimals": "https://www.youtube.com/watch?v=kgv_UnE_iXQ",
    "order of operations": "https://www.youtube.com/watch?v=dAgfnK528RA",
    "equivalent fractions": "https://www.youtube.com/watch?v=qcHHbaKo_7E",
    "conversion": "https://www.youtube.com/watch?v=do_IbHId2Os",
    "fraction": "https://www.youtube.com/watch?v=CA9Xp80ZfIs",
    "decimal": "https://www.youtube.com/watch?v=9VvU6Sow064",
    "mixed fractions": "https://www.youtube.com/watch?v=1xuf6Z40Y8U",
    "percent": "https://www.youtube.com/watch?v=JeVSmq1Nrpw",
    "ratio": "https://www.youtube.com/watch?v=HpdMJaKa9P0",
    "rate": "https://www.youtube.com/watch?v=LIYfbe7_EaA",
    "expression": "https://www.youtube.com/watch?v=LpUeZ8T_6mE",
    "terms": "https://www.youtube.com/watch?v=0AfS8vS4Zks",
    "like terms": "https://www.youtube.com/watch?v=H_N1_Z9_X7Y",
    "distributive": "https://www.youtube.com/watch?v=v-6MShC82ow",
    "simplifying": "https://www.youtube.com/watch?v=3S9S8p_UuU4",
    "polynomial": "https://www.youtube.com/watch?v=ffLLmV4mZwU",
    "one step": "https://www.youtube.com/watch?v=L0_K895874A",
    "two step": "https://www.youtube.com/watch?v=LDIiYKYvvdA",
    "solving": "https://www.youtube.com/watch?v=bAerID24QJ0",
    "multi": "https://www.youtube.com/watch?v=f15zA0PhSek",
    "more than two": "https://www.youtube.com/watch?v=13YFOnR_kio",
    "inequality": "https://www.youtube.com/watch?v=njpSAtIDZ30",
    "pattern": "https://www.youtube.com/watch?v=8mGeU_uIs9I",
    "table": "https://www.youtube.com/watch?v=7u_X6X8tU-A",
    "slope": "https://www.youtube.com/watch?v=v8VSDg_WQlA",
    "graph": "https://www.youtube.com/watch?v=f58_XvY9o30",
    "write linear equation": "https://www.youtube.com/watch?v=eHPTyYbNmxw",
    "from situation": "https://www.youtube.com/watch?v=m682S8xUuYg",
    "graphing": "https://www.youtube.com/watch?v=S0_qX4VJhMQ",
    "substitution": "https://www.youtube.com/watch?v=zsD965-X-Vw",
    "system": "https://www.youtube.com/watch?v=fNk_zzaMoBA",
    "angles": "https://www.youtube.com/watch?v=DGKwdHMiqCg",
    "triangle": "https://www.youtube.com/watch?v=mLeNaZcy-hE",
    "polygon": "https://www.youtube.com/watch?v=IaoZhhx_I9s",
    "perimeter": "https://www.youtube.com/watch?v=AAY1bsazcgM",
    "area": "https://www.youtube.com/watch?v=xCdxURXMdFY",
    "volume": "https://www.youtube.com/watch?v=qJwecTgce6c",
    "surface area": "https://www.youtube.com/watch?v=uK5E_T6fX7c",
    "3d": "https://www.youtube.com/watch?v=ZqzAOZasN9A",
    "mean": "https://www.youtube.com/watch?v=B1HEzQu68tY",
    "median": "https://www.youtube.com/watch?v=B1HEzQu68tY",
    "mode": "https://www.youtube.com/watch?v=B1HEzQu68tY",
    "range": "https://www.youtube.com/watch?v=B1HEzQu68tY",
    "plot": "https://www.youtube.com/watch?v=mH_S_zT3qH0",
    "histogram": "https://www.youtube.com/watch?v=gY-mreidV-4",
    "scatter": "https://www.youtube.com/watch?v=pGZ37A47nI0",
    "sampling": "https://www.youtube.com/watch?v=pTuj57uXWlk",
    "single": "https://www.youtube.com/watch?v=KzfWUEJjG18",
    "venn": "https://www.youtube.com/watch?v=XdfS2-W8WnU",
    "two": "https://www.youtube.com/watch?v=Y-XU9rL-pY0",
    "combinatorics": "https://www.youtube.com/watch?v=Tr6Yp3_78uY",
    "counting": "https://www.youtube.com/watch?v=O129Sscf6lE",
    "factoring": "https://www.youtube.com/watch?v=f_Vp_74p_G8",
    "quadratic": "https://www.youtube.com/watch?v=6QYatS7vV0o",
    "quadratic formula": "https://www.youtube.com/watch?v=EeVqtpuGgu8"
}

hierarchy_skills_list = []
for domain_data in HIERARCHY.values():
    for skill_group in domain_data.values():
        hierarchy_skills_list.extend(skill_group)

css = """
body, .gradio-container {
    font-size: 16px;
}
"""

groq_key = os.getenv("GROQ_KEY")
backend.init_db()
client = Groq(api_key=groq_key)
model_id = "llama-3.3-70b-versatile"

session_skills = []

def assign_cluster(skill):
    skill_lower = str(skill).lower()
    for cluster, keywords in CLUSTERS.items():
        if any(keyword in skill_lower for keyword in keywords):
            return cluster
    return "Other"

def assign_hierarchy(skill):
    skill_lower = str(skill).lower()
    for domain, levels in HIERARCHY.items():
        for level, keywords in levels.items():
            if any(keyword in skill_lower for keyword in keywords):
                return domain, level
    return "Other", -1

def find_most_similar_prediction(current_session_skills):
    all_logs = backend.get_all_logs()
    if not all_logs:
        return [], "Database is empty."

    student_histories = defaultdict(list)
    for user_id, skill in all_logs:
        student_histories[str(user_id)].append(str(skill))
    
    if not student_histories:
        return [], "No student data available."
        
    random_student = random.choice(list(student_histories.keys()))
    selected_history = student_histories[random_student][-30:]
    
    formatted_history = []
    for s in selected_history:
        domain, level = assign_hierarchy(s)
        formatted_history.append(f"(skill={s}, cluster={assign_cluster(s)}, level={level})")

    prompt = f"""You are a student knowledge model.
    Reference Student history: {", ".join(formatted_history)}
    The current user has expressed difficulty with: {", ".join(current_session_skills)}
    Available hierarchy skills: {", ".join(hierarchy_skills_list)}
    
    Predict the NEXT 3 most likely skills (ranked) for the current user.
    Reply ONLY with JSON: {{"skills": ["Skill_A", "Skill_B", "Skill_C"]}}"""

    try:
        response = requests.post(
            OLLAMA_URL, 
            json={
                "model": MODEL,
                "messages": [{"role": "user", "content": prompt}],
                "stream": False,
                "format": "json"
            }, 
            timeout=15
        )
        
        response.raise_for_status()
        raw_data = response.json()
        text_content = raw_data.get("message", {}).get("content", "{}")
        parsed = json.loads(text_content)
        preds = parsed.get("skills", [])
        
        for i, skill in enumerate(preds, 1):
            print(f"Rank {i}: {skill}")
            
        return preds, f"Random match based on Student ID: {random_student}"

    except Exception as e:
        print(f"Detailed LLM Error: {e}")
        return [], f"LLM processing error: {str(e)}"

def get_youtube_id(url):
    pattern = r'(?:https?://)?(?:www\.)?(?:youtube\.com/(?:watch\?v=|embed/)|youtu\.be/)([a-zA-Z0-9_-]{11})'
    match = re.search(pattern, url)
    return match.group(1) if match else None

def display_video_and_predict():
    preds, info_msg = find_most_similar_prediction(session_skills)
    
    print(f"Final Output: {info_msg} Recommendations: {preds}")
    
    if not preds:
        return "<p style='text-align:center;'>No recommendation available.</p>", f"### Personalized Path\n{info_msg}"

    main_skill = preds[0]
    main_url = SKILL_VIDEOS.get(main_skill, "https://www.youtube.com/watch?v=WUvTyaaNkzM")
    video_id = get_youtube_id(main_url)
    
    video_html = f'''
    <div style="display: flex; flex-direction: column; align-items: center;">
        <h3 style="margin-bottom: 10px;">Main Lesson: {main_skill.title()}</h3>
        <iframe width="100%" height="450" src="https://www.youtube.com/embed/{video_id}?autoplay=1" frameborder="0" allowfullscreen></iframe>
    </div>
    '''

    other_markdown = f"### Personalized Path\n{info_msg}\n\n#### Other recommendation\n"
    for skill in preds[1:]:
        url = SKILL_VIDEOS.get(skill, "#")
        other_markdown += f"* [{skill.title()}]({url})\n"

    return video_html, other_markdown

def chat_local(message, history):
    system_content = f"""You are a diagnostic assistant for a maths learning app.
Your ONLY job is to identify what the student does not understand in maths.
How to behave:
1. Ask the student what topic or concept they are struggling with or don't understand.
2. Ask short clarifying questions if needed to pinpoint the exact difficulty.
3. Once you have enough information about one topic, you MUST respond EXACTLY in this format:
   "Got it! I'm going to select some exercises to help you with that. [SKILL:skill_name_here]"
   NEVER say "Got it!" without the [SKILL:] tag. The [SKILL:] tag is mandatory.
4. After saying "Got it!", ask the student if they have any other topic they struggle with and keep the conversation going.
5. Never explain, never teach, never solve anything.
Map what the student says to the most relevant skill from this list: {all_skills}
NEVER mention the skills list to the student, only include it in the [SKILL:] tag.
Rules:
- Maximum 2 sentences per response.
- Always end with a question.
- If the student asks you to explain something, say: "I'm not a tutor! Tell me what you don't understand and I'll find the right exercises for you." """
    
    messages = [{"role": "system", "content": system_content}]
    for m in history:
        messages.append({"role": m["role"], "content": m["content"]})
    
    try:
        response = client.chat.completions.create(
            model=model_id,
            messages=messages,
            max_tokens=300,
        )
        return response.choices[0].message.content
    except Exception as e:
        print(f"API Error: {e}")
        return None

def respond(message, chat_history, user_id):
    global session_skills
    chat_history.append({"role": "user", "content": message})
    bot_message = chat_local(message, chat_history)
    chat_history.append({"role": "assistant", "content": bot_message or "Sorry, I couldn't generate a response."})
    
    if bot_message and "Got it!" in bot_message:
        match = re.search(r'\[SKILL:(.*?)\]', bot_message)
        if match:
            skill = match.group(1).strip()
            if skill not in session_skills:
                session_skills.append(skill)
            backend.add_skill(user_id, skill)
            chat_history[-1]["content"] = re.sub(r'\[SKILL:(.*?)\]', f"We'll be working on: {skill}", bot_message).strip()
        
    return "", chat_history

def handle_login(email, password):
    user_id = backend.login(email, password)
    if user_id:
        return gr.update(visible=False), gr.update(visible=True), "", user_id
    return gr.update(visible=True), gr.update(visible=False), "## <span style='color:red'>Invalid email or password</span>", None

def reset_skills():
    global session_skills
    session_skills = []

with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
    with gr.Column(visible=True) as auth_block:
        gr.Markdown("# Identification")
        with gr.Tab("Sign In"):
            u = gr.Textbox(label="Email")
            p = gr.Textbox(label="Password", type="password")
            btn_l = gr.Button("Sign In", variant="primary")
            out_l = gr.Markdown()
        with gr.Tab("Create account"):
            nu = gr.Textbox(label="Username")
            nemail = gr.Textbox(label="Email")
            np = gr.Textbox(label="Password", type="password")
            btn_r = gr.Button("Sign Up")
            out_r = gr.Textbox(label="Status")
            btn_r.click(backend.create_user, [nemail, np, nu], out_r)

    with gr.Column(visible=False) as main_block:
        gr.Markdown("# AI LEARNING ASSISTANT")
        user_id_state = gr.State(None)
        with gr.Row():
            with gr.Column(scale=1):
                chatbot = gr.Chatbot(height=450)
                msg = gr.Textbox(placeholder="Tell me what you don't understand...", label="Tutor Chat")
                with gr.Row():
                    send_btn = gr.Button("Send", variant="primary")
                    clear_btn = gr.Button("Clear")
                logout_btn = gr.Button("Logout", variant="secondary")
            with gr.Column(scale=1):
                video_output = gr.HTML(value="<p style='text-align:center;'>Video will appear here.</p>")
                prediction_output = gr.Markdown()
                change_video = gr.Button("Get Video Lesson & Recommendation", variant="primary")

    btn_l.click(
        fn=handle_login,
        inputs=[u, p],
        outputs=[auth_block, main_block, out_l, user_id_state]
    )

    send_btn.click(respond, [msg, chatbot, user_id_state], [msg, chatbot])
    msg.submit(respond, [msg, chatbot, user_id_state], [msg, chatbot])
    clear_btn.click(lambda: ([], reset_skills()), None, chatbot)
    change_video.click(fn=display_video_and_predict, outputs=[video_output, prediction_output])
    
    logout_btn.click(lambda: (gr.update(visible=True), gr.update(visible=False), "", None), None, [auth_block, main_block, out_l, user_id_state])

demo.launch()