Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +5 -82
src/streamlit_app.py
CHANGED
|
@@ -7,8 +7,6 @@ from PIL import Image
|
|
| 7 |
import sympy as sp
|
| 8 |
import time
|
| 9 |
import tempfile
|
| 10 |
-
import pickle
|
| 11 |
-
import pickletools
|
| 12 |
|
| 13 |
# Page config
|
| 14 |
st.set_page_config(page_title="🎓 Smart Math Teacher", layout="centered")
|
|
@@ -63,13 +61,8 @@ try:
|
|
| 63 |
if os.path.exists(zip_path):
|
| 64 |
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
| 65 |
zip_ref.extractall(temp_image_folder)
|
| 66 |
-
else:
|
| 67 |
-
st.warning(f"Zip file not found: {zip_path}. Using default images if available.")
|
| 68 |
-
except PermissionError as e:
|
| 69 |
-
st.error(f"Permission error accessing files: {e}")
|
| 70 |
-
st.info("Using default images without extraction.")
|
| 71 |
except Exception as e:
|
| 72 |
-
st.
|
| 73 |
|
| 74 |
if not os.path.exists(dataset_path):
|
| 75 |
st.error(f"Dataset not found: {dataset_path}")
|
|
@@ -108,31 +101,15 @@ if not subset_df.empty and st.session_state.question_index < len(subset_df):
|
|
| 108 |
if pd.notna(question.get('image')):
|
| 109 |
image_name = str(question['image']).strip()
|
| 110 |
image_found = False
|
| 111 |
-
|
| 112 |
-
# Check in temp directory first
|
| 113 |
for root, _, files in os.walk(temp_image_folder):
|
| 114 |
for file in files:
|
| 115 |
if file.lower().startswith(image_name.lower()) or os.path.splitext(file)[0].lower() == image_name.lower():
|
| 116 |
try:
|
| 117 |
-
|
| 118 |
-
st.image(Image.open(img_path), use_column_width=True)
|
| 119 |
image_found = True
|
| 120 |
break
|
| 121 |
-
except
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
if not image_found:
|
| 125 |
-
# Fallback to original image folder
|
| 126 |
-
for root, _, files in os.walk(image_folder):
|
| 127 |
-
for file in files:
|
| 128 |
-
if file.lower().startswith(image_name.lower()) or os.path.splitext(file)[0].lower() == image_name.lower():
|
| 129 |
-
try:
|
| 130 |
-
st.image(Image.open(os.path.join(root, file)), use_column_width=True)
|
| 131 |
-
image_found = True
|
| 132 |
-
break
|
| 133 |
-
except Exception as e:
|
| 134 |
-
st.warning(f"❌ Couldn't load image: {e}")
|
| 135 |
-
|
| 136 |
if not image_found:
|
| 137 |
st.warning("❌ Image not found.")
|
| 138 |
|
|
@@ -170,65 +147,11 @@ elif subset_df.empty:
|
|
| 170 |
else:
|
| 171 |
st.success("🏁 You've completed all questions in this category!")
|
| 172 |
|
| 173 |
-
#
|
| 174 |
-
# ✅ Simple "Ask Any Math Question" Section (no model)
|
| 175 |
-
# -------------------------------------------------------------
|
| 176 |
-
|
| 177 |
-
qa_data = {
|
| 178 |
-
"what is the area of a triangle": "Area = (1/2) × base × height",
|
| 179 |
-
"what is the area of a square": "Area = side²",
|
| 180 |
-
"what is the area of a circle": "Area = π × radius²",
|
| 181 |
-
"perimeter of square": "Perimeter = 4 × side",
|
| 182 |
-
"perimeter of rectangle": "Perimeter = 2 × (length + width)",
|
| 183 |
-
"volume of cube": "Volume = side³",
|
| 184 |
-
"volume of sphere": "Volume = (4/3) × π × radius³",
|
| 185 |
-
"what is pi": "π ≈ 3.14159",
|
| 186 |
-
"how to calculate speed": "Speed = Distance ÷ Time",
|
| 187 |
-
"what is the derivative of x squared": "The derivative of x² is 2x."
|
| 188 |
-
}
|
| 189 |
-
|
| 190 |
-
# Store QA data using pickle (optional demo)
|
| 191 |
-
try:
|
| 192 |
-
with open('qa_data.pkl', 'wb') as f:
|
| 193 |
-
pickle.dump(qa_data, f)
|
| 194 |
-
with open('qa_data.pkl', 'rb') as f:
|
| 195 |
-
pickletools.dis(f)
|
| 196 |
-
except Exception as e:
|
| 197 |
-
st.warning(f"Could not create pickle file: {e}")
|
| 198 |
-
|
| 199 |
-
st.markdown("## 💡 Ask Any Math Question")
|
| 200 |
-
question_input = st.text_input("Ask a question like 'What is the area of a circle?' or 'Derivative of x^2'")
|
| 201 |
-
|
| 202 |
-
if question_input:
|
| 203 |
-
# simple lookup instead of embeddings
|
| 204 |
-
found = None
|
| 205 |
-
for q in qa_data:
|
| 206 |
-
if q in question_input.lower():
|
| 207 |
-
found = qa_data[q]
|
| 208 |
-
break
|
| 209 |
-
|
| 210 |
-
if found:
|
| 211 |
-
st.success(f"🤖 Answer:\n\n**{found}**")
|
| 212 |
-
else:
|
| 213 |
-
try:
|
| 214 |
-
if "derivative" in question_input.lower():
|
| 215 |
-
expr = question_input.lower().split("of")[-1].strip()
|
| 216 |
-
var = sp.symbols('x')
|
| 217 |
-
derivative = sp.diff(sp.sympify(expr.replace("^", "**")), var)
|
| 218 |
-
st.success(f"🧮 Derivative of {expr} is:\n\n**{derivative}**")
|
| 219 |
-
else:
|
| 220 |
-
result = sp.sympify(question_input.replace("^", "**"))
|
| 221 |
-
simplified = sp.simplify(result)
|
| 222 |
-
st.success(f"✅ Answer: {simplified}")
|
| 223 |
-
except Exception as e:
|
| 224 |
-
st.warning(f"⚠️ Couldn't understand the question. Error: {e}")
|
| 225 |
-
|
| 226 |
-
# Cleanup function (optional)
|
| 227 |
def cleanup():
|
| 228 |
import shutil
|
| 229 |
if 'temp_dir' in st.session_state and os.path.exists(st.session_state.temp_dir):
|
| 230 |
shutil.rmtree(st.session_state.temp_dir)
|
| 231 |
|
| 232 |
-
# Register cleanup when app closes
|
| 233 |
import atexit
|
| 234 |
atexit.register(cleanup)
|
|
|
|
| 7 |
import sympy as sp
|
| 8 |
import time
|
| 9 |
import tempfile
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Page config
|
| 12 |
st.set_page_config(page_title="🎓 Smart Math Teacher", layout="centered")
|
|
|
|
| 61 |
if os.path.exists(zip_path):
|
| 62 |
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
| 63 |
zip_ref.extractall(temp_image_folder)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
+
st.warning(f"Error extracting images: {e}")
|
| 66 |
|
| 67 |
if not os.path.exists(dataset_path):
|
| 68 |
st.error(f"Dataset not found: {dataset_path}")
|
|
|
|
| 101 |
if pd.notna(question.get('image')):
|
| 102 |
image_name = str(question['image']).strip()
|
| 103 |
image_found = False
|
|
|
|
|
|
|
| 104 |
for root, _, files in os.walk(temp_image_folder):
|
| 105 |
for file in files:
|
| 106 |
if file.lower().startswith(image_name.lower()) or os.path.splitext(file)[0].lower() == image_name.lower():
|
| 107 |
try:
|
| 108 |
+
st.image(Image.open(os.path.join(root, file)), use_column_width=True)
|
|
|
|
| 109 |
image_found = True
|
| 110 |
break
|
| 111 |
+
except:
|
| 112 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
if not image_found:
|
| 114 |
st.warning("❌ Image not found.")
|
| 115 |
|
|
|
|
| 147 |
else:
|
| 148 |
st.success("🏁 You've completed all questions in this category!")
|
| 149 |
|
| 150 |
+
# Cleanup function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
def cleanup():
|
| 152 |
import shutil
|
| 153 |
if 'temp_dir' in st.session_state and os.path.exists(st.session_state.temp_dir):
|
| 154 |
shutil.rmtree(st.session_state.temp_dir)
|
| 155 |
|
|
|
|
| 156 |
import atexit
|
| 157 |
atexit.register(cleanup)
|