import gradio as gr import joblib import numpy as np import pandas as pd from huggingface_hub import hf_hub_download from sklearn.preprocessing import StandardScaler, OneHotEncoder, LabelEncoder # Load the trained model and scaler objects from file REPO_ID = "Hemg/modelxxx" # hugging face repo ID MODEL_FILENAME = "predjob.joblib" # model file name SCALER_FILENAME = "scalejob.joblib" # scaler file name model = joblib.load(hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME)) scaler = joblib.load(hf_hub_download(repo_id=REPO_ID, filename=SCALER_FILENAME)) def encode_categorical_columns(df): label_encoder = LabelEncoder() # Identify categorical columns ordinal_columns = df.select_dtypes(include=['object']).columns # Encode ordinal columns using LabelEncoder for col in ordinal_columns: df[col] = label_encoder.fit_transform(df[col]) # Get nominal columns for one-hot encoding nominal_columns = df.select_dtypes(include=['object']).columns.difference(ordinal_columns) # Apply one-hot encoding to nominal columns (drop the first column to avoid multicollinearity) df = pd.get_dummies(df, columns=nominal_columns, drop_first=True) return df def predict_performance(Location, Course,Faculty, College,Source, Event, Presenter, Visited_Parent, Visited_College_for_Inquiry, Attended_Any_Event, College_Fee, GPA, Year): input_data = [Location, Course, Faculty, Source, Event, Presenter, Visited_Parent, Visited_College_for_Inquiry, Attended_Any_Event, College_Fee, GPA, Year] # Updated feature names to use spaces instead of underscores to match training data feature_names = [ "Location", "Course", "Faculty", "College","Source", "Event", "Presenter", "Visited Parent", "Visited College for Inquiry", "Attended Any Event", "College Fee", "GPA", "Year" ] input_df = pd.DataFrame([input_data], columns=feature_names) # Debug print 2: Show DataFrame before encoding print("\nDataFrame before encoding:") print(input_df) # Encode categorical columns df = encode_categorical_columns(input_df) # Debug print 3: Show DataFrame after encoding print("\nDataFrame after encoding:") print(df) # Scale input data using the loaded scaler scaled_input = scaler.transform(df) # Make the prediction prediction = model.predict(scaled_input)[0] # Clip the prediction to be between 0 and 1 prediction = np.clip(prediction, 0, 1) # Debug print print("\nPrediction details:") print(f"Raw prediction: {prediction}") return f"Chance of Admission: {prediction:.1f}" # Create Gradio interface iface = gr.Interface( fn=predict_performance, inputs=[ gr.Radio(["Kathmandu", "Bhaktapur", "Lalitpur", "Kritipur"], label="Location"), gr.Radio(["MSc IT & Applied Security", "BSc (Hons) Computing", "BSc (Hons) Computing with Artificial Intelligence", "BSc (Hons) Computer Networking & IT Security", "BSc (Hons) Multimedia Technologies", "MBA", "BA (Hons) Accounting & Finance", "BA (Hons) Business Administration"], label="Course"), gr.Radio(["Science", "Management", "Humanities"], label="Faculty"), gr.Radio(["Event", "Facebook", "Instagram", "Offline", "Recommendation"], label="Source"), gr.Radio(["New Year", "Dashain", "Orientation", "Fresher's Party", "Holi Festival", "Welcome Ceremony"], label="Event"), gr.Radio(["Ram", "Gita", "Manish", "Shyam", "Raj", "Hari", "Rina", "Shree"], label="Presenter"), gr.Radio(["Yes", "No"], label="Visited Parent"), gr.Radio(["Trinity", "CCRC", "KMC", "SOS", "ISMT", "St. Xavier's", "Everest", "Prime"], label="College") gr.Radio(["Yes", "No"], label="Visited College for Inquiry"), gr.Radio(["New Year", "Dashain", "Orientation", "Fresher's Party", "Holi Festival", "Welcome Ceremony"], label="Event"), gr.Radio(["Yes", "No"], label="Attended Any Event"), gr.Radio(["Yes", "No"], label="Visited Parent") ], outputs="text", title="Chance of Student Admission", description="Predict the chances of a student's admission based on various inputs." ) # Run the app if __name__ == "__main__": iface.launch(share=True)