Upload app.py
Browse files
app.py
CHANGED
|
@@ -1807,11 +1807,24 @@ def ask_ai(problem: str, sympy_info: dict, history: list) -> str:
|
|
| 1807 |
)
|
| 1808 |
|
| 1809 |
system_prompt = (
|
| 1810 |
-
"You are Saad.AI, a
|
|
|
|
| 1811 |
"If anyone asks who made you, who built you, who created you, or who invented you, "
|
| 1812 |
"always say: I was built by Saad, a passionate developer who created me from scratch "
|
| 1813 |
"to help BSc Mathematics students. Never mention Groq, Meta, Gemini, or any AI company as your creator.\n\n"
|
| 1814 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1815 |
"โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ\n"
|
| 1816 |
"CORE FORMATTING RULES โ follow every rule without exception\n"
|
| 1817 |
"โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ\n\n"
|
|
@@ -2219,28 +2232,17 @@ with st.sidebar:
|
|
| 2219 |
|
| 2220 |
# Header
|
| 2221 |
st.markdown("""
|
| 2222 |
-
<div style="text-align:center; padding:
|
| 2223 |
<div style="
|
| 2224 |
-
font-size:2.
|
| 2225 |
font-weight:700;
|
| 2226 |
-
|
| 2227 |
-
-
|
| 2228 |
-
-
|
| 2229 |
-
|
| 2230 |
-
|
| 2231 |
-
|
| 2232 |
-
|
| 2233 |
-
font-family: 'Inter', sans-serif;
|
| 2234 |
-
">๐ Saad.AI</div>
|
| 2235 |
-
<div style="font-size:0.8rem; color:#6b7280; letter-spacing:0.3px; margin-bottom:0.7rem;">
|
| 2236 |
-
BSc Mathematics ยท Step-by-Step Solver ยท SymPy Verified
|
| 2237 |
-
</div>
|
| 2238 |
-
<div style="display:flex; gap:0.4rem; justify-content:center; flex-wrap:wrap;">
|
| 2239 |
-
<span style="background:#0f172a; border:1px solid #1e3a5f; color:#60a5fa; padding:3px 10px; border-radius:20px; font-size:0.68rem; font-family:monospace;">โก Groq Free</span>
|
| 2240 |
-
<span style="background:#0f172a; border:1px solid #1e3a5f; color:#60a5fa; padding:3px 10px; border-radius:20px; font-size:0.68rem; font-family:monospace;">๐งฎ SymPy Verified</span>
|
| 2241 |
-
<span style="background:#0f172a; border:1px solid #1e3a5f; color:#60a5fa; padding:3px 10px; border-radius:20px; font-size:0.68rem; font-family:monospace;">๐ Step-by-Step</span>
|
| 2242 |
-
<span style="background:#0f172a; border:1px solid #1e3a5f; color:#60a5fa; padding:3px 10px; border-radius:20px; font-size:0.68rem; font-family:monospace;">โ LaTeX</span>
|
| 2243 |
-
<span style="background:#0f172a; border:1px solid #1e3a5f; color:#60a5fa; padding:3px 10px; border-radius:20px; font-size:0.68rem; font-family:monospace;">๐ Graphs</span>
|
| 2244 |
</div>
|
| 2245 |
</div>
|
| 2246 |
""", unsafe_allow_html=True)
|
|
@@ -2248,43 +2250,45 @@ st.markdown("""
|
|
| 2248 |
# Welcome message if no chat
|
| 2249 |
if not st.session_state.messages:
|
| 2250 |
st.markdown("""
|
| 2251 |
-
<div style="text-align:center; padding:
|
| 2252 |
-
<div style="
|
| 2253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2254 |
</div>
|
| 2255 |
-
<div style="
|
| 2256 |
-
|
| 2257 |
-
<div style="font-size:1.3rem; margin-bottom:0.2rem;">๐</div>Calculus
|
| 2258 |
-
</div>
|
| 2259 |
-
<div style="background:#111827; border:1px solid #1f2937; border-radius:10px; padding:0.7rem 0.5rem; text-align:center; font-size:0.75rem; color:#9ca3af;">
|
| 2260 |
-
<div style="font-size:1.3rem; margin-bottom:0.2rem;">๐ข</div>Linear Algebra
|
| 2261 |
-
</div>
|
| 2262 |
-
<div style="background:#111827; border:1px solid #1f2937; border-radius:10px; padding:0.7rem 0.5rem; text-align:center; font-size:0.75rem; color:#9ca3af;">
|
| 2263 |
-
<div style="font-size:1.3rem; margin-bottom:0.2rem;">๐</div>ODEs
|
| 2264 |
-
</div>
|
| 2265 |
-
<div style="background:#111827; border:1px solid #1f2937; border-radius:10px; padding:0.7rem 0.5rem; text-align:center; font-size:0.75rem; color:#9ca3af;">
|
| 2266 |
-
<div style="font-size:1.3rem; margin-bottom:0.2rem;">๐งฎ</div>Numerical Methods
|
| 2267 |
-
</div>
|
| 2268 |
-
<div style="background:#111827; border:1px solid #1f2937; border-radius:10px; padding:0.7rem 0.5rem; text-align:center; font-size:0.75rem; color:#9ca3af;">
|
| 2269 |
-
<div style="font-size:1.3rem; margin-bottom:0.2rem;">๐</div>Number Theory
|
| 2270 |
-
</div>
|
| 2271 |
-
<div style="background:#111827; border:1px solid #1f2937; border-radius:10px; padding:0.7rem 0.5rem; text-align:center; font-size:0.75rem; color:#9ca3af;">
|
| 2272 |
-
<div style="font-size:1.3rem; margin-bottom:0.2rem;">๐</div>Diff. Geometry
|
| 2273 |
-
</div>
|
| 2274 |
-
<div style="background:#111827; border:1px solid #1f2937; border-radius:10px; padding:0.7rem 0.5rem; text-align:center; font-size:0.75rem; color:#9ca3af;">
|
| 2275 |
-
<div style="font-size:1.3rem; margin-bottom:0.2rem;">๐</div>Hydro Mechanics
|
| 2276 |
-
</div>
|
| 2277 |
-
<div style="background:#111827; border:1px solid #1f2937; border-radius:10px; padding:0.7rem 0.5rem; text-align:center; font-size:0.75rem; color:#9ca3af;">
|
| 2278 |
-
<div style="font-size:1.3rem; margin-bottom:0.2rem;">๐</div>Real Analysis II
|
| 2279 |
-
</div>
|
| 2280 |
-
<div style="background:#111827; border:1px solid #1f2937; border-radius:10px; padding:0.7rem 0.5rem; text-align:center; font-size:0.75rem; color:#9ca3af;">
|
| 2281 |
-
<div style="font-size:1.3rem; margin-bottom:0.2rem;">๐</div>Graph Plotting
|
| 2282 |
-
</div>
|
| 2283 |
</div>
|
| 2284 |
-
<div style="
|
| 2285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2286 |
</div>
|
| 2287 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2288 |
""", unsafe_allow_html=True)
|
| 2289 |
|
| 2290 |
# Render chat history
|
|
@@ -2325,30 +2329,53 @@ else:
|
|
| 2325 |
if problem and problem != st.session_state.last_submitted:
|
| 2326 |
st.session_state.last_submitted = problem
|
| 2327 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2328 |
# Show user message immediately
|
| 2329 |
with st.chat_message("user", avatar="๐งโ๐"):
|
| 2330 |
st.markdown(problem)
|
| 2331 |
|
| 2332 |
-
# Show AI response
|
| 2333 |
with st.chat_message("assistant", avatar="๐"):
|
| 2334 |
-
|
| 2335 |
-
|
| 2336 |
-
|
| 2337 |
-
|
| 2338 |
-
|
| 2339 |
-
|
| 2340 |
-
|
| 2341 |
-
|
| 2342 |
-
|
| 2343 |
-
|
| 2344 |
-
|
| 2345 |
-
|
| 2346 |
-
|
| 2347 |
-
|
| 2348 |
-
|
| 2349 |
-
|
| 2350 |
-
|
| 2351 |
-
|
|
|
|
| 2352 |
|
| 2353 |
# Save to history
|
| 2354 |
st.session_state.messages.append({"role": "user", "content": problem})
|
|
|
|
| 1807 |
)
|
| 1808 |
|
| 1809 |
system_prompt = (
|
| 1810 |
+
"You are Saad.AI, a BSc Mathematics assistant built by Saad. "
|
| 1811 |
+
"You are friendly, helpful, and professional โ like ChatGPT or Claude. "
|
| 1812 |
"If anyone asks who made you, who built you, who created you, or who invented you, "
|
| 1813 |
"always say: I was built by Saad, a passionate developer who created me from scratch "
|
| 1814 |
"to help BSc Mathematics students. Never mention Groq, Meta, Gemini, or any AI company as your creator.\n\n"
|
| 1815 |
|
| 1816 |
+
"=== CONVERSATION MODE ===\n"
|
| 1817 |
+
"You have TWO modes:\n\n"
|
| 1818 |
+
"MODE A โ CASUAL (when sympy type is 'casual' or message is a greeting/small talk):\n"
|
| 1819 |
+
" โ Respond naturally like ChatGPT or Claude โ warm, friendly, conversational.\n"
|
| 1820 |
+
" โ NO math structure. NO steps. NO boxed answers. NO LaTeX.\n"
|
| 1821 |
+
" โ Just reply naturally in 1-3 sentences.\n"
|
| 1822 |
+
" โ Examples: 'Hi!' โ 'Hey! How can I help you today?'\n"
|
| 1823 |
+
" 'How are you?' โ 'Doing great! Ready to tackle some math. What would you like to solve?'\n"
|
| 1824 |
+
" 'What can you do?' โ Briefly explain you solve BSc Math problems step by step.\n\n"
|
| 1825 |
+
"MODE B โ MATH (when sympy type is anything else โ actual math problem):\n"
|
| 1826 |
+
" โ Use full math structure below.\n\n"
|
| 1827 |
+
|
| 1828 |
"โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ\n"
|
| 1829 |
"CORE FORMATTING RULES โ follow every rule without exception\n"
|
| 1830 |
"โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ\n\n"
|
|
|
|
| 2232 |
|
| 2233 |
# Header
|
| 2234 |
st.markdown("""
|
| 2235 |
+
<div style="text-align:center; padding:2rem 0 1rem 0;">
|
| 2236 |
<div style="
|
| 2237 |
+
font-size:2.4rem;
|
| 2238 |
font-weight:700;
|
| 2239 |
+
color:#ffffff;
|
| 2240 |
+
letter-spacing:0.5px;
|
| 2241 |
+
margin-bottom:0.4rem;
|
| 2242 |
+
font-family:'Inter',sans-serif;
|
| 2243 |
+
">Saad.AI</div>
|
| 2244 |
+
<div style="font-size:0.85rem; color:#6b7280;">
|
| 2245 |
+
Your BSc Mathematics Assistant
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2246 |
</div>
|
| 2247 |
</div>
|
| 2248 |
""", unsafe_allow_html=True)
|
|
|
|
| 2250 |
# Welcome message if no chat
|
| 2251 |
if not st.session_state.messages:
|
| 2252 |
st.markdown("""
|
| 2253 |
+
<div style="text-align:center; padding:3rem 0 2rem 0;">
|
| 2254 |
+
<div style="
|
| 2255 |
+
font-size:3rem;
|
| 2256 |
+
margin-bottom:0.8rem;
|
| 2257 |
+
animation: pulse 2s infinite;
|
| 2258 |
+
">๐งฎ</div>
|
| 2259 |
+
<div style="font-size:1.1rem; color:#d1d5db; font-weight:500; margin-bottom:0.5rem;">
|
| 2260 |
+
How can I help you today?
|
| 2261 |
</div>
|
| 2262 |
+
<div style="font-size:0.82rem; color:#4b5563; margin-bottom:2rem;">
|
| 2263 |
+
Ask me anything โ math problems, explanations, or just say hi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2264 |
</div>
|
| 2265 |
+
<div style="display:flex; flex-wrap:wrap; gap:0.5rem; justify-content:center; max-width:600px; margin:0 auto;">
|
| 2266 |
+
<span style="background:#111827; border:1px solid #1f2937; color:#9ca3af; padding:0.5rem 1rem; border-radius:20px; font-size:0.8rem; cursor:pointer;">
|
| 2267 |
+
๐ Differentiate xยณ + 2x
|
| 2268 |
+
</span>
|
| 2269 |
+
<span style="background:#111827; border:1px solid #1f2937; color:#9ca3af; padding:0.5rem 1rem; border-radius:20px; font-size:0.8rem; cursor:pointer;">
|
| 2270 |
+
โซ Integrate sin(x)ยทeหฃ
|
| 2271 |
+
</span>
|
| 2272 |
+
<span style="background:#111827; border:1px solid #1f2937; color:#9ca3af; padding:0.5rem 1rem; border-radius:20px; font-size:0.8rem; cursor:pointer;">
|
| 2273 |
+
๐ข Solve ODE dy/dx + 2y = eโปหฃ
|
| 2274 |
+
</span>
|
| 2275 |
+
<span style="background:#111827; border:1px solid #1f2937; color:#9ca3af; padding:0.5rem 1rem; border-radius:20px; font-size:0.8rem; cursor:pointer;">
|
| 2276 |
+
๐งฎ Newton-Raphson xยณโ2xโ5=0
|
| 2277 |
+
</span>
|
| 2278 |
+
<span style="background:#111827; border:1px solid #1f2937; color:#9ca3af; padding:0.5rem 1rem; border-radius:20px; font-size:0.8rem; cursor:pointer;">
|
| 2279 |
+
๐ Find curvature of y=xยฒ
|
| 2280 |
+
</span>
|
| 2281 |
+
<span style="background:#111827; border:1px solid #1f2937; color:#9ca3af; padding:0.5rem 1rem; border-radius:20px; font-size:0.8rem; cursor:pointer;">
|
| 2282 |
+
๐ Plot y = xยฒ โ 4
|
| 2283 |
+
</span>
|
| 2284 |
</div>
|
| 2285 |
</div>
|
| 2286 |
+
<style>
|
| 2287 |
+
@keyframes pulse {
|
| 2288 |
+
0%, 100% { opacity: 1; }
|
| 2289 |
+
50% { opacity: 0.6; }
|
| 2290 |
+
}
|
| 2291 |
+
</style>
|
| 2292 |
""", unsafe_allow_html=True)
|
| 2293 |
|
| 2294 |
# Render chat history
|
|
|
|
| 2329 |
if problem and problem != st.session_state.last_submitted:
|
| 2330 |
st.session_state.last_submitted = problem
|
| 2331 |
|
| 2332 |
+
# โโ Detect casual / non-math messages โโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 2333 |
+
p_lower = problem.lower().strip()
|
| 2334 |
+
casual_keywords = [
|
| 2335 |
+
"hi", "hello", "hey", "how are you", "how r u", "what's up",
|
| 2336 |
+
"whats up", "good morning", "good evening", "good night",
|
| 2337 |
+
"who are you", "what are you", "what can you do", "help",
|
| 2338 |
+
"thanks", "thank you", "bye", "goodbye", "ok", "okay",
|
| 2339 |
+
"what is your name", "your name", "who made you", "who built you",
|
| 2340 |
+
"how do you work", "what do you do", "sup", "hlo", "hlw",
|
| 2341 |
+
"what u doing", "what are you doing", "hows it going",
|
| 2342 |
+
]
|
| 2343 |
+
is_casual = (
|
| 2344 |
+
any(p_lower == kw for kw in casual_keywords) or
|
| 2345 |
+
any(p_lower.startswith(kw) for kw in casual_keywords) or
|
| 2346 |
+
(len(p_lower.split()) <= 4 and not any(c in p_lower for c in [
|
| 2347 |
+
"=", "+", "-", "*", "/", "^", "โซ", "โ", "โ", "dx", "dy",
|
| 2348 |
+
"sin", "cos", "tan", "log", "lim", "diff", "solve", "find",
|
| 2349 |
+
"calculate", "compute", "prove", "matrix", "eigen", "gcd",
|
| 2350 |
+
"integral", "derivative", "equation", "theorem"
|
| 2351 |
+
]))
|
| 2352 |
+
)
|
| 2353 |
+
|
| 2354 |
# Show user message immediately
|
| 2355 |
with st.chat_message("user", avatar="๐งโ๐"):
|
| 2356 |
st.markdown(problem)
|
| 2357 |
|
| 2358 |
+
# Show AI response
|
| 2359 |
with st.chat_message("assistant", avatar="๐"):
|
| 2360 |
+
if is_casual:
|
| 2361 |
+
# โโ Casual message โ no SymPy, no math structure โโโโโโโโโ
|
| 2362 |
+
casual_sympy = {"type": "casual", "result": None, "latex": ""}
|
| 2363 |
+
answer = ask_ai_streaming(problem, casual_sympy, st.session_state.messages)
|
| 2364 |
+
else:
|
| 2365 |
+
# โโ Math message โ full SymPy + structured response โโโโโโโ
|
| 2366 |
+
import random
|
| 2367 |
+
spinner_msgs = [
|
| 2368 |
+
"๐งฎ Computing with SymPy...",
|
| 2369 |
+
"โซ Integrating the solution...",
|
| 2370 |
+
"โ Summing it all up...",
|
| 2371 |
+
"๐ Applying the formula...",
|
| 2372 |
+
"๐ข Crunching the numbers...",
|
| 2373 |
+
"๐ Verifying the answer...",
|
| 2374 |
+
]
|
| 2375 |
+
with st.spinner(random.choice(spinner_msgs)):
|
| 2376 |
+
sympy_result = run_sympy(problem)
|
| 2377 |
+
answer = ask_ai_streaming(problem, sympy_result, st.session_state.messages)
|
| 2378 |
+
plot_graph(problem, sympy_result)
|
| 2379 |
|
| 2380 |
# Save to history
|
| 2381 |
st.session_state.messages.append({"role": "user", "content": problem})
|