Update quant_solver.py
Browse files- quant_solver.py +121 -176
quant_solver.py
CHANGED
|
@@ -1,185 +1,130 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
-
import
|
| 4 |
-
import re
|
| 5 |
-
from statistics import mean, median
|
| 6 |
-
from typing import Dict, Optional
|
| 7 |
|
| 8 |
try:
|
| 9 |
-
|
| 10 |
except Exception:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
from models import
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
def
|
| 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 |
else:
|
| 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 |
-
return SolverResult(
|
| 79 |
-
domain="quant",
|
| 80 |
-
solved=True,
|
| 81 |
-
topic="ratio",
|
| 82 |
-
answer_value=f"{a * unit:g}",
|
| 83 |
-
internal_answer=f"{a * unit:g}",
|
| 84 |
-
steps=[
|
| 85 |
-
"Add the ratio parts.",
|
| 86 |
-
"Divide the total by the sum of the ratio.",
|
| 87 |
-
"Multiply by the requested ratio component.",
|
| 88 |
-
],
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
def _solve_remainder(text: str) -> Optional[SolverResult]:
|
| 93 |
-
|
| 94 |
-
t = clean_math_text(text).lower()
|
| 95 |
-
|
| 96 |
-
m = re.search(r"remainder.*?(\d+).*?divided by (\d+)", t)
|
| 97 |
-
|
| 98 |
-
if not m:
|
| 99 |
-
return None
|
| 100 |
-
|
| 101 |
-
a = int(m.group(1))
|
| 102 |
-
b = int(m.group(2))
|
| 103 |
-
|
| 104 |
-
r = a % b
|
| 105 |
-
|
| 106 |
-
return SolverResult(
|
| 107 |
-
domain="quant",
|
| 108 |
-
solved=True,
|
| 109 |
-
topic="number_theory",
|
| 110 |
-
answer_value=str(r),
|
| 111 |
-
internal_answer=str(r),
|
| 112 |
-
steps=[
|
| 113 |
-
"Divide the number by the divisor.",
|
| 114 |
-
"The remainder is the leftover after division.",
|
| 115 |
-
],
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
def _solve_percent(text: str) -> Optional[SolverResult]:
|
| 120 |
-
|
| 121 |
-
lower = clean_math_text(text).lower()
|
| 122 |
-
|
| 123 |
-
m = re.search(r"(\d+)% of a number is (\d+)", lower)
|
| 124 |
-
|
| 125 |
-
if m:
|
| 126 |
-
p = float(m.group(1))
|
| 127 |
-
val = float(m.group(2))
|
| 128 |
-
|
| 129 |
-
ans = val / (p / 100)
|
| 130 |
-
|
| 131 |
-
return SolverResult(
|
| 132 |
-
domain="quant",
|
| 133 |
-
solved=True,
|
| 134 |
-
topic="percent",
|
| 135 |
-
answer_value=f"{ans:g}",
|
| 136 |
-
internal_answer=f"{ans:g}",
|
| 137 |
)
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
domain="quant",
|
| 160 |
-
solved=True,
|
| 161 |
-
topic="algebra",
|
| 162 |
-
answer_value=str(ans),
|
| 163 |
-
internal_answer=str(ans),
|
| 164 |
-
)
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
def solve_quant(text: str) -> SolverResult:
|
| 168 |
-
|
| 169 |
-
for fn in (
|
| 170 |
-
_solve_successive_percent,
|
| 171 |
-
_solve_ratio_total,
|
| 172 |
-
_solve_remainder,
|
| 173 |
-
_solve_percent,
|
| 174 |
-
_solve_linear_equation,
|
| 175 |
-
):
|
| 176 |
-
result = fn(text)
|
| 177 |
-
if result:
|
| 178 |
-
return result
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
+
from typing import List, Optional
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
try:
|
| 6 |
+
from transformers import pipeline
|
| 7 |
except Exception:
|
| 8 |
+
pipeline = None
|
| 9 |
+
|
| 10 |
+
from models import RetrievedChunk
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class GeneratorEngine:
|
| 14 |
+
def __init__(self, model_name: str = "google/flan-t5-small"):
|
| 15 |
+
self.model_name = model_name
|
| 16 |
+
self.pipe = None
|
| 17 |
+
|
| 18 |
+
if pipeline is not None:
|
| 19 |
+
try:
|
| 20 |
+
self.pipe = pipeline("text2text-generation", model=model_name)
|
| 21 |
+
except Exception:
|
| 22 |
+
self.pipe = None
|
| 23 |
+
|
| 24 |
+
def available(self) -> bool:
|
| 25 |
+
return self.pipe is not None
|
| 26 |
+
|
| 27 |
+
def _notes_block(self, retrieval_context: List[RetrievedChunk]) -> str:
|
| 28 |
+
if not retrieval_context:
|
| 29 |
+
return ""
|
| 30 |
+
lines = []
|
| 31 |
+
for chunk in retrieval_context[:3]:
|
| 32 |
+
text = (chunk.text or "").strip().replace("\n", " ")
|
| 33 |
+
if len(text) > 220:
|
| 34 |
+
text = text[:217].rstrip() + "…"
|
| 35 |
+
lines.append(f"- {chunk.topic}: {text}")
|
| 36 |
+
return "\n".join(lines)
|
| 37 |
+
|
| 38 |
+
def _template_fallback(
|
| 39 |
+
self,
|
| 40 |
+
user_text: str,
|
| 41 |
+
question_text: Optional[str],
|
| 42 |
+
topic: str,
|
| 43 |
+
intent: str,
|
| 44 |
+
retrieval_context: Optional[List[RetrievedChunk]] = None,
|
| 45 |
+
) -> str:
|
| 46 |
+
question = (question_text or user_text or "").strip()
|
| 47 |
+
notes = self._notes_block(retrieval_context or [])
|
| 48 |
+
|
| 49 |
+
if intent == "hint":
|
| 50 |
+
base = "Start by identifying the exact relationship between the quantities before doing any arithmetic."
|
| 51 |
+
elif intent in {"instruction", "method"}:
|
| 52 |
+
base = "Translate the wording into an equation, ratio, or percent relationship, then solve one step at a time."
|
| 53 |
+
elif intent in {"walkthrough", "step_by_step", "explain", "concept"}:
|
| 54 |
+
base = "First identify what the question is asking, then map the values into the correct quantitative structure, and only then compute."
|
| 55 |
else:
|
| 56 |
+
base = "This does not match a strong solver rule yet, so begin by identifying the target quantity and the relationship connecting the numbers."
|
| 57 |
+
|
| 58 |
+
if notes:
|
| 59 |
+
return f"{base}\n\nRelevant notes:\n{notes}"
|
| 60 |
+
return base
|
| 61 |
+
|
| 62 |
+
def _build_prompt(
|
| 63 |
+
self,
|
| 64 |
+
user_text: str,
|
| 65 |
+
question_text: Optional[str],
|
| 66 |
+
topic: str,
|
| 67 |
+
intent: str,
|
| 68 |
+
retrieval_context: Optional[List[RetrievedChunk]] = None,
|
| 69 |
+
) -> str:
|
| 70 |
+
question = (question_text or user_text or "").strip()
|
| 71 |
+
notes = self._notes_block(retrieval_context or [])
|
| 72 |
+
|
| 73 |
+
prompt = [
|
| 74 |
+
"You are a concise GMAT tutor.",
|
| 75 |
+
f"Topic: {topic or 'general'}",
|
| 76 |
+
f"Intent: {intent or 'answer'}",
|
| 77 |
+
"",
|
| 78 |
+
f"Question: {question}",
|
| 79 |
+
]
|
| 80 |
+
|
| 81 |
+
if notes:
|
| 82 |
+
prompt.extend(["", "Relevant teaching notes:", notes])
|
| 83 |
+
|
| 84 |
+
prompt.extend(
|
| 85 |
+
[
|
| 86 |
+
"",
|
| 87 |
+
"Respond briefly and clearly.",
|
| 88 |
+
"If the problem is not fully solvable from the parse, give the next best method step.",
|
| 89 |
+
"Do not invent facts.",
|
| 90 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
)
|
| 92 |
|
| 93 |
+
return "\n".join(prompt)
|
| 94 |
+
|
| 95 |
+
def generate(
|
| 96 |
+
self,
|
| 97 |
+
user_text: str,
|
| 98 |
+
question_text: Optional[str] = None,
|
| 99 |
+
topic: str = "",
|
| 100 |
+
intent: str = "answer",
|
| 101 |
+
retrieval_context: Optional[List[RetrievedChunk]] = None,
|
| 102 |
+
chat_history=None,
|
| 103 |
+
max_new_tokens: int = 96,
|
| 104 |
+
**kwargs,
|
| 105 |
+
) -> Optional[str]:
|
| 106 |
+
prompt = self._build_prompt(
|
| 107 |
+
user_text=user_text,
|
| 108 |
+
question_text=question_text,
|
| 109 |
+
topic=topic,
|
| 110 |
+
intent=intent,
|
| 111 |
+
retrieval_context=retrieval_context or [],
|
| 112 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
if self.pipe is not None:
|
| 115 |
+
try:
|
| 116 |
+
out = self.pipe(prompt, max_new_tokens=max_new_tokens, do_sample=False)
|
| 117 |
+
if out and isinstance(out, list):
|
| 118 |
+
text = str(out[0].get("generated_text", "")).strip()
|
| 119 |
+
if text:
|
| 120 |
+
return text
|
| 121 |
+
except Exception:
|
| 122 |
+
pass
|
| 123 |
+
|
| 124 |
+
return self._template_fallback(
|
| 125 |
+
user_text=user_text,
|
| 126 |
+
question_text=question_text,
|
| 127 |
+
topic=topic,
|
| 128 |
+
intent=intent,
|
| 129 |
+
retrieval_context=retrieval_context or [],
|
| 130 |
+
)
|