markobinario commited on
Commit
203e1c7
Β·
verified Β·
1 Parent(s): d58d591

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -57
app.py CHANGED
@@ -19,20 +19,20 @@ def get_course_recommendations(stanine, gwa, strand, hobbies):
19
 
20
  # Validate inputs
21
  if not stanine or not gwa or not strand or not hobbies:
22
- return "Please fill in all fields", "", ""
23
 
24
  try:
25
  stanine = int(stanine)
26
  gwa = float(gwa)
27
 
28
  if stanine < 1 or stanine > 9:
29
- return "Stanine must be between 1-9", "", ""
30
 
31
  if gwa < 75 or gwa > 100:
32
- return "GWA must be between 75-100", "", ""
33
 
34
  if strand not in ["STEM", "ABM", "HUMSS", "GAS", "TVL"]:
35
- return "Strand must be one of: STEM, ABM, HUMSS, GAS, TVL", "", ""
36
 
37
  # Store current user input
38
  current_user_input = {
@@ -46,51 +46,100 @@ def get_course_recommendations(stanine, gwa, strand, hobbies):
46
  recommendations = recommender.predict_course(stanine, gwa, strand, hobbies)
47
  current_recommendations = recommendations
48
 
49
- # Format recommendations
50
- result_text = "πŸŽ“ Course Recommendations:\n\n"
51
- for i, (course, confidence) in enumerate(recommendations, 1):
52
- confidence_percent = confidence * 100
53
- result_text += f"{i}. {course} (Confidence: {confidence_percent:.1f}%)\n"
54
-
55
- # Show top recommendation
56
- top_course = recommendations[0][0] if recommendations else "No recommendations"
57
- confidence = recommendations[0][1] * 100 if recommendations else 0
 
 
 
 
 
 
 
 
58
 
59
- return result_text, top_course, f"{confidence:.1f}%"
60
 
61
  except ValueError as e:
62
- return f"Invalid input: {str(e)}", "", ""
63
  except Exception as e:
64
- return f"Error getting recommendations: {str(e)}", "", ""
65
 
66
- def rate_recommendation(rating):
67
- """Rate the current recommendation (like/dislike)"""
68
  global current_recommendations, current_user_input
69
 
70
  if not current_recommendations or not current_user_input:
71
  return "No recommendations to rate. Please get recommendations first."
72
 
73
  try:
74
- # Convert rating to numeric
75
- rating_value = 1 if rating == "πŸ‘ Like" else 0
76
-
77
- # Get the top recommendation
78
- top_course = current_recommendations[0][0]
79
-
80
- # Add feedback to database
81
- success = recommender.add_feedback(
82
- course=top_course,
83
- stanine=current_user_input['stanine'],
84
- gwa=current_user_input['gwa'],
85
- strand=current_user_input['strand'],
86
- rating=rating_value,
87
- hobbies=current_user_input['hobbies']
88
- )
 
 
 
 
 
89
 
90
- if success:
91
- return f"βœ… Thank you for your feedback! Your rating for '{top_course}' has been recorded."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  else:
93
- return "❌ Failed to record feedback. Please try again."
94
 
95
  except Exception as e:
96
  return f"Error recording feedback: {str(e)}"
@@ -116,15 +165,15 @@ def create_interface():
116
  with gr.Column(scale=1):
117
  gr.Markdown("### πŸ“ Your Profile")
118
 
119
- stanine_input = gr.Slider(
120
- minimum=1, maximum=9, step=1, value=5,
121
  label="Stanine Score (1-9)",
 
122
  info="Your stanine score from standardized tests"
123
  )
124
 
125
- gwa_input = gr.Slider(
126
- minimum=75, maximum=100, step=0.1, value=85,
127
  label="GWA (75-100)",
 
128
  info="Your Grade Weighted Average"
129
  )
130
 
@@ -146,33 +195,43 @@ def create_interface():
146
  train_model_btn = gr.Button("πŸ€– Train Model", variant="secondary")
147
 
148
  with gr.Column(scale=1):
149
- gr.Markdown("### πŸŽ“ Recommendations")
150
 
151
- recommendations_output = gr.Textbox(
152
- label="Course Recommendations",
153
- lines=8,
154
  interactive=False
155
  )
156
 
157
- top_course_output = gr.Textbox(
158
- label="Top Recommendation",
159
- interactive=False
 
160
  )
161
 
162
- confidence_output = gr.Textbox(
163
- label="Confidence Score",
164
  interactive=False
165
  )
166
 
167
- gr.Markdown("### πŸ‘ Rate Your Recommendation")
 
 
 
 
 
 
 
 
 
168
 
169
- rating_buttons = gr.Radio(
170
  choices=["πŸ‘ Like", "πŸ‘Ž Dislike"],
171
- label="How do you rate this recommendation?",
172
  interactive=True
173
  )
174
 
175
- rate_btn = gr.Button("Submit Rating", variant="secondary")
176
 
177
  rating_feedback = gr.Textbox(
178
  label="Rating Feedback",
@@ -183,12 +242,12 @@ def create_interface():
183
  get_recommendations_btn.click(
184
  fn=get_course_recommendations,
185
  inputs=[stanine_input, gwa_input, strand_input, hobbies_input],
186
- outputs=[recommendations_output, top_course_output, confidence_output]
187
  )
188
 
189
- rate_btn.click(
190
- fn=rate_recommendation,
191
- inputs=[rating_buttons],
192
  outputs=[rating_feedback]
193
  )
194
 
 
19
 
20
  # Validate inputs
21
  if not stanine or not gwa or not strand or not hobbies:
22
+ return "Please fill in all fields", "", "", "", "", ""
23
 
24
  try:
25
  stanine = int(stanine)
26
  gwa = float(gwa)
27
 
28
  if stanine < 1 or stanine > 9:
29
+ return "Stanine must be between 1-9", "", "", "", "", ""
30
 
31
  if gwa < 75 or gwa > 100:
32
+ return "GWA must be between 75-100", "", "", "", "", ""
33
 
34
  if strand not in ["STEM", "ABM", "HUMSS", "GAS", "TVL"]:
35
+ return "Strand must be one of: STEM, ABM, HUMSS, GAS, TVL", "", "", "", "", ""
36
 
37
  # Store current user input
38
  current_user_input = {
 
46
  recommendations = recommender.predict_course(stanine, gwa, strand, hobbies)
47
  current_recommendations = recommendations
48
 
49
+ # Format top 3 recommendations
50
+ if len(recommendations) >= 3:
51
+ course1 = f"{recommendations[0][0]} (Confidence: {recommendations[0][1]*100:.1f}%)"
52
+ course2 = f"{recommendations[1][0]} (Confidence: {recommendations[1][1]*100:.1f}%)"
53
+ course3 = f"{recommendations[2][0]} (Confidence: {recommendations[2][1]*100:.1f}%)"
54
+ elif len(recommendations) == 2:
55
+ course1 = f"{recommendations[0][0]} (Confidence: {recommendations[0][1]*100:.1f}%)"
56
+ course2 = f"{recommendations[1][0]} (Confidence: {recommendations[1][1]*100:.1f}%)"
57
+ course3 = "No third recommendation available"
58
+ elif len(recommendations) == 1:
59
+ course1 = f"{recommendations[0][0]} (Confidence: {recommendations[0][1]*100:.1f}%)"
60
+ course2 = "No second recommendation available"
61
+ course3 = "No third recommendation available"
62
+ else:
63
+ course1 = "No recommendations available"
64
+ course2 = ""
65
+ course3 = ""
66
 
67
+ return course1, course2, course3, None, None, None
68
 
69
  except ValueError as e:
70
+ return f"Invalid input: {str(e)}", "", "", None, None, None
71
  except Exception as e:
72
+ return f"Error getting recommendations: {str(e)}", "", "", None, None, None
73
 
74
+ def submit_all_ratings(course1_rating, course2_rating, course3_rating):
75
+ """Submit ratings for all three recommendations"""
76
  global current_recommendations, current_user_input
77
 
78
  if not current_recommendations or not current_user_input:
79
  return "No recommendations to rate. Please get recommendations first."
80
 
81
  try:
82
+ results = []
83
+ ratings_submitted = 0
84
+
85
+ # Rate first recommendation
86
+ if course1_rating and len(current_recommendations) >= 1:
87
+ rating_value = 1 if course1_rating == "πŸ‘ Like" else 0
88
+ course = current_recommendations[0][0]
89
+ success = recommender.add_feedback(
90
+ course=course,
91
+ stanine=current_user_input['stanine'],
92
+ gwa=current_user_input['gwa'],
93
+ strand=current_user_input['strand'],
94
+ rating=rating_value,
95
+ hobbies=current_user_input['hobbies']
96
+ )
97
+ if success:
98
+ results.append(f"βœ… Rating for '{course}' recorded")
99
+ ratings_submitted += 1
100
+ else:
101
+ results.append(f"❌ Failed to record rating for '{course}'")
102
 
103
+ # Rate second recommendation
104
+ if course2_rating and len(current_recommendations) >= 2:
105
+ rating_value = 1 if course2_rating == "πŸ‘ Like" else 0
106
+ course = current_recommendations[1][0]
107
+ success = recommender.add_feedback(
108
+ course=course,
109
+ stanine=current_user_input['stanine'],
110
+ gwa=current_user_input['gwa'],
111
+ strand=current_user_input['strand'],
112
+ rating=rating_value,
113
+ hobbies=current_user_input['hobbies']
114
+ )
115
+ if success:
116
+ results.append(f"βœ… Rating for '{course}' recorded")
117
+ ratings_submitted += 1
118
+ else:
119
+ results.append(f"❌ Failed to record rating for '{course}'")
120
+
121
+ # Rate third recommendation
122
+ if course3_rating and len(current_recommendations) >= 3:
123
+ rating_value = 1 if course3_rating == "πŸ‘ Like" else 0
124
+ course = current_recommendations[2][0]
125
+ success = recommender.add_feedback(
126
+ course=course,
127
+ stanine=current_user_input['stanine'],
128
+ gwa=current_user_input['gwa'],
129
+ strand=current_user_input['strand'],
130
+ rating=rating_value,
131
+ hobbies=current_user_input['hobbies']
132
+ )
133
+ if success:
134
+ results.append(f"βœ… Rating for '{course}' recorded")
135
+ ratings_submitted += 1
136
+ else:
137
+ results.append(f"❌ Failed to record rating for '{course}'")
138
+
139
+ if ratings_submitted > 0:
140
+ return f"Thank you! {ratings_submitted} rating(s) submitted successfully.\n\n" + "\n".join(results)
141
  else:
142
+ return "Please select at least one rating before submitting."
143
 
144
  except Exception as e:
145
  return f"Error recording feedback: {str(e)}"
 
165
  with gr.Column(scale=1):
166
  gr.Markdown("### πŸ“ Your Profile")
167
 
168
+ stanine_input = gr.Textbox(
 
169
  label="Stanine Score (1-9)",
170
+ placeholder="Enter your stanine score (1-9)",
171
  info="Your stanine score from standardized tests"
172
  )
173
 
174
+ gwa_input = gr.Textbox(
 
175
  label="GWA (75-100)",
176
+ placeholder="Enter your GWA (75-100)",
177
  info="Your Grade Weighted Average"
178
  )
179
 
 
195
  train_model_btn = gr.Button("πŸ€– Train Model", variant="secondary")
196
 
197
  with gr.Column(scale=1):
198
+ gr.Markdown("### πŸŽ“ Top 3 Course Recommendations")
199
 
200
+ # Display top 3 recommendations
201
+ course1_output = gr.Textbox(
202
+ label="1st Recommendation",
203
  interactive=False
204
  )
205
 
206
+ course1_rating = gr.Radio(
207
+ choices=["πŸ‘ Like", "πŸ‘Ž Dislike"],
208
+ label="Rate 1st Recommendation",
209
+ interactive=True
210
  )
211
 
212
+ course2_output = gr.Textbox(
213
+ label="2nd Recommendation",
214
  interactive=False
215
  )
216
 
217
+ course2_rating = gr.Radio(
218
+ choices=["πŸ‘ Like", "πŸ‘Ž Dislike"],
219
+ label="Rate 2nd Recommendation",
220
+ interactive=True
221
+ )
222
+
223
+ course3_output = gr.Textbox(
224
+ label="3rd Recommendation",
225
+ interactive=False
226
+ )
227
 
228
+ course3_rating = gr.Radio(
229
  choices=["πŸ‘ Like", "πŸ‘Ž Dislike"],
230
+ label="Rate 3rd Recommendation",
231
  interactive=True
232
  )
233
 
234
+ submit_ratings_btn = gr.Button("Submit All Ratings", variant="primary")
235
 
236
  rating_feedback = gr.Textbox(
237
  label="Rating Feedback",
 
242
  get_recommendations_btn.click(
243
  fn=get_course_recommendations,
244
  inputs=[stanine_input, gwa_input, strand_input, hobbies_input],
245
+ outputs=[course1_output, course2_output, course3_output, course1_rating, course2_rating, course3_rating]
246
  )
247
 
248
+ submit_ratings_btn.click(
249
+ fn=submit_all_ratings,
250
+ inputs=[course1_rating, course2_rating, course3_rating],
251
  outputs=[rating_feedback]
252
  )
253