Hemg's picture
Update app.py
045de33 verified
raw
history blame
5.25 kB
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):
# Create a copy of the DataFrame to avoid modifying the original
df = df.copy()
# Define the expected categorical columns and their order
categorical_columns = [
"Location", "Course", "Faculty", "College", "Source", "Event",
"Presenter", "Visited Parent", "Visited College for Inquiry",
"Attended Any Event"
]
# Define the expected numeric columns
numeric_columns = ["College Fee", "GPA", "Year"]
# Create label encoder dictionary
label_encoders = {}
# Encode each categorical column
for col in categorical_columns:
label_encoders[col] = LabelEncoder()
df[col] = label_encoders[col].fit_transform(df[col].astype(str))
# Ensure numeric columns are float type
for col in numeric_columns:
df[col] = df[col].astype(float)
# Ensure columns are in the correct order
df = df[categorical_columns + numeric_columns]
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):
# Create input DataFrame with consistent column names
input_data = {
"Location": [Location],
"Course": [Course],
"Faculty": [Faculty],
"College": [College],
"Source": [Source],
"Event": [Event],
"Presenter": [Presenter],
"Visited Parent": [Visited_Parent],
"Visited College for Inquiry": [Visited_College_for_Inquiry],
"Attended Any Event": [Attended_Any_Event],
"College Fee": [float(College_Fee)],
"GPA": [float(GPA)],
"Year": [float(Year)]
}
input_df = pd.DataFrame(input_data)
# 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)
# Ensure the DataFrame columns match the order used during scaler fitting
expected_columns = ["Location", "Course", "Faculty", "College", "Source", "Event",
"Presenter", "Visited Parent", "Visited College for Inquiry",
"Attended Any Event", "College Fee", "GPA", "Year"]
df = df[expected_columns]
# 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(["Trinity", "CCRC", "KMC", "SOS", "ISMT", "St. Xavier's", "Everest", "Prime"], label="College"),
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(["Yes", "No"], label="Attended Any Event"),
gr.Number(label="College Fee"),
gr.Number(label="GPA"),
gr.Number(label="Year")
],
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)