msgasu commited on
Commit
dfbc1a2
·
verified ·
1 Parent(s): 053cdda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -8
app.py CHANGED
@@ -48,6 +48,24 @@ def grade_to_numeric(grade):
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
  """
@@ -184,10 +202,29 @@ def explain_recommendation(student_info, top_recommendation):
184
  except Exception as e:
185
  return f"Error generating explanation: {str(e)}"
186
 
187
- def predict_career(desired_career, aggregate, interests, strengths, english, core_maths, science, social_studies,
188
  elective_maths, physics, biology, chemistry):
189
 
190
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  # Create student data dictionary with all required fields
192
  student_info = {
193
  "StudentID": "STU_TEMP",
@@ -229,7 +266,10 @@ def predict_career(desired_career, aggregate, interests, strengths, english, cor
229
  # Get explanation
230
  explanation = explain_recommendation(student_info, top_recommendation)
231
 
232
- return recommendations + "\n" + explanation
 
 
 
233
  except Exception as e:
234
  import traceback
235
  error_details = traceback.format_exc()
@@ -248,12 +288,11 @@ with gr.Blocks(title="Career Course Recommendation System") as demo:
248
  with gr.Row():
249
  with gr.Column(scale=2):
250
  gr.Markdown("### Student Information")
251
- desired_career = gr.Dropdown(
252
- choices=["Medicine", "Pharmacy", "Law", "Computer Science", "Engineering", "Business", "Nursing", "Agriculture", "Journalism", "Education"],
253
  label="Desired Career",
254
- info="Select your desired career path"
 
255
  )
256
- aggregate = gr.Slider(minimum=6, maximum=37, value=15, step=1, label="Aggregate Score", info="Lower is better (6 is best, 37 is worst)")
257
  interests = gr.Textbox(label="Interests (comma separated)", placeholder="Reading,Dancing,Physics", info="List your interests separated by commas")
258
  strengths = gr.Textbox(label="Strengths (comma separated)", placeholder="Communication,Creativity", info="List your strengths separated by commas")
259
 
@@ -290,7 +329,7 @@ with gr.Blocks(title="Career Course Recommendation System") as demo:
290
  - E8: Pass (8 points)
291
  - F9: Fail (9 points)
292
 
293
- *Lower points are better. Aggregate is the sum of your best subjects.*
294
  """)
295
 
296
  submit_btn = gr.Button("Get Recommendations", variant="primary", size="lg")
@@ -298,7 +337,7 @@ with gr.Blocks(title="Career Course Recommendation System") as demo:
298
 
299
  submit_btn.click(
300
  fn=predict_career,
301
- inputs=[desired_career, aggregate, interests, strengths, english, core_maths, science, social_studies,
302
  elective_maths, physics, biology, chemistry],
303
  outputs=output
304
  )
 
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
  """
 
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
  # 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()
 
288
  with gr.Row():
289
  with gr.Column(scale=2):
290
  gr.Markdown("### Student Information")
291
+ desired_career = gr.Textbox(
 
292
  label="Desired Career",
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
 
 
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
 
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
  )