Spaces:
Sleeping
Sleeping
Update app.py
Browse files18 unmodified lines19202122232425262728293031323334353637387 unmodified lines46474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959619 unmodified lines11611711811912012112212312412512612712812913015 unmodified lines1461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771784 unmodified lines18318418518618718818919019119219319418 unmodified lines# Validate inputsif not stanine or not gwa or not strand or not hobbies:return "Please fill in all fields", "", ""try:stanine = int(stanine)gwa = float(gwa)if stanine < 1 or stanine > 9:return "Stanine must be between 1-9", "", ""if gwa < 75 or gwa > 100:return "GWA must be between 75-100", "", ""if strand not in ["STEM", "ABM", "HUMSS", "GAS", "TVL"]:return "Strand must be one of: STEM, ABM, HUMSS, GAS, TVL", "", ""# Store current user inputcurrent_user_input = {7 unmodified linesrecommendations = recommender.predict_course(stanine, gwa, strand, hobbies)current_recommendations = recommendations# Format recommendationsresult_text = "π Course Recommendations:\n\n"for i, (course, confidence) in enumerate(recommendations, 1):confidence_percent = confidence * 100result_text += f"{i}. {course} (Confidence: {confidence_percent:.1f}%)\n"# Show top recommendationtop_course = recommendations[0][0] if recommendations else "No recommendations"confidence = recommendations[0][1] * 100 if recommendations else 0return result_text, top_course, f"{confidence:.1f}%"except ValueError as e:return f"Invalid input: {str(e)}", "", ""except Exception as e:return f"Error getting recommendations: {str(e)}", "", ""def rate_recommendation(rating):"""Rate the current recommendation (like/dislike)"""global current_recommendations, current_user_inputif not current_recommendations or not current_user_input:return "No recommendations to rate. Please get recommendations first."try:# Convert rating to numericrating_value = 1 if rating == "π Like" else 0# Get the top recommendationtop_course = current_recommendations[0][0]# Add feedback to databasesuccess = recommender.add_feedback(course=top_course,stanine=current_user_input['stanine'],gwa=current_user_input['gwa'],strand=current_user_input['strand'],rating=rating_value,hobbies=current_user_input['hobbies'])if success:return f"β Thank you for your feedback! Your rating for '{top_course}' has been recorded."else:return "β Failed to record feedback. Please try again."except Exception as e:return f"Error recording feedback: {str(e)}"19 unmodified lineswith gr.Column(scale=1):gr.Markdown("### π Your Profile")stanine_input = gr.Slider(minimum=1, maximum=9, step=1, value=5,label="Stanine Score (1-9)",info="Your stanine score from standardized tests")gwa_input = gr.Slider(minimum=75, maximum=100, step=0.1, value=85,label="GWA (75-100)",info="Your Grade Weighted Average")15 unmodified linestrain_model_btn = gr.Button("π€ Train Model", variant="secondary")with gr.Column(scale=1):gr.Markdown("### π Recommendations")recommendations_output = gr.Textbox(label="Course Recommendations",lines=8,interactive=False)top_course_output = gr.Textbox(label="Top Recommendation",interactive=False)confidence_output = gr.Textbox(label="Confidence Score",interactive=False)gr.Markdown("### π Rate Your Recommendation")rating_buttons = gr.Radio(choices=["π Like", "π Dislike"],label="How do you rate this recommendation?",interactive=True)rate_btn = gr.Button("Submit Rating", variant="secondary")rating_feedback = gr.Textbox(label="Rating Feedback",4 unmodified linesget_recommendations_btn.click(fn=get_course_recommendations,inputs=[stanine_input, gwa_input, strand_input, hobbies_input],outputs=[recommendations_output, top_course_output, confidence_output])rate_btn.click(fn=rate_recommendation,inputs=[rating_buttons],outputs=[rating_feedback])18 unmodified lines19202122232425262728293031323334353637387 unmodified lines46474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414519 unmodified lines16516616716816917017117217317417517617717817915 unmodified lines1951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362374 unmodified lines24224324424524624724824925025125225318 unmodified lines# Validate inputsif not stanine or not gwa or not strand or not hobbies:return "Please fill in all fields", "", "", "", "", ""try:stanine = int(stanine)gwa = float(gwa)if stanine < 1 or stanine > 9:return "Stanine must be between 1-9", "", "", "", "", ""if gwa < 75 or gwa > 100:return "GWA must be between 75-100", "", "", "", "", ""if strand not in ["STEM", "ABM", "HUMSS", "GAS", "TVL"]:return "Strand must be one of: STEM, ABM, HUMSS, GAS, TVL", "", "", "", "", ""# Store current user inputcurrent_user_input = {7 unmodified linesrecommendations = recommender.predict_course(stanine, gwa, strand, hobbies)current_recommendations = recommendations# Format top 3 recommendationsif len(recommendations) >= 3:course1 = f"{recommendations[0][0]} (Confidence: {recommendations[0][1]*100:.1f}%)"course2 = f"{recommendations[1][0]} (Confidence: {recommendations[1][1]*100:.1f}%)"course3 = f"{recommendations[2][0]} (Confidence: {recommendations[2][1]*100:.1f}%)"elif len(recommendations) == 2:course1 = f"{recommendations[0][0]} (Confidence: {recommendations[0][1]*100:.1f}%)"course2 = f"{recommendations[1][0]} (Confidence: {recommendations[1][1]*100:.1f}%)"course3 = "No third recommendation available"elif len(recommendations) == 1:course1 = f"{recommendations[0][0]} (Confidence: {recommendations[0][1]*100:.1f}%)"course2 = "No second recommendation available"course3 = "No third recommendation available"else:course1 = "No recommendations available"course2 = ""course3 = ""return course1, course2, course3, None, None, Noneexcept ValueError as e:return f"Invalid input: {str(e)}", "", "", None, None, Noneexcept Exception as e:return f"Error getting recommendations: {str(e)}", "", "", None, None, Nonedef submit_all_ratings(course1_rating, course2_rating, course3_rating):"""Submit ratings for all three recommendations"""global current_recommendations, current_user_inputif not current_recommendations or not current_user_input:return "No recommendations to rate. Please get recommendations first."try:results = []ratings_submitted = 0# Rate first recommendationif course1_rating and len(current_recommendations) >= 1:rating_value = 1 if course1_rating == "π Like" else 0course = current_recommendations[0][0]success = recommender.add_feedback(course=course,stanine=current_user_input['stanine'],gwa=current_user_input['gwa'],strand=current_user_input['strand'],rating=rating_value,hobbies=current_user_input['hobbies'])if success:results.append(f"β Rating for '{course}' recorded")ratings_submitted += 1else:results.append(f"β Failed to record rating for '{course}'")# Rate second recommendationif course2_rating and len(current_recommendations) >= 2:rating_value = 1 if course2_rating == "π Like" else 0course = current_recommendations[1][0]success = recommender.add_feedback(course=course,stanine=current_user_input['stanine'],gwa=current_user_input['gwa'],strand=current_user_input['strand'],rating=rating_value,hobbies=current_user_input['hobbies'])if success:results.append(f"β Rating for '{course}' recorded")ratings_submitted += 1else:results.append(f"β Failed to record rating for '{course}'")# Rate third recommendationif course3_rating and len(current_recommendations) >= 3:rating_value = 1 if course3_rating == "π Like" else 0course = current_recommendations[2][0]success = recommender.add_feedback(course=course,stanine=current_user_input['stanine'],gwa=current_user_input['gwa'],strand=current_user_input['strand'],rating=rating_value,hobbies=current_user_input['hobbies'])if success:results.append(f"β Rating for '{course}' recorded")ratings_submitted += 1else:results.append(f"β Failed to record rating for '{course}'")if ratings_submitted > 0:return f"Thank you! {ratings_submitted} rating(s) submitted successfully.\n\n" + "\n".join(results)else:return "Please select at least one rating before submitting."except Exception as e:return f"Error recording feedback: {str(e)}"19 unmodified lineswith gr.Column(scale=1):gr.Markdown("### π Your Profile")stanine_input = gr.Textbox(label="Stanine Score (1-9)",placeholder="Enter your stanine score (1-9)",info="Your stanine score from standardized tests")gwa_input = gr.Textbox(label="GWA (75-100)",placeholder="Enter your GWA (75-100)",info="Your Grade Weighted Average")15 unmodified linestrain_model_btn = gr.Button("π€ Train Model", variant="secondary")with gr.Column(scale=1):gr.Markdown("### π Top 3 Course Recommendations")# Display top 3 recommendationscourse1_output = gr.Textbox(label="1st Recommendation",interactive=False)course1_rating = gr.Radio(choices=["π Like", "π Dislike"],label="Rate 1st Recommendation",interactive=True)course2_output = gr.Textbox(label="2nd Recommendation",interactive=False)course2_rating = gr.Radio(choices=["π Like", "π Dislike"],label="Rate 2nd Recommendation",interactive=True)course3_output = gr.Textbox(label="3rd Recommendation",interactive=False)course3_rating = gr.Radio(choices=["π Like", "π Dislike"],label="Rate 3rd Recommendation",interactive=True)submit_ratings_btn = gr.Button("Submit All Ratings", variant="primary")rating_feedback = gr.Textbox(label="Rating Feedback",4 unmodified linesget_recommendations_btn.click(fn=get_course_recommendations,inputs=[stanine_input, gwa_input, strand_input, hobbies_input],outputs=[course1_output, course2_output, course3_output, course1_rating, course2_rating, course3_rating])submit_ratings_btn.click(fn=submit_all_ratings,inputs=[course1_rating, course2_rating, course3_rating],outputs=[rating_feedback])
app.py+116-57