import pandas as pd import joblib import gradio as gr # Load the trained model pipeline model = joblib.load('best_model_pipeline_3_24.pkl') columns = [ 'gender', 'it_program', 'current_res', 'fam_size', 'guardian', 'health_stat', 'travel_time', 'study_time', 'mot_edu', 'fat_edu', 'fam_rel_qual', 'parents_cohab_stat', 'add_type', 'current_stat', 'extra_curricular', 'hang_out_friends', 'current_cgpa', 'prev_edu', 'current_sem', 'calc', 'pf', 'matric_or_o', 'fsc_or_a', 'sem_1_sgpa', 'sem_2_sgpa' ] def preprocess_input(input_data): # Perform the same preprocessing steps as in the training phase grade_mapping = { 'A+': 4.0, 'A': 3.85, 'A-': 3.7, 'B+': 3.3, 'B': 3.0, 'B-': 2.7, 'C+': 2.3, 'C': 2.0, 'C-': 1.7, 'D+': 1.3, 'D': 1.0, 'D-': 0.7, 'F': 0.0 } grade_columns = ['calc', 'pf'] for col in grade_columns: input_data[col] = grade_mapping[input_data[col]] # Replace "3+" with 3 in the Failures column # Convert input data to DataFrame and ensure the correct order of columns input_df = pd.DataFrame([input_data], columns=columns) return input_df def predict( gender, it_program, current_res, fam_size, guardian, health_stat, travel_time, study_time, mot_edu, fat_edu, fam_rel_qual, parents_cohab_stat, add_type, current_stat, extra_curricular, hang_out_friends, current_cgpa, prev_edu, current_sem, calc, pf, matric_or_o, fsc_or_a, sem_1_sgpa, sem_2_sgpa ): input_data = { 'gender': gender, 'it_program': it_program, 'current_res': current_res, 'fam_size': fam_size, 'guardian': guardian, 'health_stat': health_stat, 'travel_time': travel_time, 'study_time': study_time, 'mot_edu': mot_edu, 'fat_edu': fat_edu, 'fam_rel_qual': fam_rel_qual, 'parents_cohab_stat': parents_cohab_stat, 'add_type': add_type, 'current_stat': current_stat, 'extra_curricular': extra_curricular, 'hang_out_friends': hang_out_friends, 'current_cgpa': current_cgpa, 'prev_edu': prev_edu, 'current_sem': current_sem, 'calc': calc, 'pf': pf, 'matric_or_o': matric_or_o, 'fsc_or_a': fsc_or_a, 'sem_1_sgpa': sem_1_sgpa, 'sem_2_sgpa': sem_2_sgpa } # Preprocess the input data input_df = preprocess_input(input_data) # Make prediction prediction = model.predict(input_df) return prediction[0] # Create a Gradio interface with multiple inputs iface = gr.Interface( fn=predict, inputs=[ gr.Dropdown(choices=["Male", "Female"], label="Gender"), gr.Dropdown(choices=["CS", "DS", "SE"], label="IT Program"), gr.Dropdown(choices=["Hostel", "Home"], label="Current residence"), gr.Dropdown(choices=["LT3", "GT3"], label="Family size"), gr.Dropdown(choices=["Father", "Mother", "Grandparents"], label="Guardian"), gr.Slider(1, 5, step=1, label="Health Status"), gr.Slider(1, 4, step=1, label="Travel time to university"), gr.Slider(1, 4, step=1, label="Study time"), gr.Slider(0, 4, step=1, label="Mothers education"), gr.Slider(0, 4, step=1, label="Fathers education"), gr.Slider(1, 5, step=1, label="Family relations quality"), gr.Dropdown(choices=["Together", "Abroad"], label="Parents cohabitation status"), gr.Dropdown(choices=["Urban", "Rural"], label="Home address Type"), gr.Dropdown(choices=["Internship", "Full time student", "Job"], label="Status"), gr.Dropdown(choices=["Yes", "No"], label="Participation in extra-curricular activities"), gr.Slider(1, 5, step=1, label="Going out with friends"), gr.Number(label="Current CGPA"), gr.Dropdown(choices=["Matriculation and FSc/ICS", "O and A levels"], label="Previous education"), gr.Dropdown(choices=[1, 2, 3, 4],label="Current Semester"), gr.Dropdown(choices=["A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D", "F"], label="Grade in Calculus"), gr.Dropdown(choices=["A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D", "F"], label="Grade in Programming Fundamentals (PF)"), gr.Number(label="Percentage in Matric or O levels"), gr.Number(label="Percentage in FSc or A levels"), gr.Number(label="Semester 1 GPA (SGPA)"), gr.Number(label="Semester 2 GPA (SGPA)") ], outputs="text", live=False ) # Launch the Gradio interface iface.launch()