demo commited on
Commit
88f4aa9
·
verified ·
1 Parent(s): dcb3fa3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -47
app.py CHANGED
@@ -3,12 +3,11 @@ import joblib
3
  import numpy as np
4
  import pandas as pd
5
  from huggingface_hub import hf_hub_download
6
- from sklearn.preprocessing import StandardScaler, OneHotEncoder, LabelEncoder
7
 
8
- # Load the trained model and scaler objects from file
9
- REPO_ID = "Hemg/marketpredict" # Hugging Face repo ID
10
- MoDEL_FILENAME = "stx.joblib" # Model file name
11
- SCALER_FILENAME = "scaler.joblib" # Scaler file name
12
 
13
  model = joblib.load(hf_hub_download(repo_id=REPO_ID, filename=MoDEL_FILENAME))
14
  scaler = joblib.load(hf_hub_download(repo_id=REPO_ID, filename=SCALER_FILENAME))
@@ -25,45 +24,100 @@ def encode_categorical_columns(df):
25
 
26
  return df
27
 
28
- # Define the prediction function
29
- def predict_performance(Year, Instagram_Advertising, Facebook_Advertising, Event_Expenses, Internet_Expenses):
30
- # Prepare input data (represents independent variables for house prediction)
31
- input_data = [[Year, Instagram_Advertising, Facebook_Advertising, Event_Expenses, Internet_Expenses]]
32
-
33
- # Get the feature names from the Gradio interface inputs
34
- feature_names = ["Year", "Instagram_Advertising", "Facebook_Advertising", "Event_Expenses", "Internet_Expenses"]
35
-
36
- # Create a Pandas DataFrame with the input data and feature names
37
- input_df = pd.DataFrame(input_data, columns=feature_names)
38
-
39
- input_df = encode_categorical_columns(input_df)
40
-
41
- # Scale the input data using the loaded scaler
42
- scaled_input = scaler.transform(input_df)
43
-
44
- # Make predictions using the loaded model
45
- prediction = model.predict(scaled_input)[0]
46
-
47
- # Return the result as HTML with custom styling (green color and larger font)
48
- return f'<p style="font-size: 24px; color: green;">Forecast no of. Students admission: {prediction:,.0f}</p>'
49
-
50
-
51
-
52
- def create_demo():
53
- # Create the Gradio app
54
- demo = gr.Interface(
55
- fn=predict_performance,
56
- inputs=[
57
- gr.Slider(minimum=2024, maximum=2025, step=1, label="Year",info="The forecasted Year"),
58
- gr.Slider(minimum=10000, maximum=45000, step=500, label="Instagram_Advertising", info="How much do you spend on Instagram ads Yearly($)?"),
59
- gr.Slider(minimum=10000, maximum=75000, step=500, label="Facebook_Advertising", info="How much do you spend on Facebook ads Yearly($)?"),
60
- gr.Slider(minimum=20000, maximum=100000, step=500, label="Event_Expenses", info="What’s your typical budget for events($)?"),
61
- gr.Slider(minimum=5000, maximum=45000, step=500, label="Internet_Expenses", info="How much do you spend on internet Yearly($)?")
62
- ],
63
- outputs=gr.HTML(), # Specify the output as HTML
64
- title="Student Admission Forecast",
65
- description="Forecast of chances of student admission based on marketing expenditures"
66
- )
67
- demo.launch(auth=("admin", "Gr@ce"), share=True )
68
-
69
- create_demo()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import numpy as np
4
  import pandas as pd
5
  from huggingface_hub import hf_hub_download
6
+ from sklearn.preprocessing import StandardScaler, LabelEncoder
7
 
8
+ REPO_ID = "Hemg/modelxxx"
9
+ MoDEL_FILENAME = "studentpredict.joblib"
10
+ SCALER_FILENAME = "studentscaler.joblib"
 
11
 
12
  model = joblib.load(hf_hub_download(repo_id=REPO_ID, filename=MoDEL_FILENAME))
13
  scaler = joblib.load(hf_hub_download(repo_id=REPO_ID, filename=SCALER_FILENAME))
 
24
 
25
  return df
26
 
