ArchiMathur commited on
Commit
d4cc85a
·
verified ·
1 Parent(s): 8216ac1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -84
app.py CHANGED
@@ -5,7 +5,8 @@ import pickle
5
  import sklearn
6
  from datasets import load_dataset
7
  import joblib
8
- import requests
 
9
 
10
  # Read the data
11
  data = pd.read_csv("mldata.csv")
@@ -18,7 +19,6 @@ def load_model(model_choice):
18
  elif model_choice == "Decision Tree":
19
  with open('dtreeweights.pkl', 'rb') as pickleFile:
20
  return pickle.load(pickleFile)
21
-
22
  else:
23
  raise ValueError("Invalid model selection")
24
 
@@ -30,7 +30,7 @@ categorical_cols = data[[
30
  'interested career area ',
31
  'Type of company want to settle in?',
32
  'Interested Type of Books'
33
- ]]
34
 
35
  # Assign category codes
36
  for i in categorical_cols:
@@ -52,6 +52,9 @@ book_interest_references = create_embedding_dict('Interested Type of Books')
52
 
53
  # Function to fetch job listings
54
  def fetch_job_listings(job_title):
 
 
 
55
  url = "https://jobs-api14.p.rapidapi.com/v2/list"
56
  querystring = {
57
  "query": job_title,
@@ -61,12 +64,13 @@ def fetch_job_listings(job_title):
61
  "employmentTypes": "fulltime;parttime;intern;contractor"
62
  }
63
  headers = {
64
- "x-rapidapi-key": "714f5a2539msh798d996c3243876p19c71ajsnfcd7ce481cb9",
65
  "x-rapidapi-host": "jobs-api14.p.rapidapi.com"
66
  }
67
 
68
  try:
69
- response = requests.get(url, headers=headers, params=querystring)
 
70
  job_data = response.json()
71
 
72
  # Process and format job listings
@@ -91,12 +95,13 @@ def rfprediction(model_choice, name, logical_thinking, hackathon_attend, coding_
91
  self_learning, extra_course, certificate_code, worskhop_code, read_writing_skill, memory_capability,
92
  subject_interest, career_interest, company_intend, senior_elder_advise, book_interest, introvert_extro,
93
  team_player, management_technical, smart_hardworker):
94
- # Load the selected model
95
- rfmodel = load_model(model_choice)
96
 
