saad-sust commited on
Commit
a4d1f05
Β·
verified Β·
1 Parent(s): 1c312f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -32
app.py CHANGED
@@ -2,61 +2,62 @@ import streamlit as st
2
  from transformers import pipeline
3
  import torch
4
 
5
- # Professional Academic Layout
6
- st.set_page_config(page_title="B.Sc. Math Engine", page_icon="πŸ“")
 
 
 
 
 
 
 
 
7
  st.title("πŸ“ B.Sc. Mathematics Engine")
8
- st.write("Advanced Proof & Logic Solver | SUST Dept. of Mathematics")
9
 
10
  @st.cache_resource
11
  def load_engine():
12
- # SmolLM2-1.7B: The best middle-ground for logic vs speed on free CPU
13
  model_id = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
14
  return pipeline(
15
  "text-generation",
16
  model=model_id,
17
- device=-1, # Force CPU usage
18
  torch_dtype=torch.float32
19
  )
20
 
21
  pipe = load_engine()
22
 
23
- # Strict Academic System Prompt
24
  system_instruction = (
25
- "You are a Senior Mathematics Professor at a top-tier university. "
26
- "Follow these strict logical rules for all solutions:\n\n"
27
-
28
- "1. STRUCTURE: Always start with 'Definitions & Prerequisites', followed by 'Step-by-Step Proof', and end with 'Conclusion'.\n"
29
-
30
- "2. GROUP THEORY: To prove a subgroup H is normal, you must show gH = Hg for all g in G. "
31
- "If the index [G:H]=2, use the partition G = H U (G-H) to show left and right cosets are identical.\n"
32
-
33
- "3. NUMBER THEORY: For divisibility (e.g., n^k - n), use Fermat's Little Theorem or Induction. "
34
- "For congruences ax ≑ b (mod n), always calculate d = gcd(a, n) first. If d does not divide b, state 'No Solution'.\n"
35
-
36
- "4. REAL ANALYSIS: For limits, use the epsilon-delta or epsilon-N definition. "
37
- "For convergence, state which test you are using (Ratio, Root, or Comparison).\n"
38
-
39
- "5. COMPLEX ANALYSIS: To check differentiability, verify Cauchy-Riemann equations (ux = vy and uy = -vx). "
40
- "For integrals, identify poles and use the Residue Theorem.\n"
41
-
42
- "6. FORMATTING: Use LaTeX for EVERY mathematical symbol. Use '$$' for centered equations and '$' for inline math. "
43
- "Never say 'it is obvious'; explain every logical jump."
44
  )
45
 
46
- user_query = st.chat_input("Enter your problem...")
 
47
 
48
  if user_query:
49
  with st.chat_message("user"):
50
  st.write(user_query)
 
51
  with st.chat_message("assistant"):
52
- with st.spinner("Processing logic... (Usually takes 45-90s on CPU)"):
53
- # Format specifically for SmolLM2
54
- prompt = f"<|im_start|>system\n{system_instruction}<|im_end|>\n<|im_start|>user\n{user_query}<|im_end|>\n<|im_start|>assistant\n"
55
 
56
- # do_sample=False ensures the math is consistent (Temperature 0)
57
- result = pipe(prompt, max_new_tokens=600, do_sample=False)
58
  response = result[0]['generated_text'].split("<|im_start|>assistant\n")[-1]
59
 
60
  st.markdown(response)
 
61
 
62
- st.sidebar.info("Model: SmolLM2-1.7B | Mode: Exact Logic")
 
 
2
  from transformers import pipeline
3
  import torch
4
 
5
+ # 1. Page Configuration for a Professional Academic Interface
6
+ st.set_page_config(page_title="B.Sc. Math Engine V5", page_icon="πŸ“", layout="centered")
7
+
8
+ st.markdown("""
9
+ <style>
10
+ .stApp { background-color: #f0f2f6; }
11
+ .stChatMessage { border-radius: 15px; border: 1px solid #d1d5db; }
12
+ </style>
13
+ """, unsafe_allow_html=True)
14
+
15
  st.title("πŸ“ B.Sc. Mathematics Engine")
16
+ st.caption("Advanced Logic & Proof System | Optimized for SUST 3rd Year")
17
 
18
  @st.cache_resource
19
  def load_engine():
20
+ # SmolLM2-1.7B is the best logic engine for Free CPU spaces
21
  model_id = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
22
  return pipeline(
23
  "text-generation",
24
  model=model_id,
25
+ device=-1,
26
  torch_dtype=torch.float32
27
  )
28
 
29
  pipe = load_engine()
30
 
31
+ # 2. THE LOGIC CONTROLLER (Prevents common B.Sc. Math errors)
32
  system_instruction = (
33
+ "You are a Senior Mathematics Professor. You must follow a 'Verify-then-Solve' protocol.\n\n"
34
+ "RULE 1 (DIVISIBILITY): For ax ≑ b (mod n), you MUST check if d = gcd(a, n) divides b. "
35
+ "If it divides, divide the entire congruence by d: (a/d)x ≑ (b/d) (mod n/d) BEFORE finding the inverse.\n\n"
36
+ "RULE 2 (GROUPS): To prove H is normal where [G:H]=2, use the fact that gH and Hg are both "
37
+ "the complement G\\H when g is not in H. Do not assume gh=hg.\n\n"
38
+ "RULE 3 (REASONING): Think step-by-step. State the theorem you are using (e.g., Lagrange, Fermat, Cauchy-Riemann) "
39
+ "before applying it.\n\n"
40
+ "RULE 4 (FORMAT): Use LaTeX ($...$ or $$...$$) for all math. Never skip algebraic steps."
 
 
 
 
 
 
 
 
 
 
 
41
  )
42
 
43
+ # 3. User Interface
44
+ user_query = st.chat_input("Enter your problem (e.g., Prove H is normal if [G:H]=2)")
45
 
46
  if user_query:
47
  with st.chat_message("user"):
48
  st.write(user_query)
49
+
50
  with st.chat_message("assistant"):
51
+ with st.spinner("⏳ Analyzing mathematical structures and verifying logic..."):
52
+ # We add "Let's think step by step" to the prompt to trigger Chain-of-Thought
53
+ prompt = f"<|im_start|>system\n{system_instruction}<|im_end|>\n<|im_start|>user\n{user_query}\nLet's think step by step.<|im_end|>\n<|im_start|>assistant\n"
54
 
55
+ # Increased max_new_tokens for longer, detailed proofs
56
+ result = pipe(prompt, max_new_tokens=900, temperature=0.1, do_sample=True, top_p=0.9)
57
  response = result[0]['generated_text'].split("<|im_start|>assistant\n")[-1]
58
 
59
  st.markdown(response)
60
+ st.download_button("πŸ“© Download Proof", response, file_name="math_solution.txt")
61
 
62
+ st.sidebar.markdown("### πŸŽ“ Academic Modules")
63
+ st.sidebar.info("β€’ Abstract Algebra\nβ€’ Real Analysis\nβ€’ Number Theory\nβ€’ Complex Variables")