Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,60 @@
|
|
| 1 |
-
import
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import joblib
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
"
|
| 14 |
-
"
|
| 15 |
-
"
|
| 16 |
-
"
|
| 17 |
-
"
|
| 18 |
-
"
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
"
|
| 26 |
-
"
|
| 27 |
-
"
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
"
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
"
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
"
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
"
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
X_new_df = pd.DataFrame([[country,education,expericence,employment]],
|
| 64 |
-
columns = ['Country', 'EdLevel', 'YearsCodePro', 'Employment'])
|
| 65 |
-
print("##########")
|
| 66 |
-
print("##########")
|
| 67 |
-
print("##########")
|
| 68 |
-
print(model)
|
| 69 |
-
print("##########")
|
| 70 |
-
print("##########")
|
| 71 |
-
print("##########")
|
| 72 |
-
salary = model.predict(X_new_df)
|
| 73 |
-
|
| 74 |
-
st.subheader(f"The estimated salary is {salary[0]:.2f} $")\
|
| 75 |
-
|
|
|
|
| 1 |
+
import requests
|
|
|
|
|
|
|
| 2 |
import joblib
|
| 3 |
+
import os
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
+
|
| 9 |
+
countries = [
|
| 10 |
+
"United States of America", "Other", "Germany",
|
| 11 |
+
"United Kingdom of Great Britain and Northern Ireland",
|
| 12 |
+
"India", "Canada", "France", "Brazil", "Spain",
|
| 13 |
+
"Netherlands", "Australia", "Italy", "Poland",
|
| 14 |
+
"Sweden", "Russian Federation", "Switzerland", "Turkey",
|
| 15 |
+
"Israel", "Austria", "Norway", "Portugal", "Denmark",
|
| 16 |
+
"Belgium", "Finland", "Mexico", "New Zealand", "Greece",
|
| 17 |
+
"South Africa", "Pakistan", "Czech Republic",
|
| 18 |
+
"Iran, Islamic Republic of"
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# Hugging Face model details
|
| 23 |
+
#model_url = "https://huggingface.co/hassanshe7/salary_model/resolve/main/salary_model.pkl?download=true"
|
| 24 |
+
model_url = "https://huggingface.co/spaces/hassanshe7/Stackoverflow_salary_prediction/resolve/main/salary_model.pkl?download=true"
|
| 25 |
+
model_path = "salary_model.pkl"
|
| 26 |
+
|
| 27 |
+
# Download model with authentication
|
| 28 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 29 |
+
response = requests.get(model_url, headers=headers)
|
| 30 |
+
|
| 31 |
+
if response.status_code == 200:
|
| 32 |
+
with open(model_path, "wb") as f:
|
| 33 |
+
f.write(response.content)
|
| 34 |
+
print("Model downloaded successfully.")
|
| 35 |
+
else:
|
| 36 |
+
print(f"Failed to download model: {response.text}")
|
| 37 |
+
exit(1)
|
| 38 |
+
|
| 39 |
+
# Load the trained model
|
| 40 |
+
model = joblib.load(model_path)
|
| 41 |
+
|
| 42 |
+
# Define prediction function
|
| 43 |
+
def predict_salary(country, education, experience, remote):
|
| 44 |
+
input_data = pd.DataFrame([[country, education, experience, remote]],
|
| 45 |
+
columns=['Country', 'EdLevel', 'YearsCodePro', 'RemoteWork'])
|
| 46 |
+
prediction = model.predict(input_data)[0]
|
| 47 |
+
return f"Estimated Salary: ${round(prediction, 2)}"
|
| 48 |
+
|
| 49 |
+
# Define Gradio app
|
| 50 |
+
inputs = [
|
| 51 |
+
gr.Dropdown(countries, label="Country"),
|
| 52 |
+
gr.Dropdown(["Bachelor’s degree", "Master’s degree", "Post grad", "Less than a Bachelors"], label="Education Level"),
|
| 53 |
+
gr.Number(label="Years of Professional Experience"),
|
| 54 |
+
gr.Dropdown(["Fully remote", "Hybrid", "In-office"], label="Remote Work Status")
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
output = gr.Textbox(label="Predicted Salary")
|
| 58 |
+
|
| 59 |
+
app = gr.Interface(fn=predict_salary, inputs=inputs, outputs=output, title="Salary Prediction App")
|
| 60 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|