Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -84,7 +84,7 @@ def submit_all_ratings(course1_rating, course2_rating, course3_rating):
|
|
| 84 |
|
| 85 |
# Rate first recommendation
|
| 86 |
if course1_rating and len(current_recommendations) >= 1:
|
| 87 |
-
rating_value =
|
| 88 |
course = current_recommendations[0][0]
|
| 89 |
success = recommender.add_feedback_with_learning(
|
| 90 |
course=course,
|
|
@@ -95,14 +95,14 @@ def submit_all_ratings(course1_rating, course2_rating, course3_rating):
|
|
| 95 |
hobbies=current_user_input['hobbies']
|
| 96 |
)
|
| 97 |
if success:
|
| 98 |
-
results.append(f"
|
| 99 |
ratings_submitted += 1
|
| 100 |
else:
|
| 101 |
-
results.append(f"
|
| 102 |
|
| 103 |
# Rate second recommendation
|
| 104 |
if course2_rating and len(current_recommendations) >= 2:
|
| 105 |
-
rating_value =
|
| 106 |
course = current_recommendations[1][0]
|
| 107 |
success = recommender.add_feedback_with_learning(
|
| 108 |
course=course,
|
|
@@ -113,14 +113,14 @@ def submit_all_ratings(course1_rating, course2_rating, course3_rating):
|
|
| 113 |
hobbies=current_user_input['hobbies']
|
| 114 |
)
|
| 115 |
if success:
|
| 116 |
-
results.append(f"
|
| 117 |
ratings_submitted += 1
|
| 118 |
else:
|
| 119 |
-
results.append(f"
|
| 120 |
|
| 121 |
# Rate third recommendation
|
| 122 |
if course3_rating and len(current_recommendations) >= 3:
|
| 123 |
-
rating_value =
|
| 124 |
course = current_recommendations[2][0]
|
| 125 |
success = recommender.add_feedback_with_learning(
|
| 126 |
course=course,
|
|
@@ -131,10 +131,10 @@ def submit_all_ratings(course1_rating, course2_rating, course3_rating):
|
|
| 131 |
hobbies=current_user_input['hobbies']
|
| 132 |
)
|
| 133 |
if success:
|
| 134 |
-
results.append(f"
|
| 135 |
ratings_submitted += 1
|
| 136 |
else:
|
| 137 |
-
results.append(f"
|
| 138 |
|
| 139 |
if ratings_submitted > 0:
|
| 140 |
return f"Thank you! {ratings_submitted} rating(s) submitted successfully.\n\n" + "\n".join(results)
|
|
@@ -148,33 +148,33 @@ def train_model():
|
|
| 148 |
"""Train the model with current data"""
|
| 149 |
try:
|
| 150 |
accuracy = recommender.train_model(use_database=True)
|
| 151 |
-
return f"
|
| 152 |
except Exception as e:
|
| 153 |
-
return f"
|
| 154 |
|
| 155 |
def get_available_courses_info():
|
| 156 |
"""Get information about available courses from database"""
|
| 157 |
try:
|
| 158 |
courses = db_connection.get_available_courses()
|
| 159 |
if courses:
|
| 160 |
-
return f"
|
| 161 |
else:
|
| 162 |
-
return "
|
| 163 |
except Exception as e:
|
| 164 |
-
return f"
|
| 165 |
|
| 166 |
# Create Gradio interface
|
| 167 |
def create_interface():
|
| 168 |
with gr.Blocks(title="Course AI Recommender", theme=gr.themes.Soft()) as demo:
|
| 169 |
gr.Markdown("""
|
| 170 |
-
#
|
| 171 |
|
| 172 |
Get personalized course recommendations based on your academic profile and interests!
|
| 173 |
""")
|
| 174 |
|
| 175 |
with gr.Row():
|
| 176 |
with gr.Column(scale=1):
|
| 177 |
-
gr.Markdown("###
|
| 178 |
|
| 179 |
stanine_input = gr.Textbox(
|
| 180 |
label="Stanine Score (1-9)",
|
|
@@ -201,14 +201,14 @@ def create_interface():
|
|
| 201 |
info="List your hobbies and interests (comma-separated)"
|
| 202 |
)
|
| 203 |
|
| 204 |
-
get_recommendations_btn = gr.Button("
|
| 205 |
|
| 206 |
-
train_model_btn = gr.Button("
|
| 207 |
|
| 208 |
-
show_courses_btn = gr.Button("
|
| 209 |
|
| 210 |
with gr.Column(scale=1):
|
| 211 |
-
gr.Markdown("###
|
| 212 |
|
| 213 |
# Display top 3 recommendations
|
| 214 |
course1_output = gr.Textbox(
|
|
@@ -217,7 +217,7 @@ def create_interface():
|
|
| 217 |
)
|
| 218 |
|
| 219 |
course1_rating = gr.Radio(
|
| 220 |
-
choices=["
|
| 221 |
label="Rate 1st Recommendation",
|
| 222 |
interactive=True
|
| 223 |
)
|
|
@@ -228,7 +228,7 @@ def create_interface():
|
|
| 228 |
)
|
| 229 |
|
| 230 |
course2_rating = gr.Radio(
|
| 231 |
-
choices=["
|
| 232 |
label="Rate 2nd Recommendation",
|
| 233 |
interactive=True
|
| 234 |
)
|
|
@@ -239,7 +239,7 @@ def create_interface():
|
|
| 239 |
)
|
| 240 |
|
| 241 |
course3_rating = gr.Radio(
|
| 242 |
-
choices=["
|
| 243 |
label="Rate 3rd Recommendation",
|
| 244 |
interactive=True
|
| 245 |
)
|
|
@@ -301,14 +301,14 @@ if __name__ == "__main__":
|
|
| 301 |
# Try to load existing model
|
| 302 |
try:
|
| 303 |
recommender.load_model()
|
| 304 |
-
print("
|
| 305 |
except:
|
| 306 |
-
print("
|
| 307 |
try:
|
| 308 |
recommender.train_model(use_database=False)
|
| 309 |
-
print("
|
| 310 |
except Exception as e:
|
| 311 |
-
print(f"
|
| 312 |
|
| 313 |
# Create and launch interface
|
| 314 |
demo = create_interface()
|
|
|
|
| 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_with_learning(
|
| 90 |
course=course,
|
|
|
|
| 95 |
hobbies=current_user_input['hobbies']
|
| 96 |
)
|
| 97 |
if success:
|
| 98 |
+
results.append(f"[OK] Rating for '{course}' recorded")
|
| 99 |
ratings_submitted += 1
|
| 100 |
else:
|
| 101 |
+
results.append(f"[ERROR] 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_with_learning(
|
| 108 |
course=course,
|
|
|
|
| 113 |
hobbies=current_user_input['hobbies']
|
| 114 |
)
|
| 115 |
if success:
|
| 116 |
+
results.append(f"[OK] Rating for '{course}' recorded")
|
| 117 |
ratings_submitted += 1
|
| 118 |
else:
|
| 119 |
+
results.append(f"[ERROR] 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_with_learning(
|
| 126 |
course=course,
|
|
|
|
| 131 |
hobbies=current_user_input['hobbies']
|
| 132 |
)
|
| 133 |
if success:
|
| 134 |
+
results.append(f"[OK] Rating for '{course}' recorded")
|
| 135 |
ratings_submitted += 1
|
| 136 |
else:
|
| 137 |
+
results.append(f"[ERROR] 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)
|
|
|
|
| 148 |
"""Train the model with current data"""
|
| 149 |
try:
|
| 150 |
accuracy = recommender.train_model(use_database=True)
|
| 151 |
+
return f"[OK] Model trained successfully! Accuracy: {accuracy:.3f}"
|
| 152 |
except Exception as e:
|
| 153 |
+
return f"[ERROR] Error training model: {str(e)}"
|
| 154 |
|
| 155 |
def get_available_courses_info():
|
| 156 |
"""Get information about available courses from database"""
|
| 157 |
try:
|
| 158 |
courses = db_connection.get_available_courses()
|
| 159 |
if courses:
|
| 160 |
+
return f"Available courses in database: {len(courses)}\n\n" + "\n".join([f"β’ {course}" for course in courses[:10]]) + (f"\n... and {len(courses)-10} more" if len(courses) > 10 else "")
|
| 161 |
else:
|
| 162 |
+
return "No courses found in database. Please check the /courses endpoint."
|
| 163 |
except Exception as e:
|
| 164 |
+
return f"[ERROR] Error fetching courses: {str(e)}"
|
| 165 |
|
| 166 |
# Create Gradio interface
|
| 167 |
def create_interface():
|
| 168 |
with gr.Blocks(title="Course AI Recommender", theme=gr.themes.Soft()) as demo:
|
| 169 |
gr.Markdown("""
|
| 170 |
+
# Course AI Machine Learning Recommender
|
| 171 |
|
| 172 |
Get personalized course recommendations based on your academic profile and interests!
|
| 173 |
""")
|
| 174 |
|
| 175 |
with gr.Row():
|
| 176 |
with gr.Column(scale=1):
|
| 177 |
+
gr.Markdown("### Your Profile")
|
| 178 |
|
| 179 |
stanine_input = gr.Textbox(
|
| 180 |
label="Stanine Score (1-9)",
|
|
|
|
| 201 |
info="List your hobbies and interests (comma-separated)"
|
| 202 |
)
|
| 203 |
|
| 204 |
+
get_recommendations_btn = gr.Button("Get Recommendations", variant="primary")
|
| 205 |
|
| 206 |
+
train_model_btn = gr.Button("Train Model", variant="secondary")
|
| 207 |
|
| 208 |
+
show_courses_btn = gr.Button("Show Available Courses", variant="secondary")
|
| 209 |
|
| 210 |
with gr.Column(scale=1):
|
| 211 |
+
gr.Markdown("### Top 3 Course Recommendations")
|
| 212 |
|
| 213 |
# Display top 3 recommendations
|
| 214 |
course1_output = gr.Textbox(
|
|
|
|
| 217 |
)
|
| 218 |
|
| 219 |
course1_rating = gr.Radio(
|
| 220 |
+
choices=["Like", "Dislike"],
|
| 221 |
label="Rate 1st Recommendation",
|
| 222 |
interactive=True
|
| 223 |
)
|
|
|
|
| 228 |
)
|
| 229 |
|
| 230 |
course2_rating = gr.Radio(
|
| 231 |
+
choices=["Like", "Dislike"],
|
| 232 |
label="Rate 2nd Recommendation",
|
| 233 |
interactive=True
|
| 234 |
)
|
|
|
|
| 239 |
)
|
| 240 |
|
| 241 |
course3_rating = gr.Radio(
|
| 242 |
+
choices=["Like", "Dislike"],
|
| 243 |
label="Rate 3rd Recommendation",
|
| 244 |
interactive=True
|
| 245 |
)
|
|
|
|
| 301 |
# Try to load existing model
|
| 302 |
try:
|
| 303 |
recommender.load_model()
|
| 304 |
+
print("[OK] Loaded existing model")
|
| 305 |
except:
|
| 306 |
+
print("[WARNING] No existing model found. Training with basic data...")
|
| 307 |
try:
|
| 308 |
recommender.train_model(use_database=False)
|
| 309 |
+
print("[OK] Model trained with basic data")
|
| 310 |
except Exception as e:
|
| 311 |
+
print(f"[ERROR] Error training model: {e}")
|
| 312 |
|
| 313 |
# Create and launch interface
|
| 314 |
demo = create_interface()
|