97
- # Create DataFrame
98
- df = pd.DataFrame.from_dict(
99
- {
 
 
 
100
  "logical_thinking": [logical_thinking],
101
  "hackathon_attend": [hackathon_attend],
102
  "coding_skills": [coding_skills],
@@ -106,11 +111,11 @@ def rfprediction(model_choice, name, logical_thinking, hackathon_attend, coding_
106
  "certificate": [certificate_code],
107
  "workshop": [worskhop_code],
108
  "read_writing_skills": [
109
- (0 if "poor" in read_writing_skill else 1 if "medium" in read_writing_skill else 2)
110
- ],
111
  "memory_capability": [
112
- (0 if "poor" in memory_capability else 1 if "medium" in memory_capability else 2)
113
- ],
114
  "subject_interest": [subject_interest],
115
  "career_interest": [career_interest],
116
  "company_intend": [company_intend],
@@ -118,74 +123,80 @@ def rfprediction(model_choice, name, logical_thinking, hackathon_attend, coding_
118
  "book_interest": [book_interest],
119
  "introvert_extro": [introvert_extro],
120
  "team_player": [team_player],
121
- "management_technical":[management_technical],
122
  "smart_hardworker": [smart_hardworker]
 
 
 
 
 
 
 
 
 
 
123
  }
124
- )
125
-
126
- # Replace string values with numeric representations
127
- df = df.replace({
128
- "certificate": certificates_references,
129
- "workshop": workshop_references,
130
- "subject_interest": subjects_interest_references,
131
- "career_interest": career_interest_references,
132
- "company_intend": company_intends_references,
133
- "book_interest": book_interest_references
134
- })
135
-
136
- # Dummy encoding
137
- userdata_list = df.values.tolist()
138
-
139
- # Management-Technical dummy encoding
140
- if(df["management_technical"].values == "Management"):
141
- userdata_list[0].extend([1])
142
- userdata_list[0].extend([0])
143
- userdata_list[0].remove('Management')
144
- elif(df["management_technical"].values == "Technical"):
145
- userdata_list[0].extend([0])
146
- userdata_list[0].extend([1])
147
- userdata_list[0].remove('Technical')
148
- else:
149
- return "Error in Management-Technical encoding"
150
 
151
- # Smart-Hard worker dummy encoding
152
- if(df["smart_hardworker"].values == "smart worker"):
153
- userdata_list[0].extend([1])
154
- userdata_list[0].extend([0])
155
- userdata_list[0].remove('smart worker')
156
- elif(df["smart_hardworker"].values == "hard worker"):
157
- userdata_list[0].extend([0])
158
- userdata_list[0].extend([1])
159
- userdata_list[0].remove('hard worker')
160
- else:
161
- return "Error in Smart-Hard worker encoding"
162
 
163
- # Prediction
164
- prediction_result_all = rfmodel.predict_proba(userdata_list)
165
-
166
- # Create result dictionary with probabilities
167
- result_list = {
168
- "Applications Developer": float(prediction_result_all[0][0]),
169
- "CRM Technical Developer": float(prediction_result_all[0][1]),
170
- "Database Developer": float(prediction_result_all[0][2]),
171
- "Mobile Applications Developer": float(prediction_result_all[0][3]),
172
- "Network Security Engineer": float(prediction_result_all[0][4]),
173
- "Software Developer": float(prediction_result_all[0][5]),
174
- "Software Engineer": float(prediction_result_all[0][6]),
175
- "Software Quality Assurance (QA)/ Testing": float(prediction_result_all[0][7]),
176
- "Systems Security Administrator": float(prediction_result_all[0][8]),
177
- "Technical Support": float(prediction_result_all[0][9]),
178
- "UX Designer": float(prediction_result_all[0][10]),
179
- "Web Developer": float(prediction_result_all[0][11]),
180
- }
181
-
182
- # Find the top predicted career
183
- top_career = max(result_list, key=result_list.get)
184
-
185
- # Fetch job listings for the top predicted career
186
- job_suggestions = fetch_job_listings(top_career)
 
 
 
 
 
187
 
188
- return result_list, job_suggestions
 
 
189
 
190
  # Lists for dropdown menus
191
  cert_list = ["app development", "distro making", "full stack", "hadoop", "information security", "machine learning", "python", "r programming", "shell programming"]
@@ -217,8 +228,8 @@ demo = gr.Interface(
217
  gr.Slider(minimum=0, maximum=6, value=0, step=1, label="Do you attend any Hackathons?", info="Scale: 0 - 6 | 0 - if not attended any"),
218
  gr.Slider(minimum=1, maximum=9, value=5, step=1, label="How do you rate your coding skills?", info="Scale: 1 - 9"),
219
  gr.Slider(minimum=1, maximum=9, value=3, step=1, label="How do you rate your public speaking skills/confidency?", info="Scale: 1 - 9"),
220
- gr.Radio({"Yes", "No"}, type="index", label="Are you a self-learning person? *"),
221
- gr.Radio({"Yes", "No"}, type="index", label="Do you take extra courses in uni (other than IT)? *"),
222
  gr.Dropdown(cert_list, label="Select a certificate you took!"),
223
  gr.Dropdown(workshop_list, label="Select a workshop you attended!"),
224
  gr.Dropdown(skill, label="Select your read and writing skill"),
@@ -226,17 +237,17 @@ demo = gr.Interface(
226
  gr.Dropdown(subject_list, label="What subject you are interested in?"),
227
  gr.Dropdown(career_list, label="Which IT-Career do you have interests in?"),
228
  gr.Dropdown(company_list, label="Do you have any interested company that you intend to settle in?"),
229
- gr.Radio({"Yes", "No"}, type="index", label="Do you ever seek any advices from senior or elders? *"),
230
  gr.Dropdown(book_list, label="Select your interested genre of book!"),
231
- gr.Radio({"Yes", "No"}, type="index", label="Are you an Introvert?| No - extrovert *"),
232
- gr.Radio({"Yes", "No"}, type="index", label="Ever worked in a team? *"),
233
  gr.Dropdown(Choice_list, label="Which area do you prefer: Management or Technical?"),
234
  gr.Dropdown(worker_list, label="Are you a Smart worker or Hard worker?")
235
  ],
236
  outputs=create_output_component(),
237
- title="Ai-Enhanced career guidance system"
238
  )
239
 
240
  # Main execution
241
  if __name__ == "__main__":
242
- demo.launch(share=True)
 
5
  import sklearn
6
  from datasets import load_dataset
7
  import joblib
8
+ import requests
9
+ import os
10
 
11
  # Read the data
12
  data = pd.read_csv("mldata.csv")
 
19
  elif model_choice == "Decision Tree":
20
  with open('dtreeweights.pkl', 'rb') as pickleFile:
21
  return pickle.load(pickleFile)
 
22
  else:
23
  raise ValueError("Invalid model selection")
24
 
 
30
  'interested career area ',
31
  'Type of company want to settle in?',
32
  'Interested Type of Books'
33
+ ]].copy()
34
 
