Spaces:
Sleeping
Add interactive algebra solver module and fix worksheet parsing
Browse files- Created comprehensive algebra_solver.py module with functions for:
- Speed calculations (distance/time)
- Volume calculations (mass/density)
- Linear equation solving (ax + b = c)
- Quadratic equation solving (h = coefficient * t²)
- Answer validation with tolerance checking
- Enhanced worksheet fallback content with:
- Interactive input fields for all problem types
- JavaScript solve functions for real-time calculation
- Visual feedback with color changes on solve
- Placeholder values matching worksheet examples
- Fixed HTML parsing issues:
- Cleaned up malformed image tags
- Fixed broken button attributes
- Improved regex patterns for content styling
- Enhanced fallback content robustness
- Integrated algebra solver into main app:
- Added solver import and initialization
- Created solve_algebra_problem() function
- Enhanced worksheet with interactive elements
- Maintained answer key hiding functionality
All worksheet problems now have working solve buttons that calculate
correct answers in real-time with proper formatting and visual feedback.
- algebra_solver.py +231 -0
- app.py +100 -6
|
@@ -0,0 +1,231 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Algebra Problem Solver Module
|
| 3 |
+
Solves the types of algebra problems found in the science worksheet
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import math
|
| 7 |
+
from typing import Dict, List, Tuple, Union
|
| 8 |
+
|
| 9 |
+
class AlgebraSolver:
|
| 10 |
+
"""Solves various algebra problems from science worksheets"""
|
| 11 |
+
|
| 12 |
+
def __init__(self):
|
| 13 |
+
self.solutions = {}
|
| 14 |
+
|
| 15 |
+
def solve_speed_problem(self, distance: float, time: float) -> Dict[str, Union[float, str]]:
|
| 16 |
+
"""
|
| 17 |
+
Solve speed calculation problems: Speed = Distance / Time
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
distance: Distance in km
|
| 21 |
+
time: Time in hours
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
Dictionary with solution details
|
| 25 |
+
"""
|
| 26 |
+
try:
|
| 27 |
+
speed = distance / time
|
| 28 |
+
return {
|
| 29 |
+
"problem_type": "speed",
|
| 30 |
+
"formula": "Speed = Distance / Time",
|
| 31 |
+
"calculation": f"{distance} / {time}",
|
| 32 |
+
"answer": round(speed, 2),
|
| 33 |
+
"unit": "km/h",
|
| 34 |
+
"solution": f"Speed = {distance} km / {time} h = {speed} km/h"
|
| 35 |
+
}
|
| 36 |
+
except ZeroDivisionError:
|
| 37 |
+
return {"error": "Time cannot be zero"}
|
| 38 |
+
|
| 39 |
+
def solve_volume_problem(self, mass: float, density: float) -> Dict[str, Union[float, str]]:
|
| 40 |
+
"""
|
| 41 |
+
Solve volume calculation problems: Volume = Mass / Density
|
| 42 |
+
|
| 43 |
+
Args:
|
| 44 |
+
mass: Mass in grams
|
| 45 |
+
density: Density in g/cm³
|
| 46 |
+
|
| 47 |
+
Returns:
|
| 48 |
+
Dictionary with solution details
|
| 49 |
+
"""
|
| 50 |
+
try:
|
| 51 |
+
volume = mass / density
|
| 52 |
+
return {
|
| 53 |
+
"problem_type": "volume",
|
| 54 |
+
"formula": "Volume = Mass / Density",
|
| 55 |
+
"calculation": f"{mass} / {density}",
|
| 56 |
+
"answer": round(volume, 2),
|
| 57 |
+
"unit": "cm³",
|
| 58 |
+
"solution": f"Volume = {mass} g / {density} g/cm³ = {volume} cm³"
|
| 59 |
+
}
|
| 60 |
+
except ZeroDivisionError:
|
| 61 |
+
return {"error": "Density cannot be zero"}
|
| 62 |
+
|
| 63 |
+
def solve_linear_equation(self, a: float, b: float, c: float) -> Dict[str, Union[float, str]]:
|
| 64 |
+
"""
|
| 65 |
+
Solve linear equations of the form: ax + b = c
|
| 66 |
+
|
| 67 |
+
Args:
|
| 68 |
+
a: Coefficient of x
|
| 69 |
+
b: Constant term
|
| 70 |
+
c: Right side constant
|
| 71 |
+
|
| 72 |
+
Returns:
|
| 73 |
+
Dictionary with solution details
|
| 74 |
+
"""
|
| 75 |
+
try:
|
| 76 |
+
if a == 0:
|
| 77 |
+
return {"error": "Coefficient 'a' cannot be zero"}
|
| 78 |
+
|
| 79 |
+
x = (c - b) / a
|
| 80 |
+
return {
|
| 81 |
+
"problem_type": "linear_equation",
|
| 82 |
+
"equation": f"{a}x + {b} = {c}",
|
| 83 |
+
"steps": [
|
| 84 |
+
f"{a}x + {b} = {c}",
|
| 85 |
+
f"{a}x = {c} - {b}",
|
| 86 |
+
f"{a}x = {c - b}",
|
| 87 |
+
f"x = {c - b} / {a}",
|
| 88 |
+
f"x = {x}"
|
| 89 |
+
],
|
| 90 |
+
"answer": round(x, 2),
|
| 91 |
+
"solution": f"x = {x}"
|
| 92 |
+
}
|
| 93 |
+
except Exception as e:
|
| 94 |
+
return {"error": str(e)}
|
| 95 |
+
|
| 96 |
+
def solve_quadratic_equation(self, h: float, coefficient: float = 4.9) -> Dict[str, Union[float, str]]:
|
| 97 |
+
"""
|
| 98 |
+
Solve quadratic equations of the form: h = coefficient * t²
|
| 99 |
+
|
| 100 |
+
Args:
|
| 101 |
+
h: Height value
|
| 102 |
+
coefficient: Coefficient (default 4.9 for gravity)
|
| 103 |
+
|
| 104 |
+
Returns:
|
| 105 |
+
Dictionary with solution details
|
| 106 |
+
"""
|
| 107 |
+
try:
|
| 108 |
+
if coefficient <= 0:
|
| 109 |
+
return {"error": "Coefficient must be positive"}
|
| 110 |
+
|
| 111 |
+
if h < 0:
|
| 112 |
+
return {"error": "Height cannot be negative"}
|
| 113 |
+
|
| 114 |
+
t_squared = h / coefficient
|
| 115 |
+
t = math.sqrt(t_squared)
|
| 116 |
+
|
| 117 |
+
return {
|
| 118 |
+
"problem_type": "quadratic_equation",
|
| 119 |
+
"equation": f"h = {coefficient} * t²",
|
| 120 |
+
"given": f"h = {h}",
|
| 121 |
+
"steps": [
|
| 122 |
+
f"{h} = {coefficient} * t²",
|
| 123 |
+
f"t² = {h} / {coefficient}",
|
| 124 |
+
f"t² = {t_squared}",
|
| 125 |
+
f"t = √{t_squared}",
|
| 126 |
+
f"t = {t}"
|
| 127 |
+
],
|
| 128 |
+
"answer": round(t, 2),
|
| 129 |
+
"unit": "s",
|
| 130 |
+
"solution": f"t = {t} s"
|
| 131 |
+
}
|
| 132 |
+
except Exception as e:
|
| 133 |
+
return {"error": str(e)}
|
| 134 |
+
|
| 135 |
+
def solve_worksheet_problems(self) -> Dict[str, Dict]:
|
| 136 |
+
"""
|
| 137 |
+
Solve all the problems from the worksheet example
|
| 138 |
+
|
| 139 |
+
Returns:
|
| 140 |
+
Dictionary with all solutions
|
| 141 |
+
"""
|
| 142 |
+
solutions = {}
|
| 143 |
+
|
| 144 |
+
# Practice Problems
|
| 145 |
+
solutions["speed_bike"] = self.solve_speed_problem(45, 1.5)
|
| 146 |
+
solutions["volume_density"] = self.solve_volume_problem(30, 5)
|
| 147 |
+
|
| 148 |
+
# Word Problems
|
| 149 |
+
solutions["chemistry_linear"] = self.solve_linear_equation(3, 5, 20)
|
| 150 |
+
solutions["physics_quadratic"] = self.solve_quadratic_equation(19.6, 4.9)
|
| 151 |
+
|
| 152 |
+
return solutions
|
| 153 |
+
|
| 154 |
+
def validate_student_answer(self, problem_type: str, student_answer: float,
|
| 155 |
+
problem_data: Dict) -> Dict[str, Union[bool, str, float]]:
|
| 156 |
+
"""
|
| 157 |
+
Validate student's answer against the correct solution
|
| 158 |
+
|
| 159 |
+
Args:
|
| 160 |
+
problem_type: Type of problem (speed, volume, linear, quadratic)
|
| 161 |
+
student_answer: Student's numerical answer
|
| 162 |
+
problem_data: Problem parameters
|
| 163 |
+
|
| 164 |
+
Returns:
|
| 165 |
+
Validation result
|
| 166 |
+
"""
|
| 167 |
+
try:
|
| 168 |
+
if problem_type == "speed":
|
| 169 |
+
correct = self.solve_speed_problem(problem_data["distance"], problem_data["time"])
|
| 170 |
+
elif problem_type == "volume":
|
| 171 |
+
correct = self.solve_volume_problem(problem_data["mass"], problem_data["density"])
|
| 172 |
+
elif problem_type == "linear":
|
| 173 |
+
correct = self.solve_linear_equation(problem_data["a"], problem_data["b"], problem_data["c"])
|
| 174 |
+
elif problem_type == "quadratic":
|
| 175 |
+
correct = self.solve_quadratic_equation(problem_data["h"], problem_data.get("coefficient", 4.9))
|
| 176 |
+
else:
|
| 177 |
+
return {"error": "Unknown problem type"}
|
| 178 |
+
|
| 179 |
+
if "error" in correct:
|
| 180 |
+
return correct
|
| 181 |
+
|
| 182 |
+
tolerance = 0.01 # Allow small rounding differences
|
| 183 |
+
is_correct = abs(student_answer - correct["answer"]) < tolerance
|
| 184 |
+
|
| 185 |
+
return {
|
| 186 |
+
"is_correct": is_correct,
|
| 187 |
+
"student_answer": student_answer,
|
| 188 |
+
"correct_answer": correct["answer"],
|
| 189 |
+
"difference": abs(student_answer - correct["answer"]),
|
| 190 |
+
"feedback": "Correct!" if is_correct else f"Try again. The correct answer is {correct['answer']}"
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
except Exception as e:
|
| 194 |
+
return {"error": str(e)}
|
| 195 |
+
|
| 196 |
+
def test_algebra_solver():
|
| 197 |
+
"""Test the algebra solver with worksheet problems"""
|
| 198 |
+
solver = AlgebraSolver()
|
| 199 |
+
|
| 200 |
+
print("🧮 Testing Algebra Solver Module")
|
| 201 |
+
print("=" * 50)
|
| 202 |
+
|
| 203 |
+
# Test all worksheet problems
|
| 204 |
+
solutions = solver.solve_worksheet_problems()
|
| 205 |
+
|
| 206 |
+
for problem_name, solution in solutions.items():
|
| 207 |
+
print(f"\n📝 {problem_name.replace('_', ' ').title()}:")
|
| 208 |
+
if "error" in solution:
|
| 209 |
+
print(f"❌ Error: {solution['error']}")
|
| 210 |
+
else:
|
| 211 |
+
print(f"✅ {solution['solution']}")
|
| 212 |
+
if "steps" in solution:
|
| 213 |
+
print(" Steps:")
|
| 214 |
+
for step in solution["steps"]:
|
| 215 |
+
print(f" • {step}")
|
| 216 |
+
|
| 217 |
+
# Test validation
|
| 218 |
+
print(f"\n🔍 Testing Answer Validation:")
|
| 219 |
+
|
| 220 |
+
# Test correct answer
|
| 221 |
+
validation = solver.validate_student_answer("speed", 30.0, {"distance": 45, "time": 1.5})
|
| 222 |
+
print(f"Student answer 30.0 for speed problem: {validation}")
|
| 223 |
+
|
| 224 |
+
# Test incorrect answer
|
| 225 |
+
validation = solver.validate_student_answer("speed", 25.0, {"distance": 45, "time": 1.5})
|
| 226 |
+
print(f"Student answer 25.0 for speed problem: {validation}")
|
| 227 |
+
|
| 228 |
+
return solutions
|
| 229 |
+
|
| 230 |
+
if __name__ == "__main__":
|
| 231 |
+
test_algebra_solver()
|
|
@@ -7,6 +7,7 @@ import sqlite3
|
|
| 7 |
from datetime import datetime
|
| 8 |
from email.mime.text import MIMEText
|
| 9 |
from email.mime.multipart import MIMEMultipart
|
|
|
|
| 10 |
|
| 11 |
# Get Hugging Face token from environment variable
|
| 12 |
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
|
@@ -301,10 +302,12 @@ Use algebra to find the missing value in each science scenario.
|
|
| 301 |
|
| 302 |
**Now you try:**
|
| 303 |
1. A bike travels **45 km** in **1.5 hours**. What is its speed?
|
| 304 |
-
Speed = <input type="text" style="border-bottom: 2px solid #3498db; border-top: none; border-left: none; border-right: none; width: 60px; text-align: center;"> / <input type="text" style="border-bottom: 2px solid #3498db; border-top: none; border-left: none; border-right: none; width: 60px; text-align: center;"> = <span style="background: #e8f4fd; border: 2px solid #3498db; padding: 4px 8px; border-radius: 4px; font-weight: bold; color: #2c3e50;">[ANSWER: _____]</span>
|
|
|
|
| 305 |
|
| 306 |
2. The density of an object is **5 g/cm³**, and its mass is **30 g**. What is its volume?
|
| 307 |
-
(Density = Mass / Volume) Volume = <input type="text" style="border-bottom: 2px solid #3498db; border-top: none; border-left: none; border-right: none; width: 60px; text-align: center;"> / <input type="text" style="border-bottom: 2px solid #3498db; border-top: none; border-left: none; border-right: none; width: 60px; text-align: center;"> = <span style="background: #e8f4fd; border: 2px solid #3498db; padding: 4px 8px; border-radius: 4px; font-weight: bold; color: #2c3e50;">[ANSWER: _____]</span>
|
|
|
|
| 308 |
|
| 309 |
✅ **Check your work:** <input type="checkbox" id="work-check" style="margin-right: 8px; transform: scale(1.2);">
|
| 310 |
|
|
@@ -318,12 +321,14 @@ Use algebra to find the missing value in each science scenario.
|
|
| 318 |
Read each problem, write an equation, and solve.
|
| 319 |
|
| 320 |
1. **Chemistry:** A reaction produces **3x + 5 = 20** grams of a substance. Find **x**.
|
| 321 |
-
Equation: <input type="text" style="border-bottom: 2px solid #3498db; border-top: none; border-left: none; border-right: none; width:
|
| 322 |
-
Solution: x = <span style="background: #e8f4fd; border: 2px solid #3498db; padding: 4px 8px; border-radius: 4px; font-weight: bold; color: #2c3e50;">[ANSWER: _____]</span>
|
|
|
|
| 323 |
|
| 324 |
2. **Physics:** A ball falls from a height **h = 4.9t²** meters. If **h = 19.6 m**, find **t**.
|
| 325 |
-
Equation: <input type="text" style="border-bottom: 2px solid #3498db; border-top: none; border-left: none; border-right: none; width:
|
| 326 |
-
Solution: t = <span style="background: #e8f4fd; border: 2px solid #3498db; padding: 4px 8px; border-radius: 4px; font-weight: bold; color: #2c3e50;">[ANSWER: _____]</span>
|
|
|
|
| 327 |
|
| 328 |
✅ **Show your steps:** <input type="checkbox" id="steps-check" style="margin-right: 8px; transform: scale(1.2);">
|
| 329 |
|
|
@@ -403,6 +408,43 @@ function selectRating(rating) {{
|
|
| 403 |
event.target.style.border = '2px solid #2c3e50';
|
| 404 |
event.target.style.color = 'white';
|
| 405 |
}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
</script>"""
|
| 407 |
else:
|
| 408 |
# Generic fallback for other content types
|
|
@@ -1912,6 +1954,42 @@ def convert_math_to_html(text):
|
|
| 1912 |
|
| 1913 |
return text
|
| 1914 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1915 |
def apply_content_styling(content, content_type):
|
| 1916 |
"""Apply simple and robust styling to educational content"""
|
| 1917 |
import re
|
|
@@ -1924,6 +2002,19 @@ def apply_content_styling(content, content_type):
|
|
| 1924 |
content = re.sub(r'="[^"]*padding: [^"]*"[^"]*"', '', content)
|
| 1925 |
content = re.sub(r'="[^"]*margin: [^"]*"[^"]*"', '', content)
|
| 1926 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1927 |
# Simple styling approach - just wrap in a clean container
|
| 1928 |
styled_content = f"""
|
| 1929 |
<div style="
|
|
@@ -2175,6 +2266,9 @@ def chat_with_albert(user_message, user_name, age_group, chat_history):
|
|
| 2175 |
# Initialize database on startup
|
| 2176 |
init_database()
|
| 2177 |
|
|
|
|
|
|
|
|
|
|
| 2178 |
# Create Gradio interface with custom CSS
|
| 2179 |
with gr.Blocks(
|
| 2180 |
title="BrightMind AI - Educational Content Generator",
|
|
|
|
| 7 |
from datetime import datetime
|
| 8 |
from email.mime.text import MIMEText
|
| 9 |
from email.mime.multipart import MIMEMultipart
|
| 10 |
+
from algebra_solver import AlgebraSolver
|
| 11 |
|
| 12 |
# Get Hugging Face token from environment variable
|
| 13 |
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
|
|
|
| 302 |
|
| 303 |
**Now you try:**
|
| 304 |
1. A bike travels **45 km** in **1.5 hours**. What is its speed?
|
| 305 |
+
Speed = <input type="text" id="distance1" placeholder="45" style="border-bottom: 2px solid #3498db; border-top: none; border-left: none; border-right: none; width: 60px; text-align: center;"> / <input type="text" id="time1" placeholder="1.5" style="border-bottom: 2px solid #3498db; border-top: none; border-left: none; border-right: none; width: 60px; text-align: center;"> = <span id="speed-result" style="background: #e8f4fd; border: 2px solid #3498db; padding: 4px 8px; border-radius: 4px; font-weight: bold; color: #2c3e50;">[ANSWER: _____]</span>
|
| 306 |
+
<button onclick="solveSpeed()" style="background: #3498db; color: white; border: none; padding: 5px 10px; border-radius: 4px; margin-left: 10px; cursor: pointer;">Solve</button>
|
| 307 |
|
| 308 |
2. The density of an object is **5 g/cm³**, and its mass is **30 g**. What is its volume?
|
| 309 |
+
(Density = Mass / Volume) Volume = <input type="text" id="mass1" placeholder="30" style="border-bottom: 2px solid #3498db; border-top: none; border-left: none; border-right: none; width: 60px; text-align: center;"> / <input type="text" id="density1" placeholder="5" style="border-bottom: 2px solid #3498db; border-top: none; border-left: none; border-right: none; width: 60px; text-align: center;"> = <span id="volume-result" style="background: #e8f4fd; border: 2px solid #3498db; padding: 4px 8px; border-radius: 4px; font-weight: bold; color: #2c3e50;">[ANSWER: _____]</span>
|
| 310 |
+
<button onclick="solveVolume()" style="background: #3498db; color: white; border: none; padding: 5px 10px; border-radius: 4px; margin-left: 10px; cursor: pointer;">Solve</button>
|
| 311 |
|
| 312 |
✅ **Check your work:** <input type="checkbox" id="work-check" style="margin-right: 8px; transform: scale(1.2);">
|
| 313 |
|
|
|
|
| 321 |
Read each problem, write an equation, and solve.
|
| 322 |
|
| 323 |
1. **Chemistry:** A reaction produces **3x + 5 = 20** grams of a substance. Find **x**.
|
| 324 |
+
Equation: <input type="text" id="a1" placeholder="3" style="border-bottom: 2px solid #3498db; border-top: none; border-left: none; border-right: none; width: 40px; text-align: center;">x + <input type="text" id="b1" placeholder="5" style="border-bottom: 2px solid #3498db; border-top: none; border-left: none; border-right: none; width: 40px; text-align: center;"> = <input type="text" id="c1" placeholder="20" style="border-bottom: 2px solid #3498db; border-top: none; border-left: none; border-right: none; width: 40px; text-align: center;">
|
| 325 |
+
Solution: x = <span id="linear-result" style="background: #e8f4fd; border: 2px solid #3498db; padding: 4px 8px; border-radius: 4px; font-weight: bold; color: #2c3e50;">[ANSWER: _____]</span>
|
| 326 |
+
<button onclick="solveLinear()" style="background: #3498db; color: white; border: none; padding: 5px 10px; border-radius: 4px; margin-left: 10px; cursor: pointer;">Solve</button>
|
| 327 |
|
| 328 |
2. **Physics:** A ball falls from a height **h = 4.9t²** meters. If **h = 19.6 m**, find **t**.
|
| 329 |
+
Equation: h = <input type="text" id="coeff1" placeholder="4.9" style="border-bottom: 2px solid #3498db; border-top: none; border-left: none; border-right: none; width: 40px; text-align: center;">t², h = <input type="text" id="h1" placeholder="19.6" style="border-bottom: 2px solid #3498db; border-top: none; border-left: none; border-right: none; width: 50px; text-align: center;"> m
|
| 330 |
+
Solution: t = <span id="quadratic-result" style="background: #e8f4fd; border: 2px solid #3498db; padding: 4px 8px; border-radius: 4px; font-weight: bold; color: #2c3e50;">[ANSWER: _____]</span>
|
| 331 |
+
<button onclick="solveQuadratic()" style="background: #3498db; color: white; border: none; padding: 5px 10px; border-radius: 4px; margin-left: 10px; cursor: pointer;">Solve</button>
|
| 332 |
|
| 333 |
✅ **Show your steps:** <input type="checkbox" id="steps-check" style="margin-right: 8px; transform: scale(1.2);">
|
| 334 |
|
|
|
|
| 408 |
event.target.style.border = '2px solid #2c3e50';
|
| 409 |
event.target.style.color = 'white';
|
| 410 |
}}
|
| 411 |
+
|
| 412 |
+
function solveSpeed() {{
|
| 413 |
+
const distance = parseFloat(document.getElementById('distance1').value) || 45;
|
| 414 |
+
const time = parseFloat(document.getElementById('time1').value) || 1.5;
|
| 415 |
+
const speed = distance / time;
|
| 416 |
+
document.getElementById('speed-result').textContent = '[ANSWER: ' + speed.toFixed(1) + ' km/h]';
|
| 417 |
+
document.getElementById('speed-result').style.background = '#d4edda';
|
| 418 |
+
document.getElementById('speed-result').style.border = '2px solid #28a745';
|
| 419 |
+
}}
|
| 420 |
+
|
| 421 |
+
function solveVolume() {{
|
| 422 |
+
const mass = parseFloat(document.getElementById('mass1').value) || 30;
|
| 423 |
+
const density = parseFloat(document.getElementById('density1').value) || 5;
|
| 424 |
+
const volume = mass / density;
|
| 425 |
+
document.getElementById('volume-result').textContent = '[ANSWER: ' + volume.toFixed(1) + ' cm³]';
|
| 426 |
+
document.getElementById('volume-result').style.background = '#d4edda';
|
| 427 |
+
document.getElementById('volume-result').style.border = '2px solid #28a745';
|
| 428 |
+
}}
|
| 429 |
+
|
| 430 |
+
function solveLinear() {{
|
| 431 |
+
const a = parseFloat(document.getElementById('a1').value) || 3;
|
| 432 |
+
const b = parseFloat(document.getElementById('b1').value) || 5;
|
| 433 |
+
const c = parseFloat(document.getElementById('c1').value) || 20;
|
| 434 |
+
const x = (c - b) / a;
|
| 435 |
+
document.getElementById('linear-result').textContent = '[ANSWER: x = ' + x.toFixed(1) + ']';
|
| 436 |
+
document.getElementById('linear-result').style.background = '#d4edda';
|
| 437 |
+
document.getElementById('linear-result').style.border = '2px solid #28a745';
|
| 438 |
+
}}
|
| 439 |
+
|
| 440 |
+
function solveQuadratic() {{
|
| 441 |
+
const h = parseFloat(document.getElementById('h1').value) || 19.6;
|
| 442 |
+
const coefficient = parseFloat(document.getElementById('coeff1').value) || 4.9;
|
| 443 |
+
const t = Math.sqrt(h / coefficient);
|
| 444 |
+
document.getElementById('quadratic-result').textContent = '[ANSWER: t = ' + t.toFixed(1) + ' s]';
|
| 445 |
+
document.getElementById('quadratic-result').style.background = '#d4edda';
|
| 446 |
+
document.getElementById('quadratic-result').style.border = '2px solid #28a745';
|
| 447 |
+
}}
|
| 448 |
</script>"""
|
| 449 |
else:
|
| 450 |
# Generic fallback for other content types
|
|
|
|
| 1954 |
|
| 1955 |
return text
|
| 1956 |
|
| 1957 |
+
def solve_algebra_problem(problem_type, *args):
|
| 1958 |
+
"""Solve algebra problems interactively"""
|
| 1959 |
+
try:
|
| 1960 |
+
if problem_type == "speed":
|
| 1961 |
+
distance, time = args
|
| 1962 |
+
result = algebra_solver.solve_speed_problem(float(distance), float(time))
|
| 1963 |
+
elif problem_type == "volume":
|
| 1964 |
+
mass, density = args
|
| 1965 |
+
result = algebra_solver.solve_volume_problem(float(mass), float(density))
|
| 1966 |
+
elif problem_type == "linear":
|
| 1967 |
+
a, b, c = args
|
| 1968 |
+
result = algebra_solver.solve_linear_equation(float(a), float(b), float(c))
|
| 1969 |
+
elif problem_type == "quadratic":
|
| 1970 |
+
h, coefficient = args
|
| 1971 |
+
result = algebra_solver.solve_quadratic_equation(float(h), float(coefficient))
|
| 1972 |
+
else:
|
| 1973 |
+
return "❌ Unknown problem type"
|
| 1974 |
+
|
| 1975 |
+
if "error" in result:
|
| 1976 |
+
return f"❌ Error: {result['error']}"
|
| 1977 |
+
|
| 1978 |
+
# Format the solution nicely
|
| 1979 |
+
solution_text = f"✅ **{result['solution']}**\n\n"
|
| 1980 |
+
if "steps" in result:
|
| 1981 |
+
solution_text += "**Steps:**\n"
|
| 1982 |
+
for i, step in enumerate(result["steps"], 1):
|
| 1983 |
+
solution_text += f"{i}. {step}\n"
|
| 1984 |
+
elif "formula" in result:
|
| 1985 |
+
solution_text += f"**Formula:** {result['formula']}\n"
|
| 1986 |
+
solution_text += f"**Calculation:** {result['calculation']}\n"
|
| 1987 |
+
|
| 1988 |
+
return solution_text
|
| 1989 |
+
|
| 1990 |
+
except Exception as e:
|
| 1991 |
+
return f"❌ Error solving problem: {str(e)}"
|
| 1992 |
+
|
| 1993 |
def apply_content_styling(content, content_type):
|
| 1994 |
"""Apply simple and robust styling to educational content"""
|
| 1995 |
import re
|
|
|
|
| 2002 |
content = re.sub(r'="[^"]*padding: [^"]*"[^"]*"', '', content)
|
| 2003 |
content = re.sub(r'="[^"]*margin: [^"]*"[^"]*"', '', content)
|
| 2004 |
|
| 2005 |
+
# Fix broken image tags and malformed HTML
|
| 2006 |
+
content = re.sub(r'<div style>.*?</div>', '', content)
|
| 2007 |
+
content = re.sub(r'stylelazy">', 'loading="lazy">', content)
|
| 2008 |
+
content = re.sub(r'<img[^>]*style[^>]*>', '', content)
|
| 2009 |
+
|
| 2010 |
+
# Fix malformed button attributes
|
| 2011 |
+
content = re.sub(r'styleselectRating\(\d+\)"', '', content)
|
| 2012 |
+
content = re.sub(r'styletext"', '', content)
|
| 2013 |
+
content = re.sub(r'style="[^"]*"[^"]*style="[^"]*"', 'style="margin-right: 8px; transform: scale(1.2);"', content)
|
| 2014 |
+
|
| 2015 |
+
# Clean up any remaining malformed attributes
|
| 2016 |
+
content = re.sub(r'="[^"]*"[^"]*"', '', content)
|
| 2017 |
+
|
| 2018 |
# Simple styling approach - just wrap in a clean container
|
| 2019 |
styled_content = f"""
|
| 2020 |
<div style="
|
|
|
|
| 2266 |
# Initialize database on startup
|
| 2267 |
init_database()
|
| 2268 |
|
| 2269 |
+
# Initialize algebra solver
|
| 2270 |
+
algebra_solver = AlgebraSolver()
|
| 2271 |
+
|
| 2272 |
# Create Gradio interface with custom CSS
|
| 2273 |
with gr.Blocks(
|
| 2274 |
title="BrightMind AI - Educational Content Generator",
|