msgasu commited on
Commit
6cbbb17
·
verified ·
1 Parent(s): 5acb9a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -44
app.py CHANGED
@@ -6,7 +6,19 @@ import numpy as np
6
  # Load your trained model
7
  model = joblib.load('trained_model.joblib')
8
 
9
- # Define grade to numeric conversion
 
 
 
 
 
 
 
 
 
 
 
 
10
  def grade_to_numeric(grade):
11
  if pd.isna(grade) or grade == "":
12
  return np.nan
@@ -16,10 +28,15 @@ def grade_to_numeric(grade):
16
  }
17
  return grade_map.get(grade, np.nan)
18
 
19
- def predict_career(desired_career, aggregate, english, core_maths, science,
20
- social_studies, elective_maths, chemistry, physics, biology, interests):
21
- # Create input dictionary
22
- input_data = {
 
 
 
 
 
23
  "Desired_Career": desired_career,
24
  "Aggregate": aggregate,
25
  "English": english,
@@ -29,61 +46,71 @@ def predict_career(desired_career, aggregate, english, core_maths, science,
29
  "Elective Maths": elective_maths,
30
  "Chemistry": chemistry,
31
  "Physics": physics,
32
- "Biology": biology,
33
- "Interests": interests
34
- }
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  # Convert to DataFrame
37
- student_df = pd.DataFrame([input_data])
38
 
39
  # Convert grades to numerical
40
  grade_cols = ['English', 'Core Maths', 'Science', 'Social Studies',
41
  'Elective Maths', 'Chemistry', 'Physics', 'Biology']
42
 
43
  for col in grade_cols:
44
- student_df[col] = student_df[col].apply(grade_to_numeric)
45
 
 
 
 
 
 
46
  try:
 
 
 
 
 
 
 
47
  # Make prediction
48
- probabilities = model.predict_proba(student_df)[0]
49
  classes = model.classes_
50
 
51
- # Get top 5 recommendations
52
  top5_idx = np.argsort(probabilities)[::-1][:5]
53
  recommendations = [
54
- (classes[i], float(probabilities[i]))
55
  for i in top5_idx
56
  ]
57
 
58
- # Format output with analysis
59
  output = "## Top Career Recommendations\n\n"
60
  for course, prob in recommendations:
61
  output += f"**{course}** ({prob*100:.1f}% match)\n"
62
 
63
- # Add strengths analysis
64
- output += "\n## Your Academic Strengths\n"
65
- strong_subjects = []
66
- if student_df['English'].iloc[0] <= 3:
67
- strong_subjects.append("English")
68
- if student_df['Core Maths'].iloc[0] <= 3:
69
- strong_subjects.append("Mathematics")
70
- if student_df['Science'].iloc[0] <= 3:
71
- strong_subjects.append("Science")
72
-
73
- if strong_subjects:
74
- output += f"- Excellent performance in: {', '.join(strong_subjects)}\n"
75
- else:
76
- output += "- Good overall academic performance\n"
77
-
78
- # Add interest alignment
79
- output += f"\n## Interest Alignment\n- Your interests: {interests}\n"
80
 
81
  return output
82
 
83
  except Exception as e:
84
  return f"Error generating recommendations: {str(e)}"
85
 
86
- # Define Gradio interface
87
  with gr.Blocks(title="Career Path Predictor") as interface:
88
  gr.Markdown("# Career Recommendation System")
89
  gr.Markdown("Enter your academic details to get personalized career recommendations")
@@ -103,7 +130,11 @@ with gr.Blocks(title="Career Path Predictor") as interface:
103
  )
104
  interests = gr.Textbox(
105
  label="Your Interests (comma separated)",
106
- placeholder="e.g. programming, biology, creative writing"
 
 
 
 
107
  )
108
 
109
  with gr.Column():
@@ -122,13 +153,13 @@ with gr.Blocks(title="Career Path Predictor") as interface:
122
  label="Science Grade",
123
  value="B2"
