Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,18 +2,13 @@ import joblib
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
-
import sys
|
| 6 |
-
|
| 7 |
-
# Workaround for numpy._core issue
|
| 8 |
-
if not hasattr(np, '_core'):
|
| 9 |
-
sys.modules['numpy._core'] = np.core
|
| 10 |
|
| 11 |
# Load your trained model
|
| 12 |
model = joblib.load('trained_model.joblib')
|
| 13 |
|
| 14 |
# Define grade to numeric conversion (same as before)
|
| 15 |
def grade_to_numeric(grade):
|
| 16 |
-
if pd.isna(grade) or grade == "":
|
| 17 |
return np.nan
|
| 18 |
grade_map = {
|
| 19 |
"A1": 1, "B2": 2, "B3": 3, "C4": 4, "C5": 5, "C6": 6,
|
|
@@ -30,24 +25,24 @@ def preprocess_input(desired_career, aggregate, english, core_maths, science,
|
|
| 30 |
|
| 31 |
# Create a dictionary with all inputs
|
| 32 |
input_data = {
|
| 33 |
-
"Desired_Career": desired_career,
|
| 34 |
-
"Aggregate": aggregate,
|
| 35 |
-
"English": english,
|
| 36 |
-
"Core Maths": core_maths,
|
| 37 |
-
"Science": science,
|
| 38 |
-
"Social Studies": social_studies,
|
| 39 |
-
"Electives": electives,
|
| 40 |
-
"Elective Maths": elective_maths,
|
| 41 |
-
"Business Management": business_management,
|
| 42 |
-
"Government": government,
|
| 43 |
-
"Chemistry": chemistry,
|
| 44 |
-
"Physics": physics,
|
| 45 |
-
"Economics": economics,
|
| 46 |
-
"Visual Arts": visual_arts,
|
| 47 |
-
"Geography": geography,
|
| 48 |
-
"E-ICT": e_ict,
|
| 49 |
-
"Literature": literature,
|
| 50 |
-
"Biology": biology
|
| 51 |
}
|
| 52 |
|
| 53 |
# Convert to DataFrame
|
|
@@ -71,46 +66,58 @@ def predict_career(desired_career, aggregate, english, core_maths, science,
|
|
| 71 |
physics=None, economics=None, visual_arts=None, geography=None,
|
| 72 |
e_ict=None, literature=None, biology=None):
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
# Create Gradio interface
|
| 102 |
inputs = [
|
| 103 |
gr.Dropdown(["Medicine", "Pharmacy", "Law", "Computer Science", "Engineering",
|
| 104 |
"Business", "Nursing", "Agriculture", "Journalism", "Education"],
|
| 105 |
label="Desired Career"),
|
| 106 |
-
gr.Number(label="Aggregate Score"),
|
| 107 |
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"], label="English"),
|
| 108 |
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"], label="Core Maths"),
|
| 109 |
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"], label="Science"),
|
| 110 |
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"], label="Social Studies"),
|
| 111 |
gr.Textbox(label="Electives (comma separated)"),
|
| 112 |
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9", None], label="Elective Maths (optional)"),
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
]
|
| 115 |
|
| 116 |
outputs = gr.Textbox(label="Recommended Courses")
|
|
@@ -123,4 +130,5 @@ interface = gr.Interface(
|
|
| 123 |
description="Enter your academic information to get career recommendations"
|
| 124 |
)
|
| 125 |
|
| 126 |
-
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Load your trained model
|
| 7 |
model = joblib.load('trained_model.joblib')
|
| 8 |
|
| 9 |
# Define grade to numeric conversion (same as before)
|
| 10 |
def grade_to_numeric(grade):
|
| 11 |
+
if pd.isna(grade) or grade == "" or grade is None:
|
| 12 |
return np.nan
|
| 13 |
grade_map = {
|
| 14 |
"A1": 1, "B2": 2, "B3": 3, "C4": 4, "C5": 5, "C6": 6,
|
|
|
|
| 25 |
|
| 26 |
# Create a dictionary with all inputs
|
| 27 |
input_data = {
|
| 28 |
+
"Desired_Career": str(desired_career) if desired_career is not None else "",
|
| 29 |
+
"Aggregate": int(aggregate) if aggregate is not None else 0,
|
| 30 |
+
"English": str(english) if english is not None else "",
|
| 31 |
+
"Core Maths": str(core_maths) if core_maths is not None else "",
|
| 32 |
+
"Science": str(science) if science is not None else "",
|
| 33 |
+
"Social Studies": str(social_studies) if social_studies is not None else "",
|
| 34 |
+
"Electives": str(electives) if electives is not None else "",
|
| 35 |
+
"Elective Maths": str(elective_maths) if elective_maths is not None else "",
|
| 36 |
+
"Business Management": str(business_management) if business_management is not None else "",
|
| 37 |
+
"Government": str(government) if government is not None else "",
|
| 38 |
+
"Chemistry": str(chemistry) if chemistry is not None else "",
|
| 39 |
+
"Physics": str(physics) if physics is not None else "",
|
| 40 |
+
"Economics": str(economics) if economics is not None else "",
|
| 41 |
+
"Visual Arts": str(visual_arts) if visual_arts is not None else "",
|
| 42 |
+
"Geography": str(geography) if geography is not None else "",
|
| 43 |
+
"E-ICT": str(e_ict) if e_ict is not None else "",
|
| 44 |
+
"Literature": str(literature) if literature is not None else "",
|
| 45 |
+
"Biology": str(biology) if biology is not None else ""
|
| 46 |
}
|
| 47 |
|
| 48 |
# Convert to DataFrame
|
|
|
|
| 66 |
physics=None, economics=None, visual_arts=None, geography=None,
|
| 67 |
e_ict=None, literature=None, biology=None):
|
| 68 |
|
| 69 |
+
try:
|
| 70 |
+
# Preprocess input
|
| 71 |
+
processed_input = preprocess_input(
|
| 72 |
+
desired_career, aggregate, english, core_maths, science,
|
| 73 |
+
social_studies, electives, elective_maths, business_management,
|
| 74 |
+
government, chemistry, physics, economics, visual_arts, geography,
|
| 75 |
+
e_ict, literature, biology
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Make prediction
|
| 79 |
+
prediction = model.predict(processed_input)
|
| 80 |
+
probabilities = model.predict_proba(processed_input)[0]
|
| 81 |
+
|
| 82 |
+
# Get top 3 recommendations
|
| 83 |
+
class_indices = np.argsort(probabilities)[::-1][:3]
|
| 84 |
+
classes = model.classes_
|
| 85 |
+
recommendations = [
|
| 86 |
+
(str(classes[idx]), float(probabilities[idx]))
|
| 87 |
+
for idx in class_indices
|
| 88 |
+
]
|
| 89 |
+
|
| 90 |
+
# Format output
|
| 91 |
+
output = "\n".join(
|
| 92 |
+
[f"{course}: {prob*100:.1f}%" for course, prob in recommendations]
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
return output
|
| 96 |
+
except Exception as e:
|
| 97 |
+
return f"Error processing input: {str(e)}"
|
| 98 |
|
| 99 |
# Create Gradio interface
|
| 100 |
inputs = [
|
| 101 |
gr.Dropdown(["Medicine", "Pharmacy", "Law", "Computer Science", "Engineering",
|
| 102 |
"Business", "Nursing", "Agriculture", "Journalism", "Education"],
|
| 103 |
label="Desired Career"),
|
| 104 |
+
gr.Number(label="Aggregate Score", precision=0),
|
| 105 |
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"], label="English"),
|
| 106 |
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"], label="Core Maths"),
|
| 107 |
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"], label="Science"),
|
| 108 |
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9"], label="Social Studies"),
|
| 109 |
gr.Textbox(label="Electives (comma separated)"),
|
| 110 |
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9", None], label="Elective Maths (optional)"),
|
| 111 |
+
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9", None], label="Business Management (optional)"),
|
| 112 |
+
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9", None], label="Government (optional)"),
|
| 113 |
+
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9", None], label="Chemistry (optional)"),
|
| 114 |
+
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9", None], label="Physics (optional)"),
|
| 115 |
+
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9", None], label="Economics (optional)"),
|
| 116 |
+
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9", None], label="Visual Arts (optional)"),
|
| 117 |
+
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9", None], label="Geography (optional)"),
|
| 118 |
+
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9", None], label="E-ICT (optional)"),
|
| 119 |
+
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9", None], label="Literature (optional)"),
|
| 120 |
+
gr.Dropdown(["A1", "B2", "B3", "C4", "C5", "C6", "D7", "E8", "F9", None], label="Biology (optional)"),
|
| 121 |
]
|
| 122 |
|
| 123 |
outputs = gr.Textbox(label="Recommended Courses")
|
|
|
|
| 130 |
description="Enter your academic information to get career recommendations"
|
| 131 |
)
|
| 132 |
|
| 133 |
+
if __name__ == "__main__":
|
| 134 |
+
interface.launch()
|