saad-sust commited on
Commit
a8fa158
Β·
verified Β·
1 Parent(s): 9d5e30f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -57
app.py CHANGED
@@ -1,76 +1,55 @@
1
  import streamlit as st
2
  import sympy as sp
3
  import re
 
4
 
5
- # 1. PAGE CONFIG
6
  st.set_page_config(page_title="SAAD AI ACADEMY", layout="centered")
7
 
8
- # 2. DARK UI
9
- st.markdown("""
10
- <style>
11
- .stApp { background: #0f172a; color: white; }
12
- h1, h2, h3 { color: #3b82f6 !important; }
13
- .stTextInput > div > div > input { background-color: #1e293b; color: white; border: 1px solid #3b82f6; }
14
- </style>
15
- """, unsafe_allow_html=True)
16
-
17
- st.title("πŸ“ SAAD AI ACADEMY")
18
- st.subheader("B.Sc. Math Engine v6.0 (Zero-Error Logic)")
19
-
20
- # 3. COMPUTATION ENGINE
21
- def solve_math(query):
22
  try:
23
- x = sp.Symbol('x')
24
-
25
- # A. HANDLE LINEAR CONGRUENCE: "solve 14x = 30 mod 44"
26
  if "mod" in query.lower():
27
- # Extract numbers using Regex
28
  nums = re.findall(r'\d+', query)
29
  if len(nums) >= 3:
30
  a, b, n = map(int, nums[:3])
31
- # SymPy's solver for ax ≑ b (mod n)
32
- solutions = sp.solve_linear_congruence(a, b, n)
33
- return f"The solutions are: x \equiv {solutions} \pmod{{{n}}}"
34
- return "Format error. Try: 'solve 14x = 30 mod 44'"
35
-
36
- # B. HANDLE DERIVATIVES: "derivative of x**2 + 5*x"
37
- if "derivative" in query.lower() or "diff" in query.lower():
38
- expr_str = query.split("of")[-1].strip()
39
- expr = sp.sympify(expr_str)
40
- return sp.diff(expr, x)
41
-
42
- # C. HANDLE INTEGRALS: "integrate x**2"
43
- if "integrate" in query.lower():
44
- expr_str = query.split("integrate")[-1].strip()
45
- expr = sp.sympify(expr_str)
46
- return sp.integrate(expr, x)
47
 
48
- # D. GENERAL SOLVE: "solve x**2 - 4"
49
- if "solve" in query.lower() and "mod" not in query.lower():
50
- expr_str = query.split("solve")[-1].strip()
51
- expr = sp.sympify(expr_str)
52
- return sp.solve(expr, x)
53
-
54
- return "I can solve Congruences, Derivatives, Integrals, and Equations. Please be specific!"
 
 
 
 
 
 
 
 
55
 
56
  except Exception as e:
57
- return f"Logic Error: {e}. Please use Python syntax like x**2 for xΒ²."
 
 
 
 
58
 
59
- # 4. INTERFACE
60
- user_input = st.text_input("Enter your problem:", placeholder="e.g., solve 14x = 30 mod 44")
61
 
62
  if user_input:
63
- res = solve_math(user_input)
64
- st.markdown("---")
65
- st.markdown("### 🎯 Result:")
66
- if isinstance(res, str):
67
- st.info(res)
68
  else:
69
- # Display as beautiful LaTeX
70
- st.latex(sp.latex(res))
71
 
72
- # 5. SIDEBAR
73
- st.sidebar.markdown("### πŸŽ“ Academic Profile")
74
- st.sidebar.write("**Name:** Almuyed Saad")
75
- st.sidebar.write("**Inst:** SUST")
76
- st.sidebar.info("This engine uses SymPy for symbolic math, ensuring 100% precision for B.Sc. coursework.")
 
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.")