arshad1234321 commited on
Commit
08aeab9
·
verified ·
1 Parent(s): b1f6ef1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import pickle
4
+ import pandas as pd
5
+ from sklearn.preprocessing import StandardScaler
6
+ from sklearn.linear_model import LogisticRegression, Perceptron
7
+ from sklearn.model_selection import train_test_split
8
+ from sklearn.metrics import accuracy_score
9
+
10
+ def load_or_train_models():
11
+ """Load models from disk or train and save them if not found."""
12
+ try:
13
+ with open("scaler.pkl", "rb") as f:
14
+ scaler = pickle.load(f)
15
+ with open("logistic_regression.pkl", "rb") as f:
16
+ logistic_regression = pickle.load(f)
17
+ with open("perceptron.pkl", "rb") as f:
18
+ perceptron = pickle.load(f)
19
+ except FileNotFoundError:
20
+ print("Training models...")
21
+ df = pd.read_excel("Student-Employability-Datasets.xlsx", sheet_name="Data")
22
+ X = df.iloc[:, 1:-2].values
23
+ y = (df["CLASS"] == "Employable").astype(int)
24
+
25
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
26
+
27
+ scaler = StandardScaler()
28
+ X_train_scaled = scaler.fit_transform(X_train)
29
+ X_test_scaled = scaler.transform(X_test)
30
+
31
+ logistic_regression = LogisticRegression(random_state=42)
32
+ logistic_regression.fit(X_train_scaled, y_train)
33
+
34
+ perceptron = Perceptron(random_state=42)
35
+ perceptron.fit(X_train_scaled, y_train)
36
+
37
+ with open("scaler.pkl", "wb") as f:
38
+ pickle.dump(scaler, f)
39
+ with open("logistic_regression.pkl", "wb") as f:
40
+ pickle.dump(logistic_regression, f)
41
+ with open("perceptron.pkl", "wb") as f:
42
+ pickle.dump(perceptron, f)
43
+
44
+ return scaler, logistic_regression, perceptron
45
+ scaler, logistic_regression, perceptron = load_or_train_models()
46
+
47
+ def predict_employability(name, ga, mos, pc, ma, sc, api, cs, model_choice):
48
+ """Predict employability based on input features and selected model."""
49
+ name = name if name else "The candidate"
50
+ input_data = np.array([[ga, mos, pc, ma, sc, api, cs]])
51
+ input_scaled = scaler.transform(input_data)
52
+
53
+ model = logistic_regression if model_choice == "Logistic Regression" else perceptron
54
+ prediction = model.predict(input_scaled)[0]
55
+
56
+ return f"{name} is {'Employable 😊' if prediction == 1 else 'Less Employable - Work Hard! 💪'}"
57
+ with gr.Blocks() as app:
58
+ gr.Markdown("# Employability Evaluation ")
59
+
60
+ with gr.Row():
61
+ with gr.Column():
62
+ name = gr.Textbox(label="Name")
63
+ sliders = [
64
+ gr.Slider(1, 5, step=1, label=label) for label in [
65
+ "General Appearance", "Manner of Speaking", "Physical Condition",
66
+ "Mental Alertness", "Self Confidence", "Ability to Present Ideas",
67
+ "Communication Skills"
68
+ ]
69
+ ]
70
+ model_choice = gr.Radio(["Logistic Regression", "Perceptron"], label="Select Model")
71
+ predict_btn = gr.Button("Get Evaluation")
72
+
73
+ with gr.Column():
74
+ result_output = gr.Textbox(label="Employability Prediction")
75
+
76
+ predict_btn.click(
77
+ fn=predict_employability,
78
+ inputs=[name] + sliders + [model_choice],
79
+ outputs=[result_output]
80
+ )
81
+ app.launch(share=True)