Download SmartED_ML/app.py from msgasu/career-recommender: direct link, hf CLI and curl.
- Browser
- Download file 4.58 kB
-
https://huggingface.co/msgasu/career-recommender/resolve/main/SmartED_ML/app.py
- Command line
-
hf download hf://msgasu/career-recommender/SmartED_ML/app.py
-
curl -L -o app.py https://huggingface.co/msgasu/career-recommender/resolve/main/SmartED_ML/app.py
4.58 kB
| import joblib | |
| import pandas as pd | |
| import numpy as np | |
| import gradio as gr | |
| # Load your trained model | |
| model = joblib.load('trained_model.joblib') | |
| # Define grade to numeric conversion (same as before) | |
| def grade_to_numeric(grade): | |
| if pd.isna(grade) or grade == "": | |
| return np.nan | |
| grade_map = { | |
| "A1": 1, "B2": 2, "B3": 3, "C4": 4, "C5": 5, "C6": 6, | |
| "D7": 7, "E8": 8, "F9": 9 | |
| } | |
| return grade_map.get(grade, np.nan) | |
| # Define your preprocessing function | |
| def preprocess_input(desired_career, aggregate, english, core_maths, science, | |
| social_studies, electives, elective_maths=None, | |
| business_management=None, government=None, chemistry=None, | |
| physics=None, economics=None, visual_arts=None, geography=None, | |
| e_ict=None, literature=None, biology=None): | |
| # Create a dictionary with all inputs | |
| input_data = { | |
| "Desired_Career": desired_career, | |
| "Aggregate": aggregate, | |
| "English": english, | |
| "Core Maths": core_maths, | |
| "Science": science, | |
| "Social Studies": social_studies, | |
| "Electives": electives, | |
| "Elective Maths": elective_maths, | |
| "Business Management": business_management, | |
| "Government": government, | |
| "Chemistry": chemistry, | |
| "Physics": physics, | |
| "Economics": economics, | |
| "Visual Arts": visual_arts, | |
| "Geography": geography, | |
| "E-ICT": e_ict, | |
| "Literature": literature, | |
| "Biology": biology | |
| } | |
| # Convert to DataFrame | |
| student_df = pd.DataFrame([input_data]) | |
| # Convert grades to numerical | |
| grade_cols = ['English', 'Core Maths', 'Science', 'Social Studies', | |
| 'Elective Maths', 'Business Management', 'Government', | |
| 'Chemistry', 'Physics', 'Economics', 'Visual Arts', | |
| 'Geography', 'E-ICT', 'Literature', 'Biology'] | |
| for col in grade_cols: | |
| if col in student_df.columns: | |
| student_df[col] = student_df[col].apply(grade_to_numeric) | |
| return student_df | |
| def predict_career(desired_career, aggregate, english, core_maths, science, | |
| social_studies, electives, elective_maths=None, | |
| business_management=None, government=None, chemistry=None, | |
| physics=None, economics=None, visual_arts=None, geography=None, | |
| e_ict=None, literature=None, biology=None): | |
| # Preprocess input | |
| processed_input = preprocess_input( | |
| desired_career, aggregate, english, core_maths, science, | |
| social_studies, electives, elective_maths, business_management, | |
| government, chemistry, physics, economics, visual_arts, geography, | |
| e_ict, literature, biology | |
| ) | |
| # Make prediction | |
| prediction = model.predict(processed_input) | |
| probabilities = model.predict_proba(processed_input)[0] | |
| # Get top 3 recommendations | |
| class_indices = np.argsort(probabilities)[::-1][:3] | |
| classes = model.classes_ | |
| recommendations = [ | |
| (classes[idx], float(probabilities[idx])) | |
| for idx in class_indices | |
| ] | |
| # Format output | |
| output = "\n".join( | |
| [f"{course}: {prob*100:.1f}%" for course, prob in recommendations] | |
| ) | |
| return output | |
| # Create Gradio interface | |
| inputs = [ | |
| gr.Dropdown(["Medicine", "Pharmacy", "Law", "Computer Science", "Engineering", | |
| "Business", "Nursing", "Agriculture", "Journalism", "Education"], | |
| label="Desired Career"), | |
| gr.Number(label="Aggregate Score"), | |
| gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"], label="English"), | |
| gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"], label="Core Maths"), | |
| gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"], label="Science"), | |
| gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"], label="Social Studies"), | |
| gr.Textbox(label="Electives (comma separated)"), | |
| gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9", None], label="Elective Maths (optional)"), | |
| # Add other optional subjects similarly | |
| ] | |
| outputs = gr.Textbox(label="Recommended Courses") | |
| interface = gr.Interface( | |
| fn=predict_career, | |
| inputs=inputs, | |
| outputs=outputs, | |
| title="Career Path Recommender", | |
| description="Enter your academic information to get career recommendations" | |
| ) | |
| interface.launch() |