saad-sust commited on
Commit
6262fed
·
verified ·
1 Parent(s): a8fa158

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -30
app.py CHANGED
@@ -1,55 +1,57 @@
1
  import streamlit as st
2
  import sympy as sp
3
  import re
4
- from sympy.ntheory.modular import solve_linear_congruence
5
 
6
  # 1. SETUP
7
  st.set_page_config(page_title="SAAD AI ACADEMY", layout="centered")
8
 
9
- # 2. THE UNIVERSAL SOLVER FUNCTION
10
  def universal_solver(query):
11
  try:
12
- # A. Check for Modular Congruence first (special case)
 
 
13
  if "mod" in query.lower():
14
  nums = re.findall(r'\d+', query)
15
  if len(nums) >= 3:
16
  a, b, n = map(int, nums[:3])
17
- sol = solve_linear_congruence(a, b, n)
18
- return f"Number Theory Result: x ≡ {sol[0]} (mod {sol[1]})" if sol else "No Solution."
19
-
20
- # B. For everything else (Calculus, Algebra, Limits)
21
- # We clean the query to remove words like "solve" or "find"
22
- clean_query = query.lower().replace("solve", "").replace("find", "").replace("the", "").strip()
23
-
24
- # SymPy turns your text into a math expression
25
- obj = sp.sympify(clean_query)
26
-
27
- # If it's an equation (has an '='), solve it
28
- if "=" in query:
29
- parts = query.split("=")
30
- equation = sp.Eq(sp.sympify(parts[0]), sp.sympify(parts[1]))
31
- return sp.solve(equation)
32
-
33
- # Otherwise, just simplify or "do" the math (like derivatives)
34
- return sp.simplify(obj)
 
 
 
 
35
 
36
  except Exception as e:
37
- return f"Enter a valid math expression (e.g., x**2 + 5*x or 14x=30 mod 44). Error: {e}"
38
 
39
  # 3. UI
40
  st.title("📐 SAAD AI ACADEMY")
41
- st.write("Enter any B.Sc. Level problem (Algebra, Calculus, Number Theory)")
42
-
43
- user_input = st.text_input("Input:", placeholder="e.g. diff(sin(x)*exp(x), x) or x**2 - 5*x + 6 = 0")
44
 
45
  if user_input:
46
  result = universal_solver(user_input)
47
  st.divider()
48
- st.subheader("Result:")
49
- if isinstance(result, (list, tuple, sp.Basic)):
50
- st.latex(sp.latex(result))
51
  else:
52
- st.write(result)
53
 
54
  # 4. SIDEBAR
55
- st.sidebar.info("Universal Mode: Uses SymPy Symbolic Parsing.")
 
1
  import streamlit as st
2
  import sympy as sp
3
  import re
 
4
 
5
  # 1. SETUP
6
  st.set_page_config(page_title="SAAD AI ACADEMY", layout="centered")
7
 
8
+ # 2. THE STABLE SOLVER
9
  def universal_solver(query):
10
  try:
11
+ x = sp.Symbol('x')
12
+
13
+ # A. MODULAR CONGRUENCE (The 14x = 30 mod 44 logic)
14
  if "mod" in query.lower():
15
  nums = re.findall(r'\d+', query)
16
  if len(nums) >= 3:
17
  a, b, n = map(int, nums[:3])
18
+ # Using solveset on a modular equation is the most stable way
19
+ equation = sp.Eq(sp.Mod(a*x, n), b % n)
20
+ sol = sp.solveset(equation, x, domain=sp.S.Integers)
21
+ return f"Result: ${sp.latex(sol)}$"
22
+
23
+ # B. CALCULUS (Derivatives/Integrals)
24
+ if "diff" in query.lower() or "derivative" in query.lower():
25
+ expr = sp.sympify(query.split("of")[-1].strip())
26
+ return sp.diff(expr, x)
27
+
28
+ if "integrate" in query.lower():
29
+ expr = sp.sympify(query.split("integrate")[-1].strip())
30
+ return sp.integrate(expr, x)
31
+
32
+ # C. GENERAL ALGEBRA
33
+ clean_query = query.replace("solve", "").replace("find", "").strip()
34
+ if "=" in clean_query:
35
+ parts = clean_query.split("=")
36
+ eq = sp.Eq(sp.sympify(parts[0]), sp.sympify(parts[1]))
37
+ return sp.solve(eq, x)
38
+
39
+ return sp.simplify(sp.sympify(clean_query))
40
 
41
  except Exception as e:
42
+ return f"Please check syntax. Use x**2 for powers. Error: {e}"
43
 
44
  # 3. UI
45
  st.title("📐 SAAD AI ACADEMY")
46
+ user_input = st.text_input("Enter problem:", placeholder="e.g. 14x = 30 mod 44")
 
 
47
 
48
  if user_input:
49
  result = universal_solver(user_input)
50
  st.divider()
51
+ if isinstance(result, str) and "Result" in result:
52
+ st.markdown(result)
 
53
  else:
54
+ st.latex(sp.latex(result))
55
 
56
  # 4. SIDEBAR
57
+ st.sidebar.info("v6.1: Stable Solver (Python 3.13 Ready)")