Job_V1 / app.py
poudel's picture
Update app.py
74fc87d verified
import gradio as gr
import joblib
import pandas as pd
import numpy as np
from huggingface_hub import hf_hub_download
# Download the model from Hugging Face hub
model_filename = hf_hub_download(repo_id="poudel/Job_Predictor", filename="random_forest_pipeline.pkl")
# Load the model directly
loaded_model = joblib.load(model_filename)
# Download the dataset (CSV) from Hugging Face hub
data_filename = hf_hub_download(repo_id="poudel/Job_Predictor", filename="cleaned_erecruit_open_data.csv")
# Load the CSV dataset
data = pd.read_csv(data_filename)
# Get unique values for dropdowns
position_titles = data['PositionTitle'].unique().tolist()
designations = data['Designation'].unique().tolist()
agencies = data['Agency'].unique().tolist()
vacancy_types = data['VacancyType'].unique().tolist()
employment_categories = data['EmploymentCategory'].unique().tolist()
locations = data['Locations'].unique().tolist()
vacancy_6_months_or_less = data['Vacancy6MonthsOrLess'].unique().tolist()
# Define a function to make predictions based on user input
def predict_applicants(position_title, designation, agency, vacancy_type, employment_category, location, vacancy_6_months_or_less, number_of_vacancies, number_of_successful_applicants):
# Create a DataFrame from the inputs
input_data = pd.DataFrame({
'PositionTitle': [position_title],
'Designation': [designation],
'Agency': [agency],
'VacancyType': [vacancy_type],
'EmploymentCategory': [employment_category],
'Locations': [location],
'Vacancy6MonthsOrLess': [vacancy_6_months_or_less],
'NumberOfSuccessfulApplicants': [number_of_successful_applicants],
'NumberOfVacancies': [number_of_vacancies]
})
# Calculate additional features
input_data['Success_Ratio'] = input_data['NumberOfSuccessfulApplicants'] / input_data['NumberOfVacancies'].replace(0, np.nan)
input_data['Applicants_per_Vacancy'] = input_data['NumberOfVacancies'] / np.where(input_data['NumberOfSuccessfulApplicants'] == 0, np.nan, input_data['NumberOfSuccessfulApplicants'])
# Avoid inplace modification, return to the column
input_data['Success_Ratio'] = input_data['Success_Ratio'].fillna(0)
input_data['Applicants_per_Vacancy'] = input_data['Applicants_per_Vacancy'].fillna(0)
# Make predictions using the loaded model pipeline
try:
prediction = loaded_model.predict(input_data)
return f"Predicted Number of Applicants: {int(prediction[0])}"
except Exception as e:
return f"Error during prediction: {str(e)}"
# Create the Gradio Blocks Interface
with gr.Blocks() as interface:
# Add a title and description
gr.Markdown("# NT's Job Predictor")
gr.Markdown("Select the job details below to predict the number of applicants for a given position.")
with gr.Row():
position_title_input = gr.Dropdown(choices=position_titles, label="Position Title", value=None)
designation_input = gr.Dropdown(choices=designations, label="Designation", value=None)
agency_input = gr.Dropdown(choices=agencies, label="Agency", value=None)
with gr.Row():
vacancy_type_input = gr.Dropdown(choices=vacancy_types, label="Vacancy Type", value=None)
employment_category_input = gr.Dropdown(choices=employment_categories, label="Employment Category", value=None)
location_input = gr.Dropdown(choices=locations, label="Locations", value=None)
vacancy_6_months_or_less_input = gr.Dropdown(choices=vacancy_6_months_or_less, label="Vacancy 6 Months or Less", value=None)
with gr.Row():
number_of_vacancies_input = gr.Number(label="Past Number of Vacancies", value=None)
number_of_successful_applicants_input = gr.Number(label="Past Number of Successful Applicants", value=None)
predict_button = gr.Button("Predict")
predicted_applicants_output = gr.Textbox(label="Predicted Number of Applicants")
predict_button.click(
fn=predict_applicants,
inputs=[
position_title_input,
designation_input,
agency_input,
vacancy_type_input,
employment_category_input,
location_input,
vacancy_6_months_or_less_input,
number_of_vacancies_input,
number_of_successful_applicants_input
],
outputs=predicted_applicants_output
)
interface.launch(share=True)