ptit-job-recommendation / inference.py
trangannh's picture
Create inference.py
7317baa verified
import pickle
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
from typing import List, Dict
# Function to load model components
def load_model_components(model_path):
with open(model_path, 'rb') as f:
model_components = pickle.load(f)
return model_components
# Function to recommend jobs based on input skills and major
def recommend_jobs_for_input_skills(input_hard_skills: str, input_soft_skills: str, input_major: str,
jobs_data: pd.DataFrame, model_path: str) -> List[str]:
# Load saved model components
tfidf_vectorizer_skills, tfidf_vectorizer_majors, companies_skills_vec, companies_majors_vec = load_model_components(model_path)
# Vectorize input skills and major
input_hard_skills_vec = tfidf_vectorizer_skills.transform([input_hard_skills])
input_soft_skills_vec = tfidf_vectorizer_skills.transform([input_soft_skills])
input_major_vec = tfidf_vectorizer_majors.transform([input_major])
# Average the vectorized hard and soft skills
input_skills_vec = (input_hard_skills_vec + input_soft_skills_vec) / 2
# Compute similarities
skills_similarity = cosine_similarity(input_skills_vec, companies_skills_vec)
major_similarity = cosine_similarity(input_major_vec, companies_majors_vec)
# Ensure the number of companies in both similarities is aligned
if skills_similarity.shape[1] != major_similarity.shape[1]:
min_dim = min(skills_similarity.shape[1], major_similarity.shape[1])
skills_similarity = skills_similarity[:, :min_dim]
major_similarity = major_similarity[:, :min_dim]
# Combine similarities
combined_similarity = (skills_similarity + major_similarity) / 2
# Get top 3 job recommendations
sorted_company_indices = np.argsort(-combined_similarity[0])
recommended_jobs = jobs_data.iloc[sorted_company_indices]['Major'].values[:3]
return recommended_jobs.tolist()
# Example usage for testing
if __name__ == "__main__":
input_hard_skills = "Python, Java, Finance, Excel"
input_soft_skills = "Communication, Teamwork"
input_major = "Computer Science"
jobs_data = pd.read_csv("jobs_data.csv")
model_path = "recommendation_model.pkl"
recommended_jobs = recommend_jobs_for_input_skills(input_hard_skills, input_soft_skills, input_major, jobs_data, model_path)
print("Recommended Jobs based on input skills and major:")
print(recommended_jobs)