Update app.py
Browse files
app.py
CHANGED
@@ -6,128 +6,97 @@ from huggingface_hub import hf_hub_download
|
|
6 |
from sklearn.preprocessing import StandardScaler, OneHotEncoder, LabelEncoder
|
7 |
|
8 |
# Load the trained model and scaler objects from file
|
9 |
-
REPO_ID = "Hemg/modelxxx" # hugging face repo ID
|
10 |
-
MODEL_FILENAME = "predjob.joblib" # model file name
|
11 |
-
SCALER_FILENAME = "scalejob.joblib" # scaler file name
|
12 |
|
13 |
-
model = joblib.load(hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME))
|
14 |
-
scaler = joblib.load(hf_hub_download(repo_id=REPO_ID, filename=SCALER_FILENAME))
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def encode_categorical_columns(df):
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
# Define the expected numeric columns
|
28 |
-
numeric_columns = ["College Fee", "GPA", "Year"]
|
29 |
-
|
30 |
-
# Create label encoder dictionary
|
31 |
-
label_encoders = {}
|
32 |
-
|
33 |
-
# Encode each categorical column
|
34 |
-
for col in categorical_columns:
|
35 |
-
label_encoders[col] = LabelEncoder()
|
36 |
-
df[col] = label_encoders[col].fit_transform(df[col].astype(str))
|
37 |
-
|
38 |
-
# Ensure numeric columns are float type
|
39 |
-
for col in numeric_columns:
|
40 |
-
df[col] = df[col].astype(float)
|
41 |
-
|
42 |
-
# Ensure columns are in the correct order
|
43 |
-
df = df[categorical_columns + numeric_columns]
|
44 |
-
|
45 |
return df
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
"Visited College for Inquiry": [Visited_College_for_Inquiry],
|
60 |
-
"Attended Any Event": [Attended_Any_Event],
|
61 |
-
"College Fee": [float(College_Fee)],
|
62 |
-
"GPA": [float(GPA)],
|
63 |
-
"Year": [float(Year)]
|
64 |
-
}
|
65 |
|
66 |
-
input_df = pd.DataFrame(input_data)
|
67 |
|
68 |
# Debug print 2: Show DataFrame before encoding
|
69 |
print("\nDataFrame before encoding:")
|
70 |
print(input_df)
|
71 |
|
72 |
-
# Encode categorical columns
|
73 |
df = encode_categorical_columns(input_df)
|
74 |
|
75 |
# Debug print 3: Show DataFrame after encoding
|
76 |
print("\nDataFrame after encoding:")
|
77 |
print(df)
|
78 |
|
79 |
-
# Ensure the DataFrame columns match the order used during scaler fitting
|
80 |
-
expected_columns = ["Location", "Course", "Faculty", "College", "Source", "Event",
|
81 |
-
"Presenter", "Visited Parent", "Visited College for Inquiry",
|
82 |
-
"Attended Any Event", "College Fee", "GPA", "Year"]
|
83 |
-
df = df[expected_columns]
|
84 |
-
|
85 |
-
# Scale input data using the loaded scaler
|
86 |
scaled_input = scaler.transform(df)
|
87 |
|
88 |
-
#
|
89 |
-
|
|
|
90 |
|
91 |
-
|
92 |
-
prediction = np.clip(prediction, 0, 1)
|
93 |
|
94 |
-
# Debug print
|
95 |
print("\nPrediction details:")
|
96 |
print(f"Raw prediction: {prediction}")
|
|
|
|
|
|
|
|
|
97 |
|
98 |
-
return f"Chance of Admission: {
|
99 |
-
|
100 |
-
# Create Gradio interface
|
101 |
iface = gr.Interface(
|
102 |
fn=predict_performance,
|
103 |
inputs=[
|
104 |
gr.Radio(["Kathmandu", "Bhaktapur", "Lalitpur", "Kritipur"], label="Location"),
|
105 |
-
gr.
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
"
|
110 |
-
"BA (Hons) Business Administration"], label="
|
111 |
gr.Radio(["Science", "Management", "Humanities"], label="Faculty"),
|
112 |
-
gr.Radio(["Trinity", "CCRC", "KMC", "SOS", "ISMT", "St. Xavier's", "Everest", "Prime"], label="College"),
|
113 |
gr.Radio(["Event", "Facebook", "Instagram", "Offline", "Recommendation"], label="Source"),
|
114 |
-
gr.Radio(["
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
gr.Radio(["
|
119 |
-
|
120 |
-
gr.Radio(["Yes", "No"], label="
|
121 |
-
|
122 |
-
gr.Number(label="College Fee"),
|
123 |
-
gr.Number(label="GPA"),
|
124 |
-
gr.Number(label="Year")
|
125 |
],
|
126 |
outputs="text",
|
127 |
-
title="
|
128 |
-
description="
|
129 |
)
|
130 |
|
131 |
# Run the app
|
132 |
if __name__ == "__main__":
|
133 |
-
iface.launch(share=True)
|
|
|
6 |
from sklearn.preprocessing import StandardScaler, OneHotEncoder, LabelEncoder
|
7 |
|
8 |
# Load the trained model and scaler objects from file
|
|
|
|
|
|
|
9 |
|
|
|
|
|
10 |
|
11 |
+
REPO_ID = "Hemg/modelxxx" # hugging face repo ID
|
12 |
+
MoDEL_FILENAME = "studentpredict.joblib" # model file name
|
13 |
+
SCALER_FILENAME ="studentscaler.joblib" # scaler file name
|
14 |
+
|
15 |
+
model = joblib.load(hf_hub_download(repo_id=REPO_ID, filename=MoDEL_FILENAME))
|
16 |
+
|
17 |
+
scaler = joblib.load(hf_hub_download(repo_id=REPO_ID, filename=SCALER_FILENAME))
|
18 |
+
|
19 |
def encode_categorical_columns(df):
|
20 |
+
label_encoder = LabelEncoder()
|
21 |
+
ordinal_columns = df.select_dtypes(include=['object']).columns
|
22 |
+
|
23 |
+
for col in ordinal_columns:
|
24 |
+
df[col] = label_encoder.fit_transform(df[col])
|
25 |
+
|
26 |
+
nominal_columns = df.select_dtypes(include=['object']).columns.difference(ordinal_columns)
|
27 |
+
df = pd.get_dummies(df, columns=nominal_columns, drop_first=True)
|
28 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
return df
|
30 |
|
31 |
+
# Define the prediction function
|
32 |
+
def predict_performance(Location, College_Fee,College, GPA, Year, Course_Interested, Faculty, Source,
|
33 |
+
Visited_College_for_Inquiry_Only, Event, Attended_Any_Events,
|
34 |
+
Presenter, Visited_Parents):
|
35 |
+
|
36 |
+
input_data = [[Location, College_Fee,College, GPA, Year, Course_Interested, Faculty, Source,
|
37 |
+
Visited_College_for_Inquiry_Only, Event, Attended_Any_Events,
|
38 |
+
Presenter, Visited_Parents]]
|
39 |
+
|
40 |
+
feature_names = ["Location", "College Fee","College", "GPA", "Year", "Course Interested",
|
41 |
+
"Faculty", "Source", "Visited College for Inquiry Only", "Event",
|
42 |
+
"Attended Any Events", "Presenter", "Visited Parents"]
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
input_df = pd.DataFrame(input_data, columns=feature_names)
|
45 |
|
46 |
# Debug print 2: Show DataFrame before encoding
|
47 |
print("\nDataFrame before encoding:")
|
48 |
print(input_df)
|
49 |
|
|
|
50 |
df = encode_categorical_columns(input_df)
|
51 |
|
52 |
# Debug print 3: Show DataFrame after encoding
|
53 |
print("\nDataFrame after encoding:")
|
54 |
print(df)
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
scaled_input = scaler.transform(df)
|
57 |
|
58 |
+
# Debug print 4: Show scaled input
|
59 |
+
print("\nScaled input:")
|
60 |
+
print(scaled_input)
|
61 |
|
62 |
+
prediction = model.predict(scaled_input)[0]
|
|
|
63 |
|
64 |
+
# Debug print 5: Show prediction details
|
65 |
print("\nPrediction details:")
|
66 |
print(f"Raw prediction: {prediction}")
|
67 |
+
prediction_probability = 1 / (1 + np.exp(-prediction))
|
68 |
+
print(f"Probability: {prediction_probability}")
|
69 |
+
prediction_percentage = prediction_probability * 100
|
70 |
+
print(f"Percentage: {prediction_percentage}")
|
71 |
|
72 |
+
return f"Chance of Admission: {prediction_percentage:.1f}%"
|
73 |
+
|
|
|
74 |
iface = gr.Interface(
|
75 |
fn=predict_performance,
|
76 |
inputs=[
|
77 |
gr.Radio(["Kathmandu", "Bhaktapur", "Lalitpur", "Kritipur"], label="Location"),
|
78 |
+
gr.Slider(minimum=1000000, maximum=1700000, label="College Fee"),
|
79 |
+
gr.Slider(minimum=2, maximum=3, label="GPA"), # Fixed GPA input
|
80 |
+
gr.Slider(minimum=2019, maximum=2025, step=1, label="Year"),
|
81 |
+
gr.Radio(["MSc IT & Applied Security", "BSc (Hons) Computing", "BSc (Hons) Computing with Artificial Intelligence",
|
82 |
+
"BSc (Hons) Computer Networking & IT Security", "BSc (Hons) Multimedia Technologies", "MBA",
|
83 |
+
"BA (Hons) Accounting & Finance", "BA (Hons) Business Administration"], label="Course_Interested"),
|
84 |
gr.Radio(["Science", "Management", "Humanities"], label="Faculty"),
|
|
|
85 |
gr.Radio(["Event", "Facebook", "Instagram", "Offline", "Recommendation"], label="Source"),
|
86 |
+
gr.Radio(["Yes", "No"], label="visited_college_for_inquery_only"),
|
87 |
+
gr.Radio(["New Year", "Dashain", "Orientation", "Fresher's Party", "Holi Festival", "Welcome Ceremony"],
|
88 |
+
label="attended_event_name"), # Changed from Slider to Radio
|
89 |
+
gr.Radio(["Yes", "No"], label="attended_any_event"),
|
90 |
+
gr.Radio(["Trinity", "CCRC", "KMC", "SOS", "ISMT", "St. Xavier's", "Everest", "Prime"], label="College"),
|
91 |
+
gr.Radio(["Ram", "Gita", "Manish", "Shyam", "Raj", "Hari", "Rina", "Shree"], label="Presenter"),
|
92 |
+
gr.Radio(["Yes", "No"], label="visited_with_parents") ["Trinity", "CCRC", "KMC", "SOS", "ISMT", "St. Xavier's", "Everest", "Prime"]
|
93 |
+
|
|
|
|
|
|
|
94 |
],
|
95 |
outputs="text",
|
96 |
+
title="chances of student admission",
|
97 |
+
description="chances of student admission"
|
98 |
)
|
99 |
|
100 |
# Run the app
|
101 |
if __name__ == "__main__":
|
102 |
+
iface.launch(share=True)
|