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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -50
app.py CHANGED
@@ -1,62 +1,76 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
- import torch
4
 
5
  # 1. PAGE CONFIG
6
- st.set_page_config(page_title="SAAD AI ACADEMY", page_icon="πŸ“", layout="centered")
7
 
8
- # 2. DARK MODE UI
9
  st.markdown("""
10
  <style>
11
- .stApp { background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%); color: #ffffff; }
12
- .stChatMessage { background: rgba(255, 255, 255, 0.05) !important; border-radius: 15px !important; border: 1px solid rgba(255, 255, 255, 0.1) !important; }
13
- h1, h2, h3, p, span { color: #ffffff !important; }
14
- .stCaption { color: #3b82f6 !important; }
15
  </style>
16
  """, unsafe_allow_html=True)
17
 
18
  st.title("πŸ“ SAAD AI ACADEMY")
19
- st.caption("Stable Mathematics Engine | SUST Logic System")
20
-
21
- # 3. STABLE LOCAL LOADING
22
- @st.cache_resource
23
- def load_engine():
24
- model_id = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
25
- # Loading locally to ensure 100% uptime and no API timeouts
26
- return pipeline("text-generation", model=model_id, device=-1, torch_dtype=torch.float32)
27
-
28
- with st.spinner("⏳ Restoring the Stable Engine..."):
29
- pipe = load_engine()
30
-
31
- # 4. FIXED LOGIC INSTRUCTIONS
32
- # Added a check so it doesn't give random math for greetings
33
- system_instruction = (
34
- "You are a helpful assistant and a Senior Mathematics Professor. "
35
- "If the user greets you or asks a general question, respond normally. "
36
- "ONLY if the user provides a math problem, follow these rules:\n"
37
- "1. State the math domain.\n"
38
- "2. Show all steps using LaTeX ($...$).\n"
39
- "3. Bold the final result."
40
- )
41
-
42
- # 5. INTERFACE
43
- user_query = st.chat_input("Enter your query...")
44
-
45
- if user_query:
46
- with st.chat_message("user"):
47
- st.write(user_query)
48
-
49
- with st.chat_message("assistant"):
50
- with st.spinner("🧠 Processing..."):
51
- prompt = f"<|im_start|>system\n{system_instruction}<|im_end|>\n<|im_start|>user\n{user_query}<|im_end|>\n<|im_start|>assistant\n"
52
-
53
- # Fixed generation settings to prevent hallucinations
54
- result = pipe(prompt, max_new_tokens=800, temperature=0.7, do_sample=True)
55
- response = result[0]['generated_text'].split("<|im_start|>assistant\n")[-1]
56
- st.markdown(response)
57
-
58
- # 6. SIDEBAR
59
- st.sidebar.markdown(f"### πŸŽ“ Student Profile")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  st.sidebar.write("**Name:** Almuyed Saad")
61
  st.sidebar.write("**Inst:** SUST")
62
- st.sidebar.write("**Major:** Mathematics")
 
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.")