Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,58 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
st.set_page_config(page_title="SAAD AI ACADEMY", page_icon="π", layout="centered")
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
try:
|
| 9 |
-
HF_TOKEN = st.secrets["HF_TOKEN"]
|
| 10 |
-
except:
|
| 11 |
-
st.error("Error: HF_TOKEN not found in Space Secrets.")
|
| 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 |
-
# STEP 3: UI UPGRADE (Dark/Neon Glassmorphism)
|
| 18 |
st.markdown("""
|
| 19 |
<style>
|
| 20 |
-
.stApp {
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
}
|
| 24 |
-
.stChatMessage {
|
| 25 |
-
background: rgba(255, 255, 255, 0.05) !important;
|
| 26 |
-
backdrop-filter: blur(10px);
|
| 27 |
-
border-radius: 15px !important;
|
| 28 |
-
border: 1px solid rgba(255, 255, 255, 0.1) !important;
|
| 29 |
-
margin-bottom: 15px;
|
| 30 |
-
}
|
| 31 |
-
section[data-testid="stSidebar"] {
|
| 32 |
-
background-color: #0f172a !important;
|
| 33 |
-
}
|
| 34 |
-
h1, h2, h3, p, span {
|
| 35 |
-
color: #ffffff !important;
|
| 36 |
-
}
|
| 37 |
-
.stCaption {
|
| 38 |
-
color: #3b82f6 !important;
|
| 39 |
-
}
|
| 40 |
</style>
|
| 41 |
""", unsafe_allow_html=True)
|
| 42 |
|
| 43 |
-
# STEP 4: APP TITLES
|
| 44 |
st.title("π SAAD AI ACADEMY")
|
| 45 |
-
st.caption("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
system_instruction = (
|
| 50 |
-
"You are a Senior Mathematics Professor.
|
| 51 |
-
"
|
| 52 |
-
"
|
| 53 |
-
"
|
| 54 |
-
"
|
| 55 |
)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
payload = {
|
| 60 |
-
"inputs": full_prompt,
|
| 61 |
-
"parameters": {"max_new_tokens": 1000, "temperature": 0.1, "return_full_text": False},
|
| 62 |
-
"options": {"wait_for_model": True}
|
| 63 |
-
}
|
| 64 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
| 65 |
-
return response.json()
|
| 66 |
-
|
| 67 |
-
# STEP 6: CHAT INTERFACE
|
| 68 |
-
user_query = st.chat_input("Ask a B.Sc. level math question (Analysis, Algebra, Calculus...)")
|
| 69 |
|
| 70 |
if user_query:
|
| 71 |
with st.chat_message("user"):
|
| 72 |
st.write(user_query)
|
| 73 |
|
| 74 |
with st.chat_message("assistant"):
|
| 75 |
-
with st.spinner("
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
st.download_button("π© Download Proof", response, file_name="math_solution.txt")
|
| 81 |
-
else:
|
| 82 |
-
st.error("API error or timeout. Please check your internet or try again in 10 seconds.")
|
| 83 |
|
| 84 |
-
#
|
| 85 |
st.sidebar.markdown(f"### π Student Profile")
|
| 86 |
st.sidebar.write("**Name:** Almuyed Saad")
|
| 87 |
st.sidebar.write("**Inst:** SUST")
|
| 88 |
-
st.sidebar.write("**Major:** Mathematics")
|
| 89 |
-
st.sidebar.divider()
|
| 90 |
-
st.sidebar.info("Module: Universal Logic Engine V5.1")
|
|
|
|
| 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 Local Mathematics Engine | SUST Logic System")
|
| 20 |
+
|
| 21 |
+
# 3. LOCAL MODEL LOADING (Slow start, but 100% stable)
|
| 22 |
+
@st.cache_resource
|
| 23 |
+
def load_engine():
|
| 24 |
+
# We use the same model, but load it locally to avoid API errors
|
| 25 |
+
model_id = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
|
| 26 |
+
return pipeline("text-generation", model=model_id, device=-1, torch_dtype=torch.float32)
|
| 27 |
|
| 28 |
+
with st.spinner("β³ Loading Math Engine into memory... This takes 1-2 minutes on first run."):
|
| 29 |
+
pipe = load_engine()
|
| 30 |
+
|
| 31 |
+
# 4. UNIVERSAL LOGIC INSTRUCTIONS
|
| 32 |
system_instruction = (
|
| 33 |
+
"You are a Senior Mathematics Professor. Solve every problem with absolute rigor.\n"
|
| 34 |
+
"1. State the mathematical domain.\n"
|
| 35 |
+
"2. Check all constraints/conditions.\n"
|
| 36 |
+
"3. Show every algebraic step using LaTeX ($...$).\n"
|
| 37 |
+
"4. Bold the final result."
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# 5. INTERFACE
|
| 41 |
+
user_query = st.chat_input("Ask a B.Sc. level math question...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
if user_query:
|
| 44 |
with st.chat_message("user"):
|
| 45 |
st.write(user_query)
|
| 46 |
|
| 47 |
with st.chat_message("assistant"):
|
| 48 |
+
with st.spinner("π§ Thinking..."):
|
| 49 |
+
prompt = f"<|im_start|>system\n{system_instruction}<|im_end|>\n<|im_start|>user\n{user_query}<|im_end|>\n<|im_start|>assistant\n"
|
| 50 |
+
result = pipe(prompt, max_new_tokens=800, temperature=0.1, do_sample=True)
|
| 51 |
+
response = result[0]['generated_text'].split("<|im_start|>assistant\n")[-1]
|
| 52 |
+
st.markdown(response)
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# 6. SIDEBAR
|
| 55 |
st.sidebar.markdown(f"### π Student Profile")
|
| 56 |
st.sidebar.write("**Name:** Almuyed Saad")
|
| 57 |
st.sidebar.write("**Inst:** SUST")
|
| 58 |
+
st.sidebar.write("**Major:** Mathematics")
|
|
|
|
|
|