124
  )
 
 
 
 
 
125
 
126
  with gr.Row():
127
- social_studies = gr.Dropdown(
128
- ["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"],
129
- label="Social Studies Grade",
130
- value="B2"
131
- )
132
  elective_maths = gr.Dropdown(
133
  ["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"],
134
  label="Elective Maths Grade",
@@ -139,8 +170,6 @@ with gr.Blocks(title="Career Path Predictor") as interface:
139
  label="Chemistry Grade",
140
  value="B2"
141
  )
142
-
143
- with gr.Row():
144
  physics = gr.Dropdown(
145
  ["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"],
146
  label="Physics Grade",
@@ -158,17 +187,21 @@ with gr.Blocks(title="Career Path Predictor") as interface:
158
  submit.click(
159
  fn=predict_career,
160
  inputs=[desired_career, aggregate, english, core_maths, science,
161
- social_studies, elective_maths, chemistry, physics, biology, interests],
 
162
  outputs=output
163
  )
164
 
165
  gr.Examples(
166
  examples=[
167
- ["Medicine", 6, "A1", "A1", "A1", "A1", "A1", "A1", "B2", "A1", "biology, research"],
168
- ["Computer Science", 9, "B2", "A1", "B2", "B3", "A1", "B2", "B2", "C4", "programming, math"]
 
 
169
  ],
170
  inputs=[desired_career, aggregate, english, core_maths, science,
171
- social_studies, elective_maths, chemistry, physics, biology, interests]
 
172
  )
173
 
174
  interface.launch()
 
6
  # Load your trained model
7
  model = joblib.load('trained_model.joblib')
8
 
9
+ # Define all possible features the model expects
10
+ ALL_INTERESTS = ['Research', 'Art', 'Cooking', 'Creativity', 'Technology',
11
+ 'Reading', 'Physics', 'Entrepreneurship', 'Public Speaking',
12
+ 'Dancing', 'Mathematics', 'Playing Football', 'Problem-Solving',
13
+ 'Writing', 'Music', 'Leadership']
14
+
15
+ ALL_STRENGTHS = ['Logical Reasoning', 'Hands-on Skills', 'Detail-Oriented',
16
+ 'Leadership', 'Innovative Thinking', 'Teamwork',
17
+ 'Analytical Thinking', 'Communication', 'Creativity']
18
+
19
+ OTHER_FEATURES = ['E-ICT', 'Economics', 'Government', 'Geography',
20
+ 'Business Management', 'Visual Arts', 'Literature']
21
+
22
  def grade_to_numeric(grade):
23
  if pd.isna(grade) or grade == "":
24
  return np.nan
 
28
  }
29
  return grade_map.get(grade, np.nan)
30
 
31
+ def create_feature_vector(desired_career, aggregate, english, core_maths, science,
32
+ social_studies, elective_maths, chemistry, physics, biology,
33
+ interests, strengths):
34
+ # Create base dictionary with all features initialized to 0 or NaN
35
+ features = {f: 0 for f in ALL_INTERESTS + ALL_STRENGTHS}
36
+ features.update({f: np.nan for f in OTHER_FEATURES})
37
+
38
+ # Add core academic features
39
+ features.update({
40
  "Desired_Career": desired_career,
41
  "Aggregate": aggregate,
42
  "English": english,
 
46
  "Elective Maths": elective_maths,
47
  "Chemistry": chemistry,
48
  "Physics": physics,
49
+ "Biology": biology
50
+ })
51
+
52
+ # Process interests
53
+ for interest in [i.strip() for i in interests.split(',')]:
54
+ interest_key = f"interest_{interest}"
55
+ if interest_key in features:
56
+ features[interest_key] = 1
57
+
58
+ # Process strengths
59
+ for strength in [s.strip() for s in strengths.split(',')]:
60
+ strength_key = f"strength_{strength}"
61
+ if strength_key in features:
62
+ features[strength_key] = 1
63
 
64
  # Convert to DataFrame
