saad-sust commited on
Commit
6b6c68d
·
verified ·
1 Parent(s): 5b12b5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -13
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 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
 
 
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