Spaces:
Sleeping
Sleeping
Update app.py
Browse files83 unmodified lines84858687878889904 unmodified lines9596979898991001011011021031041051051061071084 unmodified lines1131141151161161171181191191201211221231231241251264 unmodified lines1311321331341341351361371371381391407 unmodified lines14814915015115115215315315415515615715815916016016116216216316416416516616716816917017017117217317417517617717717817918020 unmodified lines2012022032042042052062062072082082092102112112122132142 unmodified lines2172182192202202212222234 unmodified lines2282292302312312322332344 unmodified lines23924024124224224324424555 unmodified lines30130230330430430530630630730830930931031131131231331483 unmodified lines # Rate first recommendation if course1_rating and len(current_recommendations) >= 1: rating_value = "like" if course1_rating == "π Like" else "dislike" rating_value = 1 if course1_rating == "Like" else 0 course = current_recommendations[0][0] success = recommender.add_feedback_with_learning( course=course,4 unmodified lines hobbies=current_user_input['hobbies'] ) if success: results.append(f"β
Rating for '{course}' recorded") results.append(f"[OK] Rating for '{course}' recorded") ratings_submitted += 1 else: results.append(f"β Failed to record rating for '{course}'") results.append(f"[ERROR] Failed to record rating for '{course}'") # Rate second recommendation if course2_rating and len(current_recommendations) >= 2: rating_value = "like" if course2_rating == "π Like" else "dislike" rating_value = 1 if course2_rating == "Like" else 0 course = current_recommendations[1][0] success = recommender.add_feedback_with_learning( course=course,4 unmodified lines hobbies=current_user_input['hobbies'] ) if success: results.append(f"β
Rating for '{course}' recorded") results.append(f"[OK] Rating for '{course}' recorded") ratings_submitted += 1 else: results.append(f"β Failed to record rating for '{course}'") results.append(f"[ERROR] Failed to record rating for '{course}'") # Rate third recommendation if course3_rating and len(current_recommendations) >= 3: rating_value = "like" if course3_rating == "π Like" else "dislike" rating_value = 1 if course3_rating == "Like" else 0 course = current_recommendations[2][0] success = recommender.add_feedback_with_learning( course=course,4 unmodified lines hobbies=current_user_input['hobbies'] ) if success: results.append(f"β
Rating for '{course}' recorded") results.append(f"[OK] Rating for '{course}' recorded") ratings_submitted += 1 else: results.append(f"β Failed to record rating for '{course}'") results.append(f"[ERROR] 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)7 unmodified lines """Train the model with current data""" try: accuracy = recommender.train_model(use_database=True) return f"β
Model trained successfully! Accuracy: {accuracy:.3f}" return f"[OK] Model trained successfully! Accuracy: {accuracy:.3f}" except Exception as e: return f"β Error training model: {str(e)}" return f"[ERROR] Error training model: {str(e)}"
def get_available_courses_info(): """Get information about available courses from database""" try: courses = db_connection.get_available_courses() if courses: 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 "") 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 "") else: return "π No courses found in database. Please check the /courses endpoint." return "No courses found in database. Please check the /courses endpoint." except Exception as e: return f"β Error fetching courses: {str(e)}" return f"[ERROR] Error fetching courses: {str(e)}"
# Create Gradio interfacedef create_interface(): with gr.Blocks(title="Course AI Recommender", theme=gr.themes.Soft()) as demo: gr.Markdown(""" # π Course AI Machine Learning Recommender # Course AI Machine Learning Recommender Get personalized course recommendations based on your academic profile and interests! """) with gr.Row(): with gr.Column(scale=1): gr.Markdown("### π Your Profile") gr.Markdown("### Your Profile") stanine_input = gr.Textbox( label="Stanine Score (1-9)",20 unmodified lines info="List your hobbies and interests (comma-separated)" ) get_recommendations_btn = gr.Button("π― Get Recommendations", variant="primary") get_recommendations_btn = gr.Button("Get Recommendations", variant="primary") train_model_btn = gr.Button("π€ Train Model", variant="secondary") train_model_btn = gr.Button("Train Model", variant="secondary") show_courses_btn = gr.Button("π Show Available Courses", variant="secondary") show_courses_btn = gr.Button("Show Available Courses", variant="secondary") with gr.Column(scale=1): gr.Markdown("### π Top 3 Course Recommendations") gr.Markdown("### Top 3 Course Recommendations") # Display top 3 recommendations course1_output = gr.Textbox(2 unmodified lines ) course1_rating = gr.Radio( choices=["π Like", "π Dislike"], choices=["Like", "Dislike"], label="Rate 1st Recommendation", interactive=True )4 unmodified lines ) course2_rating = gr.Radio( choices=["π Like", "π Dislike"], choices=["Like", "Dislike"], label="Rate 2nd Recommendation", interactive=True )4 unmodified lines ) course3_rating = gr.Radio( choices=["π Like", "π Dislike"], choices=["Like", "Dislike"], label="Rate 3rd Recommendation", interactive=True )55 unmodified lines # Try to load existing model try: recommender.load_model() print("β
Loaded existing model") print("[OK] Loaded existing model") except: print("β οΈ No existing model found. Training with basic data...") print("[WARNING] No existing model found. Training with basic data...") try: recommender.train_model(use_database=False) print("β
Model trained with basic data") print("[OK] Model trained with basic data") except Exception as e: print(f"β Error training model: {e}") print(f"[ERROR] Error training model: {e}") # Create and launch interface demo = create_interface()app.py+27-27