Update app.py
Browse files
app.py
CHANGED
|
@@ -645,6 +645,99 @@ def test_calculate_titre():
|
|
| 645 |
# -----------------------------
|
| 646 |
|
| 647 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 648 |
with gr.Blocks() as demo:
|
| 649 |
gr.Markdown("# Nigerian AI Chemistry Laboratory Tutor")
|
| 650 |
gr.Markdown(
|
|
|
|
| 645 |
# -----------------------------
|
| 646 |
|
| 647 |
|
| 648 |
+
def calculate_concentration(
|
| 649 |
+
known_concentration,
|
| 650 |
+
known_volume,
|
| 651 |
+
unknown_volume,
|
| 652 |
+
unknown_coefficient,
|
| 653 |
+
known_coefficient
|
| 654 |
+
):
|
| 655 |
+
"""Calculate unknown concentration using reaction stoichiometry."""
|
| 656 |
+
|
| 657 |
+
try:
|
| 658 |
+
ck = float(known_concentration)
|
| 659 |
+
vk = float(known_volume)
|
| 660 |
+
vu = float(unknown_volume)
|
| 661 |
+
cu = float(unknown_coefficient)
|
| 662 |
+
ck_coeff = float(known_coefficient)
|
| 663 |
+
except (TypeError, ValueError):
|
| 664 |
+
return "Error: Please enter valid numerical values."
|
| 665 |
+
|
| 666 |
+
values = [ck, vk, vu, cu, ck_coeff]
|
| 667 |
+
|
| 668 |
+
if not all(math.isfinite(value) for value in values):
|
| 669 |
+
return "Error: All inputs must be finite numbers."
|
| 670 |
+
|
| 671 |
+
if ck <= 0:
|
| 672 |
+
return "Error: Known concentration must be greater than zero."
|
| 673 |
+
|
| 674 |
+
if vk <= 0 or vu <= 0:
|
| 675 |
+
return "Error: Both volumes must be greater than zero."
|
| 676 |
+
|
| 677 |
+
if (
|
| 678 |
+
cu <= 0 or ck_coeff <= 0
|
| 679 |
+
or not cu.is_integer()
|
| 680 |
+
or not ck_coeff.is_integer()
|
| 681 |
+
):
|
| 682 |
+
return "Error: Reaction coefficients must be positive integers."
|
| 683 |
+
|
| 684 |
+
concentration = (
|
| 685 |
+
ck * vk * cu
|
| 686 |
+
) / (
|
| 687 |
+
vu * ck_coeff
|
| 688 |
+
)
|
| 689 |
+
|
| 690 |
+
return f"Concentration = {concentration:.4f} mol/dm³"
|
| 691 |
+
|
| 692 |
+
|
| 693 |
+
def test_calculate_concentration():
|
| 694 |
+
tests = [
|
| 695 |
+
{
|
| 696 |
+
"name": "HCl and NaOH: 1:1 ratio",
|
| 697 |
+
"inputs": (0.100, 24.30, 25.00, 1, 1),
|
| 698 |
+
"expected": "Concentration = 0.0972"
|
| 699 |
+
},
|
| 700 |
+
{
|
| 701 |
+
"name": "H2SO4 and NaOH: 1:2 ratio",
|
| 702 |
+
"inputs": (0.100, 24.30, 25.00, 1, 2),
|
| 703 |
+
"expected": "Concentration = 0.0486"
|
| 704 |
+
},
|
| 705 |
+
{
|
| 706 |
+
"name": "Zero known concentration",
|
| 707 |
+
"inputs": (0, 24.30, 25.00, 1, 1),
|
| 708 |
+
"expected": "Error"
|
| 709 |
+
},
|
| 710 |
+
{
|
| 711 |
+
"name": "Zero unknown volume",
|
| 712 |
+
"inputs": (0.100, 24.30, 0, 1, 1),
|
| 713 |
+
"expected": "Error"
|
| 714 |
+
},
|
| 715 |
+
{
|
| 716 |
+
"name": "Invalid reaction coefficient",
|
| 717 |
+
"inputs": (0.100, 24.30, 25.00, 0, 1),
|
| 718 |
+
"expected": "Error"
|
| 719 |
+
}
|
| 720 |
+
]
|
| 721 |
+
|
| 722 |
+
passed = 0
|
| 723 |
+
|
| 724 |
+
for test in tests:
|
| 725 |
+
result = calculate_concentration(*test["inputs"])
|
| 726 |
+
|
| 727 |
+
if result.startswith(test["expected"]):
|
| 728 |
+
print(f"PASS: {test['name']}")
|
| 729 |
+
passed += 1
|
| 730 |
+
else:
|
| 731 |
+
print(f"FAIL: {test['name']}")
|
| 732 |
+
print(f" Expected to start with: {test['expected']}")
|
| 733 |
+
print(f" Actual result: {result}")
|
| 734 |
+
|
| 735 |
+
print(
|
| 736 |
+
f"\nConcentration calculator: "
|
| 737 |
+
f"{passed}/{len(tests)} tests passed."
|
| 738 |
+
)
|
| 739 |
+
|
| 740 |
+
|
| 741 |
with gr.Blocks() as demo:
|
| 742 |
gr.Markdown("# Nigerian AI Chemistry Laboratory Tutor")
|
| 743 |
gr.Markdown(
|