Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,38 +16,42 @@ st.markdown("""
|
|
| 16 |
""", unsafe_allow_html=True)
|
| 17 |
|
| 18 |
st.title("📐 SAAD AI ACADEMY")
|
| 19 |
-
st.caption("Stable
|
| 20 |
|
| 21 |
-
# 3. LOCAL
|
| 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("⏳
|
| 29 |
pipe = load_engine()
|
| 30 |
|
| 31 |
-
# 4.
|
|
|
|
| 32 |
system_instruction = (
|
| 33 |
-
"You are a
|
| 34 |
-
"
|
| 35 |
-
"
|
| 36 |
-
"
|
| 37 |
-
"
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
# 5. INTERFACE
|
| 41 |
-
user_query = st.chat_input("
|
| 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("🧠
|
| 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 |
-
|
|
|
|
|
|
|
| 51 |
response = result[0]['generated_text'].split("<|im_start|>assistant\n")[-1]
|
| 52 |
st.markdown(response)
|
| 53 |
|
|
|
|
| 16 |
""", unsafe_allow_html=True)
|
| 17 |
|
| 18 |
st.title("📐 SAAD AI ACADEMY")
|
| 19 |
+
st.caption("Stable Mathematics Engine | SUST Logic System")
|
| 20 |
|
| 21 |
+
# 3. STABLE LOCAL LOADING
|
| 22 |
@st.cache_resource
|
| 23 |
def load_engine():
|
|
|
|
| 24 |
model_id = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
|
| 25 |
+
# Loading locally to ensure 100% uptime and no API timeouts
|
| 26 |
return pipeline("text-generation", model=model_id, device=-1, torch_dtype=torch.float32)
|
| 27 |
|
| 28 |
+
with st.spinner("⏳ Restoring the Stable Engine..."):
|
| 29 |
pipe = load_engine()
|
| 30 |
|
| 31 |
+
# 4. FIXED LOGIC INSTRUCTIONS
|
| 32 |
+
# Added a check so it doesn't give random math for greetings
|
| 33 |
system_instruction = (
|
| 34 |
+
"You are a helpful assistant and a Senior Mathematics Professor. "
|
| 35 |
+
"If the user greets you or asks a general question, respond normally. "
|
| 36 |
+
"ONLY if the user provides a math problem, follow these rules:\n"
|
| 37 |
+
"1. State the math domain.\n"
|
| 38 |
+
"2. Show all steps using LaTeX ($...$).\n"
|
| 39 |
+
"3. Bold the final result."
|
| 40 |
)
|
| 41 |
|
| 42 |
# 5. INTERFACE
|
| 43 |
+
user_query = st.chat_input("Enter your query...")
|
| 44 |
|
| 45 |
if user_query:
|
| 46 |
with st.chat_message("user"):
|
| 47 |
st.write(user_query)
|
| 48 |
|
| 49 |
with st.chat_message("assistant"):
|
| 50 |
+
with st.spinner("🧠 Processing..."):
|
| 51 |
prompt = f"<|im_start|>system\n{system_instruction}<|im_end|>\n<|im_start|>user\n{user_query}<|im_end|>\n<|im_start|>assistant\n"
|
| 52 |
+
|
| 53 |
+
# Fixed generation settings to prevent hallucinations
|
| 54 |
+
result = pipe(prompt, max_new_tokens=800, temperature=0.7, do_sample=True)
|
| 55 |
response = result[0]['generated_text'].split("<|im_start|>assistant\n")[-1]
|
| 56 |
st.markdown(response)
|
| 57 |
|