msgasu commited on
Commit
331e788
·
verified ·
1 Parent(s): dfbc1a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -57
app.py CHANGED
@@ -48,24 +48,6 @@ def grade_to_numeric(grade):
48
  }
49
  return grade_map.get(grade, np.nan)
50
 
51
- # Function to calculate aggregate score based on grades
52
- def calculate_aggregate(grades_dict):
53
- # Filter out empty grades
54
- valid_grades = {k: grade_to_numeric(v) for k, v in grades_dict.items() if v and v != ""}
55
-
56
- # Need at least 4 grades to calculate aggregate
57
- if len(valid_grades) < 4:
58
- return None
59
-
60
- # Sort grades by value (lower is better)
61
- sorted_grades = sorted(valid_grades.values())
62
-
63
- # Take the best (lowest) 4 grades for aggregate
64
- best_grades = sorted_grades[:4]
65
-
66
- # Calculate aggregate
67
- return sum(best_grades)
68
-
69
  # Function to extract interests and strengths into separate columns
70
  def extract_traits(df, column_name, prefix, all_traits=None):
71
  """
@@ -177,18 +159,7 @@ def explain_recommendation(student_info, top_recommendation):
177
  # Career alignment
178
  explanation += f"- Your career interest in {student_info['Desired_Career']}\n"
179
 
180
- # Academic strengths
181
- subjects = []
182
- if grade_to_numeric(student_info.get('Core Maths', '')) <= 3:
183
- subjects.append("Mathematics")
184
- if grade_to_numeric(student_info.get('English', '')) <= 3:
185
- subjects.append("English")
186
- if grade_to_numeric(student_info.get('Science', '')) <= 3:
187
- subjects.append("Science")
188
- if subjects:
189
- explanation += f"- Your strong performance in {', '.join(subjects)}\n"
190
-
191
- # Interests and strengths match
192
  explanation += f"- Your interests in {student_info['Interests']}\n"
193
  explanation += f"- Your strengths in {student_info['Strengths']}\n"
194
 
@@ -202,29 +173,10 @@ def explain_recommendation(student_info, top_recommendation):
202
  except Exception as e:
203
  return f"Error generating explanation: {str(e)}"
204
 
205
- def predict_career(desired_career, interests, strengths, english, core_maths, science, social_studies,
206
  elective_maths, physics, biology, chemistry):
207
 
208
  try:
209
- # Collect all grades in a dictionary
210
- grades = {
211
- "English": english,
212
- "Core Maths": core_maths,
213
- "Science": science,
214
- "Social Studies": social_studies,
215
- "Elective Maths": elective_maths,
216
- "Physics": physics,
217
- "Biology": biology,
218
- "Chemistry": chemistry
219
- }
220
-
221
- # Calculate aggregate automatically
222
- aggregate = calculate_aggregate(grades)
223
-
224
- # If not enough grades were provided
225
- if aggregate is None:
226
- return "Error: Please provide at least 4 subject grades to calculate aggregate score."
227
-
228
  # Create student data dictionary with all required fields
229
  student_info = {
230
  "StudentID": "STU_TEMP",
@@ -266,10 +218,7 @@ def predict_career(desired_career, interests, strengths, english, core_maths, sc
266
  # Get explanation
267
  explanation = explain_recommendation(student_info, top_recommendation)
268
 
269
- # Add aggregate information to the output
270
- aggregate_info = f"Calculated Aggregate Score: {aggregate}\n\n"
271
-
272
- return aggregate_info + recommendations + "\n" + explanation
273
  except Exception as e:
274
  import traceback
275
  error_details = traceback.format_exc()
@@ -293,8 +242,14 @@ with gr.Blocks(title="Career Course Recommendation System") as demo:
293
  placeholder="Enter your desired career path (e.g. Medicine, Computer Science, Engineering)",
294
  info="Enter your desired career path"
295
  )
 
296
  interests = gr.Textbox(label="Interests (comma separated)", placeholder="Reading,Dancing,Physics", info="List your interests separated by commas")
297
- strengths = gr.Textbox(label="Strengths (comma separated)", placeholder="Communication,Creativity", info="List your strengths separated by commas")
 
 
 
 
 
298
 
299
  gr.Markdown("### Core Subjects (Required)")
300
  with gr.Row():
@@ -329,7 +284,7 @@ with gr.Blocks(title="Career Course Recommendation System") as demo:
329
  - E8: Pass (8 points)
330
  - F9: Fail (9 points)
331
 
332
- *Lower points are better. Aggregate is the sum of your best 4 subjects.*
333
  """)
334
 
335
  submit_btn = gr.Button("Get Recommendations", variant="primary", size="lg")
@@ -337,7 +292,7 @@ with gr.Blocks(title="Career Course Recommendation System") as demo:
337
 
338
  submit_btn.click(
339
  fn=predict_career,
340
- inputs=[desired_career, interests, strengths, english, core_maths, science, social_studies,
341
  elective_maths, physics, biology, chemistry],
342
  outputs=output
343
  )
 
48
  }
49
  return grade_map.get(grade, np.nan)
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  # Function to extract interests and strengths into separate columns
52
  def extract_traits(df, column_name, prefix, all_traits=None):
53
  """
 
159
  # Career alignment
160
  explanation += f"- Your career interest in {student_info['Desired_Career']}\n"
161
 
162
+ # Interests match
 
 
 
 
 
 
 
 
 
 
 
163
  explanation += f"- Your interests in {student_info['Interests']}\n"
164
  explanation += f"- Your strengths in {student_info['Strengths']}\n"
165
 
 
173
  except Exception as e:
174
  return f"Error generating explanation: {str(e)}"
175
 
176
+ def predict_career(desired_career, aggregate, interests, strengths, english, core_maths, science, social_studies,
177
  elective_maths, physics, biology, chemistry):
178
 
179
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  # Create student data dictionary with all required fields
181
  student_info = {
182
  "StudentID": "STU_TEMP",
 
218
  # Get explanation
219
  explanation = explain_recommendation(student_info, top_recommendation)
220
 
221
+ return recommendations + "\n" + explanation
 
 
 
222
  except Exception as e:
223
  import traceback
224
  error_details = traceback.format_exc()
 
242
  placeholder="Enter your desired career path (e.g. Medicine, Computer Science, Engineering)",
243
  info="Enter your desired career path"
244
  )
245
+ aggregate = gr.Slider(minimum=6, maximum=37, value=15, step=1, label="Aggregate Score", info="Lower is better (6 is best, 37 is worst)")
246
  interests = gr.Textbox(label="Interests (comma separated)", placeholder="Reading,Dancing,Physics", info="List your interests separated by commas")
247
+ strengths = gr.Textbox(
248
+ label="Strengths (comma separated)",
249
+ placeholder="Communication,Creativity",
250
+ info="List your strengths separated by commas",
251
+ value="Communication,Creativity,Logical Reasoning,Analytical Thinking"
252
+ )
253
 
254
  gr.Markdown("### Core Subjects (Required)")
255
  with gr.Row():
 
284
  - E8: Pass (8 points)
285
  - F9: Fail (9 points)
286
 
287
+ *Lower points are better. Aggregate is the sum of your best subjects.*
288
  """)
289
 
290
  submit_btn = gr.Button("Get Recommendations", variant="primary", size="lg")
 
292
 
293
  submit_btn.click(
294
  fn=predict_career,
295
+ inputs=[desired_career, aggregate, interests, strengths, english, core_maths, science, social_studies,
296
  elective_maths, physics, biology, chemistry],
297
  outputs=output
298
  )