65
+ df = pd.DataFrame([features])
66
 
67
  # Convert grades to numerical
68
  grade_cols = ['English', 'Core Maths', 'Science', 'Social Studies',
69
  'Elective Maths', 'Chemistry', 'Physics', 'Biology']
70
 
71
  for col in grade_cols:
72
+ df[col] = df[col].apply(grade_to_numeric)
73
 
74
+ return df
75
+
76
+ def predict_career(desired_career, aggregate, english, core_maths, science,
77
+ social_studies, elective_maths, chemistry, physics, biology,
78
+ interests, strengths):
79
  try:
80
+ # Create complete feature vector
81
+ input_df = create_feature_vector(
82
+ desired_career, aggregate, english, core_maths, science,
83
+ social_studies, elective_maths, chemistry, physics, biology,
84
+ interests, strengths
85
+ )
86
+
87
  # Make prediction
88
+ probabilities = model.predict_proba(input_df)[0]
89
  classes = model.classes_
90
 
91
+ # Get top recommendations
92
  top5_idx = np.argsort(probabilities)[::-1][:5]
93
  recommendations = [
94
+ (classes[i], float(probabilities[i]))
95
  for i in top5_idx
96
  ]
97
 
98
+ # Format output
99
  output = "## Top Career Recommendations\n\n"
100
  for course, prob in recommendations:
101
  output += f"**{course}** ({prob*100:.1f}% match)\n"
102
 
103
+ output += "\n## Your Profile Highlights\n"
104
+ output += f"- Desired Career: {desired_career}\n"
105
+ output += f"- Key Interests: {interests}\n"
106
+ output += f"- Core Strengths: {strengths}\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
  return output
109
 
110
  except Exception as e:
111
  return f"Error generating recommendations: {str(e)}"
112
 
113
+ # Gradio Interface
114
  with gr.Blocks(title="Career Path Predictor") as interface:
115
  gr.Markdown("# Career Recommendation System")
116
  gr.Markdown("Enter your academic details to get personalized career recommendations")
 
130
  )
131
  interests = gr.Textbox(
132
  label="Your Interests (comma separated)",
133
+ placeholder="e.g. research, technology, leadership"
134
+ )
135
+ strengths = gr.Textbox(
136
+ label="Your Strengths (comma separated)",
137
+ placeholder="e.g. analytical thinking, teamwork"
138
  )
139
 
140
  with gr.Column():
 
153
  label="Science Grade",
154
  value="B2"
155
  )
156
+ social_studies = gr.Dropdown(
157
+ ["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"],
158
+ label="Social Studies Grade",
159
+ value="B2"
160
+ )
161
 
162
  with gr.Row():
 
 
 
 
 
163
  elective_maths = gr.Dropdown(
164
  ["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"],
165
  label="Elective Maths Grade",
 
170
  label="Chemistry Grade",
171
  value="B2"
172
  )
 
 
173
  physics = gr.Dropdown(
174
  ["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"],
175
  label="Physics Grade",
 
187
  submit.click(
188
  fn=predict_career,
189
  inputs=[desired_career, aggregate, english, core_maths, science,
190
+ social_studies, elective_maths, chemistry, physics, biology,
191
+ interests, strengths],
192
  outputs=output
193
  )
194
 
195
  gr.Examples(
196
  examples=[
197
+ ["Medicine", 6, "A1", "A1", "A1", "A1", "A1", "A1", "B2", "A1",
198
+ "research, biology, leadership", "analytical thinking, detail-oriented"],
199
+ ["Computer Science", 9, "B2", "A1", "B2", "B3", "A1", "B2", "B2", "C4",
200
+ "technology, problem-solving, mathematics", "logical reasoning, innovative thinking"]
201
  ],
202
  inputs=[desired_career, aggregate, english, core_maths, science,
203
+ social_studies, elective_maths, chemistry, physics, biology,
204
+ interests, strengths]
205
  )
206
 
207
  interface.launch()