Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pickle
|
| 4 |
+
|
| 5 |
+
# Load the trained model, label encoder, and vectorizer
|
| 6 |
+
def load_resources():
|
| 7 |
+
with open("model.pkl", "rb") as f:
|
| 8 |
+
model = pickle.load(f)
|
| 9 |
+
with open("label_encoder.pkl", "rb") as f:
|
| 10 |
+
label_enc = pickle.load(f)
|
| 11 |
+
with open("vectorizer.pkl", "rb") as f:
|
| 12 |
+
vectorizer = pickle.load(f)
|
| 13 |
+
return model, label_enc, vectorizer
|
| 14 |
+
|
| 15 |
+
# Prediction function
|
| 16 |
+
def predict_project(fname, lname, email, role, experience, skills):
|
| 17 |
+
# Load resources (model, label encoder, and vectorizer)
|
| 18 |
+
model, label_enc, vectorizer = load_resources()
|
| 19 |
+
|
| 20 |
+
# Prepare the new employee data
|
| 21 |
+
new_employee = {
|
| 22 |
+
'fname': fname,
|
| 23 |
+
'lname': lname,
|
| 24 |
+
'email': email,
|
| 25 |
+
'role': role, # Role can be 'Senior' or 'Intern'
|
| 26 |
+
'experience': experience,
|
| 27 |
+
'skills': skills,
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
# 1. Encode the role
|
| 31 |
+
new_employee['role'] = label_enc.transform([new_employee['role']])[0] # Convert 'Intern' or 'Senior' to 0 or 1
|
| 32 |
+
|
| 33 |
+
# 2. Transform the skills using the same vectorizer used during training
|
| 34 |
+
skills_vector = vectorizer.transform([new_employee['skills']])
|
| 35 |
+
|
| 36 |
+
# 3. Combine the features (experience, role, and skills)
|
| 37 |
+
X_new_employee = np.hstack((
|
| 38 |
+
np.array([[new_employee['experience']]]), # Reshape experience to 2D
|
| 39 |
+
np.array([[new_employee['role']]]), # Reshape role to 2D
|
| 40 |
+
skills_vector.toarray() # skills_vector.toarray() is already 2D
|
| 41 |
+
))
|
| 42 |
+
|
| 43 |
+
# 4. Predict the project ID for the new employee
|
| 44 |
+
predicted_project = model.predict(X_new_employee)[0]
|
| 45 |
+
|
| 46 |
+
return f"The predicted project ID for {new_employee['fname']} {new_employee['lname']} is {predicted_project}"
|
| 47 |
+
|
| 48 |
+
# Create the Gradio interface
|
| 49 |
+
interface = gr.Interface(
|
| 50 |
+
fn=predict_project,
|
| 51 |
+
inputs=[
|
| 52 |
+
gr.Textbox(label="First Name"),
|
| 53 |
+
gr.Textbox(label="Last Name"),
|
| 54 |
+
gr.Textbox(label="Email"),
|
| 55 |
+
gr.Dropdown(choices=["Intern", "Senior"], label="Role"),
|
| 56 |
+
gr.Number(label="Experience (in years)", min=0),
|
| 57 |
+
gr.Textbox(label="Skills (comma separated)"),
|
| 58 |
+
],
|
| 59 |
+
outputs="text",
|
| 60 |
+
live=True,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Launch the Gradio app
|
| 64 |
+
interface.launch()
|