Update app.py
Browse files
app.py
CHANGED
|
@@ -129,20 +129,16 @@ def generate_tutor_output(subject: str, difficulty: str, student_input: str) ->
|
|
| 129 |
if enhanced_topic:
|
| 130 |
break
|
| 131 |
|
| 132 |
-
# Strengthened prompt
|
| 133 |
if enhanced_topic:
|
| 134 |
logging.debug(f"Using enhanced topic: {enhanced_topic}")
|
| 135 |
prompt = f"""
|
| 136 |
You are an expert tutor specializing in {enhanced_topic} at the {difficulty} level. The student has asked: "{student_input}"
|
| 137 |
Generate a detailed response as a valid JSON object with exactly these keys and content:
|
| 138 |
- "lesson": A comprehensive lesson (3-4 paragraphs, at least 200 words) based on national educational standards, including historical context.
|
| 139 |
-
- "example": A detailed step-by-step example problem with a full solution, formatted as:
|
| 140 |
-
- "Example Problem:" followed by the question
|
| 141 |
-
- "Step 1:", "Step 2:", etc., each on a new line
|
| 142 |
-
- "Answer:" with the final solution
|
| 143 |
- "real_world_problem": A challenging real-world application of this concept (at least 100 words).
|
| 144 |
-
- "quiz": A
|
| 145 |
-
- "1. Question text\n a) option1\n b) option2\n c) option3\n Correct answer: a"
|
| 146 |
Ensure all sections are fully populated with relevant content. Return only the JSON object, enclosed in ```json``` markers, with no additional text outside the markers.
|
| 147 |
"""
|
| 148 |
else:
|
|
@@ -151,13 +147,9 @@ def generate_tutor_output(subject: str, difficulty: str, student_input: str) ->
|
|
| 151 |
You are an expert tutor in {subject_lower} at the {difficulty} level. The student has asked: "{student_input}"
|
| 152 |
Generate a detailed response as a valid JSON object with exactly these keys and content:
|
| 153 |
- "lesson": A descriptive, engaging lesson (3-4 paragraphs, at least 200 words) on the topic.
|
| 154 |
-
- "example": An example problem with a full solution, formatted as:
|
| 155 |
-
- "Example Problem:" followed by the question
|
| 156 |
-
- "Step 1:", "Step 2:", etc., each on a new line
|
| 157 |
-
- "Answer:" with the final solution
|
| 158 |
- "real_world_problem": A real-world problem solvable using the lesson concepts (at least 100 words).
|
| 159 |
-
- "quiz": A
|
| 160 |
-
- "1. Question text\n a) option1\n b) option2\n c) option3\n Correct answer: a"
|
| 161 |
Ensure all sections are fully populated with relevant content. Return only the JSON object, enclosed in ```json``` markers, with no additional text outside the markers.
|
| 162 |
"""
|
| 163 |
|
|
@@ -185,7 +177,7 @@ def generate_tutor_output(subject: str, difficulty: str, student_input: str) ->
|
|
| 185 |
}
|
| 186 |
],
|
| 187 |
model=target_model,
|
| 188 |
-
max_tokens=4000,
|
| 189 |
)
|
| 190 |
|
| 191 |
response_content = completion.choices[0].message.content
|
|
@@ -205,7 +197,7 @@ def generate_tutor_output(subject: str, difficulty: str, student_input: str) ->
|
|
| 205 |
logging.warning("No JSON markers found in response")
|
| 206 |
result = None
|
| 207 |
|
| 208 |
-
# Improved fallback parsing
|
| 209 |
if not result:
|
| 210 |
logging.debug("Falling back to text parsing")
|
| 211 |
sections = {
|
|
@@ -220,7 +212,6 @@ def generate_tutor_output(subject: str, difficulty: str, student_input: str) ->
|
|
| 220 |
line = line.strip()
|
| 221 |
if not line or line.startswith('```'):
|
| 222 |
continue
|
| 223 |
-
# More flexible section detection
|
| 224 |
if any(kw in line.lower() for kw in ["lesson", "1.", "overview", "introduction"]):
|
| 225 |
current_section = "lesson"
|
| 226 |
sections[current_section] += line.replace("Lesson:", "").replace("1.", "").strip() + "\n"
|
|
@@ -236,19 +227,8 @@ def generate_tutor_output(subject: str, difficulty: str, student_input: str) ->
|
|
| 236 |
elif current_section:
|
| 237 |
sections[current_section] += line + "\n"
|
| 238 |
|
| 239 |
-
# Log parsed sections for debugging
|
| 240 |
logging.debug(f"Parsed sections: {sections}")
|
| 241 |
-
|
| 242 |
-
# Check if any section was populated
|
| 243 |
-
if any(sections.values()):
|
| 244 |
-
result = sections
|
| 245 |
-
else:
|
| 246 |
-
result = {
|
| 247 |
-
"lesson": "Failed to generate lesson content",
|
| 248 |
-
"example": "Failed to generate example content",
|
| 249 |
-
"real_world_problem": "Failed to generate real-world application content",
|
| 250 |
-
"quiz": "Failed to generate quiz content"
|
| 251 |
-
}
|
| 252 |
|
| 253 |
# Ensure all keys are present and non-empty
|
| 254 |
for key in ["lesson", "example", "real_world_problem", "quiz"]:
|
|
@@ -270,7 +250,7 @@ def generate_tutor_output(subject: str, difficulty: str, student_input: str) ->
|
|
| 270 |
def process_output(output: Dict[str, Any]) -> Tuple[str, str, str, str]:
|
| 271 |
"""Process the output from generate_tutor_output into HTML format."""
|
| 272 |
try:
|
| 273 |
-
logging.debug(f"Processing output: {str(output)
|
| 274 |
|
| 275 |
lesson = markdown2.markdown(output.get("lesson", "No lesson available"))
|
| 276 |
example = markdown2.markdown(output.get("example", "No example available"))
|
|
|
|
| 129 |
if enhanced_topic:
|
| 130 |
break
|
| 131 |
|
| 132 |
+
# Strengthened prompt with strict quiz formatting
|
| 133 |
if enhanced_topic:
|
| 134 |
logging.debug(f"Using enhanced topic: {enhanced_topic}")
|
| 135 |
prompt = f"""
|
| 136 |
You are an expert tutor specializing in {enhanced_topic} at the {difficulty} level. The student has asked: "{student_input}"
|
| 137 |
Generate a detailed response as a valid JSON object with exactly these keys and content:
|
| 138 |
- "lesson": A comprehensive lesson (3-4 paragraphs, at least 200 words) based on national educational standards, including historical context.
|
| 139 |
+
- "example": A detailed step-by-step example problem with a full solution, formatted as: "Example Problem: [question]\nStep 1: [step]\nStep 2: [step]\nAnswer: [solution]".
|
|
|
|
|
|
|
|
|
|
| 140 |
- "real_world_problem": A challenging real-world application of this concept (at least 100 words).
|
| 141 |
+
- "quiz": A single string containing exactly 3 multiple-choice questions, each formatted as "1. [Question]\n a) [option]\n b) [option]\n c) [option]\n Correct answer: [letter]", separated by newlines.
|
|
|
|
| 142 |
Ensure all sections are fully populated with relevant content. Return only the JSON object, enclosed in ```json``` markers, with no additional text outside the markers.
|
| 143 |
"""
|
| 144 |
else:
|
|
|
|
| 147 |
You are an expert tutor in {subject_lower} at the {difficulty} level. The student has asked: "{student_input}"
|
| 148 |
Generate a detailed response as a valid JSON object with exactly these keys and content:
|
| 149 |
- "lesson": A descriptive, engaging lesson (3-4 paragraphs, at least 200 words) on the topic.
|
| 150 |
+
- "example": An example problem with a full solution, formatted as: "Example Problem: [question]\nStep 1: [step]\nStep 2: [step]\nAnswer: [solution]".
|
|
|
|
|
|
|
|
|
|
| 151 |
- "real_world_problem": A real-world problem solvable using the lesson concepts (at least 100 words).
|
| 152 |
+
- "quiz": A single string containing exactly 3 multiple-choice questions, each formatted as "1. [Question]\n a) [option]\n b) [option]\n c) [option]\n Correct answer: [letter]", separated by newlines.
|
|
|
|
| 153 |
Ensure all sections are fully populated with relevant content. Return only the JSON object, enclosed in ```json``` markers, with no additional text outside the markers.
|
| 154 |
"""
|
| 155 |
|
|
|
|
| 177 |
}
|
| 178 |
],
|
| 179 |
model=target_model,
|
| 180 |
+
max_tokens=4000,
|
| 181 |
)
|
| 182 |
|
| 183 |
response_content = completion.choices[0].message.content
|
|
|
|
| 197 |
logging.warning("No JSON markers found in response")
|
| 198 |
result = None
|
| 199 |
|
| 200 |
+
# Improved fallback parsing
|
| 201 |
if not result:
|
| 202 |
logging.debug("Falling back to text parsing")
|
| 203 |
sections = {
|
|
|
|
| 212 |
line = line.strip()
|
| 213 |
if not line or line.startswith('```'):
|
| 214 |
continue
|
|
|
|
| 215 |
if any(kw in line.lower() for kw in ["lesson", "1.", "overview", "introduction"]):
|
| 216 |
current_section = "lesson"
|
| 217 |
sections[current_section] += line.replace("Lesson:", "").replace("1.", "").strip() + "\n"
|
|
|
|
| 227 |
elif current_section:
|
| 228 |
sections[current_section] += line + "\n"
|
| 229 |
|
|
|
|
| 230 |
logging.debug(f"Parsed sections: {sections}")
|
| 231 |
+
result = sections
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
# Ensure all keys are present and non-empty
|
| 234 |
for key in ["lesson", "example", "real_world_problem", "quiz"]:
|
|
|
|
| 250 |
def process_output(output: Dict[str, Any]) -> Tuple[str, str, str, str]:
|
| 251 |
"""Process the output from generate_tutor_output into HTML format."""
|
| 252 |
try:
|
| 253 |
+
logging.debug(f"Processing output: {str(output)}")
|
| 254 |
|
| 255 |
lesson = markdown2.markdown(output.get("lesson", "No lesson available"))
|
| 256 |
example = markdown2.markdown(output.get("example", "No example available"))
|