saad-sust commited on
Commit
d15db53
ยท
verified ยท
1 Parent(s): 1c6da94

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -73
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 precise BSc Mathematics tutor built by Saad. "
 
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:1.4rem 0 0.8rem 0; margin-bottom:0.5rem;">
2223
  <div style="
2224
- font-size:2.2rem;
2225
  font-weight:700;
2226
- background: linear-gradient(135deg, #3b82f6 0%, #a78bfa 50%, #60a5fa 100%);
2227
- -webkit-background-clip: text;
2228
- -webkit-text-fill-color: transparent;
2229
- background-clip: text;
2230
- color: #3b82f6;
2231
- letter-spacing:1px;
2232
- margin-bottom:0.3rem;
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 &nbsp;ยท&nbsp; Step-by-Step Solver &nbsp;ยท&nbsp; 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:1.5rem 0 0.5rem 0;">
2252
- <div style="font-size:0.85rem; color:#6b7280; margin-bottom:1.2rem;">
2253
- Ask any BSc Mathematics problem โ€” I'll solve it step by step
 
 
 
 
 
2254
  </div>
2255
- <div style="display:grid; grid-template-columns:repeat(3,1fr); gap:0.5rem; margin:0 0 1rem 0;">
2256
- <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;">
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="font-size:0.72rem; color:#374151; margin-top:0.5rem;">
2285
- ๐Ÿ’ก Tip: Select an example from the sidebar or type your problem below
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 with streaming
2333
  with st.chat_message("assistant", avatar="๐Ÿ“"):
2334
- # Step 1: SymPy computation (instant)
2335
- import random
2336
- spinner_msgs = [
2337
- "๐Ÿงฎ Computing with SymPy...",
2338
- "โˆซ Integrating the solution...",
2339
- "โˆ‘ Summing it all up...",
2340
- "๐Ÿ“ Applying the formula...",
2341
- "๐Ÿ”ข Crunching the numbers...",
2342
- "๐Ÿ“Š Verifying the answer...",
2343
- ]
2344
- with st.spinner(random.choice(spinner_msgs)):
2345
- sympy_result = run_sympy(problem)
2346
-
2347
- # Step 2: Stream AI response word by word
2348
- answer = ask_ai_streaming(problem, sympy_result, st.session_state.messages)
2349
-
2350
- # Step 3: Plot graph if user asked for it
2351
- plot_graph(problem, sympy_result)
 
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})