Knight-coderr commited on
Commit
9739a70
·
verified ·
1 Parent(s): a2515bb

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +101 -0
  2. best_model.pkl +3 -0
  3. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import pandas as pd
4
+ import mlflow
5
+ from sklearn.preprocessing import LabelEncoder
6
+ from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
7
+ import streamlit as st
8
+ import numpy as np
9
+ import joblib
10
+
11
+ url = './archive/ds_salaries.csv' # replace with the actual path
12
+
13
+ # Load the dataset
14
+ data = pd.read_csv(url)
15
+
16
+ data = data.drop('Unnamed: 0', axis=1)
17
+
18
+
19
+ # Fill missing values if any
20
+ data = data.fillna(method='ffill')
21
+
22
+ # Encode categorical variables
23
+ label_encoders = {}
24
+ categorical_columns = ['experience_level', 'employment_type', 'job_title', 'salary_currency', 'employee_residence', 'company_location', 'company_size']
25
+
26
+ for col in categorical_columns:
27
+ le = LabelEncoder()
28
+ data[col] = le.fit_transform(data[col])
29
+ label_encoders[col] = le
30
+
31
+ from sklearn.model_selection import train_test_split
32
+
33
+ # Define features and target variable
34
+ X = data.drop('salary_in_usd', axis=1)
35
+ y = data['salary_in_usd']
36
+
37
+ # Split the data into training and testing sets
38
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
39
+
40
+ print(f"Training set size: {X_train.shape[0]}")
41
+ print(f"Testing set size: {X_test.shape[0]}")
42
+
43
+
44
+ # Load the best model
45
+ # best_model = mlflow.sklearn.load_model("runs:/2f24d11653334bfc8611ef5edbe52bfd/model")
46
+
47
+ # Load the best model
48
+ best_model = joblib.load('best_model.pkl')
49
+
50
+ # Streamlit app
51
+ st.title("Salary Prediction App")
52
+
53
+ # Input features
54
+ work_year = st.number_input('Work Year', min_value=2020, max_value=2024, step=1)
55
+ experience_level = st.selectbox("Experience Level", label_encoders['experience_level'].classes_)
56
+ employment_type = st.selectbox("Employment Type", label_encoders['employment_type'].classes_)
57
+ job_title = st.selectbox("Job Title", label_encoders['job_title'].classes_)
58
+ salary = st.number_input('Salary', min_value=0)
59
+ salary_currency = st.selectbox("Salary Currency", label_encoders['salary_currency'].classes_)
60
+ employee_residence = st.selectbox("Employee Residence", label_encoders['employee_residence'].classes_)
61
+ remote_ratio = st.slider("Remote Ratio", 0, 100)
62
+ company_location = st.selectbox("Company Location", label_encoders['company_location'].classes_)
63
+ company_size = st.selectbox("Company Size", label_encoders['company_size'].classes_)
64
+
65
+
66
+ def predict_salary():
67
+ # Encode input features
68
+ encoded_experience_level = label_encoders['experience_level'].transform([experience_level])[0]
69
+ encoded_employment_type = label_encoders['employment_type'].transform([employment_type])[0]
70
+ encoded_job_title = label_encoders['job_title'].transform([job_title])[0]
71
+ encoded_salary_currency = label_encoders['salary_currency'].transform([salary_currency])[0]
72
+ encoded_employee_residence = label_encoders['employee_residence'].transform([employee_residence])[0]
73
+ encoded_company_location = label_encoders['company_location'].transform([company_location])[0]
74
+ encoded_company_size = label_encoders['company_size'].transform([company_size])[0]
75
+
76
+ # Create input array matching training data format
77
+ input_features = np.array([
78
+ work_year,
79
+ encoded_experience_level,
80
+ encoded_employment_type,
81
+ encoded_job_title,
82
+ salary,
83
+ encoded_salary_currency,
84
+ encoded_employee_residence,
85
+ remote_ratio,
86
+ encoded_company_location,
87
+ encoded_company_size,
88
+ ]).reshape(1, -1)
89
+ # Make prediction
90
+ predicted_salary = best_model.predict(input_features)[0]
91
+ return predicted_salary
92
+
93
+ # Button to trigger prediction
94
+ if st.button("Predict Salary"):
95
+ predicted_salary = predict_salary()
96
+ st.write(f"Predicted Salary (in USD): {predicted_salary}")
97
+
98
+ # Display model performance metrics
99
+ st.write(f"RMSE: {mean_squared_error(y_test, best_model.predict(X_test), squared=False)}")
100
+ st.write(f"MAE: {mean_absolute_error(y_test, best_model.predict(X_test))}")
101
+ st.write(f"R²: {r2_score(y_test, best_model.predict(X_test))}")
best_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4fd0785ba695a9dbc50a84b1b3a4e38289f9aabb6bd39a50196349c6179a1c58
3
+ size 5834256
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ mlflow==2.13.2
2
+ cloudpickle==3.0.0
3
+ numpy==1.24.4
4
+ packaging==24.1
5
+ psutil==5.9.8
6
+ pyyaml==6.0.1
7
+ scikit-learn==1.3.2
8
+ scipy==1.10.1