35
  # Assign category codes
36
  for i in categorical_cols:
 
52
 
53
  # Function to fetch job listings
54
  def fetch_job_listings(job_title):
55
+ # Use environment variable for API key (more secure)
56
+ api_key = os.environ.get('RAPIDAPI_KEY', '714f5a2539msh798d996c3243876p19c71ajsnfcd7ce481cb9')
57
+
58
  url = "https://jobs-api14.p.rapidapi.com/v2/list"
59
  querystring = {
60
  "query": job_title,
 
64
  "employmentTypes": "fulltime;parttime;intern;contractor"
65
  }
66
  headers = {
67
+ "x-rapidapi-key": api_key,
68
  "x-rapidapi-host": "jobs-api14.p.rapidapi.com"
69
  }
70
 
71
  try:
72
+ response = requests.get(url, headers=headers, params=querystring, timeout=10)
73
+ response.raise_for_status()
74
  job_data = response.json()
75
 
76
  # Process and format job listings
 
95
  self_learning, extra_course, certificate_code, worskhop_code, read_writing_skill, memory_capability,
96
  subject_interest, career_interest, company_intend, senior_elder_advise, book_interest, introvert_extro,
97
  team_player, management_technical, smart_hardworker):
 
 
98
 
99
+ try:
100
+ # Load the selected model
101
+ rfmodel = load_model(model_choice)
102
+
103
+ # Create DataFrame
104
+ df = pd.DataFrame({
105
  "logical_thinking": [logical_thinking],
106
  "hackathon_attend": [hackathon_attend],
107
  "coding_skills": [coding_skills],
 
111
  "certificate": [certificate_code],
112
  "workshop": [worskhop_code],
113
  "read_writing_skills": [
114
+ (0 if "poor" in read_writing_skill else 1 if "medium" in read_writing_skill else 2)
115
+ ],
116
  "memory_capability": [
117
+ (0 if "poor" in memory_capability else 1 if "medium" in memory_capability else 2)
118
+ ],
119
  "subject_interest": [subject_interest],
120
  "career_interest": [career_interest],
121
  "company_intend": [company_intend],
 
123
  "book_interest": [book_interest],
124
  "introvert_extro": [introvert_extro],
125
  "team_player": [team_player],
126
+ "management_technical": [management_technical],
127
  "smart_hardworker": [smart_hardworker]
128
+ })
129
+
130
+ # Replace string values with numeric representations - FIX for FutureWarning
131
+ replacement_dict = {
132
+ "certificate": certificates_references,
133
+ "workshop": workshop_references,
134
+ "subject_interest": subjects_interest_references,
135
+ "career_interest": career_interest_references,
136
+ "company_intend": company_intends_references,
137
+ "book_interest": book_interest_references
138
  }
139
+
140
+ for col, mapping in replacement_dict.items():
141
+ if col in df.columns:
142
+ df[col] = df[col].map(mapping)
143
+
144
+ # Dummy encoding
145
+ userdata_list = df.values.tolist()
146
+
147
+ # Management-Technical dummy encoding
148
+ if df["management_technical"].values[0] == "Management":
149
+ userdata_list[0].extend([1, 0])
150
+ userdata_list[0].remove('Management')
151
+ elif df["management_technical"].values[0] == "Technical":
152
+ userdata_list[0].extend([0, 1])
153
+ userdata_list[0].remove('Technical')
154
+ else:
155
+ return {"Error": 1.0}, [["Error in Management-Technical encoding", "", "", ""]]
 
 
 
 
 
 
 
 
 
156
 
