Hemg commited on
Commit
451e4ba
1 Parent(s): 4b736c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -89
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
- # Create a copy of the DataFrame to avoid modifying the original
18
- df = df.copy()
19
-
20
- # Define the expected categorical columns and their order
21
- categorical_columns = [
22
- "Location", "Course", "Faculty", "College", "Source", "Event",
23
- "Presenter", "Visited Parent", "Visited College for Inquiry",
24
- "Attended Any Event"
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
- def predict_performance(Location, Course, Faculty, College, Source, Event, Presenter, Visited_Parent,
48
- Visited_College_for_Inquiry, Attended_Any_Event, College_Fee, GPA, Year):
49
- # Create input DataFrame with consistent column names
50
- input_data = {
51
- "Location": [Location],
52
- "Course": [Course],
53
- "Faculty": [Faculty],
54
- "College": [College],
55
- "Source": [Source],
56
- "Event": [Event],
57
- "Presenter": [Presenter],
58
- "Visited Parent": [Visited_Parent],
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
- # Make the prediction
89
- prediction = model.predict(scaled_input)[0]
 
90
 
91
- # Clip the prediction to be between 0 and 1
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: {prediction:.1f}"
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.Radio(["MSc IT & Applied Security", "BSc (Hons) Computing",
106
- "BSc (Hons) Computing with Artificial Intelligence",
107
- "BSc (Hons) Computer Networking & IT Security",
108
- "BSc (Hons) Multimedia Technologies", "MBA",
109
- "BA (Hons) Accounting & Finance",
110
- "BA (Hons) Business Administration"], label="Course"),
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(["New Year", "Dashain", "Orientation", "Fresher's Party",
115
- "Holi Festival", "Welcome Ceremony"], label="Event"),
116
- gr.Radio(["Ram", "Gita", "Manish", "Shyam", "Raj", "Hari", "Rina", "Shree"],
117
- label="Presenter"),
118
- gr.Radio(["Yes", "No"], label="Visited Parent"),
119
- # gr.Radio(["Trinity", "CCRC", "KMC", "SOS", "ISMT", "St. Xavier's", "Everest", "Prime"], label="College"),
120
- gr.Radio(["Yes", "No"], label="Visited College for Inquiry"),
121
- gr.Radio(["Yes", "No"], label="Attended Any Event"),
122
- gr.Number(label="College Fee"),
123
- gr.Number(label="GPA"),
124
- gr.Number(label="Year")
125
  ],
126
  outputs="text",
127
- title="Chance of Student Admission",
128
- description="Predict the chances of a student's admission based on various inputs."
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)