27
+ def predict_performance(Location, College_Fee, College, GPA, Year, Course_Interested, Faculty, Source,
28
+ Visited_College_for_Inquiry_Only, Event, Attended_Any_Events,
29
+ Presenter, Visited_Parents):
30
+ try:
31
+ input_data = [[Location, College_Fee, College, GPA, Year, Course_Interested, Faculty, Source,
32
+ Visited_College_for_Inquiry_Only, Event, Attended_Any_Events,
33
+ Presenter, Visited_Parents]]
34
+
35
+ feature_names = ["Location", "College Fee", "College", "GPA", "Year", "Course Interested",
36
+ "Faculty", "Source", "Visited College for Inquiry Only", "Event",
37
+ "Attended Any Events", "Presenter", "Visited Parents"]
38
+
39
+ input_df = pd.DataFrame(input_data, columns=feature_names)
40
+ df = encode_categorical_columns(input_df)
41
+ df = df.reindex(columns=scaler.feature_names_in_, fill_value=0)
42
+ scaled_input = scaler.transform(df)
43
+
44
+ # Get probability prediction
45
+ probabilities = model.predict_proba(scaled_input)[0]
46
+ # Take the probability of positive class (usually index 1)
47
+ admission_probability = probabilities[1]
48
+
49
+ # Ensure the probability is between 0 and 1
50
+ admission_probability = np.clip(admission_probability, 0, 1)
51
+
52
+ # Convert to percentage
53
+ prediction_percentage = admission_probability * 100
54
+
55
+ # Create styled HTML output
56
+ # html_template = """
57
+ # <div style='text-align: center; padding: 20px;'>
58
+ # <div style='font-family: Arial, sans-serif; font-size: 24px; margin-bottom: 15px;'>
59
+ # Admission Probability: <span style='font-weight: bold;'>{:.1f}%</span>
60
+ # </div>
61
+ # <div style='{style}'>
62
+ # {message}
63
+ # </div>
64
+ # </div>
65
+ # """
66
+ # Create styled HTML output
67
+ html_template = """
68
+ <div style='text-align: center; padding: 20px;'>
69
+ <div style='{style}'>
70
+ {message}
71
+ </div>
72
+ </div>
73
+ """
74
+
75
+
76
+ if prediction_percentage > 50:
77
+ style = "font-family: Arial, sans-serif; font-size: 32px; color: #28a745; font-weight: bold;"
78
+ message = "High chance of admission"
79
+ elif prediction_percentage < 50:
80
+ style = "font-family: Arial, sans-serif; font-size: 32px; color: #dc3545; font-weight: bold; text-transform: uppercase;"
81
+ message = "Lower chance of admission"
82
+ else: # exactly 50
83
+ style = "font-family: Arial, sans-serif; font-size: 32px; color: #ffc107; font-weight: bold;"
84
+ message = "Moderate chance of admission"
85
+
86
+ return html_template.format(prediction_percentage, style=style, message=message)
87
+
88
+ except Exception as e:
89
+ return f"<div style='color: red; font-family: Arial, sans-serif;'>Error in prediction: {str(e)}</div>"
90
+
91
+ # Update the Gradio interface
92
+ iface = gr.Interface(
93
+ fn=predict_performance,
94
+ inputs=[
95
+ gr.Radio(["Kathmandu", "Bhaktapur", "Lalitpur", "Kritipur"], label="Location",info="What is your current location?"),
96
+ gr.Slider(minimum=1000000, maximum=1700000,step=100000,label="College Fee", info="What 's the the total bachelor fee for the course you want to enroll?"),
97
+ gr.Radio(["Trinity", "CCRC", "KMC", "SOS", "ISMT", "St. Xavier's", "Everest", "Prime"], label="College", info="What is the name of the last college you attended?"),
98
+ gr.Slider(minimum=2, maximum=3, label="GPA", info="What is your GPA (Grade Point Average) of +2 ?"),
99
+ gr.Slider(minimum=2024, maximum=2026, step=1, label="Year", info="What is your intended year of admission?"),
100
+ #gr.Radio([2024, 2025, 2026], label="Year", info="What is your intended year of admission?")
101
+ gr.Radio(["MSc IT & Applied Security", "BSc (Hons) Computing", "BSc (Hons) Computing with Artificial Intelligence",
102
+ "BSc (Hons) Computer Networking & IT Security", "BSc (Hons) Multimedia Technologies", "MBA",
103
+ "BA (Hons) Accounting & Finance", "BA (Hons) Business Administration"], label="Course_Interested", info="Which course are you most interested in?"),
104
+ gr.Radio(["Science", "Management", "Humanities"], label="Faculty", info="what is your last stream ?"),
105
+ gr.Radio(["Event", "Facebook", "Instagram", "Offline", "Recommendation"], label="Source",info="How did you first hear about this college?"),
106
+ gr.Radio(["Yes", "No"], label="visited_college_for_inquery_only", info="Have you visited the college you're interested in for an inquiry or consultation?"),
107
+ gr.Radio(["Yes", "No"], label="attended_any_event", info="Have you attended any events organized by the college you're interested in?"),
108
+ gr.Radio(["New Year", "Dashain", "Orientation", "Fresher's Party", "Holi Festival", "Welcome Ceremony"],
109
+ label="attended_event_name", info="If yes, which events did you attend?" ),
110
+ gr.Radio(["Ram", "Gita", "Manish", "Shyam", "Raj", "Hari", "Rina", "Shree"], label="Presenter", info="who is the counser that help you while in counseling?"),
111
+ gr.Radio(["Yes", "No"], label="visited_with_parents", info="Did you visit the college with your parents?")
112
+ ],
113
+
114
+
115
+
116
+ outputs=gr.HTML(), # Changed to HTML output
117
+ title="Student Admission Prediction",
118
+ description="Predict the probability of student admission",
119
+ css="body { font-family: Arial, sans-serif; }"
120
+ )
121
+
122
+ if __name__ == "__main__":
123
+ iface.launch(share=True)