trangannh commited on
Commit
7317baa
1 Parent(s): 34250bf

Create inference.py

Browse files
Files changed (1) hide show
  1. inference.py +58 -0
inference.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+ import pandas as pd
3
+ from sklearn.feature_extraction.text import TfidfVectorizer
4
+ from sklearn.metrics.pairwise import cosine_similarity
5
+ import numpy as np
6
+ from typing import List, Dict
7
+
8
+ # Function to load model components
9
+ def load_model_components(model_path):
10
+ with open(model_path, 'rb') as f:
11
+ model_components = pickle.load(f)
12
+ return model_components
13
+
14
+ # Function to recommend jobs based on input skills and major
15
+ def recommend_jobs_for_input_skills(input_hard_skills: str, input_soft_skills: str, input_major: str,
16
+ jobs_data: pd.DataFrame, model_path: str) -> List[str]:
17
+ # Load saved model components
18
+ tfidf_vectorizer_skills, tfidf_vectorizer_majors, companies_skills_vec, companies_majors_vec = load_model_components(model_path)
19
+
20
+ # Vectorize input skills and major
21
+ input_hard_skills_vec = tfidf_vectorizer_skills.transform([input_hard_skills])
22
+ input_soft_skills_vec = tfidf_vectorizer_skills.transform([input_soft_skills])
23
+ input_major_vec = tfidf_vectorizer_majors.transform([input_major])
24
+
25
+ # Average the vectorized hard and soft skills
26
+ input_skills_vec = (input_hard_skills_vec + input_soft_skills_vec) / 2
27
+
28
+ # Compute similarities
29
+ skills_similarity = cosine_similarity(input_skills_vec, companies_skills_vec)
30
+ major_similarity = cosine_similarity(input_major_vec, companies_majors_vec)
31
+
32
+ # Ensure the number of companies in both similarities is aligned
33
+ if skills_similarity.shape[1] != major_similarity.shape[1]:
34
+ min_dim = min(skills_similarity.shape[1], major_similarity.shape[1])
35
+ skills_similarity = skills_similarity[:, :min_dim]
36
+ major_similarity = major_similarity[:, :min_dim]
37
+
38
+ # Combine similarities
39
+ combined_similarity = (skills_similarity + major_similarity) / 2
40
+
41
+ # Get top 3 job recommendations
42
+ sorted_company_indices = np.argsort(-combined_similarity[0])
43
+ recommended_jobs = jobs_data.iloc[sorted_company_indices]['Major'].values[:3]
44
+
45
+ return recommended_jobs.tolist()
46
+
47
+ # Example usage for testing
48
+ if __name__ == "__main__":
49
+ input_hard_skills = "Python, Java, Finance, Excel"
50
+ input_soft_skills = "Communication, Teamwork"
51
+ input_major = "Computer Science"
52
+
53
+ jobs_data = pd.read_csv("jobs_data.csv")
54
+ model_path = "recommendation_model.pkl"
55
+
56
+ recommended_jobs = recommend_jobs_for_input_skills(input_hard_skills, input_soft_skills, input_major, jobs_data, model_path)
57
+ print("Recommended Jobs based on input skills and major:")
58
+ print(recommended_jobs)