chauwy/Pre-Algebra.Helper
2
1import gradio as gr2import sympy as sp3from sympy.parsing.sympy_parser import parse_expr4import re5 6# ---------------- STATE ----------------7state = {8 "equation": None,9 "step": None10}11 12# ---------------- HELPERS ----------------13def fix_implicit_multiplication(text):14 return re.sub(r'(\d)(x)', r'\1*\2', text.lower())15 16def is_greeting(msg):17 return msg in ["hi", "hello", "hey"]18 19def is_equation(msg):20 return "=" in msg and "x" in msg21 22def mentions_multiplication(msg):23 return any(word in msg for word in [24 "multiply", "multiplied", "multiplication", "times"25 ])26 27def mentions_division(msg):28 return any(word in msg for word in [29 "divide", "divided", "division"30 ])31 32# ---------------- EQUATION TUTOR ----------------33def equation_tutor(message):34 message = fix_implicit_multiplication(message)35 36 # START tutoring37 if state["equation"] is None:38 try:39 lhs, rhs = message.split("=")40 state["equation"] = (lhs, rhs)41 state["step"] = "identify"42 return (43 "Let’s solve this step by step 🙂\n\n"44 "What is being done to **x**? (multiplied, added, subtracted)"45 )46 except:47 return "Please enter a valid equation like 2x=6."48 49 # STEP 1: identify operation50 if state["step"] == "identify":51 if mentions_multiplication(message):52 state["step"] = "undo"53 return "Yes 👍 x is multiplied. What operation undoes multiplication?"54 if "add" in message or "subtract" in message:55 return "Good thinking — but look again. Is x multiplied by a number?"56 return "Look closely at the equation. Is x multiplied, added, or subtracted?"57 58 # STEP 2: undo operation59 if state["step"] == "undo":60 if mentions_division(message):61 lhs, rhs = state["equation"]62 eq = sp.Eq(parse_expr(lhs), parse_expr(rhs))63 sol = sp.solve(eq)[0]64 65 state["equation"] = None66 state["step"] = None67 68 return (69 "Correct! 🎉\n\n"70 "Dividing both sides isolates x.\n"71 f"**Final answer:** x = {sol}"72 )73 return "What operation undoes multiplication?"74 75 return "Let’s keep going."76 77# ---------------- MAIN CHAT ----------------78def tutor_chat(message, history):79 msg = message.lower().strip()80 81 # Greeting82 if is_greeting(msg):83 history.append((message, "Hi! I’m your pre-algebra tutor 😊 What do you need help with?"))84 return history85 86 # 🔥 IF AN EQUATION IS IN PROGRESS, ALWAYS CONTINUE IT87 if state["equation"] is not None:88 history.append((message, equation_tutor(message)))89 return history90 91 # Start new equation92 if is_equation(msg):93 history.append((message, equation_tutor(message)))94 return history95 96 history.append((message, "You can say hi or give me a pre-algebra equation like 2x=6."))97 return history98 99# ---------------- GRADIO ----------------100with gr.Blocks() as demo:101 gr.Markdown("## 📘 Pre-Algebra Tutor (Step-by-Step)")102 chatbot = gr.Chatbot()103 msg = gr.Textbox(placeholder="Say hi or type an equation…")104 105 msg.submit(tutor_chat, [msg, chatbot], chatbot)106 msg.submit(lambda: "", None, msg)107 108demo.launch()109 