Mapping skills
Browse files
app.py
CHANGED
|
@@ -5,6 +5,8 @@ import random
|
|
| 5 |
import json
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
from groq import Groq
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
css = """
|
|
@@ -17,20 +19,12 @@ body, .gradio-container {
|
|
| 17 |
|
| 18 |
groq_key = os.getenv("GROQ_KEY")
|
| 19 |
|
|
|
|
| 20 |
backend.init_db()
|
| 21 |
client = Groq(api_key=groq_key)
|
| 22 |
model_id = "llama-3.3-70b-versatile"
|
| 23 |
|
| 24 |
-
|
| 25 |
-
"Critical Thinking",
|
| 26 |
-
"Python Programming",
|
| 27 |
-
"Problem Solving",
|
| 28 |
-
"Public Speaking",
|
| 29 |
-
"Time Management",
|
| 30 |
-
"Data Analysis",
|
| 31 |
-
"Creative Writing",
|
| 32 |
-
"Team Collaboration"
|
| 33 |
-
]
|
| 34 |
|
| 35 |
video_links = [
|
| 36 |
"https://www.youtube.com/watch?v=WUvTyaaNkzM&list=PLZHQObOWTQDMsr9K-rj53DwVRMYO3t5Yr",
|
|
@@ -41,6 +35,10 @@ video_links = [
|
|
| 41 |
"https://www.youtube.com/watch?v=MHeirBPOI6w&list=PL0o_zxa4K1BUeF2o-MlNpbRiS-oE2Kn6J",
|
| 42 |
]
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def get_youtube_id(url):
|
| 45 |
pattern = r'(?:https?://)?(?:www\.)?(?:youtube\.com/(?:watch\?v=|embed/)|youtu\.be/)([a-zA-Z0-9_-]{11})'
|
| 46 |
match = re.search(pattern, url)
|
|
@@ -53,16 +51,26 @@ def display_video(url):
|
|
| 53 |
return f'<div style="display: flex; justify-content: center;"><iframe width="100%" height="450" src="https://www.youtube.com/embed/{video_id}?autoplay=1" frameborder="0" allowfullscreen></iframe></div>'
|
| 54 |
|
| 55 |
def chat_local(message, history):
|
| 56 |
-
system_content = f"""You are
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
messages = [{"role": "system", "content": system_content}]
|
| 68 |
for m in history:
|
|
@@ -80,9 +88,11 @@ def chat_local(message, history):
|
|
| 80 |
return None
|
| 81 |
|
| 82 |
def detect_skills_in_background(history):
|
| 83 |
-
if not history: return
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
system_content = f"Analyze the conversation and identify which skills from this list are being demonstrated: {SKILLS_LIBRARY}. Return only a comma-separated list of skills found."
|
| 86 |
messages = [{"role": "system", "content": system_content}]
|
| 87 |
messages += history[-4:]
|
| 88 |
|
|
@@ -90,20 +100,27 @@ def detect_skills_in_background(history):
|
|
| 90 |
response = client.chat.completions.create(
|
| 91 |
model="llama-3.1-8b-instant",
|
| 92 |
messages=messages,
|
| 93 |
-
max_tokens=
|
| 94 |
)
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
| 98 |
|
| 99 |
def respond(message, chat_history):
|
|
|
|
| 100 |
chat_history.append({"role": "user", "content": message})
|
| 101 |
bot_message = chat_local(message, chat_history)
|
| 102 |
chat_history.append({"role": "assistant", "content": bot_message or "Sorry, I couldn't generate a response."})
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
return "", chat_history
|
| 109 |
|
|
@@ -131,6 +148,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 131 |
|
| 132 |
with gr.Column(visible=False) as main_block:
|
| 133 |
gr.Markdown("# AI LEARNING ASSISTANT")
|
|
|
|
| 134 |
with gr.Row():
|
| 135 |
with gr.Column(scale=1):
|
| 136 |
chatbot = gr.Chatbot(height=450)
|
|
@@ -148,10 +166,9 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 148 |
inputs=[u, p],
|
| 149 |
outputs=[auth_block, main_block, out_l]
|
| 150 |
)
|
| 151 |
-
|
| 152 |
send_btn.click(respond, [msg, chatbot], [msg, chatbot])
|
| 153 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 154 |
-
clear_btn.click(lambda: [], None, chatbot)
|
| 155 |
change_video.click(fn=lambda: display_video(random.choice(video_links)), outputs=video_output)
|
| 156 |
logout_btn.click(lambda: (gr.update(visible=True), gr.update(visible=False), ""), None, [auth_block, main_block, out_l])
|
| 157 |
|
|
|
|
| 5 |
import json
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
from groq import Groq
|
| 8 |
+
from utilit import skills
|
| 9 |
+
|
| 10 |
|
| 11 |
|
| 12 |
css = """
|
|
|
|
| 19 |
|
| 20 |
groq_key = os.getenv("GROQ_KEY")
|
| 21 |
|
| 22 |
+
|
| 23 |
backend.init_db()
|
| 24 |
client = Groq(api_key=groq_key)
|
| 25 |
model_id = "llama-3.3-70b-versatile"
|
| 26 |
|
| 27 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
video_links = [
|
| 30 |
"https://www.youtube.com/watch?v=WUvTyaaNkzM&list=PLZHQObOWTQDMsr9K-rj53DwVRMYO3t5Yr",
|
|
|
|
| 35 |
"https://www.youtube.com/watch?v=MHeirBPOI6w&list=PL0o_zxa4K1BUeF2o-MlNpbRiS-oE2Kn6J",
|
| 36 |
]
|
| 37 |
|
| 38 |
+
|
| 39 |
+
session_skills = []
|
| 40 |
+
|
| 41 |
+
|
| 42 |
def get_youtube_id(url):
|
| 43 |
pattern = r'(?:https?://)?(?:www\.)?(?:youtube\.com/(?:watch\?v=|embed/)|youtu\.be/)([a-zA-Z0-9_-]{11})'
|
| 44 |
match = re.search(pattern, url)
|
|
|
|
| 51 |
return f'<div style="display: flex; justify-content: center;"><iframe width="100%" height="450" src="https://www.youtube.com/embed/{video_id}?autoplay=1" frameborder="0" allowfullscreen></iframe></div>'
|
| 52 |
|
| 53 |
def chat_local(message, history):
|
| 54 |
+
system_content = f"""You are a diagnostic assistant for a maths learning app.
|
| 55 |
+
|
| 56 |
+
Your ONLY job is to identify what the student does not understand in maths.
|
| 57 |
+
|
| 58 |
+
How to behave:
|
| 59 |
+
1. Ask the student what topic or concept they are struggling with or don't understand.
|
| 60 |
+
2. Ask short clarifying questions if needed to pinpoint the exact difficulty.
|
| 61 |
+
3. Once you have enough information about one topic, you MUST respond EXACTLY in this format:
|
| 62 |
+
"Got it! I'm going to select some exercises to help you with that. [SKILL:skill_name_here]"
|
| 63 |
+
NEVER say "Got it!" without the [SKILL:] tag. The [SKILL:] tag is mandatory.
|
| 64 |
+
4. After saying "Got it!", ask the student if they have any other topic they struggle with and keep the conversation going.
|
| 65 |
+
5. Never explain, never teach, never solve anything.
|
| 66 |
+
|
| 67 |
+
Map what the student says to the most relevant skill from this list: {skills}
|
| 68 |
+
NEVER mention the skills list to the student, only include it in the [SKILL:] tag.
|
| 69 |
+
|
| 70 |
+
Rules:
|
| 71 |
+
- Maximum 2 sentences per response.
|
| 72 |
+
- Always end with a question.
|
| 73 |
+
- 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." """
|
| 74 |
|
| 75 |
messages = [{"role": "system", "content": system_content}]
|
| 76 |
for m in history:
|
|
|
|
| 88 |
return None
|
| 89 |
|
| 90 |
def detect_skills_in_background(history):
|
| 91 |
+
if not history: return
|
| 92 |
+
|
| 93 |
+
system_content = f"""Analyze the conversation and identify ONE skill from this list that the student needs to work on: {skills}.
|
| 94 |
+
Return ONLY the skill name, nothing else."""
|
| 95 |
|
|
|
|
| 96 |
messages = [{"role": "system", "content": system_content}]
|
| 97 |
messages += history[-4:]
|
| 98 |
|
|
|
|
| 100 |
response = client.chat.completions.create(
|
| 101 |
model="llama-3.1-8b-instant",
|
| 102 |
messages=messages,
|
| 103 |
+
max_tokens=20,
|
| 104 |
)
|
| 105 |
+
skill = response.choices[0].message.content.strip()
|
| 106 |
+
session_skills.append(skill)
|
| 107 |
+
print(f"Session skills: {session_skills}")
|
| 108 |
+
except Exception as e:
|
| 109 |
+
print(f"Error: {e}")
|
| 110 |
|
| 111 |
def respond(message, chat_history):
|
| 112 |
+
global session_skills
|
| 113 |
chat_history.append({"role": "user", "content": message})
|
| 114 |
bot_message = chat_local(message, chat_history)
|
| 115 |
chat_history.append({"role": "assistant", "content": bot_message or "Sorry, I couldn't generate a response."})
|
| 116 |
|
| 117 |
+
if bot_message and "Got it!" in bot_message:
|
| 118 |
+
match = re.search(r'\[SKILL:(.*?)\]', bot_message)
|
| 119 |
+
if match:
|
| 120 |
+
skill = match.group(1).strip()
|
| 121 |
+
session_skills.append(skill)
|
| 122 |
+
print(f"Session skills: {session_skills}")
|
| 123 |
+
chat_history[-1]["content"] = re.sub(r'\[SKILL:(.*?)\]', r"We'll be working on: \1", bot_message).strip()
|
| 124 |
|
| 125 |
return "", chat_history
|
| 126 |
|
|
|
|
| 148 |
|
| 149 |
with gr.Column(visible=False) as main_block:
|
| 150 |
gr.Markdown("# AI LEARNING ASSISTANT")
|
| 151 |
+
skills_state = gr.State([])
|
| 152 |
with gr.Row():
|
| 153 |
with gr.Column(scale=1):
|
| 154 |
chatbot = gr.Chatbot(height=450)
|
|
|
|
| 166 |
inputs=[u, p],
|
| 167 |
outputs=[auth_block, main_block, out_l]
|
| 168 |
)
|
|
|
|
| 169 |
send_btn.click(respond, [msg, chatbot], [msg, chatbot])
|
| 170 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 171 |
+
clear_btn.click(lambda: ([], []), None, [chatbot, skills_state])
|
| 172 |
change_video.click(fn=lambda: display_video(random.choice(video_links)), outputs=video_output)
|
| 173 |
logout_btn.click(lambda: (gr.update(visible=True), gr.update(visible=False), ""), None, [auth_block, main_block, out_l])
|
| 174 |
|
utilit.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
skills_2 = {
|
| 2 |
+
"Arithmetic Operations": {
|
| 3 |
+
1: ["whole numbers", "ordering", "number line"],
|
| 4 |
+
2: ["integers"],
|
| 5 |
+
3: ["decimals"],
|
| 6 |
+
4: ["order of operations"]
|
| 7 |
+
},
|
| 8 |
+
"Fractions Decimals Percents": {
|
| 9 |
+
1: ["equivalent fractions", "conversion"],
|
| 10 |
+
2: ["fraction", "decimal"],
|
| 11 |
+
3: ["mixed fractions"],
|
| 12 |
+
4: ["percent", "ratio", "rate"]
|
| 13 |
+
},
|
| 14 |
+
"Algebra Basics": {
|
| 15 |
+
1: ["expression", "terms"],
|
| 16 |
+
2: ["like terms", "distributive"],
|
| 17 |
+
3: ["simplifying"],
|
| 18 |
+
4: ["polynomial"]
|
| 19 |
+
},
|
| 20 |
+
"Equations and Inequalities": {
|
| 21 |
+
1: ["one step", "two step"],
|
| 22 |
+
2: ["solving"],
|
| 23 |
+
3: ["multi", "more than two"],
|
| 24 |
+
4: ["inequality"]
|
| 25 |
+
},
|
| 26 |
+
"Linear Functions": {
|
| 27 |
+
1: ["pattern", "table"],
|
| 28 |
+
2: ["slope"],
|
| 29 |
+
3: ["graph"],
|
| 30 |
+
4: ["write linear equation", "from situation"]
|
| 31 |
+
},
|
| 32 |
+
"Systems of Equations": {
|
| 33 |
+
3: ["graphing"],
|
| 34 |
+
4: ["substitution", "system"]
|
| 35 |
+
},
|
| 36 |
+
"Geometry": {
|
| 37 |
+
1: ["angles", "triangle", "polygon"],
|
| 38 |
+
2: ["perimeter", "area"],
|
| 39 |
+
3: ["volume"],
|
| 40 |
+
4: ["surface area", "3d"]
|
| 41 |
+
},
|
| 42 |
+
"Statistics": {
|
| 43 |
+
1: ["mean", "median", "mode"],
|
| 44 |
+
2: ["range"],
|
| 45 |
+
3: ["plot", "histogram"],
|
| 46 |
+
4: ["scatter", "sampling"]
|
| 47 |
+
},
|
| 48 |
+
"Probability": {
|
| 49 |
+
1: ["single"],
|
| 50 |
+
2: ["venn"],
|
| 51 |
+
3: ["two"],
|
| 52 |
+
4: ["combinatorics", "counting"]
|
| 53 |
+
},
|
| 54 |
+
"Advanced Algebra": {
|
| 55 |
+
2: ["factoring"],
|
| 56 |
+
3: ["quadratic"],
|
| 57 |
+
4: ["quadratic formula"]
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
skills = [
|
| 62 |
+
"Arithmetic Operations",
|
| 63 |
+
"Fractions Decimals Percents",
|
| 64 |
+
"Number Theory",
|
| 65 |
+
"Algebra Basics",
|
| 66 |
+
"Equations and Inequalities",
|
| 67 |
+
"Linear Functions",
|
| 68 |
+
"Systems of Equations",
|
| 69 |
+
"Geometry",
|
| 70 |
+
"Statistics",
|
| 71 |
+
"Probability",
|
| 72 |
+
"Transformations",
|
| 73 |
+
"Coordinate Geometry"
|
| 74 |
+
"Advanced Algebra"
|
| 75 |
+
]
|