157
+ # Smart-Hard worker dummy encoding
158
+ if df["smart_hardworker"].values[0] == "smart worker":
159
+ userdata_list[0].extend([1, 0])
160
+ userdata_list[0].remove('smart worker')
161
+ elif df["smart_hardworker"].values[0] == "hard worker":
162
+ userdata_list[0].extend([0, 1])
163
+ userdata_list[0].remove('hard worker')
164
+ else:
165
+ return {"Error": 1.0}, [["Error in Smart-Hard worker encoding", "", "", ""]]
 
 
166
 
167
+ # Convert to numpy array for prediction
168
+ userdata_array = np.array(userdata_list)
169
+
170
+ # Prediction
171
+ prediction_result_all = rfmodel.predict_proba(userdata_array)
172
+
173
+ # Create result dictionary with probabilities
174
+ result_list = {
175
+ "Applications Developer": float(prediction_result_all[0][0]),
176
+ "CRM Technical Developer": float(prediction_result_all[0][1]),
177
+ "Database Developer": float(prediction_result_all[0][2]),
178
+ "Mobile Applications Developer": float(prediction_result_all[0][3]),
179
+ "Network Security Engineer": float(prediction_result_all[0][4]),
180
+ "Software Developer": float(prediction_result_all[0][5]),
181
+ "Software Engineer": float(prediction_result_all[0][6]),
182
+ "Software Quality Assurance (QA)/ Testing": float(prediction_result_all[0][7]),
183
+ "Systems Security Administrator": float(prediction_result_all[0][8]),
184
+ "Technical Support": float(prediction_result_all[0][9]),
185
+ "UX Designer": float(prediction_result_all[0][10]),
186
+ "Web Developer": float(prediction_result_all[0][11]),
187
+ }
188
+
189
+ # Find the top predicted career
190
+ top_career = max(result_list, key=result_list.get)
191
+
192
+ # Fetch job listings for the top predicted career
193
+ job_suggestions = fetch_job_listings(top_career)
194
+
195
+ return result_list, job_suggestions
196
 
197
+ except Exception as e:
198
+ error_msg = f"Error during prediction: {str(e)}"
199
+ return {"Error": 1.0}, [[error_msg, "", "", ""]]
200
 
201
  # Lists for dropdown menus
202
  cert_list = ["app development", "distro making", "full stack", "hadoop", "information security", "machine learning", "python", "r programming", "shell programming"]
 
228
  gr.Slider(minimum=0, maximum=6, value=0, step=1, label="Do you attend any Hackathons?", info="Scale: 0 - 6 | 0 - if not attended any"),
229
  gr.Slider(minimum=1, maximum=9, value=5, step=1, label="How do you rate your coding skills?", info="Scale: 1 - 9"),
230
  gr.Slider(minimum=1, maximum=9, value=3, step=1, label="How do you rate your public speaking skills/confidency?", info="Scale: 1 - 9"),
231
+ gr.Radio(["Yes", "No"], type="index", label="Are you a self-learning person? *"),
232
+ gr.Radio(["Yes", "No"], type="index", label="Do you take extra courses in uni (other than IT)? *"),
233
  gr.Dropdown(cert_list, label="Select a certificate you took!"),
234
  gr.Dropdown(workshop_list, label="Select a workshop you attended!"),
235
  gr.Dropdown(skill, label="Select your read and writing skill"),
 
237
  gr.Dropdown(subject_list, label="What subject you are interested in?"),
238
  gr.Dropdown(career_list, label="Which IT-Career do you have interests in?"),
239
  gr.Dropdown(company_list, label="Do you have any interested company that you intend to settle in?"),
240
+ gr.Radio(["Yes", "No"], type="index", label="Do you ever seek any advices from senior or elders? *"),
241
  gr.Dropdown(book_list, label="Select your interested genre of book!"),
242
+ gr.Radio(["Yes", "No"], type="index", label="Are you an Introvert?| No - extrovert *"),
243
+ gr.Radio(["Yes", "No"], type="index", label="Ever worked in a team? *"),
244
  gr.Dropdown(Choice_list, label="Which area do you prefer: Management or Technical?"),
245
  gr.Dropdown(worker_list, label="Are you a Smart worker or Hard worker?")
246
  ],
247
  outputs=create_output_component(),
248
+ title="AI-Enhanced Career Guidance System"
249
  )
250
 
251
  # Main execution
252
  if __name__ == "__main__":
253
+ demo.launch(share=False) # share=True not supported on HF Spaces