Update app.py
Browse files
app.py
CHANGED
|
@@ -806,6 +806,38 @@ def calculate_acid_base_analysis(
|
|
| 806 |
"for a custom reaction."
|
| 807 |
)
|
| 808 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 809 |
steps = [
|
| 810 |
"STEP 1 — BALANCED CHEMICAL EQUATION",
|
| 811 |
equation,
|
|
@@ -826,7 +858,13 @@ def calculate_acid_base_analysis(
|
|
| 826 |
"Formula: n = C × V",
|
| 827 |
f"n(known) = {ck:.5f} mol/dm³ × {vk / 1000:.5f} dm³",
|
| 828 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 829 |
|
|
|
|
| 830 |
known_moles = ck * vk / 1000
|
| 831 |
unknown_moles = known_moles * unknown_coefficient / known_coefficient
|
| 832 |
unknown_concentration = unknown_moles / (vu / 1000)
|
|
@@ -961,7 +999,8 @@ def calculate_acid_base_analysis(
|
|
| 961 |
|
| 962 |
return "Error: Select a supported calculation type."
|
| 963 |
|
| 964 |
-
|
|
|
|
| 965 |
|
| 966 |
|
| 967 |
def test_calculate_concentration():
|
|
@@ -1078,24 +1117,29 @@ with gr.Blocks() as demo:
|
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
|
|
|
| 1081 |
# Section 3: Acid-base analysis calculator
|
| 1082 |
with gr.Tab("Acid-Base Analysis"):
|
| 1083 |
gr.Markdown(
|
| 1084 |
"### Acid–Base Analysis Calculator\n"
|
| 1085 |
-
"Select
|
| 1086 |
-
"
|
| 1087 |
)
|
| 1088 |
|
| 1089 |
acid_input = gr.Dropdown(
|
| 1090 |
-
choices=[
|
|
|
|
|
|
|
| 1091 |
value="HCl",
|
| 1092 |
-
label="Select
|
| 1093 |
)
|
| 1094 |
|
| 1095 |
base_input = gr.Dropdown(
|
| 1096 |
-
choices=[
|
|
|
|
|
|
|
| 1097 |
value="NaOH",
|
| 1098 |
-
label="Select
|
| 1099 |
)
|
| 1100 |
|
| 1101 |
calculation_input = gr.Dropdown(
|
|
@@ -1106,23 +1150,52 @@ with gr.Blocks() as demo:
|
|
| 1106 |
"Water of crystallisation"
|
| 1107 |
],
|
| 1108 |
value="Molar concentration",
|
| 1109 |
-
label="
|
| 1110 |
)
|
| 1111 |
|
| 1112 |
-
known_solution_input = gr.
|
| 1113 |
choices=["Acid", "Base"],
|
| 1114 |
value="Base",
|
| 1115 |
label="Which solution has the known concentration?"
|
| 1116 |
)
|
| 1117 |
|
| 1118 |
-
gr.
|
| 1119 |
-
|
| 1120 |
-
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1124 |
)
|
| 1125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1126 |
known_concentration_input = gr.Number(
|
| 1127 |
label="Known solution concentration (mol/dm³)",
|
| 1128 |
value=0.100,
|
|
@@ -1148,16 +1221,16 @@ with gr.Blocks() as demo:
|
|
| 1148 |
)
|
| 1149 |
|
| 1150 |
total_prepared_volume_input = gr.Number(
|
| 1151 |
-
label="Total solution
|
| 1152 |
value=250.0,
|
| 1153 |
precision=2
|
| 1154 |
)
|
| 1155 |
|
| 1156 |
-
analysis_button = gr.Button("Calculate")
|
| 1157 |
|
| 1158 |
analysis_output = gr.Textbox(
|
| 1159 |
-
label="
|
| 1160 |
-
lines=
|
| 1161 |
)
|
| 1162 |
|
| 1163 |
analysis_button.click(
|
|
@@ -1171,12 +1244,18 @@ with gr.Blocks() as demo:
|
|
| 1171 |
known_volume_input,
|
| 1172 |
unknown_volume_input,
|
| 1173 |
sample_mass_input,
|
| 1174 |
-
total_prepared_volume_input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1175 |
],
|
| 1176 |
outputs=analysis_output
|
| 1177 |
)
|
| 1178 |
|
| 1179 |
|
|
|
|
| 1180 |
test_calculate_titre()
|
| 1181 |
|
| 1182 |
|
|
|
|
| 806 |
"for a custom reaction."
|
| 807 |
)
|
| 808 |
|
| 809 |
+
|
| 810 |
+
# Indicator guidance based on the selected acid-base pair.
|
| 811 |
+
if acid == "Custom" or base == "Custom":
|
| 812 |
+
indicator_advice = (
|
| 813 |
+
"Custom reaction: indicator suitability must be established "
|
| 814 |
+
"from the titration curve or a validated method."
|
| 815 |
+
)
|
| 816 |
+
elif acid == "CH3COOH" and base in ("NaOH", "KOH"):
|
| 817 |
+
indicator_advice = (
|
| 818 |
+
"Recommended: phenolphthalein. A weak acid–strong base "
|
| 819 |
+
"titration has an alkaline equivalence region."
|
| 820 |
+
)
|
| 821 |
+
elif acid in ("HCl", "H2SO4", "HNO3") and base == "Na2CO3":
|
| 822 |
+
indicator_advice = (
|
| 823 |
+
"Recommended for complete carbonate neutralisation: methyl "
|
| 824 |
+
"orange. Follow the validated practical method."
|
| 825 |
+
)
|
| 826 |
+
elif acid in ("HCl", "H2SO4", "HNO3") and base in (
|
| 827 |
+
"NaOH", "KOH", "Ca(OH)2"
|
| 828 |
+
):
|
| 829 |
+
indicator_advice = (
|
| 830 |
+
"Possible indicators: methyl orange or phenolphthalein. "
|
| 831 |
+
"Both can be suitable for strong acid–strong base titrations; "
|
| 832 |
+
"follow the specified practical method."
|
| 833 |
+
)
|
| 834 |
+
else:
|
| 835 |
+
indicator_advice = (
|
| 836 |
+
"Indicator suitability has not been verified for this pair. "
|
| 837 |
+
"Consult a validated titration method."
|
| 838 |
+
)
|
| 839 |
+
|
| 840 |
+
|
| 841 |
steps = [
|
| 842 |
"STEP 1 — BALANCED CHEMICAL EQUATION",
|
| 843 |
equation,
|
|
|
|
| 858 |
"Formula: n = C × V",
|
| 859 |
f"n(known) = {ck:.5f} mol/dm³ × {vk / 1000:.5f} dm³",
|
| 860 |
]
|
| 861 |
+
steps.extend([
|
| 862 |
+
"",
|
| 863 |
+
"INDICATOR GUIDANCE",
|
| 864 |
+
indicator_advice,
|
| 865 |
+
])
|
| 866 |
|
| 867 |
+
|
| 868 |
known_moles = ck * vk / 1000
|
| 869 |
unknown_moles = known_moles * unknown_coefficient / known_coefficient
|
| 870 |
unknown_concentration = unknown_moles / (vu / 1000)
|
|
|
|
| 999 |
|
| 1000 |
return "Error: Select a supported calculation type."
|
| 1001 |
|
| 1002 |
+
custom_unknown_molar_mass=0,
|
| 1003 |
+
indicator="Automatic recommendation"
|
| 1004 |
|
| 1005 |
|
| 1006 |
def test_calculate_concentration():
|
|
|
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
+
|
| 1121 |
# Section 3: Acid-base analysis calculator
|
| 1122 |
with gr.Tab("Acid-Base Analysis"):
|
| 1123 |
gr.Markdown(
|
| 1124 |
"### Acid–Base Analysis Calculator\n"
|
| 1125 |
+
"Select the reactants and calculation type. The result "
|
| 1126 |
+
"will show the working, units, and indicator guidance."
|
| 1127 |
)
|
| 1128 |
|
| 1129 |
acid_input = gr.Dropdown(
|
| 1130 |
+
choices=[
|
| 1131 |
+
"HCl", "H2SO4", "HNO3", "CH3COOH", "Custom"
|
| 1132 |
+
],
|
| 1133 |
value="HCl",
|
| 1134 |
+
label="Select acid"
|
| 1135 |
)
|
| 1136 |
|
| 1137 |
base_input = gr.Dropdown(
|
| 1138 |
+
choices=[
|
| 1139 |
+
"NaOH", "KOH", "Ca(OH)2", "Na2CO3", "Custom"
|
| 1140 |
+
],
|
| 1141 |
value="NaOH",
|
| 1142 |
+
label="Select base"
|
| 1143 |
)
|
| 1144 |
|
| 1145 |
calculation_input = gr.Dropdown(
|
|
|
|
| 1150 |
"Water of crystallisation"
|
| 1151 |
],
|
| 1152 |
value="Molar concentration",
|
| 1153 |
+
label="Calculation type"
|
| 1154 |
)
|
| 1155 |
|
| 1156 |
+
known_solution_input = gr.Radio(
|
| 1157 |
choices=["Acid", "Base"],
|
| 1158 |
value="Base",
|
| 1159 |
label="Which solution has the known concentration?"
|
| 1160 |
)
|
| 1161 |
|
| 1162 |
+
indicator_input = gr.Dropdown(
|
| 1163 |
+
choices=[
|
| 1164 |
+
"Automatic recommendation",
|
| 1165 |
+
"Phenolphthalein",
|
| 1166 |
+
"Methyl orange",
|
| 1167 |
+
"Methyl red"
|
| 1168 |
+
],
|
| 1169 |
+
value="Automatic recommendation",
|
| 1170 |
+
label="Indicator used / requested"
|
| 1171 |
+
)
|
| 1172 |
+
|
| 1173 |
+
gr.Markdown("### Custom reaction details")
|
| 1174 |
+
custom_equation_input = gr.Textbox(
|
| 1175 |
+
label="Balanced equation (required for a custom reactant)",
|
| 1176 |
+
placeholder="Example: HA + BOH → BA + H₂O"
|
| 1177 |
)
|
| 1178 |
|
| 1179 |
+
custom_acid_coefficient_input = gr.Number(
|
| 1180 |
+
label="Acid coefficient in balanced equation",
|
| 1181 |
+
value=1,
|
| 1182 |
+
precision=0
|
| 1183 |
+
)
|
| 1184 |
+
|
| 1185 |
+
custom_base_coefficient_input = gr.Number(
|
| 1186 |
+
label="Base coefficient in balanced equation",
|
| 1187 |
+
value=1,
|
| 1188 |
+
precision=0
|
| 1189 |
+
)
|
| 1190 |
+
|
| 1191 |
+
custom_molar_mass_input = gr.Number(
|
| 1192 |
+
label="Unknown substance molar mass (g/mol), if custom",
|
| 1193 |
+
value=0,
|
| 1194 |
+
precision=3
|
| 1195 |
+
)
|
| 1196 |
+
|
| 1197 |
+
gr.Markdown("### Experimental measurements")
|
| 1198 |
+
|
| 1199 |
known_concentration_input = gr.Number(
|
| 1200 |
label="Known solution concentration (mol/dm³)",
|
| 1201 |
value=0.100,
|
|
|
|
| 1221 |
)
|
| 1222 |
|
| 1223 |
total_prepared_volume_input = gr.Number(
|
| 1224 |
+
label="Total prepared solution volume (mL)",
|
| 1225 |
value=250.0,
|
| 1226 |
precision=2
|
| 1227 |
)
|
| 1228 |
|
| 1229 |
+
analysis_button = gr.Button("Calculate and Show Working")
|
| 1230 |
|
| 1231 |
analysis_output = gr.Textbox(
|
| 1232 |
+
label="Step-by-step worked solution",
|
| 1233 |
+
lines=24
|
| 1234 |
)
|
| 1235 |
|
| 1236 |
analysis_button.click(
|
|
|
|
| 1244 |
known_volume_input,
|
| 1245 |
unknown_volume_input,
|
| 1246 |
sample_mass_input,
|
| 1247 |
+
total_prepared_volume_input,
|
| 1248 |
+
custom_equation_input,
|
| 1249 |
+
custom_acid_coefficient_input,
|
| 1250 |
+
custom_base_coefficient_input,
|
| 1251 |
+
custom_molar_mass_input,
|
| 1252 |
+
indicator_input
|
| 1253 |
],
|
| 1254 |
outputs=analysis_output
|
| 1255 |
)
|
| 1256 |
|
| 1257 |
|
| 1258 |
+
|
| 1259 |
test_calculate_titre()
|
| 1260 |
|
| 1261 |
|