ChemDave commited on
Commit
b754147
·
verified ·
1 Parent(s): 00245f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -1
app.py CHANGED
@@ -25,8 +25,116 @@ client = InferenceClient(
25
  # AI tutor function
26
  # -----------------------------
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  def answer(question):
29
 
 
 
30
  verified_content = json.dumps(
31
  chemistry_content,
32
  indent=2,
@@ -137,6 +245,11 @@ demo = gr.Interface(
137
  description="A curriculum-aligned chemistry practical tutor."
138
  )
139
 
140
-
 
 
 
 
 
141
  demo.launch()
142
 
 
25
  # AI tutor function
26
  # -----------------------------
27
 
28
+ def retrieve_content(question):
29
+ q = question.lower()
30
+ retrieved = {}
31
+
32
+ # Always include the rules that govern AI answers.
33
+ retrieved["ai_rules"] = chemistry_content.get("ai_rules", [])
34
+ retrieved["qa_rules"] = chemistry_content.get("qa_rules", [])
35
+
36
+ # Retrieve content about observations and inferences.
37
+ if any(word in q for word in [
38
+ "observation", "inference", "infer", "observed"
39
+ ]):
40
+ for key in [
41
+ "important_distinctions",
42
+ "general_procedure",
43
+ "data_recording"
44
+ ]:
45
+ if key in chemistry_content:
46
+ retrieved[key] = chemistry_content[key]
47
+
48
+ # Retrieve content about endpoints and equivalence points.
49
+ if any(word in q for word in [
50
+ "endpoint", "equivalence point"
51
+ ]):
52
+ for key in [
53
+ "key_definitions",
54
+ "important_distinctions",
55
+ "principle"
56
+ ]:
57
+ if key in chemistry_content:
58
+ retrieved[key] = chemistry_content[key]
59
+
60
+ # Retrieve content about titre and burette readings.
61
+ if any(word in q for word in [
62
+ "titre", "burette", "initial reading",
63
+ "final reading", "volume delivered"
64
+ ]):
65
+ for key in [
66
+ "apparatus",
67
+ "data_recording",
68
+ "general_procedure"
69
+ ]:
70
+ if key in chemistry_content:
71
+ retrieved[key] = chemistry_content[key]
72
+
73
+ # Retrieve content about calculations and stoichiometry.
74
+ if any(word in q for word in [
75
+ "calculate", "calculation", "concentration",
76
+ "stoichiometry", "moles", "c1v1", "c2v2"
77
+ ]):
78
+ for key in [
79
+ "calculation_rules",
80
+ "principle",
81
+ "key_definitions"
82
+ ]:
83
+ if key in chemistry_content:
84
+ retrieved[key] = chemistry_content[key]
85
+
86
+ # Retrieve content about indicators.
87
+ if any(word in q for word in [
88
+ "indicator", "phenolphthalein",
89
+ "methyl orange", "methyl red"
90
+ ]):
91
+ for key in [
92
+ "indicators",
93
+ "indicator_rule",
94
+ "important_distinctions"
95
+ ]:
96
+ if key in chemistry_content:
97
+ retrieved[key] = chemistry_content[key]
98
+
99
+ # Retrieve content about laboratory safety.
100
+ if any(word in q for word in [
101
+ "safety", "hazard", "spill",
102
+ "protective equipment", "precaution"
103
+ ]):
104
+ for key in ["safety", "apparatus"]:
105
+ if key in chemistry_content:
106
+ retrieved[key] = chemistry_content[key]
107
+
108
+ # Retrieve content about standards and solutions.
109
+ if any(word in q for word in [
110
+ "primary standard", "secondary standard",
111
+ "standard solution", "dilution"
112
+ ]):
113
+ for key in [
114
+ "key_definitions",
115
+ "calculation_rules",
116
+ "learning_objectives"
117
+ ]:
118
+ if key in chemistry_content:
119
+ retrieved[key] = chemistry_content[key]
120
+
121
+ # Fallback for questions not matched by our keywords.
122
+ if len(retrieved) <= 2:
123
+ for key in [
124
+ "learning_objectives",
125
+ "key_definitions",
126
+ "important_distinctions",
127
+ "principle"
128
+ ]:
129
+ if key in chemistry_content:
130
+ retrieved[key] = chemistry_content[key]
131
+
132
+ return retrieved
133
+
134
  def answer(question):
135
 
136
+ relevant_content = retrieve_content(question)
137
+
138
  verified_content = json.dumps(
139
  chemistry_content,
140
  indent=2,
 
245
  description="A curriculum-aligned chemistry practical tutor."
246
  )
247
 
248
+ print("RETRIEVAL TEST:")
249
+ print(json.dumps(
250
+ retrieve_content("What is the titre value?"),
251
+ indent=2,
252
+ ensure_ascii=False
253
+ ))
254
  demo.launch()
255