Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,271 +1,235 @@
|
|
| 1 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
-
import sympy as sp
|
| 4 |
import os
|
| 5 |
import requests
|
|
|
|
| 6 |
from sympy import (
|
| 7 |
-
symbols, diff, integrate, limit, solve,
|
| 8 |
-
|
| 9 |
-
Matrix, Symbol, Function, dsolve, Eq
|
| 10 |
-
series, Sum, Product, Rational, Integer, Float, Abs
|
| 11 |
)
|
| 12 |
from sympy.parsing.sympy_parser import (
|
| 13 |
parse_expr,
|
| 14 |
standard_transformations,
|
| 15 |
implicit_multiplication_application
|
| 16 |
)
|
| 17 |
-
import re # MUST be last โ sympy
|
| 18 |
|
| 19 |
-
# โโ
|
| 20 |
st.set_page_config(
|
| 21 |
page_title="SAAD AI ACADEMY",
|
| 22 |
page_icon="๐",
|
| 23 |
-
layout="
|
| 24 |
initial_sidebar_state="expanded"
|
| 25 |
)
|
| 26 |
|
| 27 |
-
# โโ
|
| 28 |
st.markdown("""
|
| 29 |
<style>
|
| 30 |
-
@import url('https://fonts.googleapis.com/css2?family=
|
| 31 |
-
|
| 32 |
-
:root {
|
| 33 |
-
--bg: #060b14;
|
| 34 |
-
--surface: #0d1526;
|
| 35 |
-
--card: #111d35;
|
| 36 |
-
--border: #1e3058;
|
| 37 |
-
--accent: #38bdf8;
|
| 38 |
-
--accent2: #818cf8;
|
| 39 |
-
--gold: #f59e0b;
|
| 40 |
-
--green: #34d399;
|
| 41 |
-
--text: #e2e8f0;
|
| 42 |
-
--muted: #64748b;
|
| 43 |
-
}
|
| 44 |
|
|
|
|
| 45 |
html, body, [class*="css"] {
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
}
|
| 50 |
-
|
| 51 |
#MainMenu, footer, header { visibility: hidden; }
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
.
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
padding: 2rem 2.5rem;
|
| 59 |
-
margin-bottom: 1.5rem;
|
| 60 |
-
position: relative;
|
| 61 |
-
overflow: hidden;
|
| 62 |
}
|
| 63 |
-
.
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
border-radius: 50%;
|
| 70 |
}
|
| 71 |
-
.
|
| 72 |
-
font-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
background: linear-gradient(90deg, var(--accent), var(--accent2));
|
| 76 |
-
-webkit-background-clip: text;
|
| 77 |
-
-webkit-text-fill-color: transparent;
|
| 78 |
-
margin: 0 0 0.3rem 0;
|
| 79 |
-
letter-spacing: 2px;
|
| 80 |
}
|
| 81 |
-
.
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
| 86 |
}
|
| 87 |
-
.badge-row { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-top: 1rem; }
|
| 88 |
.badge {
|
| 89 |
-
background:
|
| 90 |
-
border: 1px solid
|
| 91 |
-
color:
|
| 92 |
-
padding:
|
| 93 |
border-radius: 20px;
|
| 94 |
-
font-size: 0.
|
| 95 |
font-family: 'JetBrains Mono', monospace;
|
| 96 |
-
letter-spacing: 0.5px;
|
| 97 |
}
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
border
|
| 103 |
-
padding:
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
overflow-y: auto;
|
| 107 |
-
margin-bottom: 1rem;
|
| 108 |
}
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
margin: 0.8rem 0;
|
| 114 |
}
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
max-width:
|
| 122 |
-
font-size: 0.
|
| 123 |
-
|
| 124 |
-
}
|
| 125 |
-
.msg-ai {
|
| 126 |
-
display: flex;
|
| 127 |
-
justify-content: flex-start;
|
| 128 |
-
margin: 0.8rem 0;
|
| 129 |
-
gap: 0.6rem;
|
| 130 |
-
align-items: flex-start;
|
| 131 |
-
}
|
| 132 |
-
.ai-avatar {
|
| 133 |
-
width: 34px; height: 34px;
|
| 134 |
-
background: linear-gradient(135deg, var(--accent), var(--accent2));
|
| 135 |
-
border-radius: 50%;
|
| 136 |
-
display: flex; align-items: center; justify-content: center;
|
| 137 |
-
font-size: 0.9rem;
|
| 138 |
-
flex-shrink: 0;
|
| 139 |
-
color: #fff;
|
| 140 |
}
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
border
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
| 150 |
}
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
border-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
}
|
| 161 |
-
.
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
}
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
border-radius:
|
| 171 |
-
font-family: 'Rajdhani', sans-serif !important;
|
| 172 |
-
font-weight: 600 !important;
|
| 173 |
-
letter-spacing: 1px !important;
|
| 174 |
-
padding: 0.5rem 1.5rem !important;
|
| 175 |
-
transition: all 0.2s !important;
|
| 176 |
}
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
}
|
| 182 |
|
|
|
|
| 183 |
[data-testid="stSidebar"] {
|
| 184 |
-
background:
|
| 185 |
-
border-right: 1px solid
|
| 186 |
}
|
| 187 |
-
[data-testid="stSidebar"]
|
| 188 |
-
font-
|
| 189 |
-
color: var(--accent);
|
| 190 |
-
letter-spacing: 1px;
|
| 191 |
}
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
border-radius:
|
| 199 |
-
font-size: 0.78rem;
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
}
|
| 204 |
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
|
| 208 |
-
::-webkit-scrollbar-thumb:hover { background: var(--muted); }
|
| 209 |
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
border-color: var(--border) !important;
|
| 213 |
-
color: var(--text) !important;
|
| 214 |
-
border-radius: 10px !important;
|
| 215 |
-
}
|
| 216 |
|
| 217 |
-
/* โโ
|
| 218 |
-
|
| 219 |
-
background: #
|
| 220 |
-
border:
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
}
|
| 225 |
-
[data-testid="stChatMessageContent"] p {
|
| 226 |
-
color: #e2e8f0 !important;
|
| 227 |
-
font-family: 'Nunito', sans-serif !important;
|
| 228 |
-
font-size: 0.93rem !important;
|
| 229 |
-
line-height: 1.8 !important;
|
| 230 |
-
}
|
| 231 |
-
/* Make math blocks stand out */
|
| 232 |
-
.katex-display {
|
| 233 |
-
background: rgba(56,189,248,0.05) !important;
|
| 234 |
-
border-left: 3px solid #38bdf8 !important;
|
| 235 |
-
padding: 0.5rem 1rem !important;
|
| 236 |
-
border-radius: 0 8px 8px 0 !important;
|
| 237 |
-
margin: 0.5rem 0 !important;
|
| 238 |
}
|
| 239 |
-
|
| 240 |
</style>
|
| 241 |
""", unsafe_allow_html=True)
|
| 242 |
|
| 243 |
-
# โโ
|
| 244 |
if "messages" not in st.session_state:
|
| 245 |
st.session_state.messages = []
|
| 246 |
if "last_submitted" not in st.session_state:
|
| 247 |
-
st.session_state.last_submitted = ""
|
| 248 |
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
| 250 |
def run_sympy(problem: str) -> dict:
|
| 251 |
"""
|
| 252 |
-
|
| 253 |
-
|
|
|
|
| 254 |
"""
|
| 255 |
p = problem.lower().strip()
|
| 256 |
x = sp.Symbol('x')
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
|
| 268 |
-
|
|
|
|
| 269 |
if any(k in p for k in ["derivative", "differentiate", "d/dx", "diff"]):
|
| 270 |
raw = p
|
| 271 |
for kw in ["derivative of", "differentiate", "diff of", "d/dx of", "d/dx"]:
|
|
@@ -273,11 +237,11 @@ def run_sympy(problem: str) -> dict:
|
|
| 273 |
raw = p.split(kw, 1)[-1].strip()
|
| 274 |
break
|
| 275 |
raw = re.sub(r"\s*(dx|with\s*respect\s*to\s*x).*$", "", raw).strip()
|
| 276 |
-
expr = parse_expr(clean(raw), transformations=tfms, local_dict=
|
| 277 |
result = sp.diff(expr, x)
|
| 278 |
return {"type": "Derivative", "result": str(result), "latex": sp.latex(result)}
|
| 279 |
|
| 280 |
-
# โโ Integral โโ
|
| 281 |
elif any(k in p for k in ["integral", "integrate", "antiderivative"]):
|
| 282 |
raw = p
|
| 283 |
for kw in ["integral of", "integrate", "antiderivative of"]:
|
|
@@ -285,253 +249,278 @@ def run_sympy(problem: str) -> dict:
|
|
| 285 |
raw = p.split(kw, 1)[-1].strip()
|
| 286 |
break
|
| 287 |
raw = re.sub(r"\s*dx.*$", "", raw).strip()
|
| 288 |
-
expr = parse_expr(clean(raw), transformations=tfms, local_dict=
|
| 289 |
result = sp.integrate(expr, x)
|
| 290 |
return {"type": "Integral", "result": str(result), "latex": sp.latex(result)}
|
| 291 |
|
| 292 |
-
# โโ Limit โโ
|
| 293 |
elif "limit" in p:
|
| 294 |
match = re.search(
|
| 295 |
-
r"limit\s+of\s+([\w\s\(\)\+\-\*/\^\.\,]+?)
|
|
|
|
| 296 |
p
|
| 297 |
)
|
| 298 |
if match:
|
| 299 |
raw_expr = clean(match.group(1))
|
| 300 |
-
|
| 301 |
-
point = sp.oo if
|
| 302 |
-
expr = parse_expr(raw_expr, transformations=tfms, local_dict=
|
| 303 |
result = sp.limit(expr, x, point)
|
| 304 |
return {"type": "Limit", "result": str(result), "latex": sp.latex(result)}
|
| 305 |
|
| 306 |
-
# โโ Solve equation โโ
|
| 307 |
elif any(k in p for k in ["solve", "roots", "find x"]):
|
| 308 |
-
raw = re.sub(r"(solve|find x|roots of|roots|the equation)", "", p)
|
|
|
|
| 309 |
if "=" in raw:
|
| 310 |
lhs_s, rhs_s = raw.split("=", 1)
|
| 311 |
-
lhs_e = parse_expr(clean(lhs_s), transformations=tfms, local_dict=
|
| 312 |
-
rhs_e = parse_expr(clean(rhs_s), transformations=tfms, local_dict=
|
| 313 |
expr = lhs_e - rhs_e
|
| 314 |
else:
|
| 315 |
-
expr = parse_expr(clean(raw), transformations=tfms, local_dict=
|
|
|
|
| 316 |
if x in expr.free_symbols:
|
| 317 |
sol = sp.solve(expr, x)
|
| 318 |
sol_latex = ", ".join([sp.latex(s) for s in sol])
|
| 319 |
-
return {
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
return {"type": "Matrix", "result": "matrix_detected", "latex": ""}
|
| 324 |
|
| 325 |
-
# โโ Modular arithmetic โ delegate to
|
| 326 |
elif "mod" in p or "congruence" in p:
|
| 327 |
return {"type": "NumberTheory", "result": "mod_detected", "latex": ""}
|
| 328 |
|
| 329 |
except Exception:
|
| 330 |
-
pass # fall back
|
| 331 |
|
| 332 |
-
return
|
| 333 |
|
| 334 |
|
| 335 |
-
#
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
api_key = os.environ.get("GROQ_API_KEY", "")
|
| 340 |
if not api_key:
|
| 341 |
return (
|
| 342 |
"โ ๏ธ **GROQ_API_KEY not set.**\n\n"
|
| 343 |
-
"
|
| 344 |
-
"
|
| 345 |
-
"
|
|
|
|
| 346 |
)
|
| 347 |
|
|
|
|
| 348 |
sympy_context = ""
|
| 349 |
if (sympy_info.get("result")
|
| 350 |
and sympy_info["result"] not in (None, "matrix_detected", "mod_detected")):
|
| 351 |
sympy_context = (
|
| 352 |
-
|
| 353 |
-
f"
|
| 354 |
-
f"
|
| 355 |
-
f"
|
| 356 |
)
|
| 357 |
|
| 358 |
system_prompt = (
|
| 359 |
-
"You are SAAD AI,
|
| 360 |
-
"
|
| 361 |
-
"
|
| 362 |
-
"
|
| 363 |
-
"
|
| 364 |
-
"
|
| 365 |
-
"
|
| 366 |
-
"NEVER
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
"Topics: Calculus, Linear Algebra, Number Theory, ODEs, "
|
| 368 |
"Numerical Methods, Differential Geometry, Hydro Mechanics, "
|
| 369 |
-
"Theory of Numbers, Real Analysis II, General Math.
|
| 370 |
+ sympy_context
|
| 371 |
)
|
| 372 |
|
|
|
|
| 373 |
messages = [{"role": "system", "content": system_prompt}]
|
| 374 |
-
for msg in history[-
|
| 375 |
messages.append({"role": msg["role"], "content": msg["content"]})
|
| 376 |
messages.append({"role": "user", "content": problem})
|
| 377 |
|
| 378 |
-
headers = {
|
| 379 |
-
"Authorization": f"Bearer {api_key}",
|
| 380 |
-
"Content-Type": "application/json"
|
| 381 |
-
}
|
| 382 |
-
payload = {
|
| 383 |
-
"model": "llama-3.3-70b-versatile",
|
| 384 |
-
"messages": messages,
|
| 385 |
-
"max_tokens": 2048,
|
| 386 |
-
"temperature": 0.2
|
| 387 |
-
}
|
| 388 |
-
|
| 389 |
try:
|
| 390 |
resp = requests.post(
|
| 391 |
"https://api.groq.com/openai/v1/chat/completions",
|
| 392 |
-
headers=
|
| 393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
timeout=60
|
| 395 |
)
|
| 396 |
resp.raise_for_status()
|
| 397 |
return resp.json()["choices"][0]["message"]["content"]
|
|
|
|
| 398 |
except requests.exceptions.Timeout:
|
| 399 |
-
return "โณ Request timed out. Please
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
except Exception as e:
|
| 401 |
-
return f"โ ๏ธ
|
| 402 |
|
| 403 |
|
| 404 |
-
#
|
|
|
|
|
|
|
| 405 |
with st.sidebar:
|
| 406 |
-
st.markdown("###
|
| 407 |
-
st.
|
|
|
|
| 408 |
|
| 409 |
-
st.markdown("**๐ฏ Topics
|
| 410 |
topics = [
|
| 411 |
-
"Calculus", "Linear Algebra", "Number Theory",
|
| 412 |
-
"Differential Equations", "Numerical Methods",
|
| 413 |
-
"Differential Geometry", "Hydro Mechanics",
|
| 414 |
-
"Theory of Numbers", "Real Analysis II", "General Math"
|
| 415 |
]
|
| 416 |
-
|
| 417 |
-
|
| 418 |
|
| 419 |
-
st.
|
| 420 |
-
st.markdown("**โก
|
| 421 |
|
| 422 |
examples = {
|
| 423 |
-
"--
|
| 424 |
-
"
|
| 425 |
"โซ Integral": "Integrate sin(x) * e^x dx",
|
| 426 |
-
"๐ Eigenvalue": "Find eigenvalues of matrix [[3,1],[1,3]]",
|
| 427 |
-
"๐ Congruence": "Solve 14x โก 30 (mod 44)",
|
| 428 |
-
"๐ ODE": "Solve dy/dx + 2y = e^x",
|
| 429 |
"๐ Limit": "Find limit of sin(x)/x as x -> 0",
|
| 430 |
-
"
|
| 431 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
}
|
| 433 |
|
| 434 |
-
|
| 435 |
-
"Load
|
| 436 |
list(examples.keys()),
|
| 437 |
-
key="example_select"
|
|
|
|
| 438 |
)
|
| 439 |
|
| 440 |
-
st.
|
| 441 |
st.markdown("**๐ง Engine**")
|
| 442 |
-
st.
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
๐งฎ <b style="color:#34d399">SymPy</b> โ verified computation<br>
|
| 446 |
-
โ
Zero arithmetic errors
|
| 447 |
-
</div>
|
| 448 |
-
""", unsafe_allow_html=True)
|
| 449 |
|
| 450 |
-
st.
|
| 451 |
-
if st.button("๐๏ธ Clear Chat"):
|
| 452 |
st.session_state.messages = []
|
| 453 |
st.session_state.last_submitted = ""
|
| 454 |
st.rerun()
|
| 455 |
|
| 456 |
|
| 457 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
st.markdown("""
|
| 459 |
-
<div class="
|
| 460 |
-
<
|
| 461 |
-
<
|
| 462 |
<div class="badge-row">
|
| 463 |
-
<span class="badge">
|
| 464 |
<span class="badge">SymPy Verified</span>
|
| 465 |
<span class="badge">Step-by-Step</span>
|
| 466 |
-
<span class="badge">
|
| 467 |
-
<span class="badge">LaTeX Output</span>
|
| 468 |
</div>
|
| 469 |
</div>
|
| 470 |
""", unsafe_allow_html=True)
|
| 471 |
|
| 472 |
-
|
| 473 |
-
# โโโ Chat Display (uses st.chat_message for proper LaTeX rendering) โโโโโโโโโโโโโ
|
| 474 |
if not st.session_state.messages:
|
| 475 |
st.markdown("""
|
| 476 |
-
<div style="
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
<div style="font-family:'Rajdhani',sans-serif;font-size:1.2rem;
|
| 482 |
-
color:#475569;letter-spacing:1px;">
|
| 483 |
-
Ask any BSc Mathematics problem
|
| 484 |
-
</div>
|
| 485 |
-
<div style="font-size:0.82rem;color:#334155;margin-top:0.5rem;">
|
| 486 |
Calculus ยท Linear Algebra ยท Number Theory ยท ODEs ยท Real Analysis ยท and more
|
|
|
|
| 487 |
</div>
|
| 488 |
</div>
|
| 489 |
""", unsafe_allow_html=True)
|
| 490 |
-
else:
|
| 491 |
-
for msg in st.session_state.messages:
|
| 492 |
-
if msg["role"] == "user":
|
| 493 |
-
with st.chat_message("user", avatar="๐งโ๐"):
|
| 494 |
-
st.markdown(msg["content"])
|
| 495 |
-
else:
|
| 496 |
-
with st.chat_message("assistant", avatar="๐"):
|
| 497 |
-
st.markdown(msg["content"])
|
| 498 |
-
|
| 499 |
-
# โโโ Input Row โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 500 |
-
col1, col2 = st.columns([5, 1])
|
| 501 |
-
|
| 502 |
-
with col1:
|
| 503 |
-
prefill = examples.get(selected_example, "") if selected_example != "-- Choose --" else ""
|
| 504 |
-
user_input = st.text_input(
|
| 505 |
-
label="problem_input",
|
| 506 |
-
value=prefill,
|
| 507 |
-
placeholder="Type a BSc math problem... e.g. 'Solve dยฒy/dxยฒ + 4y = cos(2x)'",
|
| 508 |
-
label_visibility="collapsed",
|
| 509 |
-
key="user_input_field"
|
| 510 |
-
)
|
| 511 |
|
| 512 |
-
|
| 513 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 514 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 515 |
|
| 516 |
-
#
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
|
|
|
|
|
|
|
|
|
| 520 |
|
| 521 |
-
|
|
|
|
|
|
|
|
|
|
| 522 |
st.session_state.last_submitted = problem
|
| 523 |
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
|
|
|
|
| 535 |
st.session_state.messages.append({"role": "user", "content": problem})
|
| 536 |
st.session_state.messages.append({"role": "assistant", "content": answer})
|
| 537 |
-
st.rerun()
|
|
|
|
| 1 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 2 |
+
# SAAD AI ACADEMY โ BSc Mathematics Solver
|
| 3 |
+
# Engine: Groq (free) + SymPy (verified computation)
|
| 4 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 5 |
+
|
| 6 |
+
# โโ Imports (order matters: stdlib/third-party before sympy, re LAST) โโ
|
| 7 |
import streamlit as st
|
|
|
|
| 8 |
import os
|
| 9 |
import requests
|
| 10 |
+
import sympy as sp
|
| 11 |
from sympy import (
|
| 12 |
+
symbols, diff, integrate, limit, solve,
|
| 13 |
+
sympify, oo, sin, cos, tan, exp, log, sqrt, pi, E,
|
| 14 |
+
Matrix, Symbol, Function, dsolve, Eq
|
|
|
|
| 15 |
)
|
| 16 |
from sympy.parsing.sympy_parser import (
|
| 17 |
parse_expr,
|
| 18 |
standard_transformations,
|
| 19 |
implicit_multiplication_application
|
| 20 |
)
|
| 21 |
+
import re # MUST be last โ 'from sympy import *' would overwrite re otherwise
|
| 22 |
|
| 23 |
+
# โโ Page config โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 24 |
st.set_page_config(
|
| 25 |
page_title="SAAD AI ACADEMY",
|
| 26 |
page_icon="๐",
|
| 27 |
+
layout="centered",
|
| 28 |
initial_sidebar_state="expanded"
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# โโ CSS โ ChatGPT-style, compact font, clean dark theme โโโโโโโโโโโโโ
|
| 32 |
st.markdown("""
|
| 33 |
<style>
|
| 34 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600&family=JetBrains+Mono:wght@400;500&display=swap');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
/* โโ Base โโ */
|
| 37 |
html, body, [class*="css"] {
|
| 38 |
+
font-family: 'Inter', sans-serif !important;
|
| 39 |
+
font-size: 14px !important;
|
| 40 |
+
background-color: #0f0f0f !important;
|
| 41 |
+
color: #ececec !important;
|
| 42 |
+
}
|
| 43 |
+
.block-container {
|
| 44 |
+
max-width: 780px !important;
|
| 45 |
+
padding: 1rem 1.5rem 2rem 1.5rem !important;
|
| 46 |
}
|
|
|
|
| 47 |
#MainMenu, footer, header { visibility: hidden; }
|
| 48 |
+
|
| 49 |
+
/* โโ Header โโ */
|
| 50 |
+
.saad-header {
|
| 51 |
+
text-align: center;
|
| 52 |
+
padding: 1.2rem 0 0.5rem 0;
|
| 53 |
+
margin-bottom: 0.5rem;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
}
|
| 55 |
+
.saad-header h1 {
|
| 56 |
+
font-size: 1.5rem !important;
|
| 57 |
+
font-weight: 600;
|
| 58 |
+
color: #ffffff;
|
| 59 |
+
margin: 0;
|
| 60 |
+
letter-spacing: 0.5px;
|
|
|
|
| 61 |
}
|
| 62 |
+
.saad-header p {
|
| 63 |
+
font-size: 0.78rem;
|
| 64 |
+
color: #666;
|
| 65 |
+
margin: 0.2rem 0 0 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
}
|
| 67 |
+
.badge-row {
|
| 68 |
+
display: flex;
|
| 69 |
+
gap: 0.4rem;
|
| 70 |
+
justify-content: center;
|
| 71 |
+
flex-wrap: wrap;
|
| 72 |
+
margin-top: 0.6rem;
|
| 73 |
}
|
|
|
|
| 74 |
.badge {
|
| 75 |
+
background: #1a1a1a;
|
| 76 |
+
border: 1px solid #2a2a2a;
|
| 77 |
+
color: #888;
|
| 78 |
+
padding: 2px 8px;
|
| 79 |
border-radius: 20px;
|
| 80 |
+
font-size: 0.68rem;
|
| 81 |
font-family: 'JetBrains Mono', monospace;
|
|
|
|
| 82 |
}
|
| 83 |
|
| 84 |
+
/* โโ Chat messages โ override Streamlit defaults โโ */
|
| 85 |
+
[data-testid="stChatMessage"] {
|
| 86 |
+
background: transparent !important;
|
| 87 |
+
border: none !important;
|
| 88 |
+
padding: 0.3rem 0 !important;
|
| 89 |
+
margin: 0 !important;
|
| 90 |
+
gap: 0.6rem !important;
|
|
|
|
|
|
|
| 91 |
}
|
| 92 |
|
| 93 |
+
/* User message */
|
| 94 |
+
[data-testid="stChatMessage"]:has([data-testid="chatAvatarIcon-user"]) {
|
| 95 |
+
flex-direction: row-reverse !important;
|
|
|
|
| 96 |
}
|
| 97 |
+
[data-testid="stChatMessage"]:has([data-testid="chatAvatarIcon-user"])
|
| 98 |
+
[data-testid="stChatMessageContent"] {
|
| 99 |
+
background: #1e3a5f !important;
|
| 100 |
+
border: 1px solid #2a4a7f !important;
|
| 101 |
+
border-radius: 18px 4px 18px 18px !important;
|
| 102 |
+
padding: 0.6rem 0.9rem !important;
|
| 103 |
+
max-width: 80% !important;
|
| 104 |
+
font-size: 0.88rem !important;
|
| 105 |
+
color: #e2e8f0 !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
}
|
| 107 |
+
|
| 108 |
+
/* Assistant message */
|
| 109 |
+
[data-testid="stChatMessage"]:has([data-testid="chatAvatarIcon-assistant"])
|
| 110 |
+
[data-testid="stChatMessageContent"] {
|
| 111 |
+
background: #1a1a1a !important;
|
| 112 |
+
border: 1px solid #2a2a2a !important;
|
| 113 |
+
border-radius: 4px 18px 18px 18px !important;
|
| 114 |
+
padding: 0.7rem 1rem !important;
|
| 115 |
+
font-size: 0.88rem !important;
|
| 116 |
+
line-height: 1.75 !important;
|
| 117 |
+
color: #ececec !important;
|
| 118 |
}
|
| 119 |
|
| 120 |
+
/* Math display blocks */
|
| 121 |
+
.katex-display {
|
| 122 |
+
background: #161616 !important;
|
| 123 |
+
border-left: 3px solid #3b82f6 !important;
|
| 124 |
+
border-radius: 0 6px 6px 0 !important;
|
| 125 |
+
padding: 0.5rem 1rem !important;
|
| 126 |
+
margin: 0.4rem 0 !important;
|
| 127 |
+
overflow-x: auto !important;
|
| 128 |
}
|
| 129 |
+
.katex { font-size: 1em !important; }
|
| 130 |
+
|
| 131 |
+
/* Step labels bold + colored */
|
| 132 |
+
[data-testid="stChatMessageContent"] p {
|
| 133 |
+
margin: 0.2rem 0 !important;
|
| 134 |
+
font-size: 0.88rem !important;
|
| 135 |
+
line-height: 1.75 !important;
|
| 136 |
}
|
| 137 |
|
| 138 |
+
/* โโ Input box โโ */
|
| 139 |
+
[data-testid="stChatInput"] {
|
| 140 |
+
background: #1a1a1a !important;
|
| 141 |
+
border: 1px solid #2a2a2a !important;
|
| 142 |
+
border-radius: 12px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
}
|
| 144 |
+
[data-testid="stChatInput"] textarea {
|
| 145 |
+
font-size: 0.88rem !important;
|
| 146 |
+
color: #ececec !important;
|
| 147 |
+
background: transparent !important;
|
| 148 |
}
|
| 149 |
|
| 150 |
+
/* โโ Sidebar โโ */
|
| 151 |
[data-testid="stSidebar"] {
|
| 152 |
+
background: #0a0a0a !important;
|
| 153 |
+
border-right: 1px solid #1a1a1a !important;
|
| 154 |
}
|
| 155 |
+
[data-testid="stSidebar"] * {
|
| 156 |
+
font-size: 0.82rem !important;
|
|
|
|
|
|
|
| 157 |
}
|
| 158 |
|
| 159 |
+
/* โโ Buttons โโ */
|
| 160 |
+
.stButton > button {
|
| 161 |
+
background: #1a1a1a !important;
|
| 162 |
+
border: 1px solid #2a2a2a !important;
|
| 163 |
+
color: #888 !important;
|
| 164 |
+
border-radius: 8px !important;
|
| 165 |
+
font-size: 0.78rem !important;
|
| 166 |
+
padding: 0.3rem 0.8rem !important;
|
| 167 |
+
width: 100% !important;
|
| 168 |
+
text-align: left !important;
|
| 169 |
+
transition: all 0.15s !important;
|
| 170 |
+
}
|
| 171 |
+
.stButton > button:hover {
|
| 172 |
+
border-color: #3b82f6 !important;
|
| 173 |
+
color: #ececec !important;
|
| 174 |
+
background: #161b2e !important;
|
| 175 |
}
|
| 176 |
|
| 177 |
+
/* โโ Spinner โโ */
|
| 178 |
+
.stSpinner > div { border-top-color: #3b82f6 !important; }
|
|
|
|
|
|
|
| 179 |
|
| 180 |
+
/* โโ Divider โโ */
|
| 181 |
+
hr { border-color: #1a1a1a !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
+
/* โโ Selectbox โโ */
|
| 184 |
+
.stSelectbox > div > div {
|
| 185 |
+
background: #1a1a1a !important;
|
| 186 |
+
border-color: #2a2a2a !important;
|
| 187 |
+
color: #ececec !important;
|
| 188 |
+
border-radius: 8px !important;
|
| 189 |
+
font-size: 0.82rem !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
}
|
|
|
|
| 191 |
</style>
|
| 192 |
""", unsafe_allow_html=True)
|
| 193 |
|
| 194 |
+
# โโ Session state โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 195 |
if "messages" not in st.session_state:
|
| 196 |
st.session_state.messages = []
|
| 197 |
if "last_submitted" not in st.session_state:
|
| 198 |
+
st.session_state.last_submitted = ""
|
| 199 |
|
| 200 |
+
|
| 201 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 202 |
+
# SYMPY ENGINE โ exact symbolic computation, no AI needed for this
|
| 203 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 204 |
def run_sympy(problem: str) -> dict:
|
| 205 |
"""
|
| 206 |
+
Attempt symbolic computation using SymPy.
|
| 207 |
+
Returns verified result dict. Falls back silently on any error.
|
| 208 |
+
All sympy calls use sp.* explicitly to avoid wildcard name collisions.
|
| 209 |
"""
|
| 210 |
p = problem.lower().strip()
|
| 211 |
x = sp.Symbol('x')
|
| 212 |
+
tfms = standard_transformations + (implicit_multiplication_application,)
|
| 213 |
+
|
| 214 |
+
# local_dict maps common names to correct sympy objects
|
| 215 |
+
# Critical: 'e' must map to E (Euler's number), not unknown symbol
|
| 216 |
+
ld = {
|
| 217 |
+
"x": x,
|
| 218 |
+
"e": sp.E, "E": sp.E,
|
| 219 |
+
"pi": sp.pi, "PI": sp.pi,
|
| 220 |
+
"sin": sp.sin, "cos": sp.cos, "tan": sp.tan,
|
| 221 |
+
"exp": sp.exp, "log": sp.log, "ln": sp.log,
|
| 222 |
+
"sqrt": sp.sqrt, "inf": sp.oo, "oo": sp.oo
|
| 223 |
+
}
|
| 224 |
|
| 225 |
+
def clean(s):
|
| 226 |
+
"""Normalize expression: strip spaces, convert ^ to **"""
|
| 227 |
+
s = re.sub(r"\s+", "", s)
|
| 228 |
+
s = re.sub(r"\^", "**", s)
|
| 229 |
+
return s
|
| 230 |
|
| 231 |
+
try:
|
| 232 |
+
# โโ Derivative โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 233 |
if any(k in p for k in ["derivative", "differentiate", "d/dx", "diff"]):
|
| 234 |
raw = p
|
| 235 |
for kw in ["derivative of", "differentiate", "diff of", "d/dx of", "d/dx"]:
|
|
|
|
| 237 |
raw = p.split(kw, 1)[-1].strip()
|
| 238 |
break
|
| 239 |
raw = re.sub(r"\s*(dx|with\s*respect\s*to\s*x).*$", "", raw).strip()
|
| 240 |
+
expr = parse_expr(clean(raw), transformations=tfms, local_dict=ld)
|
| 241 |
result = sp.diff(expr, x)
|
| 242 |
return {"type": "Derivative", "result": str(result), "latex": sp.latex(result)}
|
| 243 |
|
| 244 |
+
# โโ Integral โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 245 |
elif any(k in p for k in ["integral", "integrate", "antiderivative"]):
|
| 246 |
raw = p
|
| 247 |
for kw in ["integral of", "integrate", "antiderivative of"]:
|
|
|
|
| 249 |
raw = p.split(kw, 1)[-1].strip()
|
| 250 |
break
|
| 251 |
raw = re.sub(r"\s*dx.*$", "", raw).strip()
|
| 252 |
+
expr = parse_expr(clean(raw), transformations=tfms, local_dict=ld)
|
| 253 |
result = sp.integrate(expr, x)
|
| 254 |
return {"type": "Integral", "result": str(result), "latex": sp.latex(result)}
|
| 255 |
|
| 256 |
+
# โโ Limit โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 257 |
elif "limit" in p:
|
| 258 |
match = re.search(
|
| 259 |
+
r"limit\s+of\s+([\w\s\(\)\+\-\*/\^\.\,]+?)"
|
| 260 |
+
r"\s+as\s+x\s*(?:->|โ|approaches)\s*([\w\.\+\-]+)",
|
| 261 |
p
|
| 262 |
)
|
| 263 |
if match:
|
| 264 |
raw_expr = clean(match.group(1))
|
| 265 |
+
pt = match.group(2).strip()
|
| 266 |
+
point = sp.oo if pt in ("inf", "infinity", "oo") else sp.sympify(pt)
|
| 267 |
+
expr = parse_expr(raw_expr, transformations=tfms, local_dict=ld)
|
| 268 |
result = sp.limit(expr, x, point)
|
| 269 |
return {"type": "Limit", "result": str(result), "latex": sp.latex(result)}
|
| 270 |
|
| 271 |
+
# โโ Solve equation โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 272 |
elif any(k in p for k in ["solve", "roots", "find x"]):
|
| 273 |
+
raw = re.sub(r"(solve|find x|roots of|roots|the equation)", "", p)
|
| 274 |
+
raw = raw.strip().strip(":").strip()
|
| 275 |
if "=" in raw:
|
| 276 |
lhs_s, rhs_s = raw.split("=", 1)
|
| 277 |
+
lhs_e = parse_expr(clean(lhs_s), transformations=tfms, local_dict=ld)
|
| 278 |
+
rhs_e = parse_expr(clean(rhs_s), transformations=tfms, local_dict=ld)
|
| 279 |
expr = lhs_e - rhs_e
|
| 280 |
else:
|
| 281 |
+
expr = parse_expr(clean(raw), transformations=tfms, local_dict=ld)
|
| 282 |
+
# Guard: only solve if x is actually in the expression
|
| 283 |
if x in expr.free_symbols:
|
| 284 |
sol = sp.solve(expr, x)
|
| 285 |
sol_latex = ", ".join([sp.latex(s) for s in sol])
|
| 286 |
+
return {
|
| 287 |
+
"type": "Equation",
|
| 288 |
+
"result": str(sol),
|
| 289 |
+
"latex": r"x \in \{" + sol_latex + r"\}"
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
# โโ Matrix / Eigenvalues โ too varied, delegate to AI โโโโโโโ
|
| 293 |
+
elif any(k in p for k in ["matrix", "determinant", "eigenvalue",
|
| 294 |
+
"eigenvector", "det("]):
|
| 295 |
return {"type": "Matrix", "result": "matrix_detected", "latex": ""}
|
| 296 |
|
| 297 |
+
# โโ Modular arithmetic โ delegate to AI โโโโโโโโโโโโโโโโโโโโโ
|
| 298 |
elif "mod" in p or "congruence" in p:
|
| 299 |
return {"type": "NumberTheory", "result": "mod_detected", "latex": ""}
|
| 300 |
|
| 301 |
except Exception:
|
| 302 |
+
pass # Silently fall back โ AI handles it without sympy hint
|
| 303 |
|
| 304 |
+
return {"type": "general", "result": None, "latex": ""}
|
| 305 |
|
| 306 |
|
| 307 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 308 |
+
# GROQ API โ free, fast, Llama 3.3 70B
|
| 309 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 310 |
+
def ask_ai(problem: str, sympy_info: dict, history: list) -> str:
|
| 311 |
api_key = os.environ.get("GROQ_API_KEY", "")
|
| 312 |
if not api_key:
|
| 313 |
return (
|
| 314 |
"โ ๏ธ **GROQ_API_KEY not set.**\n\n"
|
| 315 |
+
"**Steps to fix:**\n"
|
| 316 |
+
"1. Go to https://console.groq.com โ sign up free\n"
|
| 317 |
+
"2. API Keys โ Create API Key โ copy it\n"
|
| 318 |
+
"3. HF Space โ Settings โ Secrets โ add `GROQ_API_KEY`"
|
| 319 |
)
|
| 320 |
|
| 321 |
+
# Build sympy context if we have a verified result
|
| 322 |
sympy_context = ""
|
| 323 |
if (sympy_info.get("result")
|
| 324 |
and sympy_info["result"] not in (None, "matrix_detected", "mod_detected")):
|
| 325 |
sympy_context = (
|
| 326 |
+
"\n\nSYMPY VERIFIED RESULT โ your final answer MUST match this exactly:\n"
|
| 327 |
+
f" Operation : {sympy_info.get('type', '')}\n"
|
| 328 |
+
f" Result : {sympy_info.get('result', '')}\n"
|
| 329 |
+
f" LaTeX : {sympy_info.get('latex', '')}\n"
|
| 330 |
)
|
| 331 |
|
| 332 |
system_prompt = (
|
| 333 |
+
"You are SAAD AI, a precise BSc Mathematics tutor.\n\n"
|
| 334 |
+
"STRICT FORMATTING RULES โ follow every rule exactly:\n"
|
| 335 |
+
"1. Start with the method name: e.g. **Method: Integration by Parts**\n"
|
| 336 |
+
"2. Label steps: **Step 1:**, **Step 2:**, etc.\n"
|
| 337 |
+
"3. ALL math must be in LaTeX โ NEVER write math in plain text.\n"
|
| 338 |
+
" - Inline math: $expression$\n"
|
| 339 |
+
" - Display math (new line, centered): $$expression$$\n"
|
| 340 |
+
"4. NEVER write the same expression twice (once in LaTeX, once plain).\n"
|
| 341 |
+
" BAD: 'x = 2x = 2' GOOD: '$x = 2$'\n"
|
| 342 |
+
"5. Use emojis to mark sections:\n"
|
| 343 |
+
" ๐ for identifying/given info\n"
|
| 344 |
+
" ๐งฎ for computation steps\n"
|
| 345 |
+
" โ
for the final answer\n"
|
| 346 |
+
"6. End EVERY solution with:\n"
|
| 347 |
+
" โ
**Final Answer:** $$your_answer$$\n"
|
| 348 |
+
"7. NEVER make arithmetic errors.\n"
|
| 349 |
+
"8. Keep explanations concise โ one sentence per step.\n\n"
|
| 350 |
"Topics: Calculus, Linear Algebra, Number Theory, ODEs, "
|
| 351 |
"Numerical Methods, Differential Geometry, Hydro Mechanics, "
|
| 352 |
+
"Theory of Numbers, Real Analysis II, General Math."
|
| 353 |
+ sympy_context
|
| 354 |
)
|
| 355 |
|
| 356 |
+
# Build messages โ last 6 exchanges for context
|
| 357 |
messages = [{"role": "system", "content": system_prompt}]
|
| 358 |
+
for msg in history[-12:]:
|
| 359 |
messages.append({"role": msg["role"], "content": msg["content"]})
|
| 360 |
messages.append({"role": "user", "content": problem})
|
| 361 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
try:
|
| 363 |
resp = requests.post(
|
| 364 |
"https://api.groq.com/openai/v1/chat/completions",
|
| 365 |
+
headers={
|
| 366 |
+
"Authorization": f"Bearer {api_key}",
|
| 367 |
+
"Content-Type": "application/json"
|
| 368 |
+
},
|
| 369 |
+
json={
|
| 370 |
+
"model": "llama-3.3-70b-versatile",
|
| 371 |
+
"messages": messages,
|
| 372 |
+
"max_tokens": 2048,
|
| 373 |
+
"temperature": 0.15, # low = precise, no hallucinations
|
| 374 |
+
"top_p": 0.9
|
| 375 |
+
},
|
| 376 |
timeout=60
|
| 377 |
)
|
| 378 |
resp.raise_for_status()
|
| 379 |
return resp.json()["choices"][0]["message"]["content"]
|
| 380 |
+
|
| 381 |
except requests.exceptions.Timeout:
|
| 382 |
+
return "โณ Request timed out. Please click **SOLVE** again."
|
| 383 |
+
except requests.exceptions.HTTPError as e:
|
| 384 |
+
code = e.response.status_code if e.response else "?"
|
| 385 |
+
if code == 429:
|
| 386 |
+
return "โณ Rate limit hit. Please wait 10 seconds and try again."
|
| 387 |
+
if code == 401:
|
| 388 |
+
return "โ ๏ธ Invalid GROQ_API_KEY. Please check your key in HF Secrets."
|
| 389 |
+
return f"โ ๏ธ HTTP Error {code}: {str(e)}"
|
| 390 |
except Exception as e:
|
| 391 |
+
return f"โ ๏ธ Unexpected error: {str(e)}"
|
| 392 |
|
| 393 |
|
| 394 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 395 |
+
# SIDEBAR
|
| 396 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 397 |
with st.sidebar:
|
| 398 |
+
st.markdown("### ๐ SAAD AI ACADEMY")
|
| 399 |
+
st.caption("BSc Mathematics Engine")
|
| 400 |
+
st.divider()
|
| 401 |
|
| 402 |
+
st.markdown("**๐ฏ Topics**")
|
| 403 |
topics = [
|
| 404 |
+
"๐ Calculus", "๐ข Linear Algebra", "๐ Number Theory",
|
| 405 |
+
"๐ Differential Equations", "๐งฎ Numerical Methods",
|
| 406 |
+
"๐ Differential Geometry", "๐ Hydro Mechanics",
|
| 407 |
+
"๐ Theory of Numbers", "๐ Real Analysis II", "โ General Math"
|
| 408 |
]
|
| 409 |
+
for t in topics:
|
| 410 |
+
st.caption(t)
|
| 411 |
|
| 412 |
+
st.divider()
|
| 413 |
+
st.markdown("**โก Example Problems**")
|
| 414 |
|
| 415 |
examples = {
|
| 416 |
+
"-- Select --": "",
|
| 417 |
+
"๐ Derivative": "Find the derivative of x^3 + 5x^2 - 3x + 7",
|
| 418 |
"โซ Integral": "Integrate sin(x) * e^x dx",
|
|
|
|
|
|
|
|
|
|
| 419 |
"๐ Limit": "Find limit of sin(x)/x as x -> 0",
|
| 420 |
+
"๐ข Eigenvalues": "Find eigenvalues of matrix [[4,1],[2,3]]",
|
| 421 |
+
"๐ Congruence": "Solve 14x โก 30 (mod 44) using Euclidean algorithm",
|
| 422 |
+
"๐ ODE": "Solve dy/dx + 2y = e^(-x) with y(0) = 1",
|
| 423 |
+
"๐งฎ Newton-Raphson": "Apply Newton-Raphson to x^3 - 2x - 5 = 0, x0=2, 3 iterations",
|
| 424 |
+
"๐ Series": "Test convergence of sum 1/n^2 from n=1 to infinity",
|
| 425 |
+
"๐ Bernoulli": "Explain Bernoulli equation in fluid mechanics with example",
|
| 426 |
+
"๐ Fermat": "State and prove Fermat's Little Theorem with example",
|
| 427 |
}
|
| 428 |
|
| 429 |
+
selected = st.selectbox(
|
| 430 |
+
"Load example:",
|
| 431 |
list(examples.keys()),
|
| 432 |
+
key="example_select",
|
| 433 |
+
label_visibility="collapsed"
|
| 434 |
)
|
| 435 |
|
| 436 |
+
st.divider()
|
| 437 |
st.markdown("**๐ง Engine**")
|
| 438 |
+
st.caption("๐ค Groq Llama 3.3 70B โ reasoning")
|
| 439 |
+
st.caption("๐งฎ SymPy โ verified computation")
|
| 440 |
+
st.caption("โ
Zero arithmetic errors")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
|
| 442 |
+
st.divider()
|
| 443 |
+
if st.button("๐๏ธ Clear Chat", use_container_width=True):
|
| 444 |
st.session_state.messages = []
|
| 445 |
st.session_state.last_submitted = ""
|
| 446 |
st.rerun()
|
| 447 |
|
| 448 |
|
| 449 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 450 |
+
# MAIN AREA
|
| 451 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 452 |
+
|
| 453 |
+
# Header
|
| 454 |
st.markdown("""
|
| 455 |
+
<div class="saad-header">
|
| 456 |
+
<h1>๐ SAAD AI ACADEMY</h1>
|
| 457 |
+
<p>BSc Mathematics โ Step-by-Step Solver</p>
|
| 458 |
<div class="badge-row">
|
| 459 |
+
<span class="badge">Groq Free</span>
|
| 460 |
<span class="badge">SymPy Verified</span>
|
| 461 |
<span class="badge">Step-by-Step</span>
|
| 462 |
+
<span class="badge">LaTeX Rendered</span>
|
|
|
|
| 463 |
</div>
|
| 464 |
</div>
|
| 465 |
""", unsafe_allow_html=True)
|
| 466 |
|
| 467 |
+
# Welcome message if no chat
|
|
|
|
| 468 |
if not st.session_state.messages:
|
| 469 |
st.markdown("""
|
| 470 |
+
<div style="text-align:center; padding:2rem 1rem; color:#444;">
|
| 471 |
+
<div style="font-size:2.5rem; margin-bottom:0.5rem;">๐งฎ</div>
|
| 472 |
+
<div style="font-size:0.9rem; color:#555;">
|
| 473 |
+
Ask any BSc Mathematics problem below<br>
|
| 474 |
+
<span style="font-size:0.78rem; color:#333;">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
Calculus ยท Linear Algebra ยท Number Theory ยท ODEs ยท Real Analysis ยท and more
|
| 476 |
+
</span>
|
| 477 |
</div>
|
| 478 |
</div>
|
| 479 |
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 480 |
|
| 481 |
+
# Render chat history
|
| 482 |
+
for msg in st.session_state.messages:
|
| 483 |
+
avatar = "๐งโ๐" if msg["role"] == "user" else "๐"
|
| 484 |
+
with st.chat_message(msg["role"], avatar=avatar):
|
| 485 |
+
st.markdown(msg["content"])
|
| 486 |
+
|
| 487 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 488 |
+
# INPUT โ use st.chat_input (cleaner than text_input + button)
|
| 489 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 490 |
+
|
| 491 |
+
# Pre-fill from example selector
|
| 492 |
+
prefill = examples.get(selected, "") if selected != "-- Select --" else ""
|
| 493 |
|
| 494 |
+
# st.chat_input is the clean ChatGPT-style input bar
|
| 495 |
+
user_input = st.chat_input(
|
| 496 |
+
placeholder="Type a BSc math problem... e.g. 'Solve dยฒy/dxยฒ + 4y = cos(2x)'",
|
| 497 |
+
)
|
| 498 |
|
| 499 |
+
# Also allow clicking an example to submit it directly
|
| 500 |
+
if prefill and prefill != st.session_state.last_submitted:
|
| 501 |
+
problem = prefill
|
| 502 |
+
elif user_input and user_input.strip():
|
| 503 |
+
problem = user_input.strip()
|
| 504 |
+
else:
|
| 505 |
+
problem = ""
|
| 506 |
|
| 507 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 508 |
+
# PROCESS โ only when there's a new problem
|
| 509 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 510 |
+
if problem and problem != st.session_state.last_submitted:
|
| 511 |
st.session_state.last_submitted = problem
|
| 512 |
|
| 513 |
+
# Show user message immediately
|
| 514 |
+
with st.chat_message("user", avatar="๐งโ๐"):
|
| 515 |
+
st.markdown(problem)
|
| 516 |
+
|
| 517 |
+
# Show AI response
|
| 518 |
+
with st.chat_message("assistant", avatar="๐"):
|
| 519 |
+
with st.spinner("๐งฎ Computing..."):
|
| 520 |
+
sympy_result = run_sympy(problem)
|
| 521 |
+
answer = ask_ai(problem, sympy_result, st.session_state.messages)
|
| 522 |
+
st.markdown(answer)
|
| 523 |
|
| 524 |
+
# Save to history
|
| 525 |
st.session_state.messages.append({"role": "user", "content": problem})
|
| 526 |
st.session_state.messages.append({"role": "assistant", "content": answer})
|
|
|