Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,61 +2,62 @@ import streamlit as st
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
# Professional Academic
|
| 6 |
-
st.set_page_config(page_title="B.Sc. Math Engine", page_icon="π")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
st.title("π B.Sc. Mathematics Engine")
|
| 8 |
-
st.
|
| 9 |
|
| 10 |
@st.cache_resource
|
| 11 |
def load_engine():
|
| 12 |
-
# SmolLM2-1.7B
|
| 13 |
model_id = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
|
| 14 |
return pipeline(
|
| 15 |
"text-generation",
|
| 16 |
model=model_id,
|
| 17 |
-
device=-1,
|
| 18 |
torch_dtype=torch.float32
|
| 19 |
)
|
| 20 |
|
| 21 |
pipe = load_engine()
|
| 22 |
|
| 23 |
-
#
|
| 24 |
system_instruction = (
|
| 25 |
-
"You are a Senior Mathematics Professor
|
| 26 |
-
"
|
| 27 |
-
|
| 28 |
-
"
|
| 29 |
-
|
| 30 |
-
"
|
| 31 |
-
"
|
| 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 |
-
|
|
|
|
| 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("
|
| 53 |
-
#
|
| 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 |
-
#
|
| 57 |
-
result = pipe(prompt, max_new_tokens=
|
| 58 |
response = result[0]['generated_text'].split("<|im_start|>assistant\n")[-1]
|
| 59 |
|
| 60 |
st.markdown(response)
|
|
|
|
| 61 |
|
| 62 |
-
st.sidebar.
|
|
|
|
|
|
| 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")
|