Spaces:
Sleeping
Sleeping
Update app.py
Browse files112436489101112131415161718567892021221011122425262728293031323334351314151617181920212223242526272829303132333435363738394041373842434041444546474845464748495051525354555657585960616263646566676869707172737475767778794950515253818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471485455150151152153154155156565758596061621581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013026330430530630730830931031131231331431531631731831932032132264import gradio as grimport pandas as pdimport numpy as npfrom course_recommender import CourseRecommenderfrom database_connection import DatabaseConnectionimport os
# Initialize the recommender systemrecommender = CourseRecommender()db_connection = DatabaseConnection()
# Global variables to store current recommendationscurrent_recommendations = []current_user_input = {}
def get_course_recommendations(stanine, gwa, strand, hobbies): """Get course recommendations based on user input""" global current_recommendations, current_user_inputdef create_basic_training_data(): """Create basic training data for the course recommender - DEPRECATED""" print("WARNING: Basic training data is deprecated. Use student feedback data instead.") raise ValueError("Basic training data is no longer used. Please use student feedback data from /student_feedback_counts endpoint.") # Validate inputs if not stanine or not gwa or not strand or not hobbies: return "Please fill in all fields", "", "", "", "", "" # Define strands strands = ["STEM", "ABM", "HUMSS", "GAS", "TVL"] 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", "", "", "", "", "" # Normalize strand to uppercase for case-insensitive matching strand = strand.upper() # Define common hobbies hobbies_list = [ "Programming", "Reading", "Sports", "Music", "Art", "Gaming", "Photography", "Writing", "Dancing", "Cooking", "Traveling", "Mathematics", "Science", "History", "Literature", "Technology" ] # Generate synthetic data np.random.seed(42) # For reproducible results n_samples = 1000 data = [] for _ in range(n_samples): # Generate random but realistic data stanine = np.random.randint(1, 10) gwa = np.random.uniform(75, 100) # GWA between 75-100 strand = np.random.choice(strands) course = np.random.choice(courses) hobbies = np.random.choice(hobbies_list, size=np.random.randint(1, 4), replace=False) hobbies_str = ", ".join(hobbies) # Generate rating based on some logic if stanine >= 7 and gwa >= 85: rating = np.random.choice([4, 5], p=[0.3, 0.7]) elif stanine >= 5 and gwa >= 80: rating = np.random.choice([3, 4, 5], p=[0.2, 0.5, 0.3]) else: rating = np.random.choice([1, 2, 3, 4], p=[0.1, 0.3, 0.4, 0.2]) if strand not in ["STEM", "ABM", "HUMSS", "GAS", "TVL"]: return "Strand must be one of: STEM, ABM, HUMSS, GAS, TVL", "", "", "", "", "" count = np.random.randint(1, 10) # Store current user input current_user_input = { data.append({ 'course': course, 'stanine': stanine, 'gwa': gwa, 'strand': strand, 'hobbies': hobbies } # Get recommendations recommendations = recommender.predict_course(stanine, gwa, strand, hobbies) current_recommendations = recommendations # Format top 3 recommendations if 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, None except ValueError as e: return f"Invalid input: {str(e)}", "", "", None, None, None except Exception as e: return f"Error getting recommendations: {str(e)}", "", "", None, None, None
def submit_all_ratings(course1_rating, course2_rating, course3_rating): """Submit ratings for all three recommendations""" global current_recommendations, current_user_input 'rating': rating, 'hobbies': hobbies_str, 'count': count }) if not current_recommendations or not current_user_input: return "No recommendations to rate. Please get recommendations first." try: results = [] ratings_submitted = 0 # Rate first recommendation if course1_rating and len(current_recommendations) >= 1: rating_value = "like" if course1_rating == "π Like" else "dislike" course = current_recommendations[0][0] success = recommender.add_feedback_with_learning( 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 += 1 else: results.append(f"β 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" course = current_recommendations[1][0] success = recommender.add_feedback_with_learning( 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 += 1 else: results.append(f"β 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" course = current_recommendations[2][0] success = recommender.add_feedback_with_learning( 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 += 1 else: 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)}" return pd.DataFrame(data)
def train_model(): """Train the model with current data""" try: accuracy = recommender.train_model(use_database=True) return f"β
Model trained successfully! Accuracy: {accuracy:.3f}" except Exception as e: return f"β Error training model: {str(e)}"def save_basic_data(): """Save basic training data to CSV""" df = create_basic_training_data() df.to_csv('basic_training_data.csv', index=False) print(f"Basic training data saved with {len(df)} samples") return df
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 "") else: return "π No courses found in database. Please check the /courses endpoint." except Exception as e: return f"β 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 Get personalized course recommendations based on your academic profile and interests! """) with gr.Row(): with 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" ) strand_input = gr.Dropdown( choices=["STEM", "ABM", "HUMSS", "GAS", "TVL"], value="STEM", label="Academic Strand", info="Your current academic strand" ) hobbies_input = gr.Textbox( label="Hobbies & Interests", placeholder="e.g., Programming, Reading, Sports, Music", info="List your hobbies and interests (comma-separated)" ) get_recommendations_btn = gr.Button("π― Get Recommendations", variant="primary") train_model_btn = gr.Button("π€ Train Model", variant="secondary") show_courses_btn = gr.Button("π Show Available Courses", variant="secondary") with gr.Column(scale=1): gr.Markdown("### π Top 3 Course Recommendations") # Display top 3 recommendations course1_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", interactive=False ) courses_info = gr.Textbox( label="Available Courses", lines=8, interactive=False ) # Event handlers get_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] ) train_model_btn.click( fn=train_model, outputs=[gr.Textbox(label="Training Status", interactive=False)] ) show_courses_btn.click( fn=get_available_courses_info, outputs=[courses_info] ) # Add some example inputs gr.Markdown(""" ### π‘ Example Inputs **For STEM students:** - Stanine: 7-9, GWA: 85-95, Strand: STEM, Hobbies: Programming, Mathematics, Science **For ABM students:** - Stanine: 6-8, GWA: 80-90, Strand: ABM, Hobbies: Business, Leadership, Economics **For HUMSS students:** - Stanine: 5-8, GWA: 78-88, Strand: HUMSS, Hobbies: Literature, History, Writing """) return demo
# Initialize the interfaceif __name__ == "__main__": # Try to load existing model try: recommender.load_model() print("β
Loaded existing model") except: print("β οΈ No existing model found. Training with basic data...") try: recommender.train_model(use_database=False) print("β
Model trained with basic data") except Exception as e: print(f"β Error training model: {e}") # Create and launch interface demo = create_interface() demo.launch( server_name="0.0.0.0", server_port=7860, share=True ) save_basic_data()app.py+49-307