Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,19 +3,19 @@ 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 |
|
| 8 |
st.title("π B.Sc. Mathematics Engine")
|
| 9 |
st.write("Advanced Proof & Logic Solver | SUST Dept. of Mathematics")
|
| 10 |
|
| 11 |
@st.cache_resource
|
| 12 |
def load_engine():
|
| 13 |
-
#
|
| 14 |
-
model_id = "
|
| 15 |
return pipeline(
|
| 16 |
"text-generation",
|
| 17 |
model=model_id,
|
| 18 |
-
|
| 19 |
torch_dtype=torch.float32
|
| 20 |
)
|
| 21 |
|
|
@@ -24,31 +24,28 @@ pipe = load_engine()
|
|
| 24 |
# Strict Academic System Prompt
|
| 25 |
system_instruction = (
|
| 26 |
"You are a rigorous Mathematics Professor. "
|
| 27 |
-
"
|
| 28 |
-
"
|
| 29 |
-
"
|
| 30 |
-
"
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
-
user_query = st.chat_input("Enter your problem
|
| 34 |
|
| 35 |
if user_query:
|
| 36 |
with st.chat_message("user"):
|
| 37 |
st.write(user_query)
|
| 38 |
|
| 39 |
with st.chat_message("assistant"):
|
| 40 |
-
with st.spinner("
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
{"role": "system", "content": system_instruction},
|
| 44 |
-
{"role": "user", "content": user_query}
|
| 45 |
-
]
|
| 46 |
|
| 47 |
-
# do_sample=False (Temperature 0)
|
| 48 |
-
result = pipe(
|
| 49 |
-
response = result[0]['generated_text'][-1]
|
| 50 |
|
| 51 |
st.markdown(response)
|
| 52 |
|
| 53 |
-
st.sidebar.
|
| 54 |
-
st.sidebar.info("This engine uses a 1-Billion parameter logic model optimized for CPU execution.")
|
|
|
|
| 3 |
import torch
|
| 4 |
|
| 5 |
# Professional Academic Layout
|
| 6 |
+
st.set_page_config(page_title="B.Sc. Math Engine", page_icon="π")
|
| 7 |
|
| 8 |
st.title("π B.Sc. Mathematics Engine")
|
| 9 |
st.write("Advanced Proof & Logic Solver | SUST Dept. of Mathematics")
|
| 10 |
|
| 11 |
@st.cache_resource
|
| 12 |
def load_engine():
|
| 13 |
+
# SmolLM2-1.7B: The best middle-ground for logic vs speed on free CPU
|
| 14 |
+
model_id = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
|
| 15 |
return pipeline(
|
| 16 |
"text-generation",
|
| 17 |
model=model_id,
|
| 18 |
+
device=-1, # Force CPU usage
|
| 19 |
torch_dtype=torch.float32
|
| 20 |
)
|
| 21 |
|
|
|
|
| 24 |
# Strict Academic System Prompt
|
| 25 |
system_instruction = (
|
| 26 |
"You are a rigorous Mathematics Professor. "
|
| 27 |
+
"Instructions: "
|
| 28 |
+
"1. Use LaTeX for all mathematical notation. "
|
| 29 |
+
"2. For group theory, a homomorphism phi satisfies phi(ab) = phi(a)phi(b). "
|
| 30 |
+
"3. The kernel is the set of elements mapping to the identity element (1 for multiplication). "
|
| 31 |
+
"4. Provide clear, logical steps for the proof."
|
| 32 |
)
|
| 33 |
|
| 34 |
+
user_query = st.chat_input("Enter your problem...")
|
| 35 |
|
| 36 |
if user_query:
|
| 37 |
with st.chat_message("user"):
|
| 38 |
st.write(user_query)
|
| 39 |
|
| 40 |
with st.chat_message("assistant"):
|
| 41 |
+
with st.spinner("Processing logic... (Usually takes 45-90s on CPU)"):
|
| 42 |
+
# Format specifically for SmolLM2
|
| 43 |
+
prompt = f"<|im_start|>system\n{system_instruction}<|im_end|>\n<|im_start|>user\n{user_query}<|im_end|>\n<|im_start|>assistant\n"
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# do_sample=False ensures the math is consistent (Temperature 0)
|
| 46 |
+
result = pipe(prompt, max_new_tokens=600, do_sample=False)
|
| 47 |
+
response = result[0]['generated_text'].split("<|im_start|>assistant\n")[-1]
|
| 48 |
|
| 49 |
st.markdown(response)
|
| 50 |
|
| 51 |
+
st.sidebar.info("Model: SmolLM2-1.7B | Mode: Exact Logic")
|
|
|