Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,90 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
# 1
|
| 5 |
-
st.set_page_config(page_title="SAAD AI ACADEMY
|
| 6 |
|
| 7 |
-
# SECURE TOKEN ACCESS
|
| 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 |
st.markdown("""
|
| 18 |
<style>
|
| 19 |
-
.stApp {
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
</style>
|
| 22 |
""", unsafe_allow_html=True)
|
| 23 |
|
|
|
|
| 24 |
st.title("π SAAD AI ACADEMY")
|
| 25 |
-
st.caption("
|
| 26 |
|
| 27 |
-
#
|
|
|
|
| 28 |
system_instruction = (
|
| 29 |
-
"You are a Senior Mathematics Professor. You must
|
| 30 |
-
"RULE 1 (
|
| 31 |
-
"
|
| 32 |
-
"RULE
|
| 33 |
-
"
|
| 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
|
| 40 |
-
|
| 41 |
payload = {
|
| 42 |
"inputs": full_prompt,
|
| 43 |
-
"parameters": {
|
| 44 |
-
|
| 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 |
-
#
|
| 54 |
-
user_query = st.chat_input("
|
| 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
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 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 |
-
|
| 75 |
-
st.sidebar.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
|
| 4 |
+
# STEP 1: PAGE CONFIG
|
| 5 |
+
st.set_page_config(page_title="SAAD AI ACADEMY", page_icon="π", layout="centered")
|
| 6 |
|
| 7 |
+
# STEP 2: SECURE TOKEN ACCESS
|
| 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 |
+
background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
|
| 22 |
+
color: #ffffff;
|
| 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("Universal B.Sc. Mathematics Engine | SUST Department of Mathematics")
|
| 46 |
|
| 47 |
+
# STEP 5: UNIVERSAL LOGIC ENGINE (The New Instructions)
|
| 48 |
+
# These rules apply to ANY math problem you type.
|
| 49 |
system_instruction = (
|
| 50 |
+
"You are a Senior Mathematics Professor. You must solve every problem with absolute rigor.\n\n"
|
| 51 |
+
"RULE 1 (DEFINITION): Before solving, explicitly state the mathematical domain and the key theorem or definition involved (e.g., 'In Abstract Algebra, we use the First Isomorphism Theorem...').\n"
|
| 52 |
+
"RULE 2 (VALIDATION): Check for necessary conditions or constraints before performing operations (e.g., check if a group is finite, if a function is continuous, or if a divisor is non-zero).\n"
|
| 53 |
+
"RULE 3 (DERIVATION): Show every algebraic step clearly. Do not skip intermediate simplifications. Explain the logic of each transition.\n"
|
| 54 |
+
"RULE 4 (FORMATTING): Use LaTeX ($...$ or $$...$$) for all mathematical expressions. Use bold text for final results."
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
def query_api(prompt_text):
|
| 58 |
+
full_prompt = f"<|im_start|>system\n{system_instruction}<|im_end|>\n<|im_start|>user\n{prompt_text}\nLet's derive the solution step-by-step.<|im_end|>\n<|im_start|>assistant\n"
|
|
|
|
| 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("π Cloud API is analyzing mathematical logic..."):
|
| 76 |
+
data = query_api(user_query)
|
| 77 |
+
if isinstance(data, list) and len(data) > 0:
|
| 78 |
+
response = data[0]['generated_text']
|
| 79 |
+
st.markdown(response)
|
| 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 |
+
# STEP 7: SIDEBAR
|
| 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")
|