Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,75 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
# 1. Page Configuration
|
| 6 |
-
st.set_page_config(page_title="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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("π
|
| 16 |
-
st.caption("
|
| 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 |
-
|
| 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
|
| 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.,
|
| 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("
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
st.
|
| 63 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
|
|
|
| 3 |
|
| 4 |
+
# 1. Page Configuration
|
| 5 |
+
st.set_page_config(page_title="SAAD AI ACADEMY: Math Engine", page_icon="π", layout="centered")
|
| 6 |
+
|
| 7 |
+
# SECURE TOKEN ACCESS (This pulls from the Settings/Secrets you just saved)
|
| 8 |
+
try:
|
| 9 |
+
HF_TOKEN = st.secrets["HF_TOKEN"]
|
| 10 |
+
except:
|
| 11 |
+
st.error("Error: HF_TOKEN not found in Space Secrets. Please add it in Settings.")
|
| 12 |
+
st.stop()
|
| 13 |
+
|
| 14 |
+
API_URL = "https://api-inference.huggingface.co/models/HuggingFaceTB/SmolLM2-1.7B-Instruct"
|
| 15 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 16 |
|
| 17 |
st.markdown("""
|
| 18 |
<style>
|
| 19 |
.stApp { background-color: #f0f2f6; }
|
| 20 |
+
.stChatMessage { border-radius: 15px; border: 1px solid #d1d5db; box-shadow: 2px 2px 5px rgba(0,0,0,0.05); }
|
| 21 |
</style>
|
| 22 |
""", unsafe_allow_html=True)
|
| 23 |
|
| 24 |
+
st.title("π SAAD AI ACADEMY")
|
| 25 |
+
st.caption("High-Speed Mathematics Engine | SUST Department of Mathematics")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# 2. MASTER LOGIC PROMPT
|
|
|
|
|
|
|
| 28 |
system_instruction = (
|
| 29 |
"You are a Senior Mathematics Professor. You must follow a 'Verify-then-Solve' protocol.\n\n"
|
| 30 |
"RULE 1 (DIVISIBILITY): For ax β‘ b (mod n), you MUST check if d = gcd(a, n) divides b. "
|
| 31 |
"If it divides, divide the entire congruence by d: (a/d)x β‘ (b/d) (mod n/d) BEFORE finding the inverse.\n\n"
|
| 32 |
"RULE 2 (GROUPS): To prove H is normal where [G:H]=2, use the fact that gH and Hg are both "
|
| 33 |
"the complement G\\H when g is not in H. Do not assume gh=hg.\n\n"
|
| 34 |
+
"RULE 3 (REASONING): Think step-by-step. State the theorem you are using before applying it.\n\n"
|
|
|
|
| 35 |
"RULE 4 (FORMAT): Use LaTeX ($...$ or $$...$$) for all math. Never skip algebraic steps."
|
| 36 |
)
|
| 37 |
|
| 38 |
+
def query_api(prompt_text):
|
| 39 |
+
full_prompt = f"<|im_start|>system\n{system_instruction}<|im_end|>\n<|im_start|>user\n{prompt_text}\nLet's think step by step.<|im_end|>\n<|im_start|>assistant\n"
|
| 40 |
+
|
| 41 |
+
payload = {
|
| 42 |
+
"inputs": full_prompt,
|
| 43 |
+
"parameters": {
|
| 44 |
+
"max_new_tokens": 800,
|
| 45 |
+
"temperature": 0.1,
|
| 46 |
+
"return_full_text": False
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 51 |
+
return response.json()
|
| 52 |
+
|
| 53 |
# 3. User Interface
|
| 54 |
+
user_query = st.chat_input("Enter your problem (e.g., Solve 14x β‘ 30 mod 44)")
|
| 55 |
|
| 56 |
if user_query:
|
| 57 |
with st.chat_message("user"):
|
| 58 |
st.write(user_query)
|
| 59 |
|
| 60 |
with st.chat_message("assistant"):
|
| 61 |
+
with st.spinner("π Cloud API is solving..."):
|
| 62 |
+
try:
|
| 63 |
+
data = query_api(user_query)
|
| 64 |
+
|
| 65 |
+
if isinstance(data, dict) and "error" in data:
|
| 66 |
+
st.warning("Model is starting up. Please wait 10-20 seconds and try again.")
|
| 67 |
+
else:
|
| 68 |
+
response = data[0]['generated_text']
|
| 69 |
+
st.markdown(response)
|
| 70 |
+
st.download_button("π© Download Proof", response, file_name="math_solution.txt")
|
| 71 |
+
except Exception as e:
|
| 72 |
+
st.error("Connection error. Ensure your HF_TOKEN is correctly set in Secrets.")
|
| 73 |
+
|
| 74 |
+
st.sidebar.markdown(f"### π Student Profile\n**Name:** Almuyed Saad\n**Inst:** SUST\n**Major:** Mathematics")
|
| 75 |
+
st.sidebar.info("Module: B.Sc. Math Engine V5 (API Edition)")
|