Ujeshhh commited on
Commit
ba16e2e
Β·
verified Β·
1 Parent(s): 8784175

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -38
app.py CHANGED
@@ -6,36 +6,53 @@ 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
  # Load or train models
11
- try:
12
- with open("scaler.pkl", "rb") as scaler_file:
13
- scaler = pickle.load(scaler_file)
14
-
15
- with open("logistic_regression.pkl", "rb") as model_file:
16
- logistic_regression = pickle.load(model_file)
17
-
18
- with open("perceptron.pkl", "rb") as model_file:
19
- perceptron = pickle.load(model_file)
20
-
21
- except FileNotFoundError:
22
- print("Training models...")
 
 
 
 
 
 
 
 
 
 
23
  df = pd.read_excel("Student-Employability-Datasets.xlsx", sheet_name="Data")
24
- X = df.iloc[:, 1:-2].values
25
- y = (df["CLASS"] == "Employable").astype(int)
26
-
 
 
 
27
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
28
-
 
29
  scaler = StandardScaler()
30
  X_train_scaled = scaler.fit_transform(X_train)
31
  X_test_scaled = scaler.transform(X_test)
32
-
 
33
  logistic_regression = LogisticRegression(random_state=42)
34
  logistic_regression.fit(X_train_scaled, y_train)
35
-
36
  perceptron = Perceptron(random_state=42)
37
  perceptron.fit(X_train_scaled, y_train)
38
-
 
39
  with open("scaler.pkl", "wb") as scaler_file:
40
  pickle.dump(scaler, scaler_file)
41
  with open("logistic_regression.pkl", "wb") as model_file:
@@ -43,27 +60,44 @@ except FileNotFoundError:
43
  with open("perceptron.pkl", "wb") as model_file:
44
  pickle.dump(perceptron, model_file)
45
 
46
- # Prediction function
 
 
47
  def predict_employability(name, ga, mos, pc, ma, sc, api, cs, model_choice):
48
- if not name:
49
- name = "The candidate"
50
-
51
- input_data = np.array([[ga, mos, pc, ma, sc, api, cs]])
52
- input_scaled = scaler.transform(input_data)
53
-
54
- if model_choice == "Logistic Regression":
55
- prediction = logistic_regression.predict(input_scaled)
56
- else:
57
- prediction = perceptron.predict(input_scaled)
58
-
59
- if prediction[0] == 1:
60
- return f"{name} is Employable 😊"
61
- else:
62
- return f"{name} is Less Employable - Work Hard! πŸ’ͺ"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  # Gradio UI
65
  with gr.Blocks() as app:
66
- gr.Markdown("# Employability Evaluation πŸš€")
 
67
  with gr.Row():
68
  with gr.Column():
69
  name = gr.Textbox(label="Name")
@@ -75,7 +109,7 @@ with gr.Blocks() as app:
75
  api = gr.Slider(1, 5, step=1, label="Ability to Present Ideas")
76
  cs = gr.Slider(1, 5, step=1, label="Communication Skills")
77
  model_choice = gr.Radio(["Logistic Regression", "Perceptron"], label="Select Model")
78
-
79
  predict_btn = gr.Button("Get Evaluation")
80
 
81
  with gr.Column():
@@ -87,4 +121,6 @@ with gr.Blocks() as app:
87
  inputs=[name, ga, mos, pc, ma, sc, api, cs, model_choice],
88
  outputs=[result_output]
89
  )
90
- app.launch(server_port=7860, debug=True, share=True)
 
 
 
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
+ import os
10
 
11
  # Load or train models
12
+ model_files = ["scaler.pkl", "logistic_regression.pkl", "perceptron.pkl"]
13
+
14
+ # Check if all model files exist
15
+ if all(os.path.exists(file) for file in model_files):
16
+ try:
17
+ with open("scaler.pkl", "rb") as scaler_file:
18
+ scaler = pickle.load(scaler_file)
19
+
20
+ with open("logistic_regression.pkl", "rb") as model_file:
21
+ logistic_regression = pickle.load(model_file)
22
+
23
+ with open("perceptron.pkl", "rb") as model_file:
24
+ perceptron = pickle.load(model_file)
25
+
26
+ except Exception as e:
27
+ print(f"Error loading models: {e}")
28
+ exit()
29
+
30
+ else:
31
+ print("Training models from scratch...")
32
+
33
+ # Load dataset
34
  df = pd.read_excel("Student-Employability-Datasets.xlsx", sheet_name="Data")
35
+
36
+ # Prepare dataset
37
+ X = df.iloc[:, 1:-1].values # Select all feature columns
38
+ y = (df["CLASS"] == "Employable").astype(int) # Convert to binary labels
39
+
40
+ # Train-test split
41
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
42
+
43
+ # Feature scaling
44
  scaler = StandardScaler()
45
  X_train_scaled = scaler.fit_transform(X_train)
46
  X_test_scaled = scaler.transform(X_test)
47
+
48
+ # Train models
49
  logistic_regression = LogisticRegression(random_state=42)
50
  logistic_regression.fit(X_train_scaled, y_train)
51
+
52
  perceptron = Perceptron(random_state=42)
53
  perceptron.fit(X_train_scaled, y_train)
54
+
55
+ # Save trained models
56
  with open("scaler.pkl", "wb") as scaler_file:
57
  pickle.dump(scaler, scaler_file)
58
  with open("logistic_regression.pkl", "wb") as model_file:
 
60
  with open("perceptron.pkl", "wb") as model_file:
61
  pickle.dump(perceptron, model_file)
62
 
63
+ print("Training complete. Models saved.")
64
+
65
+ # Employability prediction function
66
  def predict_employability(name, ga, mos, pc, ma, sc, api, cs, model_choice):
67
+ try:
68
+ if not name:
69
+ name = "The candidate"
70
+
71
+ input_data = np.array([[ga, mos, pc, ma, sc, api, cs]])
72
+
73
+ # Ensure scaler is correctly loaded
74
+ if scaler is None:
75
+ return "Error: Scaler not loaded properly."
76
+
77
+ # Transform input
78
+ input_scaled = scaler.transform(input_data)
79
+
80
+ # Select model and make prediction
81
+ if model_choice == "Logistic Regression":
82
+ prediction = logistic_regression.predict(input_scaled)
83
+ elif model_choice == "Perceptron":
84
+ prediction = perceptron.predict(input_scaled)
85
+ else:
86
+ return "Error: Invalid model selection."
87
+
88
+ # Return result
89
+ if prediction[0] == 1:
90
+ return f"{name} is Employable 😊"
91
+ else:
92
+ return f"{name} is Less Employable - Work Hard! πŸ’ͺ"
93
+
94
+ except Exception as e:
95
+ return f"Error: {str(e)}"
96
 
97
  # Gradio UI
98
  with gr.Blocks() as app:
99
+ gr.Markdown("# πŸš€ Employability Evaluation System")
100
+
101
  with gr.Row():
102
  with gr.Column():
103
  name = gr.Textbox(label="Name")
 
109
  api = gr.Slider(1, 5, step=1, label="Ability to Present Ideas")
110
  cs = gr.Slider(1, 5, step=1, label="Communication Skills")
111
  model_choice = gr.Radio(["Logistic Regression", "Perceptron"], label="Select Model")
112
+
113
  predict_btn = gr.Button("Get Evaluation")
114
 
115
  with gr.Column():
 
121
  inputs=[name, ga, mos, pc, ma, sc, api, cs, model_choice],
122
  outputs=[result_output]
123
  )
124
+
125
+ # Launch the app
126
+ app.launch(server_port=7860, debug=True, share=True)